Skip to content
Extraits de code Groupes Projets
receiving_spec.rb 13,2 ko
Newer Older
  • Learn to ignore specific revisions
  • danielgrippi's avatar
    danielgrippi a validé
    #   Copyright (c) 2010-2011, Diaspora Inc.  This file is
    
    Raphael's avatar
    Raphael a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    
    Raphael's avatar
    Raphael a validé
    #   the COPYRIGHT file.
    
    require 'spec_helper'
    
    describe 'a user receives a post' do
    
      def receive_with_zord(user, person, xml)
    
        zord = Postzord::Receiver::Private.new(user, :person => person)
    
        zord.parse_and_receive(xml)
      end
    
    
        @alices_aspect = alice.aspects.where(:name => "generic").first
        @bobs_aspect = bob.aspects.where(:name => "generic").first
        @eves_aspect = eve.aspects.where(:name => "generic").first
    
    danielgrippi's avatar
    danielgrippi a validé
        @contact = alice.contact_for(bob.person)
    
      end
    
      it 'should be able to parse and store a status message from xml' do
    
        status_message = bob.post :status_message, :text => 'store this!', :to => @bobs_aspect.id
    
    
        xml = status_message.to_diaspora_xml
    
    danielgrippi's avatar
    danielgrippi a validé
        bob.delete
    
        status_message.destroy
    
    danielgrippi's avatar
    danielgrippi a validé
          receive_with_zord(alice, bob.person, xml)
    
        }.should change(Post,:count).by(1)
    
    Raphael's avatar
    Raphael a validé
      it 'should not create new aspects on message receive' do
    
    danielgrippi's avatar
    danielgrippi a validé
        num_aspects = alice.aspects.size
    
        2.times do |n|
    
          status_message = bob.post :status_message, :text => "store this #{n}!", :to => @bobs_aspect.id
    
    danielgrippi's avatar
    danielgrippi a validé
        alice.aspects.size.should == num_aspects
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      it "should show bob's post to alice" do
    
    Jonne Haß's avatar
    Jonne Haß a validé
        inlined_jobs do |queue|
    
          sm = bob.build_post(:status_message, :text => "hi")
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          sm.save!
          bob.aspects.reload
    
          bob.add_to_streams(sm, [@bobs_aspect])
    
    Jonne Haß's avatar
    Jonne Haß a validé
          queue.drain_all
    
          bob.dispatch_post(sm, :to => @bobs_aspect)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
        end
    
    
        alice.visible_shareables(Post).count.should == 1
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      end
    
    
      context 'with mentions, ' do
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
        it 'adds the notifications for the mentioned users regardless of the order they are received' do
    
    danielgrippi's avatar
    danielgrippi a validé
          Notification.should_receive(:notify).with(alice, anything(), bob.person)
          Notification.should_receive(:notify).with(eve, anything(), bob.person)
    
    danielgrippi's avatar
    danielgrippi a validé
          @sm = bob.build_post(:status_message, :text => "@{#{alice.name}; #{alice.diaspora_handle}} stuff @{#{eve.name}; #{eve.diaspora_handle}}")
          bob.add_to_streams(@sm, [bob.aspects.first])
    
          zord = Postzord::Receiver::Private.new(alice, :object => @sm, :person => bob.person)
    
          zord.receive_object
    
    
          zord = Postzord::Receiver::Private.new(eve, :object => @sm, :person => bob.person)
    
          zord.receive_object
        end
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
    
    
        it 'notifies local users who are mentioned' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @remote_person = FactoryGirl.create(:person, :diaspora_handle => "foobar@foobar.com")
    
          Contact.create!(:user => alice, :person => @remote_person, :aspects => [@alices_aspect])
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
    
    
    danielgrippi's avatar
    danielgrippi a validé
          Notification.should_receive(:notify).with(alice, anything(), @remote_person)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
    
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @sm = FactoryGirl.create(:status_message, :text => "hello @{#{alice.name}; #{alice.diaspora_handle}}", :diaspora_handle => @remote_person.diaspora_handle, :author => @remote_person)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          @sm.save
    
    
          zord = Postzord::Receiver::Private.new(alice, :object => @sm, :person => bob.person)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          zord.receive_object
        end
    
        it 'does not notify the mentioned user if the mentioned user is not friends with the post author' do
    
    danielgrippi's avatar
    danielgrippi a validé
          Notification.should_not_receive(:notify).with(alice, anything(), eve.person)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
    
    
    danielgrippi's avatar
    danielgrippi a validé
          @sm = eve.build_post(:status_message, :text => "should not notify @{#{alice.name}; #{alice.diaspora_handle}}")
          eve.add_to_streams(@sm, [eve.aspects.first])
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          @sm.save
    
    
          zord = Postzord::Receiver::Private.new(alice, :object => @sm, :person => bob.person)
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          zord.receive_object
        end
    
    maxwell's avatar
    maxwell a validé
      context 'update posts' do
    
        it 'does not update posts not marked as mutable' do
    
          status = alice.post :status_message, :text => "store this!", :to => @alices_aspect.id
    
          xml = status.to_diaspora_xml
    
    danielgrippi's avatar
    danielgrippi a validé
          receive_with_zord(bob, alice.person, xml)
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
          status.reload.text.should == 'store this!'
    
          photo = alice.post(:photo, :user_file => uploaded_photo, :text => "Original", :to => @alices_aspect.id)
    
          xml = photo.to_diaspora_xml
    
    danielgrippi's avatar
    danielgrippi a validé
          bob.reload
    
    danielgrippi's avatar
    danielgrippi a validé
          receive_with_zord(bob, alice.person, xml)
    
          photo.reload.text.should match(/foo/)
    
      describe 'profiles' do
       it 'federates tags' do
         luke, leia, raph = set_up_friends
         raph.profile.diaspora_handle = "raph@remote.net"
         raph.profile.save!
         p = raph.profile
    
         p.tag_string = "#big #rafi #style"
         p.receive(luke, raph)
         p.tags(true).count.should == 3
    
      describe 'post refs' do
    
          @status_message = bob.post(:status_message, :text => "hi", :to => @bobs_aspect.id)
    
    danielgrippi's avatar
    danielgrippi a validé
          alice.reload
    
    danielgrippi's avatar
    danielgrippi a validé
          @contact = alice.contact_for(bob.person)
    
    danielgrippi's avatar
    danielgrippi a validé
        it "adds a received post to the the contact" do
    
          alice.visible_shareables(Post).should include(@status_message)
    
          @contact.posts.should include(@status_message)
    
        it 'removes posts upon forceful removal' do
          alice.remove_contact(@contact, :force => true)
    
    danielgrippi's avatar
    danielgrippi a validé
          alice.reload
    
          alice.visible_shareables(Post).should_not include @status_message
    
        context 'dependent delete' do
          it 'deletes share_visibilities on disconnected by' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
            @person = FactoryGirl.create(:person)
    
            alice.contacts.create(:person => @person, :aspects => [@alices_aspect])
    
    Jonne Haß's avatar
    Jonne Haß a validé
            @post = FactoryGirl.create(:status_message, :author => @person)
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
            @post.share_visibilities.should be_empty
    
    danielgrippi's avatar
    danielgrippi a validé
            receive_with_zord(alice, @person, @post.to_diaspora_xml)
            @contact = alice.contact_for(@person)
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
            @contact.share_visibilities.reset
    
    danielgrippi's avatar
    danielgrippi a validé
            @contact.posts(true).should include(@post)
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
            @post.share_visibilities.reset
    
    danielgrippi's avatar
    danielgrippi a validé
              alice.disconnected_by(@person)
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
            }.should change{@post.share_visibilities(true).count}.by(-1)
    
    Raphael's avatar
    Raphael a validé
    
      describe 'comments' do
    
    
        context 'remote' do
          before do
    
    Jonne Haß's avatar
    Jonne Haß a validé
            inlined_jobs do |queue|
    
              connect_users(alice, @alices_aspect, eve, @eves_aspect)
              @post = alice.post(:status_message, :text => "hello", :to => @alices_aspect.id)
    
              xml = @post.to_diaspora_xml
    
              receive_with_zord(bob, alice.person, xml)
              receive_with_zord(eve, alice.person, xml)
    
              comment = eve.comment!(@post, 'tada')
    
    Jonne Haß's avatar
    Jonne Haß a validé
              queue.drain_all
    
              # After Eve creates her comment, it gets sent to Alice, who signs it with her private key
              # before relaying it out to the contacts on the top-level post
    
              comment.parent_author_signature = comment.sign_with_key(alice.encryption_key)
              @xml = comment.to_diaspora_xml
              comment.delete
    
    
              comment_with_whitespace = alice.comment!(@post, '   I cannot lift my thumb from the spacebar  ')
    
    Jonne Haß's avatar
    Jonne Haß a validé
              queue.drain_all
    
              @xml_with_whitespace = comment_with_whitespace.to_diaspora_xml
              @guid_with_whitespace = comment_with_whitespace.guid
              comment_with_whitespace.delete
    
          it 'should receive a relayed comment with leading whitespace' do
            eve.reload.visible_shareables(Post).size.should == 1
            post_in_db = StatusMessage.find(@post.id)
            post_in_db.comments.should == []
            receive_with_zord(eve, alice.person, @xml_with_whitespace)
    
            post_in_db.comments(true).first.guid.should == @guid_with_whitespace
          end
    
    
          it 'should correctly attach the user already on the pod' do
    
            bob.reload.visible_shareables(Post).size.should == 1
    
            post_in_db = StatusMessage.find(@post.id)
            post_in_db.comments.should == []
    
    danielgrippi's avatar
    danielgrippi a validé
            receive_with_zord(bob, alice.person, @xml)
    
    danielgrippi's avatar
    danielgrippi a validé
            post_in_db.comments(true).first.author.should == eve.person
    
          end
    
          it 'should correctly marshal a stranger for the downstream user' do
    
    danielgrippi's avatar
    danielgrippi a validé
            remote_person = eve.person.dup
            eve.person.delete
            eve.delete
    
            Person.where(:id => remote_person.id).delete_all
            Profile.where(:person_id => remote_person.id).delete_all
    
            remote_person.attributes.delete(:id) # leaving a nil id causes it to try to save with id set to NULL in postgres
    
            m = mock()
            Webfinger.should_receive(:new).twice.with(eve.person.diaspora_handle).and_return(m)
            m.should_receive(:fetch).twice.and_return{
              remote_person.save(:validate => false)
    
    Jonne Haß's avatar
    Jonne Haß a validé
              remote_person.profile = FactoryGirl.create(:profile, :person => remote_person)
    
            bob.reload.visible_shareables(Post).size.should == 1
    
            post_in_db = StatusMessage.find(@post.id)
            post_in_db.comments.should == []
    
    
    danielgrippi's avatar
    danielgrippi a validé
            receive_with_zord(bob, alice.person, @xml)
    
            post_in_db.comments(true).first.author.should == remote_person
    
        context 'local' do
          before do
    
            @post = alice.post :status_message, :text => "hello", :to => @alices_aspect.id
    
            xml = @post.to_diaspora_xml
    
            alice.share_with(eve.person, alice.aspects.first)
    
    
    danielgrippi's avatar
    danielgrippi a validé
            receive_with_zord(bob, alice.person, xml)
            receive_with_zord(eve, alice.person, xml)
    
          it 'does not raise a `Mysql2::Error: Duplicate entry...` exception on save' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
            inlined_jobs do
    
              @comment = bob.comment!(@post, 'tada')
              @xml = @comment.to_diaspora_xml
    
    Jonne Haß's avatar
    Jonne Haß a validé
                
              expect {
    
                receive_with_zord(alice, bob.person, @xml)
    
    Jonne Haß's avatar
    Jonne Haß a validé
              }.to_not raise_exception
    
    Raphael's avatar
    Raphael a validé
        end
      end
    
    Raphael's avatar
    Raphael a validé
    
    
    
      describe 'receiving mulitple versions of the same post from a remote pod' do
    
        before do
    
          @local_luke, @local_leia, @remote_raphael = set_up_friends
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.create(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago)
    
        end
    
        it 'does not update created_at or updated_at when two people save the same post' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author=> @remote_raphael, :created_at => 5.days.ago, :updated_at => 5.days.ago)
    
          xml = @post.to_diaspora_xml
          receive_with_zord(@local_luke, @remote_raphael, xml)
    
    danielgrippi's avatar
    danielgrippi a validé
          old_time = Time.now+1
    
          receive_with_zord(@local_leia, @remote_raphael, xml)
    
          (Post.find_by_guid @post.guid).updated_at.should be < old_time
          (Post.find_by_guid @post.guid).created_at.should be < old_time
    
        end
    
        it 'does not update the post if a new one is sent with a new created_at' do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 5.days.ago)
    
          old_time = @post.created_at
          xml = @post.to_diaspora_xml
          receive_with_zord(@local_luke, @remote_raphael, xml)
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.build(:status_message, :text => 'hey', :guid => '12313123', :author => @remote_raphael, :created_at => 2.days.ago)
    
          receive_with_zord(@local_luke, @remote_raphael, xml)
          (Post.find_by_guid @post.guid).created_at.day.should == old_time.day
        end
      end
    
    
    
    Raphael's avatar
    Raphael a validé
      describe 'salmon' do
    
        let(:post){alice.post :status_message, :text => "hello", :to => @alices_aspect.id}
    
    danielgrippi's avatar
    danielgrippi a validé
        let(:salmon){alice.salmon( post )}
    
    Raphael's avatar
    Raphael a validé
    
    
        it 'processes a salmon for a post' do
    
    danielgrippi's avatar
    danielgrippi a validé
          salmon_xml = salmon.xml_for(bob.person)
    
          zord = Postzord::Receiver::Private.new(bob, :salmon_xml => salmon_xml)
    
          bob.visible_shareables(Post).include?(post).should be_true
    
    Raphael's avatar
    Raphael a validé
        end
      end
    
        let(:message) { bob.post(:status_message, text: "cats", to: @bobs_aspect.id) }
        let(:zord) { Postzord::Receiver::Private.new(alice, person: bob.person) }
    
    
        it 'should accept retractions' do
          retraction = Retraction.for(message)
          xml = retraction.to_diaspora_xml
    
    
            zord.parse_and_receive(xml)
    
          }.to change(StatusMessage, :count).by(-1)
        end
    
        it 'should accept relayable retractions' do
          comment = bob.comment! message, "and dogs"
          retraction = RelayableRetraction.build(bob, comment)
          xml = retraction.to_diaspora_xml
    
          expect {
            zord.parse_and_receive xml
          }.to change(Comment, :count).by(-1)
    
    
        it 'should accept signed retractions for public posts' do
          message = bob.post(:status_message, text: "cats", public: true)
          retraction = SignedRetraction.build(bob, message)
          salmon = Postzord::Dispatcher::Public.salmon bob, retraction.to_diaspora_xml
          xml = salmon.xml_for alice.person
          zord = Postzord::Receiver::Public.new xml
    
          expect {
            zord.receive!
          }.to change(Post, :count).by(-1)
        end
    
      end
    
      it 'should marshal a profile for a person' do
        #Create person
    
    danielgrippi's avatar
    danielgrippi a validé
        person = bob.person
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
        person.profile.delete
        person.profile = Profile.new(:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com", :person_id => person.id)
    
        person.save
    
        #Cache profile for checking against marshaled profile
    
        new_profile = person.profile.dup
        new_profile.first_name = 'boo!!!'
    
        #Build xml for profile
        xml = new_profile.to_diaspora_xml
    
        zord = Postzord::Receiver::Private.new(alice, :person => person)
    
        zord.parse_and_receive(xml)
    
        #Check that marshaled profile is the same as old profile
        person = Person.find(person.id)
    
        person.profile.first_name.should == new_profile.first_name
        person.profile.last_name.should == new_profile.last_name
        person.profile.image_url.should == new_profile.image_url