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

person_spec.rb « models « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dbad049abc2ed82cd526de41180ffe82677638de (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#   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 Person do

  before do
    @user = bob
    @person  = Factory.create(:person)
  end

  describe "delegating" do
    it "delegates last_name to the profile" do
      @person.last_name.should == @person.profile.last_name
      @person.profile.update_attributes(:last_name => "Heathers")
      @person.reload.last_name.should == "Heathers"
    end
  end

  describe "vaild url" do
    it 'should allow for https urls' do
      person = Factory.create(:person, :url => "https://example.com")
      person.should be_valid
    end

    it 'should always return the correct receive url' do
      person = Factory.create(:person, :url => "https://example.com/a/bit/messed/up")
      person.receive_url.should == "https://example.com/receive/users/#{person.guid}/"
    end

    it 'should allow ports in the url' do
      person = Factory.create(:person, :url => "https://example.com:3000/")
      person.url.should == "https://example.com:3000/"
    end
  end


  describe '#diaspora_handle' do
    context 'local people' do
      it 'uses the pod config url to set the diaspora_handle' do
        new_user = Factory.create(:user)
        new_user.person.diaspora_handle.should == new_user.username + "@" + AppConfig[:pod_uri].host
      end
    end

    context 'remote people' do
      it 'stores the diaspora_handle in the database' do
        @person.diaspora_handle.include?(AppConfig[:pod_uri].host).should be false
      end
    end

    describe 'validation' do
      it 'is unique' do
        person_two = Factory.build(:person, :diaspora_handle => @person.diaspora_handle)
        person_two.should_not be_valid
      end

      it 'is case insensitive' do
        person_two = Factory.build(:person, :diaspora_handle => @person.diaspora_handle.upcase)
        person_two.should_not be_valid
      end
    end
  end

  context '.name_from_attrs' do
    before do
      @person = alice.person
      @profile = @person.profile
    end

    context 'with first name' do
      it 'should return their name for name' do
        Person.name_from_attrs(@profile.first_name, @profile.last_name, @profile.diaspora_handle).should match /#{@profile.first_name}|#{@profile.last_name}/
      end
    end

    context 'without first name' do
      it 'should display their diaspora handle' do
        Person.name_from_attrs(nil, nil, @profile.diaspora_handle).should == @profile.diaspora_handle
      end
    end
  end

  context '#name' do
    it 'calls Person.name_from_attrs' do
      profile = alice.person.profile
      Person.should_receive(:name_from_attrs).with(profile.first_name, profile.last_name, profile.person.diaspora_handle)
      alice.name
    end
  end

  describe 'xml' do
    before do
      @xml = @person.to_xml.to_s
    end

    it 'should serialize to xml' do
      @xml.include?("person").should == true
    end

    it 'should have a profile in its xml' do
      @xml.include?("first_name").should == true

    end
  end

  it '#owns? posts' do
    person_message = Factory.create(:status_message, :author => @person)
    person_two = Factory.create(:person)

    @person.owns?(person_message).should be true
    person_two.owns?(person_message).should be false
  end

  describe '#remove_all_traces' do
    before do
      @deleter = Factory(:person)
      @status = Factory.create(:status_message, :author => @deleter)
      @other_status = Factory.create(:status_message, :author => @person)
    end

    it "deletes all notifications from a person's actions" do
      note = Factory(:notification, :actors => [@deleter], :recipient => @user)
      @deleter.destroy
      Notification.where(:id => note.id).first.should be_nil
    end

    it "deletes all contacts pointing towards a person" do
      @user.activate_contact(@deleter, @user.aspects.first)
      @deleter.destroy
      @user.contact_for(@deleter).should be_nil
    end

    it "deletes all of a person's posts upon person deletion" do
      lambda {@deleter.destroy}.should change(Post, :count).by(-1)
    end

    it "deletes a person's profile" do
      lambda {
        @deleter.destroy
      }.should change(Profile, :count).by(-1)
    end

    it 'deletes all requests to a person' do
      alice.send_contact_request_to(eve.person, alice.aspects.first)
      Request.count.should == 1
      lambda {
        eve.person.destroy
      }.should change(Request, :count).by(-1)
    end

    it 'deletes all requests from a person' do
      Request.create(:sender_id => @deleter.id, :recipient_id => alice.person.id)
      Request.count.should == 1
      lambda {
        @deleter.destroy
      }.should change(Request, :count).by(-1)
    end

    it "deletes a person's comments on person deletion" do
      Factory.create(:comment, :author_id => @deleter.id, :diaspora_handle => @deleter.diaspora_handle, :text => "i love you",     :post => @other_status)
      Factory.create(:comment, :author_id => @person.id,:diaspora_handle => @person.diaspora_handle,  :text => "you are creepy", :post => @other_status)

      lambda {@deleter.destroy}.should change(Comment, :count).by(-1)
    end
  end

  describe "disconnecting" do
    before do
      @user2   = Factory(:user)
      @aspect  = @user.aspects.create(:name => "Dudes")
      @aspect2 = @user2.aspects.create(:name => "Abscence of Babes")
    end
    it 'should not delete an orphaned contact' do
      @user.activate_contact(@person, @aspect)

      lambda {@user.disconnect(@user.contact_for(@person))}.should_not change(Person, :count)
    end

    it 'should not delete an un-orphaned contact' do
      @user.activate_contact(@person, @aspect)
      @user2.activate_contact(@person, @aspect2)

      lambda {@user.disconnect(@user.contact_for(@person))}.should_not change(Person, :count)
    end
  end

  describe '.search' do
    before do
      Person.delete_all
      @user = Factory.create(:user_with_aspect)
      user_profile = @user.person.profile
      user_profile.first_name = "aiofj"
      user_profile.last_name = "asdji"
      user_profile.save

      @robert_grimm = Factory.create(:searchable_person)
      @eugene_weinstein = Factory.create(:searchable_person)
      @yevgeniy_dodis = Factory.create(:searchable_person)
      @casey_grippi = Factory.create(:searchable_person)

      @robert_grimm.profile.first_name = "Robert"
      @robert_grimm.profile.last_name  = "Grimm"
      @robert_grimm.profile.save
      @robert_grimm.reload

      @eugene_weinstein.profile.first_name = "Eugene"
      @eugene_weinstein.profile.last_name  = "Weinstein"
      @eugene_weinstein.profile.save
      @eugene_weinstein.reload

      @yevgeniy_dodis.profile.first_name = "Yevgeniy"
      @yevgeniy_dodis.profile.last_name  = "Dodis"
      @yevgeniy_dodis.profile.save
      @yevgeniy_dodis.reload

      @casey_grippi.profile.first_name = "Casey"
      @casey_grippi.profile.last_name  = "Grippi"
      @casey_grippi.profile.save
      @casey_grippi.reload
    end
    it 'is ordered by last name' do
      @robert_grimm.profile.first_name = "AAA"
      @robert_grimm.profile.save

      @eugene_weinstein.profile.first_name = "AAA"
      @eugene_weinstein.profile.save

      @yevgeniy_dodis.profile.first_name = "AAA"
      @yevgeniy_dodis.profile.save

      @casey_grippi.profile.first_name = "AAA"
      @casey_grippi.profile.save

      people = Person.search("AAA", @user)
      people.map{|p| p.name}.should == [@yevgeniy_dodis, @robert_grimm, @casey_grippi, @eugene_weinstein].map{|p|p.name}
    end

    it 'should return nothing on an empty query' do
      people = Person.search("", @user)
      people.empty?.should be true
    end

    it 'should return nothing on a two character query' do
      people = Person.search("in", @user)
      people.empty?.should be true
    end

    it 'should yield search results on partial names' do
      people = Person.search("Eug", @user)
      people.count.should == 1
      people.first.should == @eugene_weinstein

      people = Person.search("wEi", @user)
      people.count.should == 1
      people.first.should == @eugene_weinstein

      people = Person.search("gri", @user)
      people.count.should == 2
      people.first.should == @robert_grimm
      people.second.should == @casey_grippi
    end

    it 'gives results on full names' do
      people = Person.search("Casey Grippi", @user)
      people.count.should == 1
      people.first.should == @casey_grippi
    end

    it 'only displays searchable people' do
      invisible_person = Factory(:person, :profile => Factory.build(:profile,:searchable => false, :first_name => "johnson"))
      Person.search("johnson", @user).should_not include invisible_person
      Person.search("", @user).should_not include invisible_person
    end

    it 'searches on handles' do
      people = Person.search(@robert_grimm.diaspora_handle, @user)
      people.should == [@robert_grimm]
    end

    it "puts the searching user's contacts first" do
      @robert_grimm.profile.first_name = "AAA"
      @robert_grimm.profile.save

      @eugene_weinstein.profile.first_name = "AAA"
      @eugene_weinstein.profile.save

      @yevgeniy_dodis.profile.first_name = "AAA"
      @yevgeniy_dodis.profile.save

      @casey_grippi.profile.first_name = "AAA"
      @casey_grippi.profile.save

      @user.activate_contact(@casey_grippi, @user.aspects.first)

      people = Person.search("AAA", @user)
      people.map{|p| p.name}.should == [@casey_grippi, @yevgeniy_dodis, @robert_grimm, @eugene_weinstein].map{|p|p.name}
    end
    it "puts the searching user's incoming requests first" do
      requestor = Factory(:user_with_aspect)
      profile = requestor.person.profile
      profile.first_name = "AAA"
      profile.last_name = "Something"
      profile.save

      @robert_grimm.profile.first_name = "AAA"
      @robert_grimm.profile.save

      @eugene_weinstein.profile.first_name = "AAA"
      @eugene_weinstein.profile.save

      @yevgeniy_dodis.profile.first_name = "AAA"
      @yevgeniy_dodis.profile.save

      @casey_grippi.profile.first_name = "AAA"
      @casey_grippi.profile.save

      requestor.send_contact_request_to(@user.person, requestor.aspects.first)
      people = Person.search("AAA", @user)
      people.map{|p| p.name}.should == [requestor.person, @yevgeniy_dodis, @robert_grimm, @casey_grippi, @eugene_weinstein].map{|p|p.name}
    end
  end

  context 'people finders for webfinger' do
    let(:user) {Factory(:user)}
    let(:person) {Factory(:person)}

    describe '.by_account_identifier' do
      it 'should find a local users person' do
        p = Person.by_account_identifier(user.diaspora_handle)
        p.should == user.person
      end

      it 'should find remote users person' do
        p = Person.by_account_identifier(person.diaspora_handle)
        p.should == person
      end

      it 'should downcase and strip the diaspora_handle' do
        dh_upper = "    " + user.diaspora_handle.upcase + "   "
        Person.by_account_identifier(dh_upper).should == user.person
      end

      it "finds a local person with a mixed-case username" do
        user = Factory(:user, :username => "SaMaNtHa")
        person = Person.by_account_identifier(user.person.diaspora_handle)
        person.should == user.person
      end

      it "is case insensitive" do
        user1 = Factory(:user, :username => "SaMaNtHa")
        person = Person.by_account_identifier(user1.person.diaspora_handle.upcase)
        person.should == user1.person
      end

      it 'should only find people who are exact matches (1/2)' do
        user = Factory(:user, :username => "SaMaNtHa")
        person = Factory(:person, :diaspora_handle => "tomtom@tom.joindiaspora.com")
        user.person.diaspora_handle = "tom@tom.joindiaspora.com"
        user.person.save
        Person.by_account_identifier("tom@tom.joindiaspora.com").diaspora_handle.should == "tom@tom.joindiaspora.com"
      end

      it 'should only find people who are exact matches (2/2)' do
        person = Factory(:person, :diaspora_handle => "tomtom@tom.joindiaspora.com")
        person1 = Factory(:person, :diaspora_handle => "tom@tom.joindiaspora.comm")
        f = Person.by_account_identifier("tom@tom.joindiaspora.com")
        f.should be nil
      end


    end

    describe '.local_by_account_identifier' do
      it 'should find local users people' do
        p = Person.local_by_account_identifier(user.diaspora_handle)
        p.should == user.person
      end

      it 'should not find a remote person' do
        p = Person.local_by_account_identifier(@person.diaspora_handle)
        p.should be nil
      end

      it 'should call .by_account_identifier' do
        Person.should_receive(:by_account_identifier)
        Person.local_by_account_identifier(@person.diaspora_handle)
      end
    end
  end
end