diff --git a/app/models/request.rb b/app/models/request.rb index 2226a7b458f5e7f6fcddcf512df821eb6ed7c63f..cba28cdef58daeb50c1d30bc962b4a168ec42cf5 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -9,18 +9,18 @@ class Request include Diaspora::Webhooks include ROXML - xml_accessor :_id - xml_accessor :person, :as => Person - xml_accessor :destination_url - xml_accessor :callback_url - xml_accessor :exported_key, :cdata => true - - key :person_id, ObjectId - key :aspect_id, ObjectId + xml_reader :_id + xml_reader :diaspora_handle + xml_reader :destination_url + xml_reader :callback_url + + key :aspect_id, ObjectId key :destination_url, String key :callback_url, String key :exported_key, String + key :diaspora_handle, String + belongs_to :person validates_presence_of :destination_url, :callback_url @@ -30,15 +30,13 @@ class Request person = options[:from] self.new(:destination_url => options[:to], :callback_url => person.receive_url, - :person => person, - :exported_key => person.exported_key, - :aspect_id => options[:into]) + :diaspora_handle => person.diaspora_handle, + :aspect_id => options[:into]) end def reverse_for accepting_user - self.person = accepting_user.person - self.exported_key = accepting_user.exported_key - self.destination_url = self.callback_url + self.diaspora_handle = accepting_user.diaspora_handle + self.destination_url = self.callback_url self.save end diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index a651b479328853872b99714041f8821f5d518f73..1c6c4088cfa057020fd222fbda0ee6d0f85e3858 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -21,7 +21,7 @@ module Diaspora Rails.logger.debug("From: #{object.person.inspect}") if object.person - if object.is_a?(Comment) || object.is_a?(Post) + if object.is_a?(Comment) || object.is_a?(Post)|| object.is_a?(Request) e = EMWebfinger.new(object.diaspora_handle) e.on_person { |person| @@ -32,6 +32,10 @@ module Diaspora raise "Malicious Post, #{salmon_author.real_name} with id #{salmon_author.id} is sending a #{object.class} as #{sender_in_xml.real_name} with id #{sender_in_xml.id} " end + if object.is_a? Request + return receive_request object, sender_in_xml + end + raise "Not friends with that person" unless self.contact_for(salmon_author) if object.is_a?(Comment) @@ -50,17 +54,12 @@ module Diaspora raise "Malicious Post, #{salmon_author.real_name} with id #{salmon_author.id} is sending a #{object.class} as #{sender_in_xml.real_name} with id #{sender_in_xml.id} " end - if object.is_a? Request - return receive_request object, sender_in_xml - end raise "Not friends with that person" unless self.contact_for(salmon_author) if object.is_a? Retraction receive_retraction object, xml elsif object.is_a? Profile receive_profile object, xml - else - receive_post object, xml end end end @@ -68,8 +67,6 @@ module Diaspora def sender(object, xml, webfingered_person = nil) if object.is_a? Retraction sender = object.person - elsif object.is_a? Request - sender = object.person elsif object.is_a? Profile sender = Diaspora::Parser.owner_id_from_xml xml @@ -98,10 +95,9 @@ module Diaspora end def receive_request request, person - person.serialized_public_key ||= request.exported_key request.person = person request.person.save - old_request = Request.first(:id => request.id) + old_request = Request.find(request.id) Rails.logger.info("I got a reqest_id #{request.id} with old request #{old_request.inspect}") request.aspect_id = old_request.aspect_id if old_request request.save diff --git a/spec/helper_methods.rb b/spec/helper_methods.rb index e60b39535408df648f20da263666df577fedaea0..4dc1ca8f2c0a816a1207fbb8a188bdf18a8514e1 100644 --- a/spec/helper_methods.rb +++ b/spec/helper_methods.rb @@ -28,9 +28,12 @@ module HelperMethods def friend_users(user1, aspect1, user2, aspect2) request = user1.send_friend_request_to(user2.person, aspect1) - user2.receive_friend_request(request) + + user2.receive request.to_diaspora_xml, user1.person + reversed_request = user2.accept_friend_request( request.id, aspect2.id) user1.reload + user1.receive reversed_request.to_diaspora_xml, user2.person user1.reload aspect1.reload diff --git a/spec/lib/diaspora/parser_spec.rb b/spec/lib/diaspora/parser_spec.rb index 6e7d7410909fa5eaae3d21f359ada6c878f50991..fc2a021fa5f8ea17efee3454ff4bfd487c545b7b 100644 --- a/spec/lib/diaspora/parser_spec.rb +++ b/spec/lib/diaspora/parser_spec.rb @@ -35,6 +35,8 @@ describe Diaspora::Parser do end context "friending" do + + let(:good_request) { FakeHttpRequest.new(:success)} before do deliverable = Object.new deliverable.stub!(:deliver) @@ -42,30 +44,24 @@ describe Diaspora::Parser do end it "should create a new person upon getting a person request" do + webfinger_mock = EMWebfinger.new(person.diaspora_handle) + webfinger_mock.should_receive(:on_person) + + EMWebfinger.should_receive(:new).and_return(webfinger_mock) + + request = Request.instantiate(:to =>"http://www.google.com/", :from => person) xml = request.to_diaspora_xml - user3.destroy - person.destroy + user3.delete + person.delete + Person.should_receive(:by_account_identifier).exactly(2).times.and_return(person) user lambda { user.receive xml, person }.should change(Person, :count).by(1) end - it "should not create a new person if the person is already here" do - request = Request.instantiate(:to =>"http://www.google.com/", :from => user2.person) - original_person_id = user2.person.id - xml = request.to_diaspora_xml - user - lambda { user.receive xml, user2.person }.should_not change(Person, :count) - user2.reload - user2.person.reload - user2.serialized_private_key.include?("PRIVATE").should be true - - url = "http://" + request.callback_url.split("/")[2] + "/" - Person.where(:url => url).first.id.should == original_person_id - end end it "should activate the Person if I initiated a request to that url" do @@ -73,13 +69,12 @@ describe Diaspora::Parser do user.reload request.reverse_for user3 - xml = request.to_diaspora_xml + xml = user3.salmon(request).xml_for(user.person) - user3.person.destroy - user3.destroy + user3.delete - user.receive xml, user3.person - new_person = Person.first(:url => user3.person.url) + user.receive_salmon(xml) + new_person = Person.find_by_url(user3.person.url) new_person.nil?.should be false user.reload diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index e08347d69840cd184d5aa74645ad269fa781114c..daab156b7477e1f1d96262f1e24672c2ed28815d 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -19,17 +19,6 @@ describe Request do person_request.valid?.should be true end - it 'should generate xml for the User as a Person' do - - request = user.send_friend_request_to person, aspect - xml = request.to_xml.to_s - - xml.should include user.person.diaspora_handle - 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 strip the destination url' do person_request = Request.new @@ -68,4 +57,28 @@ describe Request do end end + describe 'serialization' do + it 'should not generate xml for the User as a Person' do + request = user.send_friend_request_to person, aspect + xml = request.to_xml.to_s + + xml.should_not include user.person.profile.first_name + end + + it 'should serialize the handle and not the sender' do + request = user.send_friend_request_to person, aspect + xml = request.to_xml.to_s + + xml.should include user.person.diaspora_handle + end + + it 'should not serialize the exported key' do + request = user.send_friend_request_to person, aspect + xml = request.to_xml.to_s + + xml.should_not include user.person.exported_key + end + + end + end diff --git a/spec/models/user/user_friending_spec.rb b/spec/models/user/user_friending_spec.rb index 945f3dcbf38a23dce155c108f4d152e676b8f595..a1232b80bca01545401075eeae9f5e34ad49938f 100644 --- a/spec/models/user/user_friending_spec.rb +++ b/spec/models/user/user_friending_spec.rb @@ -233,23 +233,22 @@ describe Diaspora::UserModules::Friending do end it "keeps the right counts of friends" do - user.receive_friend_request @request + user.receive @request.to_diaspora_xml, person_one - person_two.destroy - user.reload.pending_requests.size.should be 1 + user.reload.pending_requests.size.should == 1 user.friends.size.should be 0 - user.receive_friend_request @request_two - user.reload.pending_requests.size.should be 2 + user.receive @request_two.to_diaspora_xml, person_two + user.reload.pending_requests.size.should == 2 user.friends.size.should be 0 user.accept_friend_request @request.id, aspect.id - user.reload.pending_requests.size.should be 1 + user.reload.pending_requests.size.should == 1 user.friends.size.should be 1 user.contact_for(person_one).should_not be_nil user.ignore_friend_request @request_two.id - user.reload.pending_requests.size.should be 0 + user.reload.pending_requests.size.should == 0 user.friends.size.should be 1 user.contact_for(person_two).should be_nil end