person_test.rb
19.9 KB
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
require File.dirname(__FILE__) + '/../test_helper'
class PersonTest < Test::Unit::TestCase
  fixtures :profiles, :users, :environments
  def test_person_must_come_form_the_cration_of_an_user
    p = Person.new(:environment => Environment.default, :name => 'John', :identifier => 'john')
    assert !p.valid?
    p.user =  create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
    assert !p.valid?
    p = create_user('johnz', :email => 'johnz@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
    assert p.valid?
  end
  def test_can_associate_to_a_profile
    pr = Profile.new(:identifier => 'mytestprofile', :name => 'My test profile')
    pr.save!
    pe = create_user('person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
    pe.save!
    member_role = Role.create(:name => 'somerandomrole')
    pr.affiliate(pe, member_role)
    assert pe.memberships.include?(pr)
  end
  def test_can_belong_to_an_enterprise
    e = Enterprise.new(:identifier => 'enterprise', :name => 'enterprise')
    e.save!
    p = create_user('person', :email => 'person@test.net', :password => 'dhoe', :password_confirmation => 'dhoe').person
    p.save!
    member_role = Role.create(:name => 'somerandomrole')
    e.affiliate(p, member_role)
    assert p.memberships.include?(e)
    assert p.enterprises.include?(e)
  end
  should 'belong to communities' do
    c = fast_create(Community)
    p = create_user('mytestuser').person
    c.add_member(p)
    assert p.communities.include?(c), "Community should add a new member"
  end
  should 'be associated with a user' do
    u = User.new(:login => 'john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
    u.save!
    assert_equal u, Person['john'].user
  end
  should 'only one person per user' do
    u = create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe')
    p1 = u.person
    assert_equal u, p1.user
    
    p2 = Person.new(:environment => Environment.default)
    p2.user = u
    assert !p2.valid?
    assert p2.errors.invalid?(:user_id)
  end
  should "have person info fields" do
    p = Person.new(:environment => Environment.default)
    [ :name, :photo, :contact_information, :birth_date, :sex, :address, :city, :state, :country, :zip_code ].each do |i|
      assert_respond_to p, i
    end
  end
  should 'not have person_info class' do
    p = Person.new(:environment => Environment.default)
    assert_raise NoMethodError do
      p.person_info
    end
  end
  should 'change the roles of the user' do
    p = create_user('jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
    e = fast_create(Enterprise)
    r1 = Role.create(:name => 'associate')
    assert e.affiliate(p, r1)
    r2 = Role.create(:name => 'partner')
    assert p.define_roles([r2], e)
    p = Person.find(p.id)
    assert p.role_assignments.any? {|ra| ra.role == r2}
    assert !p.role_assignments.any? {|ra| ra.role == r1}
  end
  should 'report that the user has the permission' do
    p = create_user('john', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
    r = Role.create(:name => 'associate', :permissions => ['edit_profile'])
    e = fast_create(Enterprise)
    assert e.affiliate(p, r)
    p = Person.find(p.id)
    assert e.reload
    assert p.has_permission?('edit_profile', e)
    assert !p.has_permission?('destroy_profile', e)
  end
  should 'get an email address from the associated user instance' do
    p = create_user('jonh', :email => 'john@doe.org', :password => 'dhoe', :password_confirmation => 'dhoe').person
    assert_equal 'john@doe.org', p.email
  end
  should 'get no email address when there is no associated user' do
    p = Person.new(:environment => Environment.default)
    assert_nil p.email
  end
  should 'use email addreess as contact email' do
    p = Person.new
    p.stubs(:email).returns('my@email.com')
    assert_equal 'my@email.com', p.contact_email
  end
  should 'set email through person instance' do
    u = create_user('testuser')
    p = u.person
    p.email = 'damnit@example.com'
    p.save!
    u.reload
    assert_equal 'damnit@example.com', u.email
  end
  should 'not be able to change e-mail to an e-mail of other user' do
    create_user('firstuser', :email => 'user@domain.com')
    other = create_user('seconduser', :email => 'other@domain.com').person
    other.email = 'user@domain.com'
    other.valid?
    assert other.errors.invalid?(:email)
  end
  should 'be able to use an e-mail already used in other environment' do
    first = create_user('user', :email => 'user@example.com')
    other_env = fast_create(Environment)
    other = create_user('user', :email => 'other@example.com', :environment => other_env).person
    other.email = 'user@example.com'
    other.valid?
    assert !other.errors.invalid?(:email)
  end
  should 'be an admin if have permission of environment administration' do
    role = Role.create!(:name => 'just_another_admin_role')
    env = fast_create(Environment)
    person = create_user('just_another_person').person
    env.affiliate(person, role)
    assert ! person.is_admin?(env)
    role.update_attributes(:permissions => ['view_environment_admin_panel'])
    person = Person.find(person.id)
    assert person.is_admin?(env)
  end
  should 'separate admins of different environments' do
    env1 = fast_create(Environment)
    env2 = fast_create(Environment)
    # role is an admin role
    role = Role.create!(:name => 'just_another_admin_role')
    role.update_attributes(:permissions => ['view_environment_admin_panel'])
    # user is admin of env1, but not of env2
    person = create_user('just_another_person').person
    env1.affiliate(person, role)
    person = Person.find(person.id)
    assert person.is_admin?(env1)
    assert !person.is_admin?(env2)
  end
  should 'get a default home page and a RSS feed' do
    person = create_user_full('mytestuser').person
    assert_kind_of Article, person.home_page
    assert_kind_of RssFeed, person.articles.find_by_path('feed')
  end
  should 'create default set of blocks' do
    p = create_user_full('testingblocks').person
    assert p.boxes[0].blocks.map(&:class).include?(MainBlock), 'person must have a MainBlock upon creation'
    assert p.boxes[1].blocks.map(&:class).include?(ProfileInfoBlock), 'person must have a ProfileInfoBlock upon creation'
    assert p.boxes[1].blocks.map(&:class).include?(RecentDocumentsBlock), 'person must have a RecentDocumentsBlock upon creation'
    assert p.boxes[1].blocks.map(&:class).include?(TagsBlock), 'person must have a Tags Block upon creation'
    assert p.boxes[2].blocks.map(&:class).include?(CommunitiesBlock), 'person must have a CommunitiesBlock upon creation'
    assert p.boxes[2].blocks.map(&:class).include?(EnterprisesBlock), 'person must have a EnterprisesBlock upon creation'
    assert p.boxes[2].blocks.map(&:class).include?(FriendsBlock), 'person must have a FriendsBlock upon creation'
    assert_equal 7,  p.blocks.size
  end
  should 'have friends' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    
    p1.add_friend(p2)
    p1.friends.reload
    assert_equal [p2], p1.friends
    p3 = create_user('testuser3').person
    p1.add_friend(p3)
    assert_equal [p2,p3], p1.friends(true) # force reload
  end
  should 'suggest default friend groups list' do
    p = Person.new(:environment => Environment.default)
    assert_equivalent [ 'friends', 'work', 'school', 'family' ], p.suggested_friend_groups
  end
  should 'suggest current groups as well' do
    p = Person.new(:environment => Environment.default)
    p.expects(:friend_groups).returns(['group1', 'group2'])
    assert_equivalent [ 'friends', 'work', 'school', 'family', 'group1', 'group2' ], p.suggested_friend_groups
  end
  should 'accept nil friend groups when suggesting friend groups' do
    p = Person.new(:environment => Environment.default)
    p.expects(:friend_groups).returns([nil])
    assert_equivalent [ 'friends', 'work', 'school', 'family' ], p.suggested_friend_groups
  end
  should 'list friend groups' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    p3 = create_user('testuser3').person
    p4 = create_user('testuser4').person
   
    p1.add_friend(p2, 'group1')
    p1.add_friend(p3, 'group2')
    p1.add_friend(p4, 'group1')
    assert_equivalent ['group1', 'group2'], p1.friend_groups
  end
  should 'not suggest duplicated friend groups' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
   
    p1.add_friend(p2, 'friends')
    assert_equal p1.suggested_friend_groups, p1.suggested_friend_groups.uniq
  end
  should 'remove friend' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    p1.add_friend(p2, 'friends')
    assert_difference Friendship, :count, -1 do
      p1.remove_friend(p2)
    end
    assert_not_includes p1.friends(true), p2
  end
  should 'destroy friendships when person is destroyed' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    p1.add_friend(p2, 'friends')
    p2.add_friend(p1, 'friends')
    assert_difference Friendship, :count, -2 do
      p1.destroy
    end
    assert_not_includes p2.friends(true), p1
  end
  should 'destroy use when person is destroyed' do
    person = create_user('testuser').person
    assert_difference User, :count, -1 do
      person.destroy
    end
  end
  should 'return info name instead of name when info is setted' do
    p = create_user('ze_maria').person
    assert_equal 'ze_maria', p.name
    p.name = 'José'
    assert_equal 'José', p.name
  end
  should 'have favorite enterprises' do
    p = create_user('test_person').person
    e = fast_create(Enterprise)
    p.favorite_enterprises << e
    assert_includes Person.find(p.id).favorite_enterprises, e
  end
  should 'save info contact_information field' do
    person = create_user('new_person').person
    person.contact_information = 'my contact'
    person.save!
    assert_equal 'my contact', person.contact_information
  end
  should 'provide desired info fields' do 
    p = Person.new(:environment => Environment.default)
    assert p.respond_to?(:photo)
    assert p.respond_to?(:address)
    assert p.respond_to?(:contact_information)
  end
  should 'required name' do
    person = Person.new(:environment => Environment.default)
    assert !person.valid?
    assert person.errors.invalid?(:name)
  end
  should 'already request friendship' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    AddFriend.create!(:person => p1, :friend => p2)
    assert p1.already_request_friendship?(p2)
  end
  should 'have e-mail addresses' do
    env = fast_create(Environment)
    env.domains <<  Domain.new(:name => 'somedomain.com')
    person = Person.new(:environment => env, :identifier => 'testuser')
    person.expects(:environment).returns(env)
    assert_equal ['testuser@somedomain.com'], person.email_addresses
  end
  should 'not show www in e-mail addresses when force_www=true' do
    env = fast_create(Environment)
    env.domains <<  Domain.new(:name => 'somedomain.com')
    env.update_attribute(:force_www, true)
    person = Person.new(:environment => env, :identifier => 'testuser')
    person.expects(:environment).returns(env)
    assert_equal ['testuser@somedomain.com'], person.email_addresses
  end
  should 'show profile info to friend' do
    person = create_user('test_user').person
    person.public_profile = false
    person.save!
    friend = create_user('test_friend').person
    person.add_friend(friend)
    person.friends.reload
    assert person.display_info_to?(friend)
  end
  should 'have a person template' do
    env = Environment.create!(:name => 'test env')
    p = create_user('test_user', :environment => env).person
    assert_kind_of Person, p.template
  end
  should 'destroy all task that it requested when destroyed' do
    p = create_user('test_profile').person
    assert_no_difference Task, :count do
      Task.create(:requestor => p)
      p.destroy
    end
  end
  should 'person has pending tasks' do
    p1 = create_user('user_with_tasks').person
    p1.tasks << Task.new
    p2 = create_user('user_without_tasks').person
    assert_includes Person.with_pending_tasks, p1
    assert_not_includes Person.with_pending_tasks, p2
  end
  should 'person has group with pending tasks' do
    p1 = create_user('user_with_tasks').person
    c1 = fast_create(Community)
    c1.tasks << Task.new
    assert !c1.tasks.pending.empty?
    c1.add_admin(p1)
    c2 = fast_create(Community)
    p2 = create_user('user_without_tasks').person
    c2.add_admin(p2)
    assert_includes Person.with_pending_tasks, p1
    assert_not_includes Person.with_pending_tasks, p2
  end
  should 'not allow simple member to view group pending tasks' do
    c = fast_create(Community)
    c.tasks << Task.new
    p = create_user('user_without_tasks').person
    c.add_member(p)
    assert_not_includes Person.with_pending_tasks, p
  end
  should 'person has organization pending tasks' do
    c = fast_create(Community)
    c.tasks << Task.new
    p = create_user('user_with_tasks').person
    c.add_admin(p)
    assert p.has_organization_pending_tasks?
  end
  should 'select organization pending tasks' do
    c = fast_create(Community)
    c.tasks << Task.new
    p = create_user('user_with_tasks').person
    c.add_admin(p)
    assert_equal p.pending_tasks_for_organization(c), c.tasks
  end
  should 'return active_person_fields' do
    e = Environment.default
    e.expects(:active_person_fields).returns(['cell_phone', 'comercial_phone']).at_least_once
    person = Person.new(:environment => e)
    assert_equal e.active_person_fields, person.active_fields
  end
  should 'return required_person_fields' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['cell_phone', 'comercial_phone']).at_least_once
    person = Person.new(:environment => e)
    assert_equal e.required_person_fields, person.required_fields
  end
  should 'require fields if person needs' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['cell_phone']).at_least_once
    person = Person.new(:environment => e)
    assert ! person.valid?
    assert person.errors.invalid?(:cell_phone)
    person.cell_phone = '99999'
    person.valid?
    assert ! person.errors.invalid?(:cell_phone)
  end
  should 'require custom_area_of_study if area_of_study is others' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['area_of_study', 'custom_area_of_study']).at_least_once
  
    person = Person.new(:environment => e, :area_of_study => 'Others')
    assert !person.valid?
    assert person.errors.invalid?(:custom_area_of_study)
    person.custom_area_of_study = 'Customized area of study'
    person.valid?
    assert ! person.errors.invalid?(:custom_area_of_study)
  end
  should 'not require custom_area_of_study if area_of_study is not others' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['area_of_study']).at_least_once
    person = Person.new(:environment => e, :area_of_study => 'Agrometeorology')
    person.valid?
    assert ! person.errors.invalid?(:custom_area_of_study)
  end
  should 'require custom_formation if formation is others' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['formation', 'custom_formation']).at_least_once
    person = Person.new(:environment => e, :formation => 'Others')
    assert !person.valid?
    assert person.errors.invalid?(:custom_formation)
    person.custom_formation = 'Customized formation'
    person.valid?
    assert ! person.errors.invalid?(:custom_formation)
  end
  should 'not require custom_formation if formation is not others' do
    e = Environment.default
    e.expects(:required_person_fields).returns(['formation']).at_least_once
 
    person = Person.new(:environment => e, :formation => 'Agrometeorology')
    assert !person.valid?
    assert ! person.errors.invalid?(:custom_formation)
  end
  should 'identify when person is a friend' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    p1.add_friend(p2)
    p1.friends.reload
    assert p1.is_a_friend?(p2)
  end
  should 'identify when person isnt a friend' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    assert !p1.is_a_friend?(p2)
  end
  should 'refuse join community' do
    p = create_user('test_user').person
    c = fast_create(Community)
    assert p.ask_to_join?(c)
    p.refuse_join(c)
    assert !p.ask_to_join?(c)
  end
  should 'not ask to join for a member' do
    p = create_user('test_user').person
    c = fast_create(Community)
    c.add_member(p)
    assert !p.ask_to_join?(c)
  end
  should 'not ask to join if already asked' do
    p = create_user('test_user').person
    c = fast_create(Community)
    AddMember.create!(:person => p, :organization => c)
    assert !p.ask_to_join?(c)
  end
  should 'ask to join if community is not public' do
    person = fast_create(Person)
    community = fast_create(Community, :public_profile => false)
    assert person.ask_to_join?(community)
  end
  should 'not ask to join if community is not visible' do
    person = fast_create(Person)
    community = fast_create(Community, :visible => false)
    assert !person.ask_to_join?(community)
  end
  should 'save organization_website with http' do
    p = create_user('person_test').person
    p.organization_website = 'website.without.http'
    p.save
    assert_equal 'http://website.without.http', p.organization_website
  end
  should 'not add protocol for empty organization website' do
    p = create_user('person_test').person
    p.organization_website = ''
    p.save
    assert_equal '', p.organization_website
  end
  should 'save organization_website as typed if has http' do
    p = create_user('person_test').person
    p.organization_website = 'http://website.with.http'
    p.save
    assert_equal 'http://website.with.http', p.organization_website
  end
  should 'not add a friend if already is a friend' do
    p1 = create_user('testuser1').person
    p2 = create_user('testuser2').person
    assert p1.add_friend(p2)
    assert Profile['testuser1'].is_a_friend?(p2)
    assert !Profile['testuser1'].add_friend(p2)
  end
  should 'not raise exception when validates person without e-mail' do
    person = create_user('testuser1').person
    person.user.email = nil
    assert_nothing_raised ActiveRecord::RecordInvalid do
      assert !person.save
    end
  end
  should 'not rename' do
    assert_valid p = create_user('test_user').person
    assert_raise ArgumentError do
      p.identifier = 'other_person_name'
    end
  end
  should "return none on label if the person hasn't friends" do
    p = fast_create(Person)
    assert_equal 0, p.friends.count
    assert_equal "none", p.more_popular_label
  end
  should "return one friend on label if the profile has one member" do
    p1 = fast_create(Person)
    p2 = fast_create(Person)
    p1.add_friend(p2)
    assert_equal 1, p1.friends.count
    assert_equal "one friend", p1.more_popular_label
  end
  should "return the number of friends on label if the person has more than one friend" do
    p1 = fast_create(Person)
    p2 = fast_create(Person)
    p3 = fast_create(Person)
    p1.add_friend(p2)
    p1.add_friend(p3)
    assert_equal 2, p1.friends.count
    assert_equal "2 friends", p1.more_popular_label
    p4 = fast_create(Person)
    p1.add_friend(p4)
    assert_equal 3, p1.friends.count
    assert_equal "3 friends", p1.more_popular_label
  end
  should 'find more popular people' do
    Person.delete_all
    env = fast_create(Environment)
    p1 = fast_create(Person)
    p2 = fast_create(Person)
    p3 = fast_create(Person)
    p1.add_friend(p2)
    assert_equal [p1], Person.more_popular
    p2.add_friend(p1)
    p2.add_friend(p3)
    assert_equal [p2,p1] , Person.more_popular
    p2.remove_friend(p3)
    assert_equal [p1,p2] , Person.more_popular
  end
end