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

people_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e979a9c00f753718548864c793ff6132366c2d7d (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

describe PeopleController, :type => :controller do
  include_context :gon

  before do
    @user = alice
    @aspect = @user.aspects.first
    sign_in @user, scope: :user
  end

  describe '#index (search)' do
    before do
      @eugene = FactoryGirl.create(
        :person,
        profile: FactoryGirl.build(:profile, first_name: "Eugene", last_name: "w")
      )
      @korth = FactoryGirl.create(
        :person,
        profile: FactoryGirl.build(:profile, first_name: "Evan", last_name: "Korth")
      )
      @closed = FactoryGirl.create(
        :person,
        closed_account: true,
        profile:        FactoryGirl.build(:profile, first_name: "Closed", last_name: "Account")
      )
    end

    describe 'via json' do
      it 'succeeds' do
        get :index, params: {q: "Korth"}, format: :json
        expect(response).to be_success
      end

      it 'responds with json' do
        get :index, params: {q: "Korth"}, format: :json
        expect(response.body).to eq([@korth].to_json)
      end

      it 'does not assign hashes' do
        get :index, params: {q: "Korth"}, format: :json
        expect(assigns[:hashes]).to be_nil
      end

      it "doesn't include closed accounts" do
        get :index, params: {q: "Closed"}, format: :json
        expect(JSON.parse(response.body).size).to eq(0)
        get :index, params: {q: @closed.diaspora_handle}, format: :json
        expect(JSON.parse(response.body).size).to eq(0)
      end
    end

    describe 'via html' do
      context 'query is a diaspora ID' do
        before do
          @unsearchable_eugene = FactoryGirl.create(:person, :diaspora_handle => "eugene@example.org",
                                         :profile => FactoryGirl.build(:profile, :first_name => "Eugene",
                                                                   :last_name => "w", :searchable => false))
        end
        it 'finds people even if they have searchable off' do
          get :index, params: {q: "eugene@example.org"}
          expect(assigns[:people][0].id).to eq(@unsearchable_eugene.id)
        end

        it 'downcases the query term' do
          get :index, params: {q: "Eugene@Example.ORG"}
          expect(assigns[:people][0].id).to eq(@unsearchable_eugene.id)
        end

        it 'does not the background query task if the user is found' do
          get :index, params: {q: "Eugene@Example.ORG"}
          expect(assigns[:background_query]).to eq(nil)
        end

        it 'sets background query task if the user is not found' do
          get :index, params: {q: "Eugene@Example1.ORG"}
          expect(assigns[:background_query]).to eq("eugene@example1.org")
        end

        it "doesn't include closed accounts" do
          get :index, params: {q: @closed.diaspora_handle}
          expect(assigns[:people].size).to eq(0)
        end
      end

      context 'query is not a tag or a diaspora ID' do
        it 'assigns hashes' do
          get :index, params: {q: "Korth"}
          expect(assigns[:hashes]).not_to be_nil
        end

        it 'does not set the background query task' do
          get :index, params: {q: "Korth"}
          expect(assigns[:background_query]).not_to be_present
        end

        it "assigns people" do
          eugene2 = FactoryGirl.create(:person,
                            :profile => FactoryGirl.build(:profile, :first_name => "Eugene",
                                                      :last_name => "w"))
          get :index, params: {q: "Eug"}
          expect(assigns[:people].map { |x| x.id }).to match_array([@eugene.id, eugene2.id])
        end

        it "succeeds if there is exactly one match" do
          get :index, params: {q: "Korth"}
          expect(assigns[:people].length).to eq(1)
          expect(response).to be_success
        end

        it "succeeds if there are no matches" do
          get :index, params: {q: "Korthsauce"}
          expect(assigns[:people].length).to eq(0)
          expect(response).to be_success
        end

        it 'succeeds if you search for the empty term' do
          get :index, params: {q: ""}
          expect(response).to be_success
        end

        it 'succeeds if you search for punctuation' do
          get :index, params: {q: "+"}
          expect(response).to be_success
        end

        it "excludes people who have searchable off" do
          eugene2 = FactoryGirl.create(:person,
                            :profile => FactoryGirl.build(:profile, :first_name => "Eugene",
                                                      :last_name => "w", :searchable => false))
          get :index, params: {q: "Eug"}
          expect(assigns[:people]).not_to match_array([eugene2])
        end

        it "doesn't include closed accounts" do
          get :index, params: {q: "Closed"}
          expect(assigns[:people].size).to eq(0)
        end
      end
    end
  end

  describe "#show performance", :performance => true do
    before do
      require 'benchmark'
      @posts = []
      @users = []
      8.times do |n|
        user = FactoryGirl.create(:user)
        @users << user
        aspect = user.aspects.create(:name => 'people')
        connect_users(@user, @user.aspects.first, user, aspect)

        @posts << @user.post(:status_message, :text => "hello#{n}", :to => aspect.id)
      end
      @posts.each do |post|
        @users.each do |user|
          user.comment!(post, "yo#{post.text}")
        end
      end
    end

    it 'takes time' do
      expect(Benchmark.realtime {
        get :show, params: {id: @user.person.to_param}
      }).to be < 1.0
    end
  end

  describe '#show' do
    before do
      @person = FactoryGirl.create(:user).person
      @presenter = PersonPresenter.new(@person, @user)
    end

    it "404s if the id is invalid" do
      get :show, params: {id: "delicious"}
      expect(response.code).to eq("404")
    end

    it "404s if no person is found via id" do
      get :show, params: {id: "3d920397846"}
      expect(response.code).to eq("404")
    end

    it "404s if no person is found via username" do
      get :show, params: {username: "delicious"}
      expect(response.code).to eq("404")
    end

    it "returns a person presenter" do
      expect(PersonPresenter).to receive(:new).with(@person, @user).and_return(@presenter)
      get :show, params: {username: @person.username}
      expect(assigns(:presenter).to_json).to eq(@presenter.to_json)
    end

    it 'finds a person via username' do
      get :show, params: {username: @person.username}
      expect(assigns(:presenter).to_json).to eq(@presenter.to_json)
    end

    it "404s if no person is found via diaspora handle" do
      get :show, params: {username: "delicious@pod.net"}
      expect(response.code).to eq("404")
    end

    it 'finds a person via diaspora handle' do
      get :show, params: {username: @person.diaspora_handle}
      expect(assigns(:presenter).to_json).to eq(@presenter.to_json)
    end

    it 'redirects home for closed account' do
      @person = FactoryGirl.create(:person, :closed_account => true)
      get :show, params: {id: @person.to_param}
      expect(response).to be_redirect
      expect(flash[:notice]).not_to be_blank
    end

    it 'does not allow xss attacks' do
      user2 = bob
      profile = user2.profile
      profile.update_attribute(:first_name, "</script><script> alert('xss attack');</script>")
      get :show, params: {id: user2.person.to_param}
      expect(response).to be_success
      expect(response.body).not_to include(profile.first_name)
    end

    it "displays the correct number of photos" do
      16.times do |i|
        eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => true)
      end
      get :show, params: {id: eve.person.to_param}
      expect(response.body).to include ',"photos_count":16'

      eve.post(:photo, :user_file => uploaded_photo, :to => eve.aspects.first.id, :public => false)
      get :show, params: {id: eve.person.to_param}
      expect(response.body).to include ',"photos_count":16' # eve is not sharing with alice
    end

    context "when the person is the current user" do
      it "succeeds" do
        get :show, params: {id: @user.person.to_param}
        expect(response).to be_success
      end

      it 'succeeds on the mobile site' do
        get :show, params: {id: @user.person.to_param}, format: :mobile
        expect(response).to be_success
      end

      it "assigns the right person" do
        get :show, params: {id: @person.to_param}
        expect(assigns(:presenter).id).to eq(@presenter.id)
      end
    end

    context "with no user signed in" do
      before do
        sign_out :user
        @person = bob.person
      end

      it "succeeds" do
        get :show, params: {id: @person.to_param}
        expect(response.status).to eq(200)
      end

      it 'succeeds on the mobile site' do
        get :show, params: {id: @person.to_param}, format: :mobile
        expect(response).to be_success
      end

      it 'forces to sign in if the person is remote' do
        p = FactoryGirl.create(:person)

        get :show, params: {id: p.to_param}
        expect(response).to be_redirect
        expect(response).to redirect_to new_user_session_path
      end

      it "leaks no private profile info" do
        get :show, params: {id: @person.to_param}
        expect(response.body).not_to include(@person.profile.bio)
      end

      it "includes the correct meta tags" do
        presenter = PersonPresenter.new(@person)
        methods_properties = {
          comma_separated_tags: {html_attribute: "name",     name: "keywords"},
          url:                  {html_attribute: "property", name: "og:url"},
          title:                {html_attribute: "property", name: "og:title"},
          image_url:            {html_attribute: "property", name: "og:image"},
          first_name:           {html_attribute: "property", name: "og:profile:first_name"},
          last_name:            {html_attribute: "property", name: "og:profile:last_name"}
        }

        get :show, params: {id: @person.to_param}

        methods_properties.each do |method, property|
          value = presenter.send(method)
          expect(response.body).to include(
            "<meta #{property[:html_attribute]}=\"#{property[:name]}\" content=\"#{value}\" />"
          )
        end
      end
    end

    context "when the person is a contact of the current user" do
      before do
        @person = bob.person
      end

      it "succeeds" do
        get :show, params: {id: @person.to_param}
        expect(response).to be_success
      end

      it 'succeeds on the mobile site' do
        get :show, params: {id: @person.to_param}, format: :mobile
        expect(response).to be_success
      end

      it 'marks a corresponding notifications as read' do
        note = FactoryGirl.create(:notification, :recipient => @user, :target => @person, :unread => true)

        expect {
          get :show, params: {id: @person.to_param}
          note.reload
        }.to change(Notification.where(:unread => true), :count).by(-1)
      end

      it "includes private profile info" do
        get :show, params: {id: @person.to_param}
        expect(response.body).to include(@person.profile.bio)
      end

      it "preloads data using gon for the aspect memberships dropdown" do
        get :show, params: {id: @person.to_param}
        expect_gon_preloads_for_aspect_membership_dropdown(:person, true)
      end
    end

    context "when the person is not a contact of the current user" do
      before do
        @person = eve.person
      end

      it "succeeds" do
        get :show, params: {id: @person.to_param}
        expect(response).to be_success
      end

      it 'succeeds on the mobile site' do
        get :show, params: {id: @person.to_param}, format: :mobile
        expect(response).to be_success
      end

      it "leaks no private profile info" do
        get :show, params: {id: @person.to_param}
        expect(response.body).not_to include(@person.profile.bio)
      end

      it "preloads data using gon for the aspect memberships dropdown" do
        get :show, params: {id: @person.to_param}
        expect_gon_preloads_for_aspect_membership_dropdown(:person, false)
      end
    end

    context "when the user is following the person" do
      before do
        sign_out :user
        sign_in peter, scope: :user
        @person = alice.person
      end

      it "leaks no private profile info" do
        get :show, params: {id: @person.to_param}
        expect(response.body).not_to include(@person.profile.bio)
      end
    end
  end

  describe '#stream' do
    it "redirects non-json requests" do
      get :stream, params: {person_id: @user.person.to_param}
      expect(response).to be_redirect
    end

    context "person is current user" do
      it "assigns all the user's posts" do
        expect(@user.posts).to be_empty
        @user.post(:status_message, :text => "to one aspect", :to => @aspect.id)
        @user.post(:status_message, :text => "to all aspects", :to => 'all')
        @user.post(:status_message, :text => "public", :to => 'all', :public => true)
        expect(@user.reload.posts.length).to eq(3)
        get :stream, params: {person_id: @user.person.to_param}, format: :json
        expect(assigns(:stream).posts.map(&:id)).to match_array(@user.posts.map(&:id))
      end

      it "renders the comments on the user's posts" do
        cmmt = 'I mean it'
        message = @user.post :status_message, :text => 'test more', :to => @aspect.id
        @user.comment!(message, cmmt)
        get :stream, params: {person_id: @user.person.to_param}, format: :json
        expect(response).to be_success
        expect(response.body).to include(cmmt)
      end
    end

    context "person is contact of current user" do
      before do
        @person = bob.person
      end

      it "includes reshares" do
        reshare = @user.post(:reshare, :public => true, :root_guid => FactoryGirl.create(:status_message, :public => true).guid, :to => alice.aspect_ids)
        get :stream, params: {person_id: @user.person.to_param}, format: :json
        expect(assigns[:stream].posts.map { |x| x.id }).to include(reshare.id)
      end

      it "assigns only the posts the current user can see" do
        expect(bob.posts).to be_empty
        posts_user_can_see = []
        aspect_user_is_in = bob.aspects.where(:name => "generic").first
        aspect_user_is_not_in = bob.aspects.where(:name => "empty").first
        posts_user_can_see << bob.post(:status_message, :text => "to an aspect @user is in", :to => aspect_user_is_in.id)
        bob.post(:status_message, :text => "to an aspect @user is not in", :to => aspect_user_is_not_in.id)
        posts_user_can_see << bob.post(:status_message, :text => "to all aspects", :to => 'all')
        posts_user_can_see << bob.post(:status_message, :text => "public", :to => 'all', :public => true)
        expect(bob.reload.posts.length).to eq(4)

        get :stream, params: {person_id: @person.to_param}, format: :json
        expect(assigns(:stream).posts.map(&:id)).to match_array(posts_user_can_see.map(&:id))
      end
    end

    context "person is not contact of current user" do
      before do
        @person = eve.person
      end

      it "assigns only public posts" do
        expect(eve.posts).to be_empty
        eve.post(:status_message, :text => "to an aspect @user is not in", :to => eve.aspects.first.id)
        eve.post(:status_message, :text => "to all aspects", :to => 'all')
        public_post = eve.post(:status_message, :text => "public", :to => 'all', :public => true)
        expect(eve.reload.posts.length).to eq(3)

        get :stream, params: {person_id: @person.to_param}, format: :json
        expect(assigns[:stream].posts.map(&:id)).to match_array([public_post].map(&:id))
      end

      it "posts include reshares" do
        reshare = @user.post(:reshare, :public => true, :root_guid => FactoryGirl.create(:status_message, :public => true).guid, :to => alice.aspect_ids)
        get :stream, params: {person_id: @user.person.to_param}, format: :json
        expect(assigns[:stream].posts.map { |x| x.id }).to include(reshare.id)
      end
    end

    context "logged out" do
      before do
        sign_out :user
        @person = bob.person
      end

      context 'with posts' do
        before do
          @public_posts = []
          @public_posts << bob.post(:status_message, :text => "first public ", :to => bob.aspects[0].id, :public => true)
          bob.post(:status_message, :text => "to an aspect @user is not in", :to => bob.aspects[1].id)
          bob.post(:status_message, :text => "to all aspects", :to => 'all')
          @public_posts << bob.post(:status_message, :text => "public", :to => 'all', :public => true)
          @public_posts.first.created_at -= 1000
          @public_posts.first.save
        end

        it "posts include reshares" do
          reshare = @user.post(:reshare, :public => true, :root_guid => FactoryGirl.create(:status_message, :public => true).guid, :to => alice.aspect_ids)
          get :stream, params: {person_id: @user.person.to_param}, format: :json
          expect(assigns[:stream].posts.map { |x| x.id }).to include(reshare.id)
        end

        it "assigns only public posts" do
          get :stream, params: {person_id: @person.to_param}, format: :json
          expect(assigns[:stream].posts.map(&:id)).to match_array(@public_posts.map(&:id))
        end

        it 'is sorted by created_at desc' do
          get :stream, params: {person_id: @person.to_param}, format: :json
          expect(assigns[:stream].stream_posts).to eq(@public_posts.sort_by { |p| p.created_at }.reverse)
        end
      end
    end
  end

  describe '#hovercard' do
    before do
      @hover_test = FactoryGirl.create(:person)
      @hover_test.profile.tag_string = '#test #tags'
      @hover_test.profile.save!
    end

    it 'redirects html requests' do
      get :hovercard, params: {person_id: @hover_test.guid}
      expect(response).to redirect_to person_path(:id => @hover_test.guid)
    end

    it 'returns json with profile stuff' do
      get :hovercard, params: {person_id: @hover_test.guid}, format: :json
      expect(JSON.parse(response.body)["diaspora_id"]).to eq(@hover_test.diaspora_handle)
    end

    it "returns contact when sharing" do
      alice.share_with(@hover_test, alice.aspects.first)
      expect(@controller).to receive(:current_user).at_least(:once).and_return(alice)
      get :hovercard, params: {person_id: @hover_test.guid}, format: :json
      expect(JSON.parse(response.body)["contact"]).not_to be_falsy
    end

    context "with no user signed in" do
      before do
        sign_out :user
      end

      it "succeeds with local person" do
        get :hovercard, params: {person_id: bob.person.guid}, format: :json
        expect(response.status).to eq(200)
        expect(JSON.parse(response.body)["diaspora_id"]).to eq(bob.diaspora_handle)
      end

      it "succeeds with remote person" do
        get :hovercard, params: {person_id: remote_raphael.guid}, format: :json
        expect(response.status).to eq(200)
        expect(JSON.parse(response.body)["diaspora_id"]).to eq(remote_raphael.diaspora_handle)
      end
    end
  end

  describe '#refresh_search ' do
    before(:each)do
      @eugene = FactoryGirl.create(
        :person,
        profile: FactoryGirl.build(:profile, first_name: "Eugene", last_name: "w")
      )
      @korth = FactoryGirl.create(
        :person,
        profile: FactoryGirl.build(:profile, first_name: "Evan", last_name: "Korth")
      )
      @closed = FactoryGirl.create(
        :person,
        closed_account: true,
        profile:        FactoryGirl.build(:profile, first_name: "Closed", last_name: "Account")
      )
    end

    describe "via json" do
      it "returns no data when a search fails" do
        get :refresh_search, params: {q: "weweweKorth"}, format: :json
        expect(response.body).to eq({search_html: "", contacts: nil}.to_json)
      end

      it "returns no data unless a fully composed name is sent" do
        get :refresh_search, params: {q: "Korth"}
        expect(response.body).to eq({search_html: "", contacts: nil}.to_json)
      end

      it "returns with a found name" do
        get :refresh_search, params: {q: @korth.diaspora_handle}
        expect(JSON.parse(response.body)["contacts"].size).to eq(1)
      end

      it "doesn't include closed accounts" do
        get :refresh_search, params: {q: @closed.diaspora_handle}
        expect(JSON.parse(response.body)["contacts"]).to be_nil
      end
    end
  end

  describe '#diaspora_id?' do
    it 'returns true for pods on urls' do
      expect(@controller.send(:diaspora_id?, "ilya_123@pod.geraspora.de")).to be true
    end

    it 'returns true for pods on urls with port' do
      expect(@controller.send(:diaspora_id?, "ilya_123@pod.geraspora.de:12314")).to be true
    end

    it 'returns true for pods on localhost' do
      expect(@controller.send(:diaspora_id?, "ilya_123@localhost")).to be true
    end

    it 'returns true for pods on localhost and port' do
      expect(@controller.send(:diaspora_id?, "ilya_123@localhost:1234")).to be true
    end

    it 'returns true for pods on ip' do
      expect(@controller.send(:diaspora_id?, "ilya_123@1.1.1.1")).to be true
    end

    it 'returns true for pods on ip and port' do
      expect(@controller.send(:diaspora_id?, "ilya_123@1.2.3.4:1234")).to be true
    end

    it 'returns false for pods on with invalid url characters' do
      expect(@controller.send(:diaspora_id?, "ilya_123@join_diaspora.com")).to be false
    end

    it 'returns false for invalid usernames' do
      expect(@controller.send(:diaspora_id?, "ilya_2%3@joindiaspora.com")).to be false
    end
  end
end