From b404346c40383c65c373704aed23349446538b8c Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:01:58 -0700 Subject: [PATCH 1/7] Stop stubbing signature verification unless it's necessary --- spec/models/comment_spec.rb | 8 ++++++++ spec/models/user/receive_spec.rb | 1 + spec/spec_helper.rb | 15 +++++---------- spec/user_encryption_spec.rb | 14 ++++---------- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 1cd8e9b9d..9ebebab15 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -97,6 +97,10 @@ describe Comment do user.receive comment.to_diaspora_xml, user2.person end + context 'posts from a remote person' do + before(:all) do + stub_comment_signature_verification + end it 'should not send a comment a person made on his own post to anyone' do User::QUEUE.should_not_receive(:add_post_request) comment = Comment.new(:person_id => @person.id, :text => "balls", :post => @person_status) @@ -108,6 +112,10 @@ describe Comment do comment = Comment.new(:person_id => @person2.id, :text => "balls", :post => @person_status) user.receive comment.to_diaspora_xml, @person end + after(:all) do + unstub_mocha_stubs + end + end it 'should not clear the aspect post array on receiving a comment' do aspect.post_ids.include?(@user_status.id).should be true diff --git a/spec/models/user/receive_spec.rb b/spec/models/user/receive_spec.rb index 07b481b17..ab83b8c5d 100644 --- a/spec/models/user/receive_spec.rb +++ b/spec/models/user/receive_spec.rb @@ -115,6 +115,7 @@ describe User do comment_id = comment.id comment.delete + comment.post_creator_signature = comment.sign_with_key(user.encryption_key) user3.receive comment.to_diaspora_xml, user.person user3.reload diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 218def68b..80c863c67 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -25,10 +25,6 @@ RSpec.configure do |config| DatabaseCleaner.strategy = :truncation DatabaseCleaner.orm = "mongo_mapper" - config.before(:suite) do - stub_signature_verification - end - config.before(:each) do stub_sockets DatabaseCleaner.clean @@ -49,10 +45,8 @@ ImageUploader.enable_processing = false Diaspora::WebSocket.unstub!(:unsubscribe) end - def stub_signature_verification - (get_models.map{|model| model.camelize.constantize} - [User]).each do |model| - model.any_instance.stubs(:verify_signature).returns(true) - end + def stub_comment_signature_verification + Comment.any_instance.stubs(:verify_signature).returns(true) end def unstub_mocha_stubs @@ -82,11 +76,12 @@ ImageUploader.enable_processing = false aspect2.reload end - def stub_success(address = 'abc@example.com') + def stub_success(address = 'abc@example.com', opts = {}) host = address.split('@')[1] stub_request(:get, "https://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd) stub_request(:get, "http://#{host}/.well-known/host-meta").to_return(:status => 200, :body => host_xrd) - if host.include?("joindiaspora.com") + if opts[:diaspora] || host.include?("diaspora") + puts address stub_request(:get, /webfinger\/\?q=#{address}/).to_return(:status => 200, :body => finger_xrd) stub_request(:get, "http://#{host}/hcard/users/4c8eccce34b7da59ff000002").to_return(:status => 200, :body => hcard_response) else diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb index 645563b9a..581e4f414 100644 --- a/spec/user_encryption_spec.rb +++ b/spec/user_encryption_spec.rb @@ -6,7 +6,6 @@ require 'spec_helper' describe 'user encryption' do before do - unstub_mocha_stubs @user = Factory.create(:user) @aspect = @user.aspect(:name => 'dudes') @@ -14,13 +13,6 @@ describe 'user encryption' do @aspect2 = @user2.aspect(:name => 'dudes') end - after do - stub_signature_verification - #gpgdir = File.expand_path("../../db/gpg-#{Rails.env}", __FILE__) - #ctx = GPGME::Ctx.new - #keys = ctx.keys - #keys.each{|k| ctx.delete_key(k, true)} - end it 'should have a key' do @user.encryption_key.should_not be nil end @@ -34,6 +26,8 @@ describe 'user encryption' do it 'should receive and marshal a public key from a request' do remote_user = Factory.build(:user) remote_user.encryption_key.nil?.should== false + + Person.should_receive(:by_webfinger).and_return(remote_user.person) #should move this to friend request, but i found it here id = remote_user.person.id original_key = remote_user.exported_key @@ -41,13 +35,13 @@ describe 'user encryption' do request = remote_user.send_friend_request_to( @user.person, remote_user.aspect(:name => "temp")) - xml = request.to_diaspora_xml + xml = remote_user.salmon(request).xml_for(@user) remote_user.person.delete remote_user.delete person_count = Person.all.count - @user.receive xml, remote_user.person + @user.receive_salmon xml Person.all.count.should == person_count + 1 new_person = Person.first(:id => id) From 6993f3e2b59d30b1fbcce972ae870ac201bd455b Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:05:37 -0700 Subject: [PATCH 2/7] Fix spec in user_encryption --- spec/user_encryption_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/user_encryption_spec.rb b/spec/user_encryption_spec.rb index 581e4f414..709968278 100644 --- a/spec/user_encryption_spec.rb +++ b/spec/user_encryption_spec.rb @@ -27,6 +27,9 @@ describe 'user encryption' do remote_user = Factory.build(:user) remote_user.encryption_key.nil?.should== false + deliverable = Object.new + deliverable.stub!(:deliver) + Notifier.stub!(:new_request).and_return(deliverable) Person.should_receive(:by_webfinger).and_return(remote_user.person) #should move this to friend request, but i found it here id = remote_user.person.id From 61d539e555ad9fe40ad491948c67183e73b4a4a6 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:06:31 -0700 Subject: [PATCH 3/7] Moving user_encryption_spec to lib --- spec/{user_encryption_spec.rb => lib/encryptor_spec.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/{user_encryption_spec.rb => lib/encryptor_spec.rb} (100%) diff --git a/spec/user_encryption_spec.rb b/spec/lib/encryptor_spec.rb similarity index 100% rename from spec/user_encryption_spec.rb rename to spec/lib/encryptor_spec.rb From b67cee9af0c956db13a427f472ecc3ff04e61c95 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:09:20 -0700 Subject: [PATCH 4/7] Trying to make spec directory structure the same as the lib and app structures --- spec/lib/{ => diaspora}/exporter_spec.rb | 0 spec/lib/{ => diaspora}/importer_spec.rb | 0 spec/lib/{ostatus_builder_spec.rb => diaspora/ostatus_builder.rb} | 0 spec/lib/{diaspora_parser_spec.rb => diaspora/parser_spec.rb} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename spec/lib/{ => diaspora}/exporter_spec.rb (100%) rename spec/lib/{ => diaspora}/importer_spec.rb (100%) rename spec/lib/{ostatus_builder_spec.rb => diaspora/ostatus_builder.rb} (100%) rename spec/lib/{diaspora_parser_spec.rb => diaspora/parser_spec.rb} (100%) diff --git a/spec/lib/exporter_spec.rb b/spec/lib/diaspora/exporter_spec.rb similarity index 100% rename from spec/lib/exporter_spec.rb rename to spec/lib/diaspora/exporter_spec.rb diff --git a/spec/lib/importer_spec.rb b/spec/lib/diaspora/importer_spec.rb similarity index 100% rename from spec/lib/importer_spec.rb rename to spec/lib/diaspora/importer_spec.rb diff --git a/spec/lib/ostatus_builder_spec.rb b/spec/lib/diaspora/ostatus_builder.rb similarity index 100% rename from spec/lib/ostatus_builder_spec.rb rename to spec/lib/diaspora/ostatus_builder.rb diff --git a/spec/lib/diaspora_parser_spec.rb b/spec/lib/diaspora/parser_spec.rb similarity index 100% rename from spec/lib/diaspora_parser_spec.rb rename to spec/lib/diaspora/parser_spec.rb From 1a19c2819989c19198610ecb695f8e2fb68cd6ba Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:10:35 -0700 Subject: [PATCH 5/7] Move spec to user_spec --- spec/lib/encryptor_spec.rb | 3 --- spec/models/user_spec.rb | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/lib/encryptor_spec.rb b/spec/lib/encryptor_spec.rb index 709968278..0059bc308 100644 --- a/spec/lib/encryptor_spec.rb +++ b/spec/lib/encryptor_spec.rb @@ -13,9 +13,6 @@ describe 'user encryption' do @aspect2 = @user2.aspect(:name => 'dudes') end - it 'should have a key' do - @user.encryption_key.should_not be nil - end describe 'key exchange on friending' do it 'should send over a public key' do message_queue.stub!(:add_post_request) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4b2a4d02d..ed3008b84 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,6 +10,10 @@ describe User do let(:user2) { Factory(:user) } let(:aspect2) { user2.aspect(:name => 'stuff') } + it 'should have a key' do + user.encryption_key.should_not be nil + end + describe "validation" do describe "of associated person" do it "fails if person is not valid" do From d8bbe38019162fcff3919fba490cc074716445ab Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:11:51 -0700 Subject: [PATCH 6/7] Move spec to request_spec --- spec/lib/encryptor_spec.rb | 5 ----- spec/models/request_spec.rb | 1 + 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/lib/encryptor_spec.rb b/spec/lib/encryptor_spec.rb index 0059bc308..d9bc82ff0 100644 --- a/spec/lib/encryptor_spec.rb +++ b/spec/lib/encryptor_spec.rb @@ -14,11 +14,6 @@ describe 'user encryption' do end describe 'key exchange on friending' do - it 'should send over a public key' do - message_queue.stub!(:add_post_request) - request = @user.send_friend_request_to(Factory.create(:person), @aspect) - request.to_diaspora_xml.include?( @user.exported_key).should be true - end it 'should receive and marshal a public key from a request' do remote_user = Factory.build(:user) diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index c75b52317..6483c45fa 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -28,6 +28,7 @@ describe Request do xml.should include user.person.url xml.should include user.profile.first_name xml.should include user.profile.last_name + xml.should include user.exported_key end it 'should allow me to see only friend requests sent to me' do From 7dd86b3c875e45383da7cf26ef795ec5f4c29021 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 22 Oct 2010 11:18:43 -0700 Subject: [PATCH 7/7] Move comment signing spec to comment spec, make encryption spec not depend on message building --- spec/lib/encryptor_spec.rb | 58 ++++--------------------------------- spec/models/comment_spec.rb | 46 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/spec/lib/encryptor_spec.rb b/spec/lib/encryptor_spec.rb index d9bc82ff0..e993dac45 100644 --- a/spec/lib/encryptor_spec.rb +++ b/spec/lib/encryptor_spec.rb @@ -8,9 +8,6 @@ describe 'user encryption' do before do @user = Factory.create(:user) @aspect = @user.aspect(:name => 'dudes') - - @user2 = Factory.create(:user) - @aspect2 = @user2.aspect(:name => 'dudes') end describe 'key exchange on friending' do @@ -46,57 +43,12 @@ describe 'user encryption' do describe 'encryption' do before do - @message = @user.post :status_message, :message => "hi", :to => @aspect.id + @string = File.open(File.dirname(__FILE__) + '/../fixtures/fb_status').read end - it 'should encrypt large messages' do - ciphertext = @user.encrypt @message.to_diaspora_xml - ciphertext.include?(@message.to_diaspora_xml).should be false - @user.decrypt(ciphertext).include?(@message.to_diaspora_xml).should be true + it 'should encrypt a string' do + ciphertext = @user.encrypt @string + ciphertext.include?(@string).should be false + @user.decrypt(ciphertext).should == @string end end - - describe 'comments' do - before do - friend_users(@user, @aspect, @user2, @aspect2) - @remote_message = @user2.post :status_message, :message => "hello", :to => @aspect2.id - - - @message = @user.post :status_message, :message => "hi", :to => @aspect.id - end - it 'should attach the creator signature if the user is commenting' do - @user.comment "Yeah, it was great", :on => @remote_message - @remote_message.comments.first.signature_valid?.should be true - end - - it 'should sign the comment if the user is the post creator' do - message = @user.post :status_message, :message => "hi", :to => @aspect.id - @user.comment "Yeah, it was great", :on => message - message.comments.first.signature_valid?.should be true - message.comments.first.verify_post_creator_signature.should be true - end - - it 'should verify a comment made on a remote post by a different friend' do - comment = Comment.new(:person => @user2.person, :text => "cats", :post => @remote_message) - comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key) - comment.signature_valid?.should be true - comment.verify_post_creator_signature.should be false - comment.post_creator_signature = comment.send(:sign_with_key,@user.encryption_key) - comment.verify_post_creator_signature.should be true - end - - it 'should reject comments on a remote post with only a creator sig' do - comment = Comment.new(:person => @user2.person, :text => "cats", :post => @remote_message) - comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key) - comment.signature_valid?.should be true - comment.verify_post_creator_signature.should be false - end - - it 'should receive remote comments on a user post with a creator sig' do - comment = Comment.new(:person => @user2.person, :text => "cats", :post => @message) - comment.creator_signature = comment.send(:sign_with_key,@user2.encryption_key) - comment.signature_valid?.should be true - comment.verify_post_creator_signature.should be false - end - - end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 9ebebab15..66056789d 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -138,4 +138,50 @@ describe Comment do comment.to_diaspora_xml.include?(commenter.person.id.to_s).should be true end end + + describe 'comments' do + before do + friend_users(user, aspect, user2, aspect2) + @remote_message = user2.post :status_message, :message => "hello", :to => aspect2.id + + + @message = user.post :status_message, :message => "hi", :to => aspect.id + end + it 'should attach the creator signature if the user is commenting' do + user.comment "Yeah, it was great", :on => @remote_message + @remote_message.comments.first.signature_valid?.should be true + end + + it 'should sign the comment if the user is the post creator' do + message = user.post :status_message, :message => "hi", :to => aspect.id + user.comment "Yeah, it was great", :on => message + message.comments.first.signature_valid?.should be true + message.comments.first.verify_post_creator_signature.should be true + end + + it 'should verify a comment made on a remote post by a different friend' do + comment = Comment.new(:person => user2.person, :text => "cats", :post => @remote_message) + comment.creator_signature = comment.send(:sign_with_key,user2.encryption_key) + comment.signature_valid?.should be true + comment.verify_post_creator_signature.should be false + comment.post_creator_signature = comment.send(:sign_with_key,user.encryption_key) + comment.verify_post_creator_signature.should be true + end + + it 'should reject comments on a remote post with only a creator sig' do + comment = Comment.new(:person => user2.person, :text => "cats", :post => @remote_message) + comment.creator_signature = comment.send(:sign_with_key,user2.encryption_key) + comment.signature_valid?.should be true + comment.verify_post_creator_signature.should be false + end + + it 'should receive remote comments on a user post with a creator sig' do + comment = Comment.new(:person => user2.person, :text => "cats", :post => @message) + comment.creator_signature = comment.send(:sign_with_key,user2.encryption_key) + comment.signature_valid?.should be true + comment.verify_post_creator_signature.should be false + end + + end + end