Skip to content
Extraits de code Groupes Projets
connecting_spec.rb 5,52 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 User::Connecting do
    
    
      let(:aspect) { alice.aspects.first }
      let(:aspect1) { alice.aspects.create(:name => 'other') }
    
      let(:person) { Factory(:person) }
    
      let(:aspect2) { eve.aspects.create(:name => "aspect two") }
    
    
      let(:person_one) { Factory :person }
      let(:person_two) { Factory :person }
      let(:person_three) { Factory :person }
    
    Sarah Mei's avatar
    Sarah Mei a validé
    
    
      describe 'disconnecting' do
        describe '#remove_contact' do
          it 'removed non mutual contacts' do
            alice.share_with(eve.person, alice.aspects.first)
    
              alice.remove_contact alice.contact_for(eve.person)
            }.should change {
              alice.contacts(true).count
            }.by(-1)
    
          it 'removes a contacts receiving flag' do
            bob.contacts.find_by_person_id(alice.person.id).should be_receiving
    
            bob.remove_contact(bob.contact_for(alice.person))
    
            bob.contacts(true).find_by_person_id(alice.person.id).should_not be_receiving
    
        describe '#disconnected_by' do
          it 'calls remove contact' do
            bob.should_receive(:remove_contact).with(bob.contact_for(alice.person))
            bob.disconnected_by(alice.person)
    
    maxwell's avatar
    maxwell a validé
          end
    
    
          it 'removes notitications' do
            alice.share_with(eve.person, alice.aspects.first)
            Notifications::StartedSharing.where(:recipient_id => eve.id).first.should_not be_nil
            eve.disconnected_by(alice.person)
            Notifications::StartedSharing.where(:recipient_id => eve.id).first.should be_nil
          end
    
        describe '#disconnect' do
          it 'calls remove contact' do
            contact = bob.contact_for(alice.person)
    
    ilya's avatar
    ilya a validé
    
    
            bob.should_receive(:remove_contact).with(contact, {})
            bob.disconnect(contact)
    
    danielgrippi's avatar
    danielgrippi a validé
          end
    
          it 'dispatches a retraction' do
            p = mock()
    
            Postzord::Dispatcher.should_receive(:build).and_return(p)
    
            p.should_receive(:post)
    
            bob.disconnect bob.contact_for(eve.person)
    
    Raphael's avatar
    Raphael a validé
          end
    
    
          it 'should remove the contact from all aspects they are in' do
    
            contact = alice.contact_for(bob.person)
    
            new_aspect = alice.aspects.create(:name => 'new')
            alice.add_contact_to_aspect(contact, new_aspect)
    
            lambda {
              alice.disconnect(contact)
            }.should change(contact.aspects(true), :count).from(2).to(0)
          end
    
    Raphael's avatar
    Raphael a validé
        end
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
      describe '#register_share_visibilities' do
    
        it 'creates post visibilites for up to 100 posts' do
    
          Post.stub_chain(:where, :limit).and_return([Factory(:status_message)])
    
          c = Contact.create!(:user_id => alice.id, :person_id => eve.person.id)
          expect{
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
            alice.register_share_visibilities(c)
          }.to change(ShareVisibility, :count).by(1)
    
      describe '#share_with' do
        it 'finds or creates a contact' do
          lambda {
            alice.share_with(eve.person, alice.aspects.first)
          }.should change(alice.contacts, :count).by(1)
        end
    
        it 'does not set mutual on intial share request' do
          alice.share_with(eve.person, alice.aspects.first)
          alice.contacts.find_by_person_id(eve.person.id).should_not be_mutual
        end
    
        it 'does set mutual on share-back request' do
          eve.share_with(alice.person, eve.aspects.first)
          alice.share_with(eve.person, alice.aspects.first)
    
          alice.contacts.find_by_person_id(eve.person.id).should be_mutual
        end
    
        it 'adds a contact to an aspect' do
          contact = alice.contacts.create(:person => eve.person)
          alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
    
          lambda {
            alice.share_with(eve.person, alice.aspects.first)
          }.should change(contact.aspects, :count).by(1)
        end
    
    
    Manuel Schölling's avatar
    Manuel Schölling a validé
        it 'calls #register_share_visibilities with a contact' do
          eve.should_receive(:register_share_visibilities)
    
          eve.share_with(alice.person, eve.aspects.first)
        end
    
    
        context 'dispatching' do
          it 'dispatches a request on initial request' do
            contact = alice.contacts.new(:person => eve.person)
            alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
    
            contact.should_receive(:dispatch_request)
            alice.share_with(eve.person, alice.aspects.first)
          end
    
          it 'dispatches a request on a share-back' do
            eve.share_with(alice.person, eve.aspects.first)
    
            contact = alice.contact_for(eve.person)
    
            alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
    
            contact.should_receive(:dispatch_request)
            alice.share_with(eve.person, alice.aspects.first)
          end
    
    
          it 'does not dispatch a request if contact already marked as receiving' do
    
            a2 = alice.aspects.create(:name => "two")
    
    
            contact = alice.contacts.create(:person => eve.person, :receiving => true)
    
            alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)
    
            contact.should_not_receive(:dispatch_request)
            alice.share_with(eve.person, a2)
          end
    
        it 'sets receiving' do
          alice.share_with(eve.person, alice.aspects.first)
          alice.contact_for(eve.person).should be_receiving
        end
    
    danielgrippi's avatar
    danielgrippi a validé
    
        it "should mark the corresponding notification as 'read'" do
    
          notification = Factory(:notification, :target => eve.person)
    
    danielgrippi's avatar
    danielgrippi a validé
    
          Notification.where(:target_id => eve.person.id).first.unread.should be_true
          alice.share_with(eve.person, aspect)
          Notification.where(:target_id => eve.person.id).first.unread.should be_false
        end