diff --git a/app/models/request.rb b/app/models/request.rb index 90c17b8864b1fbd867708eb89884ce763db730f7..3ad42dd7605378d1ff1ddac6fafcfa3d54b0d087 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -4,15 +4,15 @@ class Request < ActiveRecord::Base require File.join(Rails.root, 'lib/diaspora/webhooks') - require File.join(Rails.root, 'lib/postzord/dispatch') + include Diaspora::Webhooks include ROXML xml_accessor :sender_handle xml_accessor :recipient_handle - belongs_to :sender, :class_name => 'Person' + belongs_to :sender, :class_name => 'Person' belongs_to :recipient, :class_name => 'Person' belongs_to :aspect @@ -34,10 +34,13 @@ class Request < ActiveRecord::Base ) end + def diaspora_handle + sender_handle + end + def sender_handle sender.diaspora_handle end - def sender_handle= sender_handle self.sender = Person.where(:diaspora_handle => sender_handle).first end @@ -45,17 +48,12 @@ class Request < ActiveRecord::Base def recipient_handle recipient.diaspora_handle end - def recipient_handle= recipient_handle self.recipient = Person.where(:diaspora_handle => recipient_handle).first end - def diaspora_handle - sender_handle - end - def notification_type(user, person) - if Contact.where(:user_id => user.id, :person_id => person.id).first + if Contact.unscoped.where(:user_id => user.id, :person_id => person.id).first Notifications::RequestAccepted else Notifications::NewRequest diff --git a/db/schema.rb b/db/schema.rb index 765daed3efe009fdb0118f4cd037dfe8a60fc51c..8d57025da488099f40db771b70ee8e6d43c4f8ba 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110331004720) do +ActiveRecord::Schema.define(:version => 20110331222629) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -250,7 +250,6 @@ ActiveRecord::Schema.define(:version => 20110331004720) do add_index "posts", ["status_message_id", "pending"], :name => "index_posts_on_status_message_id_and_pending" add_index "posts", ["status_message_id"], :name => "index_posts_on_status_message_id" add_index "posts", ["type", "pending", "id"], :name => "index_posts_on_type_and_pending_and_id" - add_index "posts", ["type"], :name => "index_posts_on_type" create_table "profiles", :force => true do |t| t.string "diaspora_handle" diff --git a/spec/models/request_spec.rb b/spec/models/request_spec.rb index fd6fb207463560457ad1556b824c9a2df160b0a8..f73a87a6691b8695decabdf246f62fa625df41d7 100644 --- a/spec/models/request_spec.rb +++ b/spec/models/request_spec.rb @@ -6,11 +6,11 @@ require 'spec_helper' describe Request do before do - @user = Factory.create(:user) - @user2 = Factory.create(:user) + @user = alice + @user2 = eve @person = Factory :person - @aspect = @user.aspects.create(:name => "dudes") - @aspect2 = @user2.aspects.create(:name => "Snoozers") + @aspect = @user.aspects.first + @aspect2 = @user2.aspects.first end describe 'validations' do @@ -54,13 +54,14 @@ describe Request do before do @request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect) end - it "returns 'request_accepted' if there is a pending contact" do - Contact.create(:user_id => @user.id, :person_id => @person.id) - @request.notification_type(@user, @person).should == Notifications::RequestAccepted + + it 'returns request_accepted' do + @user.contacts.create(:person_id => @person.id, :pending => true) + @request.notification_type(@user, @person).should == Notifications::RequestAccepted end - it 'returns new_request if there is not a pending contact' do - @request.notification_type(@user, @person).should == Notifications::NewRequest + it 'returns new_request' do + @request.notification_type(@user, @person).should == Notifications::NewRequest end end @@ -71,56 +72,37 @@ describe Request do end end - describe 'xml' do + describe '#receive' do + it 'calls receive_contact_request on user' do + request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect) + + @user2.should_receive(:receive_contact_request).with(request) + request.receive(@user2, @user.person) + end + end + + context 'xml' do before do @request = Request.new(:sender => @user.person, :recipient => @user2.person, :aspect => @aspect) @xml = @request.to_xml.to_s end - describe 'serialization' do - it 'does not generate xml for the User as a Person' do - @xml.should_not include @user.person.profile.first_name - end - it 'serializes the handle and not the sender' do + describe 'serialization' do + it 'produces valid xml' do @xml.should include @user.person.diaspora_handle - end - - it 'serializes the intended recipient handle' do @xml.should include @user2.person.diaspora_handle - end - - it 'does not serialize the exported key' do @xml.should_not include @user.person.exported_key + @xml.should_not include @user.person.profile.first_name end end - describe 'marshalling' do - before do - @marshalled = Request.from_xml @xml - end - it 'marshals the sender' do - @marshalled.sender.should == @user.person - end - it 'marshals the recipient' do - @marshalled.recipient.should == @user2.person - end - it 'knows nothing about the aspect' do - @marshalled.aspect.should be_nil - end - end - describe 'marshalling with diaspora wrapper' do - before do - @d_xml = @request.to_diaspora_xml - @marshalled = Diaspora::Parser.from_xml @d_xml - end - it 'marshals the sender' do - @marshalled.sender.should == @user.person - end - it 'marshals the recipient' do - @marshalled.recipient.should == @user2.person - end - it 'knows nothing about the aspect' do - @marshalled.aspect.should be_nil + context 'marshalling' do + it 'produces a request object' do + marshalled = Request.from_xml @xml + + marshalled.sender.should == @user.person + marshalled.recipient.should == @user2.person + marshalled.aspect.should be_nil end end end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index e4d74286b23b7bf935c6299f5beca9a417a59f0a..2e98e26905c15d229bec86e77b454f0a408ade90 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -65,12 +65,6 @@ describe Diaspora::UserModules::Connecting do received_req = @r.receive(user, person_one) }.should_not change(Contact, :count) end - - it 'enqueues a mail job' do - Resque.should_receive(:enqueue).with(Job::MailRequestReceived, user.id, person.id, anything) - zord = Postzord::Receiver.new(user, :object => @r, :person => person) - zord.receive_object - end end describe '#receive_request_acceptance' do @@ -79,18 +73,13 @@ describe Diaspora::UserModules::Connecting do @acceptance = @original_request.reverse_for(user2) end it 'connects to the acceptor' do - @acceptance.receive(user, user2.person) + user.receive_contact_request(@acceptance) user.contact_for(user2.person).should_not be_nil end it 'deletes the acceptance' do - @acceptance.receive(user, user2.person) + user.receive_contact_request(@acceptance) Request.where(:sender_id => user2.person.id, :recipient_id => user.person.id).should be_empty end - it 'enqueues a mail job' do - Resque.should_receive(:enqueue).with(Job::MailRequestAcceptance, user.id, user2.person.id, nil).once - zord = Postzord::Receiver.new(user, :object => @acceptance, :person => user2.person) - zord.receive_object - end end context 'received a contact request' do