cirandas.net

ref: master

test/api/people_test.rb


  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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
require_relative 'test_helper'

class PeopleTest < ActiveSupport::TestCase

  def setup
    Person.destroy_all
    create_and_activate_user
  end

  should 'logged user list all people' do
    login_api
    person1 = fast_create(Person, :public_profile => true)
    person2 = fast_create(Person)
    get "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [person1.id, person2.id, person.id], json.map {|c| c['id']}
  end

  should 'anonymous list all people' do
    person1 = fast_create(Person, :public_profile => true)
    person2 = fast_create(Person)
    get "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [person.id, person1.id, person2.id], json.map {|c| c['id']}
  end

  should 'logged user list all members of a community' do
    login_api
    person1 = fast_create(Person)
    person2 = fast_create(Person)
    community = fast_create(Community)
    community.add_member(person1)
    community.add_member(person2)

    get "/api/v1/profiles/#{community.id}/members?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal 2, json.count
    assert_equivalent [person1.id,person2.id], json.map{|p| p["id"]}
  end

  should 'anonymous list all members of a community' do
    person1 = fast_create(Person)
    person2 = fast_create(Person)
    community = fast_create(Community)
    community.add_member(person1)
    community.add_member(person2)

    get "/api/v1/profiles/#{community.id}/members?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal 2, json.count
    assert_equivalent [person1.id,person2.id], json.map{|p| p["id"]}
  end

  should 'logged user not list invisible people' do
    login_api
    invisible_person = fast_create(Person, :visible => false)

    get "/api/v1/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'annoymous not list invisible people' do
    invisible_person = fast_create(Person, :visible => false)

    get "/api/v1/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'logged user list private people' do
    login_api
    private_person = fast_create(Person, :public_profile => false)

    get "/api/v1/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'anonymous list private people' do
    private_person = fast_create(Person, :public_profile => false)

    get "/api/v1/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'logged user list private person for friends' do
    login_api
    p1 = fast_create(Person)
    p2 = fast_create(Person, :public_profile => false)
    person.add_friend(p2)
    p2.add_friend(person)

    get "/api/v1/people?#{params.to_query}"
    assert_includes json_response_ids, p2.id
  end

  should 'logged user get person' do
    login_api
    some_person = fast_create(Person)

    get "/api/v1/people/#{some_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal some_person.id, json['id']
  end

  should 'anonymous get person' do
    some_person = fast_create(Person)

    get "/api/v1/people/#{some_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal some_person.id, json['id']
  end

  should 'people endpoint filter by fields parameter for logged user' do
    login_api
    get "/api/v1/people?#{params.to_query}&fields=name"
    json = JSON.parse(last_response.body)
    expected = [{'name' => person.name}]
    assert_equal expected, json
  end

  should 'people endpoint filter by fields parameter with hierarchy for logged user' do
    login_api
    params[:fields] = {only: [:name, {user: [:login]}]}
    get "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    expected = [{'name' => person.name, 'user' => {'login' => 'testapi'}}]
    assert_equal expected, json
  end

  should 'get logged person' do
    login_api
    get "/api/v1/people/me?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal person.id, json['id']
  end

  should 'access me endpoint filter by fields parameter' do
    login_api
    get "/api/v1/people/me?#{params.to_query}&fields=name"
    json = JSON.parse(last_response.body)
    expected = {'name' => person.name}
    assert_equal expected, json
  end

  should 'logged user not get invisible person' do
    login_api
    person = fast_create(Person, :visible => false)

    get "/api/v1/people/#{person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal Api::Status::NOT_FOUND, last_response.status
  end

  should 'anonymous not get invisible person' do
    person = fast_create(Person, :visible => false)

    get "/api/v1/people/#{person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal Api::Status::NOT_FOUND, last_response.status
  end

  should 'get private people' do
    login_api
    private_person = fast_create(Person, :public_profile => false)

    get "/api/v1/people/#{private_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['id'], private_person.id
  end

  should 'anonymous get private people' do
    private_person = fast_create(Person, :public_profile => false)

    get "/api/v1/people/#{private_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['id'], private_person.id
  end

  should 'get private person for friends' do
    login_api
    private_person = fast_create(Person, :public_profile => false)
    person.add_friend(private_person)
    private_person.add_friend(person)

    get "/api/v1/people/#{private_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal private_person.id, json['id']
  end

  should 'list person friends' do
    login_api
    friend = fast_create(Person)
    person.add_friend(friend)
    friend.add_friend(person)
    get "/api/v1/people/#{friend.id}/friends?#{params.to_query}"
    assert_includes json_response_ids, person.id
  end

  should 'anonymous list person friends' do
    person = fast_create(Person)
    friend = fast_create(Person)
    person.add_friend(friend)
    friend.add_friend(person)
    get "/api/v1/people/#{friend.id}/friends?#{params.to_query}"
    assert_includes json_response_ids, person.id
  end

  should 'not list person invisible friends' do
    login_api
    friend = fast_create(Person)
    invisible_friend = fast_create(Person, :visible => false)
    person.add_friend(friend)
    person.add_friend(invisible_friend)
    friend.add_friend(person)
    invisible_friend.add_friend(person)

    get "/api/v1/people/#{person.id}/friends?#{params.to_query}"
    friends = json_response_ids
    assert_includes friends, friend.id
    assert_not_includes friends, invisible_friend.id
  end

  should 'create a person' do
    login_api
    login = 'some'
    params[:person] = {:login => login, :password => '123456', :password_confirmation => '123456', :email => 'some@some.com'}
    post "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal login, json['identifier']
  end

  should "return #{Api::Status::UNPROCESSABLE_ENTITY} status for invalid person creation" do
    login_api
    params[:person] = {:login => 'some'}
    post "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
  end

  should 'display permissions' do
    login_api
    community = fast_create(Community)
    community.add_member(fast_create(Person))
    community.add_member(person)
    permissions = Profile::Roles.member(person.environment.id).permissions
    get "/api/v1/people/#{person.id}/permissions?#{params.to_query}"
    json = JSON.parse(last_response.body)

    assert_equal json[community.identifier], permissions
  end

  should 'display permissions if self' do
    login_api
    get "/api/v1/people/#{person.id}/permissions?#{params.to_query}"
    assert_equal 200, last_response.status
  end

  should 'display permissions if admin' do
    login_api
    environment = person.environment
    environment.add_admin(person)
    some_person = fast_create(Person)

    get "/api/v1/people/#{some_person.id}/permissions?#{params.to_query}"
    assert_equal 200, last_response.status
  end

  should 'not display permissions if not admin or self' do
    login_api
    some_person = fast_create(Person)

    get "/api/v1/people/#{some_person.id}/permissions?#{params.to_query}"
    assert_equal 403, last_response.status
  end

  should 'not update another person' do
    login_api
    person = fast_create(Person, :environment_id => environment.id)
    post "/api/v1/people/#{person.id}?#{params.to_query}"
    assert_equal 403, last_response.status
  end

  should 'update yourself' do
    login_api
    another_name = 'Another Name'
    params[:person] = {}
    params[:person][:name] = another_name
    assert_not_equal another_name, person.name
    post "/api/v1/people/#{person.id}?#{params.to_query}"
    person.reload
    assert_equal another_name, person.name
  end

  should 'logged user display public custom fields' do
    login_api
    CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => environment)
    some_person = User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
    some_person.user.activate
    some_person.reload

    some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "true"} }
    some_person.save!

    get "/api/v1/people/#{some_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert json['additional_data'].has_key?('Custom Blog')
    assert_equal "www.blog.org", json['additional_data']['Custom Blog']
  end

  should 'logged user not display non-public custom fields' do
    login_api
    CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => environment)
    some_person = User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
    some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "0"} }
    some_person.save!
    some_person.user.activate

    get "/api/v1/people/#{some_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['additional_data'], {}
  end

  should 'display public custom fields to anonymous' do
    CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => environment)
    person.reload
    person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "true"} }
    person.save!

    get "/api/v1/people/#{person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert json['additional_data'].has_key?('Custom Blog')
    assert_equal "www.blog.org", json['additional_data']['Custom Blog']
  end

  should 'not display non-public custom fields to anonymous' do
    CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => environment)
    person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "0"} }
    person.save!

    get "/api/v1/people/#{person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['additional_data'], {}
  end

  should 'hide private fields to anonymous' do
    target_user = User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment)

    get "/api/v1/users/#{target_user.id}/?#{params.to_query}"
    json = JSON.parse(last_response.body)
    refute json.has_key?("permissions")
    refute json.has_key?("activated")
  end

  should 'display non-public custom fields to friend' do
    login_api
    CustomField.create!(:name => "Custom Blog", :format => "string", :customized_type => "Person", :active => true, :environment => environment)
    some_person = User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
    some_person.user.activate
    some_person.reload

    some_person.custom_values = { "Custom Blog" => { "value" => "www.blog.org", "public" => "0"} }
    some_person.save!

    some_person.add_friend(person)
    person.add_friend(some_person)

    get "/api/v1/people/#{some_person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert json['additional_data'].has_key?("Custom Blog")
    assert_equal "www.blog.org", json['additional_data']['Custom Blog']
  end

  PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count friends_count)

  PERSON_ATTRIBUTES.map do |attribute|
    define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_does_not_contain_the_attribute" do
      login_api
      get "/api/v1/people/me?#{params.to_query}&fields=name"
      json = JSON.parse(last_response.body)
      assert_nil json[attribute]
    end

    define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints_if_field_parameter_is_passed" do
      login_api
      get "/api/v1/people/me?#{params.to_query}&fields=#{attribute}"
      json = JSON.parse(last_response.body)
      assert_not_nil json[attribute]
    end
  end

  should 'update person image' do
    login_api
    base64_image = create_base64_image
    params.merge!({person: {image_builder: base64_image}})
    assert_nil person.image
    post "/api/v1/people/#{person.id}?#{params.to_query}"
    person.reload
    assert_not_nil person.image
    assert_equal person.image.filename, base64_image[:filename]
  end

  should 'add logged person as member of a profile' do
    login_api
    profile = fast_create(Community)
    post "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['pending'], false
    assert person.is_member_of?(profile)
  end

  should 'create task when add logged person as member of a moderated profile' do
    login_api
    profile = fast_create(Community, public_profile: false)
    profile.add_member(create_user.person)
    profile.closed = true
    profile.save!
    post "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['pending'], true
    assert !person.is_member_of?(profile)
  end

  should 'remove logged person as member of a profile' do
    login_api
    profile = fast_create(Community)
    profile.add_member(person)
    delete "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal person.identifier, json['identifier']
    assert !person.is_member_of?(profile)
  end

  should 'forbid access to add members for non logged user' do
    profile = fast_create(Community)
    post "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    assert_equal 401, last_response.status
  end

  should 'forbid access to remove members for non logged user' do
    profile = fast_create(Community)
    delete "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    assert_equal 401, last_response.status
  end

  should 'forbid to add person as member when the profile does not allow' do
    login_api
    profile = fast_create(Person)
    post "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    assert_equal 403, last_response.status
  end

  should 'forbid to add person as member when the profile is secret' do
    login_api
    profile = fast_create(Community, secret: true)
    post "/api/v1/profiles/#{profile.id}/members?#{params.to_query}"
    assert !person.is_member_of?(profile)
    assert_equal 403, last_response.status
  end

  should 'list all people of enviroment' do
    environment = fast_create(Environment)
    person1 = fast_create(Person, :public_profile => true, :environment_id => environment.id)
    person2 = fast_create(Person, :environment_id => environment.id)
    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [person1.id, person2.id], json.map {|c| c['id']}
  end

  should 'logged user not list invisible people of environment' do
    environment = fast_create(Environment)
    login_api
    invisible_person = fast_create(Person, :visible => false, :environment_id => environment.id)

    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'annoymous not list invisible people of environment' do
    environment = fast_create(Environment)
    invisible_person = fast_create(Person, :visible => false, :environment_id => environment.id)

    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'logged user list private people of environment' do
    environment = fast_create(Environment)
    login_api
    private_person = fast_create(Person, :public_profile => false, :environment_id => environment.id)

    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'anonymous list private people of environment' do
    environment = fast_create(Environment)
    private_person = fast_create(Person, :public_profile => false, :environment_id => environment.id)

    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'logged user list private person for friends of environment' do
    environment = fast_create(Environment)
    login_api
    p1 = fast_create(Person, :environment_id => environment.id)
    p2 = fast_create(Person, :public_profile => false, :environment_id => environment.id)
    person.add_friend(p2)
    p2.add_friend(person)

    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    assert_includes json_response_ids, p2.id
  end

  should 'people endpoint filter by fields parameter for logged user of environment' do
    environment = fast_create(Environment)
    person  = fast_create(Person, :environment_id => environment.id)
    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}&fields=name"
    json = JSON.parse(last_response.body)
    expected = [{'name' => person.name}]
    assert_equal expected, json
  end

  should 'people endpoint filter by fields parameter with hierarchy for logged user of environment' do
    environment = fast_create(Environment)
    person  = create_user('someuser', :environment_id => environment.id).person
    params[:fields] = {only: [:name, {user: [:login]}]}
    get "/api/v1/environments/#{environment.id}/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    expected = [{'name' => 'someuser', 'user' => {'login' => 'someuser'}}]
    assert_equal expected, json
  end

  should 'list all people of default enviroment' do
    environment = Environment.default
    person1 = fast_create(Person, :public_profile => true, :environment_id => environment.id)
    person2 = fast_create(Person, :environment_id => environment.id)
    get "/api/v1/environments/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equivalent [person1.id, person2.id, person.id], json.map {|c| c['id']}
  end

  should 'logged user not list invisible people of default environment' do
    environment = Environment.default
    login_api
    invisible_person = fast_create(Person, :visible => false, :environment_id => environment.id)

    get "/api/v1/environments/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'annoymous not list invisible people of default environment' do
    environment = Environment.default
    invisible_person = fast_create(Person, :visible => false, :environment_id => environment.id)

    get "/api/v1/environments/people?#{params.to_query}"
    assert_not_includes json_response_ids, invisible_person.id
  end

  should 'logged user list private people of default environment' do
    environment = Environment.default
    login_api
    private_person = fast_create(Person, :public_profile => false, :environment_id => environment.id)

    get "/api/v1/environments/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'anonymous list private people of default environment' do
    environment = Environment.default
    private_person = fast_create(Person, :public_profile => false, :environment_id => environment.id)

    get "/api/v1/environments/people?#{params.to_query}"
    assert_includes json_response_ids, private_person.id
  end

  should 'logged user list private person for friends of default environment' do
    environment = Environment.default
    login_api
    p1 = fast_create(Person, :environment_id => environment.id)
    p2 = fast_create(Person, :public_profile => false, :environment_id => environment.id)
    person.add_friend(p2)
    p2.add_friend(person)

    get "/api/v1/environments/people?#{params.to_query}"
    assert_includes json_response_ids, p2.id
  end

  should 'people endpoint filter by fields parameter for logged user of default environment' do
    environment = Environment.default
    login_api
    get "/api/v1/environments/people?#{params.to_query}&fields=name"
    json = JSON.parse(last_response.body)
    expected = [{'name' => person.name}]
    assert_equal expected, json
  end

  should 'people endpoint filter by fields parameter with hierarchy for logged user of default environment' do
    environment = Environment.default
    login_api
    params[:fields] = {only: [:name, {user: [:login]}]}
    get "/api/v1/environments/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    expected = [{'name' => person.name, 'user' => {'login' => 'testapi'}}]
    assert_equal expected, json
  end

  should 'return the followers of a article identified by id' do
    person1 = fast_create(Person)
    article = fast_create(Article, :profile_id => person1.id, :name => "Some thing")

    ArticleFollower.create!(:article_id => article.id, :person_id => person1.id)

    get "/api/v1/articles/#{article.id}/followers?#{params.to_query}"
    json = JSON.parse(last_response.body)

    assert_equal 1, json.length
    assert_equal person1.id, json.first['id']
  end

  should 'return the amount of followers of a article identified by id' do
    person1 = fast_create(Person)
    article = fast_create(Article, :profile_id => person1.id, :name => "Some thing")

    person2 = fast_create(Person)
    person3 = fast_create(Person)
    ArticleFollower.create!(:article_id => article.id, :person_id => person1.id)
    ArticleFollower.create!(:article_id => article.id, :person_id => person2.id)
    ArticleFollower.create!(:article_id => article.id, :person_id => person3.id)

    params[:count] = true
    get "/api/v1/articles/#{article.id}/followers?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal 3, json['count']
  end

  should 'add a new person friend' do
    login_api
    friend = create_user('friend').person
    person.add_friend(friend)
    friend.add_friend(person)
    post "/api/v1/people/#{friend.id}/friends?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['message'], 'WAITING_APPROVAL'
  end
  
  should 'remove person friend' do
    login_api
    friend = fast_create(Person)
    person.add_friend(friend)
    friend.add_friend(person)
    delete "/api/v1/people/#{friend.id}/friends?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['message'], "Friend successfuly removed"
  end  

  should 'list a person friend' do
    login_api
    friend = fast_create(Person)
    person.add_friend(friend)
    friend.add_friend(person)
    get "/api/v1/people/#{friend.id}/friends/#{person.id}?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal json['id'], person.id
  end  

  should 'search for people' do
    person1 = fast_create(Person, :public_profile => true)
    person2 = fast_create(Person, name: 'John Snow')
    params[:search] = 'john'
    get "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [person2.id], json.map {|c| c['id']}
  end

  should 'search for people with pagination' do
    5.times { fast_create(Person, name: 'John Snow') }
    params[:search] = 'john'
    params[:per_page] = 2
    get "/api/v1/people?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal 2, json.length
    assert_equal 5, last_response.headers['Total'].to_i
  end

  should 'search for friends' do
    login_api
    friend1 = fast_create(Person, name: 'John Snow')
    person.add_friend(friend1)
    friend2 = fast_create(Person, name: 'Other')
    person.add_friend(friend2)
    params[:search] = 'john'
    get "/api/v1/people/#{person.id}/friends?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal [friend1.id], json_response_ids
  end

  #####
  
  ATTRIBUTES = [:email, :country, :state, :city, :nationality, :formation, :schooling]

  ATTRIBUTES.map do |attr|

    define_method "test_should_show_#{attr}_if_it_is_a_public_attribute_to_logged_user" do
      login_api
      target_person =  User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
      target_person.public_profile = true
      target_person.visible = true
      target_person.fields_privacy={attr=> 'public'}
      target_person.save!
  
      get "/api/v1/people/#{target_person.id}/?#{params.to_query}"
      json = JSON.parse(last_response.body)
      assert json.has_key?(attr.to_s)
      assert_equal json[attr.to_s],target_person.send(attr)
    end
  
    define_method "test_should_not_show_#{attr}_if_it_is_an_private_attribute_to_logged_user_without_permission" do
      login_api
      target_person =  User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
      target_person.public_profile = true
      target_person.visible = true
      target_person.fields_privacy={attr=> 'private'}
      target_person.save!
  
      get "/api/v1/people/#{target_person.id}/?#{params.to_query}"
      json = JSON.parse(last_response.body)
      assert !json.has_key?(attr.to_s)
    end
  
    define_method "test_should_not_show_#{attr}_if_it_is_an_private_attribute_to_logged_user_with_permission" do
      login_api
      target_person =  User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
      target_person.public_profile = true
      target_person.visible = true
      target_person.fields_privacy={attr=> 'private'}
      target_person.save!
      target_person.add_friend(person)
  
      get "/api/v1/people/#{target_person.id}/?#{params.to_query}"
      json = JSON.parse(last_response.body)
      assert json.has_key?(attr.to_s)
    end
  
    define_method "test_should_not_show_email_if_it_is_a_private_attribute_to_logged_off_user" do
      logout_api
      target_person =  User.create!(:login => 'user1', :password => 'USER_PASSWORD', :password_confirmation => 'USER_PASSWORD', :email => 'test2@test.org', :environment => environment).person
      target_person.public_profile = true
      target_person.visible = true
      target_person.fields_privacy={attr=> 'private'}
      target_person.save!
  
      get "/api/v1/people/#{target_person.id}/?#{params.to_query}"
      json = JSON.parse(last_response.body)
      assert !json.has_key?(attr.to_s)
    end
  end
end