Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connecting_spec.rb « user « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 836a4195008805309ac6be1ccfe758df9f94d333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe Diaspora::UserModules::Connecting do

  let(:aspect) { alice.aspects.first }
  let(:aspect1) { alice.aspects.create(:name => 'other') }
  let(:person) { Factory.create(:person) }

  let(:aspect2) { eve.aspects.create(:name => "aspect two") }

  let(:person_one) { Factory.create :person }
  let(:person_two) { Factory.create :person }
  let(:person_three) { Factory.create :person }

  describe '#send_contact_request_to' do
    it 'should not be able to contact request an existing contact' do
      alice.activate_contact(eve.person, aspect1)

      proc {
        alice.send_contact_request_to(eve.person, aspect1)
      }.should raise_error(ActiveRecord::RecordInvalid)
    end

    it 'should not be able to contact request no-one' do
      proc {
        alice.send_contact_request_to(nil, aspect)
      }.should raise_error(ActiveRecord::RecordInvalid)
    end
    it 'creates a pending contact' do
      proc {
        alice.send_contact_request_to(eve.person, aspect1)
      }.should change(Contact.unscoped, :count).by(1)
      alice.contact_for(eve.person).pending.should == true
      alice.contact_for(eve.person).should be_pending
    end
    it 'persists no request for requester' do
      proc {
        alice.send_contact_request_to(eve.person, aspect1)
      }.should_not change{Request.where(:recipient_id => alice.person.id).count}
    end
    it 'persists a request for the recipient' do
      alice.send_contact_request_to(eve.person, aspect1)
      eve.request_from(alice.person).should_not be_nil
    end
  end

  context 'contact requesting' do
    describe  '#receive_contact_request' do
      before do
        @r = Request.diaspora_initialize(:to => alice.person, :from => person)
      end

      it 'adds a request to pending if it was not sent by user' do
        alice.receive_contact_request(@r)
        Request.where(:recipient_id => alice.person.id).all.should include @r
      end

      it 'creates no contact' do
        lambda {
          received_req = @r.receive(alice, person_one)
        }.should_not change(Contact, :count)
      end
    end

    describe '#receive_request_acceptance' do
      before do
        @original_request = alice.send_contact_request_to(eve.person, aspect)
        @acceptance = @original_request.reverse_for(eve)
      end
      it 'connects to the acceptor' do
        alice.receive_contact_request(@acceptance)
        alice.contact_for(eve.person).should_not be_nil
      end
      it 'deletes the acceptance' do
        alice.receive_contact_request(@acceptance)
        Request.where(:sender_id => eve.person.id, :recipient_id => alice.person.id).should be_empty
      end
    end

    context 'received a contact request' do
      let(:request_for_user) {Request.diaspora_initialize(:to => alice.person, :from => person)}
      let(:request2_for_user) {Request.diaspora_initialize(:to => alice.person, :from => person_one)}
      let(:request_from_myself) {Request.diaspora_initialize(:to => alice.person, :from => alice.person)}
      before do
        Request.diaspora_initialize(:from => person, :to => alice.person).save
        Request.diaspora_initialize(:from => person_one, :to => alice.person).save

        @received_request = Request.where(:sender_id => person.id, :recipient_id => alice.person.id).first
        @received_request2 = Request.where(:sender_id => person_one.id, :recipient_id => alice.person.id).first
      end

      it "should delete an accepted contact request" do
        proc {
          alice.accept_contact_request(@received_request, aspect)
        }.should change(Request, :count ).by(-1)
      end

      it "should mark the corresponding notification as 'read'" do
        notification = Factory.create(:notification, :target => @received_request)

        Notification.where(:target_id=>@received_request.id).first.unread.should be_true
        alice.accept_contact_request(@received_request, aspect)
        Notification.where(:target_id=>@received_request.id).first.unread.should be_false
      end

      it 'should be able to ignore a pending contact request' do
        proc { alice.ignore_contact_request(@received_request.id)
        }.should change(Request, :count ).by(-1)
      end

      it 'should ignore a contact request from yourself' do
        reversed_request = request_from_myself.reverse_for(alice)

        alice.receive_contact_request(reversed_request)
        reversed_request.persisted?.should be false
      end
    end

    describe 'multiple users accepting/rejecting the same person' do

      before do
        @request1 = Request.diaspora_initialize(:to => alice.person, :from => person_one)
        @request2 = Request.diaspora_initialize(:to => eve.person, :from => person_one)
        @request3 =  Request.diaspora_initialize(:to => eve.person, :from => alice.person)

        @req1_xml = @request1.to_diaspora_xml
        @req2_xml = @request2.to_diaspora_xml
        @req3_xml = @request3.to_diaspora_xml

        @request1.destroy
        @request2.destroy
        @request3.destroy
      end

      context 'request from one remote person to one local user' do
        before do
          zord = Postzord::Receiver.new(alice, :person => alice.person)
          @received_request = zord.parse_and_receive(@req3_xml)
          @received_request.reload
        end

        it 'should connect the user other user on the same pod' do
          proc {
            eve.accept_contact_request(@received_request, aspect2)
          }.should_not change(Person, :count)
          eve.contact_for(alice.person).should_not be_nil
        end

        it 'should not delete the ignored user on the same pod' do

          proc {
            eve.ignore_contact_request(@received_request.id)
          }.should_not change(Person, :count)
          eve.contact_for(alice.person).should be_nil
        end
      end

      context 'Two users receiving requests from one person' do
        before do
          zord1 = Postzord::Receiver.new(alice, :person => person_one)
          zord2 = Postzord::Receiver.new(alice, :person => person_one)

          @req_to_user = zord1.parse_and_receive(@req1_xml)
          @req_to_eve = zord2.parse_and_receive(@req2_xml)
        end

        describe '#accept_contact_request' do
          it 'should both users should connect the same person' do
            alice.accept_contact_request @req_to_user, aspect
            alice.contact_for(person_one).should_not be_nil

            eve.accept_contact_request @req_to_eve, aspect2
            eve.contact_for(person_one).should_not be_nil
          end

          it 'should keep the person around if one of the users rejects him' do
            alice.accept_contact_request @req_to_user, aspect
            alice.contact_for(person_one).should_not be_nil

            eve.ignore_contact_request @req_to_eve.id
            eve.contact_for(person_one).should be_nil
          end
        end


        it 'should keep the person around if the users ignores them' do
          alice.ignore_contact_request Request.where(:recipient_id => alice.person.id).first.id
          alice.contact_for(person_one).should be_nil

          eve.ignore_contact_request Request.where(:recipient_id => eve.person.id).first.id
          eve.contact_for(person_one).should be_nil
        end
      end


    end

    describe 'a user accepting & rejecting multiple people' do
      before do
        request = Request.diaspora_initialize(:to => alice.person, :from => person_one)
        @received_request = request.receive(alice, person_one)
      end
      describe '#accept_contact_request' do
        it "deletes the received request" do
          lambda {
            alice.accept_contact_request(@received_request, aspect)
          }.should change(Request, :count).by(-1)
        end
        it "creates a new contact" do
          lambda {
            alice.accept_contact_request(@received_request, aspect)
          }.should change(Contact, :count).by(1)
          alice.contact_for(person_one).should_not be_nil
        end
      end
      describe '#ignore_contact_request' do
        it "removes the request" do
          lambda {
            alice.ignore_contact_request(@received_request.id)
          }.should change(Request, :count).by(-1)
        end
        it "creates no new contact" do
          lambda {
            alice.ignore_contact_request(@received_request)
          }.should_not change(Contact, :count)
        end
      end
    end

    describe 'disconnecting' do

      describe 'disconnected_by' do
        it 'is disconnected by another user' do
          lambda { alice.disconnected_by bob.person }.should change {
            alice.contacts.count }.by(-1)
          alice.aspects.first.contacts.count.should == 0
        end

        it 'deletes incoming requests' do
          alice.send_contact_request_to(eve.person, alice.aspects.first)
          Request.where(:recipient_id => eve.person.id, :sender_id => alice.person.id).first.should_not be_nil
          eve.disconnected_by(alice.person)
          Request.where(:recipient_id => eve.person.id, :sender_id => alice.person.id).first.should be_nil
        end
      end

      it 'disconnects a contact on the same seed' do
        bob.aspects.first.contacts.count.should == 2
        lambda {
          bob.disconnect bob.contact_for(alice.person) }.should change {
          bob.contacts(true).count }.by(-1)
        bob.aspects.first.contacts(true).count.should == 1
      end

      it 'should remove the contact from all aspects they are in' do
        new_aspect = alice.aspects.create(:name => 'new')
        alice.add_contact_to_aspect( alice.contact_for(bob.person), new_aspect)
        alice.aspects.first.reload.contacts.count.should == 1
        new_aspect.reload.contacts.count.should == 1
        lambda { alice.disconnected_by bob.person }.should change {
          alice.contacts.count }.by(-1)
        alice.aspects.first.reload.contacts.count.should == 0
        new_aspect.reload.contacts.count.should == 0
      end

      context 'with a post' do
        before do
          StatusMessage.delete_all
          @message = alice.post(:status_message, :text => "hi", :to => alice.aspects.first.id)
        end

        it "deletes the disconnected user's posts from visible_posts" do
          bob.reload.raw_visible_posts.include?(@message).should be_true
          bob.disconnect bob.contact_for(alice.person)
          bob.reload.raw_visible_posts.include?(@message).should be_false
        end

      end
    end
  end

  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 '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

    it 'dispatches a 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 'does not dispatch a request' do
      contact = alice.contacts.create(:person => eve.person)
      alice.contacts.stub!(:find_or_initialize_by_person_id).and_return(contact)

      contact.should_not_receive(:dispatch_request)
      alice.share_with(eve.person, alice.aspects.first)
    end
  end
end