connecting_spec.rb 7.48 KB
Newer Older
1 2
# frozen_string_literal: true

danielgrippi's avatar
danielgrippi committed
3
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
Raphael's avatar
Raphael committed
4
#   licensed under the Affero General Public License version 3 or later.  See
Raphael's avatar
Raphael committed
5
#   the COPYRIGHT file.
6

7 8 9
describe User::Connecting, type: :model do
  let(:aspect1) { alice.aspects.first }
  let(:aspect2) { alice.aspects.create(name: "other") }
10

Jonne Haß's avatar
Jonne Haß committed
11
  let(:person) { FactoryGirl.create(:person) }
12

13 14 15 16 17 18 19
  describe "disconnecting" do
    describe "#disconnected_by" do
      it "removes contact sharing flag" do
        expect(bob.contacts.find_by(person_id: alice.person.id)).to be_sharing
        bob.disconnected_by(alice.person)
        expect(bob.contacts.find_by(person_id: alice.person.id)).not_to be_sharing
      end
20

21 22
      it "removes contact if not receiving" do
        eve.contacts.create(person: alice.person)
Sarah Mei's avatar
Sarah Mei committed
23

24
        expect {
25
          eve.disconnected_by(alice.person)
26
        }.to change(eve.contacts, :count).by(-1)
27
      end
28

29 30 31
      it "does not remove contact if disconnect twice" do
        contact = bob.contact_for(alice.person)
        expect(contact).to be_receiving
32

33 34 35
        expect {
          bob.disconnected_by(alice.person)
          bob.disconnected_by(alice.person)
36
        }.not_to change(bob.contacts, :count)
37

38 39 40
        contact.reload
        expect(contact).not_to be_sharing
        expect(contact).to be_receiving
41 42
      end

43
      it "removes notitications" do
44
        alice.share_with(eve.person, alice.aspects.first)
45
        expect(Notifications::StartedSharing.where(recipient_id: eve.id).first).not_to be_nil
46
        eve.disconnected_by(alice.person)
47
        expect(Notifications::StartedSharing.where(recipient_id: eve.id).first).to be_nil
48
      end
49 50
    end

51 52 53 54
    describe "#disconnect" do
      it "removes a contacts receiving flag" do
        expect(bob.contacts.find_by(person_id: alice.person.id)).to be_receiving
        bob.disconnect(bob.contact_for(alice.person))
55
        expect(bob.contacts.reload.find_by(person_id: alice.person.id)).not_to be_receiving
56 57 58 59 60 61 62
      end

      it "removes contact if not sharing" do
        contact = alice.share_with(eve.person, alice.aspects.first)

        expect {
          alice.disconnect(contact)
63
        }.to change(alice.contacts, :count).by(-1)
64 65 66
      end

      it "does not remove contact if disconnect twice" do
67
        contact = bob.contact_for(alice.person)
68 69 70 71 72
        expect(contact).to be_sharing

        expect {
          alice.disconnect(contact)
          alice.disconnect(contact)
73
        }.not_to change(bob.contacts, :count)
ilya's avatar
ilya committed
74

75 76 77
        contact.reload
        expect(contact).not_to be_receiving
        expect(contact).to be_sharing
danielgrippi's avatar
danielgrippi committed
78
      end
79

Benjamin Neff's avatar
Benjamin Neff committed
80 81 82 83 84 85 86 87 88 89 90 91
      it "dispatches a retraction for local person" do
        contact = bob.contact_for(eve.person)

        expect(contact.person.owner).to receive(:disconnected_by).with(bob.person)

        bob.disconnect(contact)
      end

      it "dispatches a retraction for remote person" do
        contact = local_leia.contact_for(remote_raphael)
        retraction = double

92
        expect(contact).to receive(:receiving=).with(false)
Benjamin Neff's avatar
Benjamin Neff committed
93 94
        expect(Retraction).to receive(:for).with(contact).and_return(retraction)
        expect(retraction).to receive(:defer_dispatch).with(local_leia)
95

Benjamin Neff's avatar
Benjamin Neff committed
96
        local_leia.disconnect(contact)
97
      end
98

99
      it "should remove the contact from all aspects they are in" do
100
        contact = alice.contact_for(bob.person)
101
        alice.add_contact_to_aspect(contact, aspect2)
