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

user_friending_spec.rb « user « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5ec769da8758a09b118c6c14e366d040f866cbd4 (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

#   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::Friending do
  let(:user) { make_user }
  let(:aspect) { user.aspect(:name => 'heroes') }
  let(:aspect1) { user.aspect(:name => 'other') }
  let(:friend) { Factory.create(:person) }

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

  let(:user2) { make_user }
  let(:aspect2) { user2.aspect(:name => "aspect two") }

  before do
    deliverable = Object.new
    deliverable.stub!(:deliver)
    Notifier.stub!(:new_request).and_return(deliverable)
    Notifier.stub!(:request_accepted).and_return(deliverable)
  end

  describe '#contact_for' do

    it 'returns a contact' do
      contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
      user.friends << contact
      user.contact_for(person_one).should be_true
    end

    it 'returns the correct contact' do
      contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
      user.friends << contact

      contact2 = Contact.create(:user => user, :person => person_two, :aspects => [aspect])
      user.friends << contact2

      contact3 = Contact.create(:user => user, :person => person_three, :aspects => [aspect])
      user.friends << contact3

      user.contact_for(person_two).person.should == person_two
    end

    it 'returns nil for a non-contact' do
      user.contact_for(person_one).should be_nil
    end

    it 'returns nil when someone else has contact with the target' do
      contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
      user.friends << contact
      user2.contact_for(person_one).should be_nil
    end
  end


  context 'friend requesting' do
    it "should assign a request to a aspect for the user that sent it out" do
      aspect.requests.size.should == 0

      user.send_friend_request_to(friend, aspect)

      aspect.reload
      aspect.requests.size.should == 1
    end

    describe  '#receive_friend_request' do
      it 'adds a request to pending if it was not sent by user' do
        r = Request.instantiate(:to => user.receive_url, :from => friend)
        r.save
        user.receive_friend_request(r)
        user.reload.pending_requests.should include r
      end

      it 'should autoaccept a request the user sent' do
        request = user.send_friend_request_to(user2.person, aspect)
        request.reverse_for(user2)
        proc{user.receive_friend_request(request)}.should change(user.reload.friends, :count).by(1)
      end
    end

    context 'received a friend request' do

      let(:request_for_user) {Request.instantiate(:to => user.receive_url, :from => friend)}
      let(:request2_for_user) {Request.instantiate(:to => user.receive_url, :from => person_one)}
      let(:request_from_myself) {Request.instantiate(:to => user.receive_url, :from => user.person)}
      before do
        request_for_user.save
        user.receive_friend_request(request_for_user)
        user.receive_friend_request(request2_for_user)
        user.reload
      end

      it "should delete an accepted friend request" do
        proc { user.accept_friend_request(request2_for_user.id, aspect.id) }.should change(
          user.reload.pending_requests, :count ).by(-1)
      end

      it 'should be able to ignore a pending friend request' do
        proc { user.ignore_friend_request(request_for_user.id) }.should change(
          user.reload.pending_requests, :count ).by(-1)
      end

      it 'should ignore a friend request from yourself' do
        
        user.pending_requests.delete_all
        user.save
        request = user.send_friend_request_to(user.person, aspect)
        request.reverse_for(user)
        request.aspect_id = nil
        user.pending_requests.delete_all
        user.save

        proc { user.receive_friend_request(request) }.should change(

          user.reload.pending_requests, :count ).by(0)
      end
    end

    it 'should not be able to friend request an existing friend' do
      friend_users(user, aspect, user2, aspect2)

      proc { user.send_friend_request_to(user2.person, aspect1) }.should raise_error
    end

    it 'should not be able to friend request yourself' do
      proc { user.send_friend_request_to(nil, aspect) }.should raise_error(RuntimeError, /befriend yourself/)
    end

    it 'should send an email on acceptance if a friend request' do
      Notifier.should_receive(:request_accepted)
      request = user.send_friend_request_to(user2.person, aspect)
      request.reverse_for(user2)
      user.receive_friend_request(request)
    end


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

      before do
        user.pending_requests.empty?.should be true
        user.friends.empty?.should be true
        user2.pending_requests.empty?.should be true
        user2.friends.empty?.should be true

        @request       = Request.instantiate(:to => user.receive_url, :from => person_one)
        @request_two   = Request.instantiate(:to => user2.receive_url, :from => person_one)
        @request_three =  Request.instantiate(:to => user2.receive_url, :from => user.person)

        @req_xml       = @request.to_diaspora_xml
        @req_two_xml   = @request_two.to_diaspora_xml
        @req_three_xml = @request_three.to_diaspora_xml

        @request.destroy
        @request_two.destroy
        @request_three.destroy
      end

      context 'request from one remote person to one local user' do
        before do
          user2.receive @req_three_xml, user.person
        end
        it 'should befriend the user other user on the same pod' do
          proc {
            user2.accept_friend_request @request_three.id, aspect2.id
          }.should_not change(Person, :count)
          user2.contact_for(user.person).should_not be_nil
        end

        it 'should not delete the ignored user on the same pod' do
          proc {
            user2.ignore_friend_request @request_three.id
          }.should_not change(Person, :count)
          user2.contact_for(user.person).should be_nil
        end

        it 'sends an email to the receiving user' do
          mail_obj = mock("mailer")
          mail_obj.should_receive(:deliver)
          Notifier.should_receive(:new_request).and_return(mail_obj)
          user.receive @req_xml, person_one
        end
      end

      context 'Two users receiving requests from one person' do
        before do
          user.receive @req_xml, person_one
          user2.receive @req_two_xml, person_one
        end

        describe '#accept_friend_request' do
          it 'should both users should befriend the same person' do
            user.accept_friend_request @request.id, aspect.id
            user.contact_for(person_one).should_not be_nil

            user2.accept_friend_request @request_two.id, aspect2.id
            user2.contact_for(person_one).should_not be_nil
          end

          it 'should keep the person around if one of the users rejects him' do
            user.accept_friend_request @request.id, aspect.id
            user.contact_for(person_one).should_not be_nil

            user2.ignore_friend_request @request_two.id
            user2.contact_for(person_one).should be_nil
          end
        end


        it 'should keep the person around if the users ignores them' do
          user.ignore_friend_request user.pending_requests.first.id
          user.contact_for(person_one).should be_nil

          user2.ignore_friend_request user2.pending_requests.first.id #@request_two.id
          user2.contact_for(person_one).should be_nil
        end
      end


    end

    describe 'a user accepting rejecting multiple people' do
      before do
        user.pending_requests.empty?.should be true
        user.friends.empty?.should be true

        @request = Request.instantiate(:to => user.receive_url, :from => person_one)
        @request_two = Request.instantiate(:to => user.receive_url, :from => person_two)
      end

      it "keeps the right counts of friends" do
        user.receive_friend_request @request

        person_two.destroy
        user.reload.pending_requests.size.should be 1
        user.friends.size.should be 0

        user.receive_friend_request @request_two
        user.reload.pending_requests.size.should be 2
        user.friends.size.should be 0

        user.accept_friend_request @request.id, aspect.id
        user.reload.pending_requests.size.should be 1
        user.friends.size.should be 1
        user.contact_for(person_one).should_not be_nil

        user.ignore_friend_request @request_two.id
        user.reload.pending_requests.size.should be 0
        user.friends.size.should be 1
        user.contact_for(person_two).should be_nil
      end
    end

    describe 'unfriending' do
      before do
        friend_users(user, aspect, user2, aspect2)
      end

      it 'should unfriend the other user on the same seed' do
        lambda { 
          user2.unfriend user.person }.should change {
          user2.reload.friends.count }.by(-1)
        aspect2.reload.people.count.should == 0
      end

      it 'is unfriended by another user' do
        lambda { user.unfriended_by user2.person }.should change {
          user.friends.count }.by(-1)
        aspect.reload.people.count.should == 0
      end

      it 'should remove the friend from all aspects they are in' do
        user.add_person_to_aspect(user2.person.id, aspect1.id)
        aspect.reload.people.count.should == 1
        aspect1.reload.people.count.should == 1
        lambda { user.unfriended_by user2.person }.should change {
          user.friends.count }.by(-1)
        aspect.reload.people.count.should == 0
        aspect1.reload.people.count.should == 0
      end

      context 'with a post' do
        before do
          @message = user.post(:status_message, :message => "hi", :to => aspect.id)
          user2.receive @message.to_diaspora_xml.to_s, user.person
          user2.unfriend user.person
          user.unfriended_by user2.person
        end
        it "deletes the unfriended user's posts from visible_posts" do
          user.reload.raw_visible_posts.include?(@message.id).should be_false
        end
        it "deletes the unfriended user's posts from the aspect's posts" do
          aspect2.posts.include?(@message).should be_false
        end
      end
    end
  end
end