102

103
        expect {
104
          alice.disconnect(contact)
105
        }.to change(contact.aspects, :count).from(2).to(0)
106
      end
107 108 109 110 111 112 113 114 115 116

      it "raises when a contact for an improperly deleted user was passed" do
        contact = alice.contact_for(bob.person)

        bob.delete
        expect {
          alice.disconnect(contact)
        }.to raise_error "FATAL: user entry is missing from the DB. Aborting"
        expect(Contact.where(id: contact.id)).to exist
      end
117
    end
118
  end
119

120 121
  describe "#share_with" do
    it "finds or creates a contact" do
122
      expect {
123
        alice.share_with(eve.person, alice.aspects.first)
124
      }.to change(alice.contacts, :count).by(1)
125
    end
126

127
    it "does not set mutual on intial share request" do
128
      alice.share_with(eve.person, alice.aspects.first)
129
      expect(alice.contacts.find_by(person_id: eve.person.id)).not_to be_mutual
130 131
    end

132
    it "does set mutual on share-back request" do
133 134
      eve.share_with(alice.person, eve.aspects.first)
      alice.share_with(eve.person, alice.aspects.first)
135

136
      expect(alice.contacts.find_by(person_id: eve.person.id)).to be_mutual
137
    end
138

139 140
    it "adds a contact to an aspect" do
      contact = alice.contacts.create(person: eve.person)
141
      allow(alice.contacts).to receive(:find_or_initialize_by).and_return(contact)
142

143
      expect {
144
        alice.share_with(eve.person, alice.aspects.first)
145
      }.to change(contact.aspects, :count).by(1)
146 147
    end

148 149 150 151 152
    context "dispatching" do
      it "dispatches a request on initial request" do
        contact = alice.contacts.new(person: eve.person)
        expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact)

153 154
        allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, contact)
155

156 157
        alice.share_with(eve.person, alice.aspects.first)
      end
158

159
      it "dispatches a request on a share-back" do
160
        eve.share_with(alice.person, eve.aspects.first)
161

162
        contact = alice.contact_for(eve.person)
163 164
        expect(alice.contacts).to receive(:find_or_initialize_by).and_return(contact)

165 166
        allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, contact)
167 168 169 170

        alice.share_with(eve.person, alice.aspects.first)
      end

171 172
      it "does not dispatch a request if contact already marked as receiving" do
        contact = alice.contacts.create(person: eve.person, receiving: true)
173
        allow(alice.contacts).to receive(:find_or_initialize_by).and_return(contact)
174

175 176
        allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, instance_of(Profile))
        expect(Diaspora::Federation::Dispatcher).not_to receive(:defer_dispatch).with(alice, instance_of(Contact))
177 178

        alice.share_with(eve.person, aspect2)
179
      end
180

181
      it "delivers profile for remote persons" do
182
        allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
183 184 185 186 187 188 189 190 191
        expect(Diaspora::Federation::Dispatcher)
          .to receive(:defer_dispatch).with(alice, alice.profile, subscriber_ids: [remote_raphael.id])

        alice.share_with(remote_raphael, alice.aspects.first)
      end

      it "does not deliver profile for remote persons" do
        allow(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch)
        expect(Diaspora::Federation::Dispatcher).not_to receive(:defer_dispatch).with(alice, alice.profile, anything)
192

193 194
        alice.share_with(eve.person, alice.aspects.first)
      end
195
    end
196

197
    it "sets receiving" do
198
      alice.share_with(eve.person, alice.aspects.first)
199
      expect(alice.contact_for(eve.person)).to be_receiving
200
    end
danielgrippi's avatar
danielgrippi committed
201 202

    it "should mark the corresponding notification as 'read'" do
203 204
      FactoryGirl.create(:notification, target: eve.person, recipient: alice, type: "Notifications::StartedSharing")
      expect(Notifications::StartedSharing.find_by(recipient_id: alice.id, target: eve.person).unread).to be_truthy
danielgrippi's avatar
danielgrippi committed
205

206 207
      alice.share_with(eve.person, aspect1)
      expect(Notifications::StartedSharing.find_by(recipient_id: alice.id, target: eve.person).unread).to be_falsey
danielgrippi's avatar
danielgrippi committed
208
    end
209
  end
210
end