profile_controller_test.rb
24 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
639
640
641
642
643
644
645
646
647
648
649
650
require File.dirname(__FILE__) + '/../test_helper'
require 'profile_controller'
# Re-raise errors caught by the controller.
class ProfileController; def rescue_action(e) raise e end; end
class ProfileControllerTest < Test::Unit::TestCase
def setup
@controller = ProfileController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@profile = create_user('testuser').person
end
attr_reader :profile
def test_local_files_reference
assert_local_files_reference
end
def test_valid_xhtml
assert_valid_xhtml
end
noosfero_test :profile => 'testuser'
should 'list friends' do
get :friends
assert_response :success
assert_template 'friends'
assert_kind_of Array, assigns(:friends)
end
should 'point to manage friends in user is seeing his own friends' do
login_as('testuser')
get :friends
assert_tag :tag => 'a', :attributes => { :href => '/myprofile/testuser/friends' }
end
should 'not point to manage friends of other users' do
login_as('ze')
get :friends
assert_no_tag :tag => 'a', :attributes => { :href => '/myprofile/testuser/friends' }
end
should 'list communities' do
get :communities
assert_response :success
assert_template 'communities'
assert_kind_of Array, assigns(:communities)
end
should 'list enterprises' do
get :enterprises
assert_response :success
assert_template 'enterprises'
assert_kind_of Array, assigns(:enterprises)
end
should 'list members (for organizations)' do
get :members
assert_response :success
assert_template 'members'
assert_kind_of Array, assigns(:members)
end
should 'list favorite enterprises' do
get :favorite_enterprises
assert_response :success
assert_template 'favorite_enterprises'
assert_kind_of Array, assigns(:favorite_enterprises)
end
should 'render join template without layout when called with AJAX' do
community = Community.create!(:name => 'my test community')
login_as(@profile.identifier)
@request.expects(:xhr?).returns(true).at_least_once
get :join, :profile => community.identifier
assert_response :success
assert_template 'join'
assert_no_tag :tag => 'html'
end
should 'render join template with layout in general' do
community = Community.create!(:name => 'my test community')
login_as(@profile.identifier)
@request.expects(:xhr?).returns(false).at_least_once
get :join, :profile => community.identifier
assert_response :success
assert_template 'join'
assert_tag :tag => 'html'
end
should 'show Join This Community button for non-member users' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
get :index, :profile => community.identifier
assert_tag :tag => 'a', :attributes => { :href => "/profile/#{community.identifier}/join" }
end
should 'not show Join This Community button for member users' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
community.add_member(@profile)
get :index, :profile => community.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/join/#{community.id}" }
end
should 'not show Join This Community button for non-registered users' do
community = Community.create!(:name => 'my test community')
get :index, :profile => community.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/leave/#{community.id}" }
end
should 'not show enterprises link to enterprise' do
ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1')
get :index, :profile => ent.identifier
assert_no_tag :tag => 'a', :content => 'Enterprises', :attributes => { :href => /profile\/#{ent.identifier}\/enterprises$/ }
end
should 'not show members link to person' do
person = create_user('person_1').person
get :index, :profile => person.identifier
assert_no_tag :tag => 'a', :content => 'Members', :attributes => { :href => /profile\/#{person.identifier}\/members$/ }
end
should 'show friends link to person' do
person = create_user('person_1').person
get :index, :profile => person.identifier
assert_tag :tag => 'a', :content => /Friends/, :attributes => { :href => /profile\/#{person.identifier}\/friends$/ }
end
should 'display tag for profile' do
@profile.articles.create!(:name => 'testarticle', :tag_list => 'tag1')
get :tag, :profile => @profile.identifier, :id => 'tag1'
assert_tag :tag => 'a', :attributes => { :href => /testuser\/testarticle$/ }
end
should 'link to the same tag but for whole environment' do
@profile.articles.create!(:name => 'testarticle', :tag_list => 'tag1')
get :tag, :profile => @profile.identifier, :id => 'tag1'
assert_tag :tag => 'a', :attributes => { :href => '/tag/tag1' }, :content => 'See content tagged with "tag1" in the entire site'
end
should 'show a link to own control panel' do
login_as(@profile.identifier)
get :index, :profile => @profile.identifier
assert_tag :tag => 'a', :content => 'Control panel'
end
should 'show a link to own control panel in my-network-block if is a group' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
community.blocks.each{|i| i.destroy}
community.boxes[0].blocks << MyNetworkBlock.new
community.add_admin(@profile)
get :index, :profile => community.identifier
assert_tag :tag => 'a', :content => 'Control panel'
end
should 'not show a link to others control panel' do
login_as(@profile.identifier)
other = create_user('person_1').person
other.blocks.each{|i| i.destroy}
other.boxes[0].blocks << ProfileInfoBlock.new
get :index, :profile => other.identifier
assert_no_tag :tag => 'ul', :attributes => { :class => 'profile-info-data' }, :descendant => { :tag => 'a', :content => 'Control panel' }
end
should 'show a link to control panel if user has profile_editor permission and is a group' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
community.add_admin(@profile)
get :index, :profile => community.identifier
assert_tag :tag => 'a', :attributes => { :href => /\/myprofile\/#{@profile.identifier}/ }, :content => 'Control panel'
end
should 'show create community in own profile' do
login_as(@profile.identifier)
get :communities, :profile => @profile.identifier
assert_tag :tag => 'a', :child => { :tag => 'span', :content => 'Create a new community' }
end
should 'not show create community on profile of other users' do
login_as(@profile.identifier)
person = create_user('person_1').person
get :communities, :profile => person.identifier
assert_no_tag :tag => 'a', :child => { :tag => 'span', :content => 'Create a new community' }
end
should 'not show Leave This Community button for non-member users' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
get :index, :profile => community.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/leave/#{community.id}" }
end
should 'show Leave This Community button for member users' do
login_as(@profile.identifier)
community = Community.create!(:name => 'my test community')
community.add_member(@profile)
get :index, :profile => community.identifier
assert_tag :tag => 'a',
:attributes => { :href => "/profile/#{community.identifier}/leave" }
end
should 'not show Leave This Community button for non-registered users' do
community = Community.create!(:name => 'my test community')
get :index, :profile => community.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{@profile.identifier}/memberships/leave/#{community.id}" }
end
should 'check access before displaying profile' do
Person.any_instance.expects(:display_info_to?).with(anything).returns(false)
get :index, :profile => @profile.identifier
assert_response 403
end
should 'display add friend button' do
login_as(@profile.identifier)
friend = create_user_full('friendtestuser').person
get :index, :profile => friend.identifier
assert_tag :tag => 'a', :content => 'Add friend'
end
should 'not display add friend button if user already request friendship' do
login_as(@profile.identifier)
friend = create_user_full('friendtestuser').person
AddFriend.create!(:person => @profile, :friend => friend)
get :index, :profile => friend.identifier
assert_no_tag :tag => 'a', :content => 'Add friend'
end
should 'not display add friend button if user already friend' do
login_as(@profile.identifier)
friend = create_user_full('friendtestuser').person
@profile.add_friend(friend)
@profile.friends.reload
assert @profile.is_a_friend?(friend)
get :index, :profile => friend.identifier
assert_no_tag :tag => 'a', :content => 'Add friend'
end
should 'show message for disabled enterprise' do
login_as(@profile.identifier)
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enabled => false)
get :index, :profile => ent.identifier
assert_tag :tag => 'div', :attributes => { :id => 'profile-disabled' }, :content => Environment.default.message_for_disabled_enterprise
end
should 'not show message for disabled enterprise to non-enterprises' do
login_as(@profile.identifier)
@profile.enabled = false; @profile.save!
get :index, :profile => @profile.identifier
assert_no_tag :tag => 'div', :attributes => { :id => 'profile-disabled' }, :content => Environment.default.message_for_disabled_enterprise
end
should 'not show message for disabled enterprise if there is a block for it' do
login_as(@profile.identifier)
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enabled => false)
ent.boxes << Box.new
ent.boxes[0].blocks << DisabledEnterpriseMessageBlock.new
ent.save
get :index, :profile => ent.identifier
assert_no_tag :tag => 'div', :attributes => {:class => 'blocks'}, :descendant => { :tag => 'div', :attributes => { :id => 'profile-disabled' }}
end
should 'display "Products" link for enterprise' do
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enabled => false)
get :index, :profile => 'my-test-enterprise'
assert_tag :tag => 'a', :attributes => { :href => '/catalog/my-test-enterprise'}, :content => /Products\/Services/
end
should 'not display "Products" link for enterprise if environment do not let' do
env = Environment.default
env.enable('disable_products_for_enterprises')
env.save!
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enabled => false, :environment => env)
get :index, :profile => 'my-test-enterprise'
assert_no_tag :tag => 'a', :attributes => { :href => '/catalog/my-test-enterprise'}, :content => /Products\/Services/
end
should 'not display "Products" link for people' do
get :index, :profile => 'ze'
assert_no_tag :tag => 'a', :attributes => { :href => '/catalog/my-test-enterprise'}, :content => /Products\/Services/
end
should 'display "Site map" link for profiles' do
profile = create_user('testmapuser').person
get :index, :profile => profile.identifier
assert_tag :tag => 'a', :content => "Site map", :attributes => { :href => '/profile/testmapuser/sitemap' }
end
should 'list top level articles in sitemap' do
get :sitemap, :profile => 'testuser'
assert_equal @profile.top_level_articles, assigns(:articles)
end
should 'list tags' do
Person.any_instance.stubs(:article_tags).returns({ 'one' => 1, 'two' => 2})
get :tags, :profile => 'testuser'
assert_tag :tag => 'div', :attributes => { :class => /main-block/ }, :descendant => { :tag => 'a', :attributes => { :href => '/profile/testuser/tag/one'} }
assert_tag :tag => 'div', :attributes => { :class => /main-block/ }, :descendant => { :tag => 'a', :attributes => { :href => '/profile/testuser/tag/two'} }
end
should 'show e-mail for friends on profile page' do
p1 = create_user('tusr1').person
p2 = create_user('tusr2', :email => 't2@t2.com').person
p2.add_friend p1
login_as 'tusr1'
get :index, :profile => 'tusr2'
assert_tag :content => /t2.*t2.com/
end
should 'not show e-mail for non friends on profile page' do
p1 = create_user('tusr1').person
p2 = create_user('tusr2', :email => 't2@t2.com').person
login_as 'tusr1'
get :index, :profile => 'tusr2'
assert_no_tag :content => /t2@t2.com/
end
should 'display contact us for enterprises' do
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise')
get :index, :profile => 'my-test-enterprise'
assert_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us'
end
should 'not display contact us for non-enterprises' do
get :index, :profile => @profile.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{@profile.identifier}/new" }, :content => 'Contact us'
end
should 'display contact us only if enabled' do
ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enable_contact_us => false)
get :index, :profile => 'my-test-enterprise'
assert_no_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us'
end
should 'display contact button only if friends' do
friend = create_user_full('friend_user').person
@profile.add_friend(friend)
env = Environment.default
env.disable('disable_contact_person')
env.save!
login_as(@profile.identifier)
get :index, :profile => friend.identifier
assert_tag :tag => 'a', :attributes => { :href => "/contact/#{friend.identifier}/new" }
end
should 'not display contact button if no friends' do
nofriend = create_user_full('no_friend').person
login_as(@profile.identifier)
get :index, :profile => nofriend.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{nofriend.identifier}/new" }
end
should 'display contact button only if friends and its enable in environment' do
friend = create_user_full('friend_user').person
env = Environment.default
env.disable('disable_contact_person')
env.save!
@profile.add_friend(friend)
login_as(@profile.identifier)
get :index, :profile => friend.identifier
assert_tag :tag => 'a', :attributes => { :href => "/contact/#{friend.identifier}/new" }
end
should 'not display contact button if friends and its disable in environment' do
friend = create_user_full('friend_user').person
env = Environment.default
env.enable('disable_contact_person')
env.save!
@profile.add_friend(friend)
login_as(@profile.identifier)
get :index, :profile => friend.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{friend.identifier}/new" }
end
should 'display contact button for community if its enable in environment' do
env = Environment.default
community = Community.create!(:name => 'my test community', :environment => env)
env.disable('disable_contact_community')
env.save!
community.add_member(@profile)
login_as(@profile.identifier)
get :index, :profile => community.identifier
assert_tag :tag => 'a', :attributes => { :href => "/contact/#{community.identifier}/new" }
end
should 'not display contact button for community if its disable in environment' do
env = Environment.default
community = Community.create!(:name => 'my test community', :environment => env)
env.enable('disable_contact_community')
env.save!
community.add_member(@profile)
login_as(@profile.identifier)
get :index, :profile => community.identifier
assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{community.identifier}/new" }
end
should 'present confirmation before joining a profile' do
community = Community.create!(:name => 'my test community')
login_as @profile.identifier
get :join, :profile => community.identifier
assert_response :success
assert_template 'join'
end
should 'actually join profile' do
community = Community.create!(:name => 'my test community')
login_as @profile.identifier
post :join, :profile => community.identifier, :confirmation => '1'
assert_response :redirect
assert_redirected_to community.url
profile = Profile.find(@profile.id)
assert profile.memberships.include?(community), 'profile should be actually added to the community'
end
should 'join profile from wizard' do
community = Community.create!(:name => 'my test community')
login_as @profile.identifier
post :join, :profile => community.identifier, :confirmation => '1', :wizard => true
assert_response :redirect
assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
profile = Profile.find(@profile.id)
assert profile.memberships.include?(community), 'profile should be actually added to the community'
end
should 'create task when join to closed organization' do
community = Community.create!(:name => 'my test community', :closed => true)
login_as @profile.identifier
assert_difference AddMember, :count do
post :join, :profile => community.identifier, :confirmation => '1'
end
end
should 'require login to join community' do
community = Community.create!(:name => 'my test community', :closed => true)
get :join, :profile => community.identifier
assert_redirected_to :controller => 'account', :action => 'login'
end
should 'require login to refuse join community' do
community = Community.create!(:name => 'my test community', :closed => true)
get :refuse_join, :profile => community.identifier
assert_redirected_to :controller => 'account', :action => 'login'
end
should 'register join refusal' do
community = Community.create!(:name => 'my test community', :closed => true)
login_as @profile.identifier
get :refuse_join, :profile => community.identifier
p = Person.find(@profile.id)
assert_includes p.refused_communities, community
end
should 'register in session join refusal' do
community = Community.create!(:name => 'my test community')
get :refuse_for_now, :profile => community.identifier
assert_not_nil session[:no_asking]
assert_includes session[:no_asking], community.id
end
should 'record only 10 communities in session' do
@request.session[:no_asking] = (1..10).to_a
community = Community.create!(:name => 'my test community')
get :refuse_for_now, :profile => community.identifier
assert_equal ((2..10).to_a + [community.id]), @request.session[:no_asking]
end
should 'present confirmation before leaving a profile' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
login_as(profile.identifier)
get :leave, :profile => community.identifier
assert_template 'leave'
assert_tag :tag => 'input', :attributes => {:value => 'Yes, I want to leave.', :type => 'submit'}
end
should 'actually leave profile' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
assert_includes profile.memberships, community
login_as(profile.identifier)
post :leave, :profile => community.identifier, :confirmation => '1'
profile = Profile.find(@profile.id)
assert_not_includes profile.memberships, community
end
should 'leave profile when on wizard' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
login_as(profile.identifier)
post :leave, :profile => community.identifier, :confirmation => '1', :wizard => true
assert_response :redirect
assert_redirected_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
profile = Profile.find(@profile.id)
assert_not_includes profile.memberships, community
end
should "offer button to close 'leave community' lightbox" do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
login_as(profile.identifier)
get :index, :profile => community.identifier
assert_tag :tag => 'a', :content => 'Leave', :attributes => { :href => "/profile/#{community.identifier}/leave", :class => /^lbOn/ }
end
should 'offer button to cancel leaving community' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
login_as(profile.identifier)
get :leave, :profile => community.identifier
assert_tag :tag => 'a', :content => "No, I don't want."
end
should 'render without layout when use lightbox to leave community' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
@request.stubs(:xhr?).returns(true)
login_as(profile.identifier)
get :leave, :profile => community.identifier
assert_no_tag :tag => 'body' # e.g. no layout
end
should 'require login to leave community' do
community = Community.create!(:name => 'my test community')
get :leave, :profile => community.identifier
assert_redirected_to :controller => 'account', :action => 'login'
end
should 'redirect to stored location after leave community' do
community = Community.create!(:name => 'my test community')
community.add_member(profile)
@request.session[:return_to] = "/profile/#{community.identifier}/to_go"
login_as(profile.identifier)
post :leave, :profile => community.identifier, :confirmation => '1'
assert_redirected_to "/profile/#{community.identifier}/to_go"
end
should 'store referer location when request leave via get' do
community = Community.create!(:name => 'my test community')
login_as(profile.identifier)
assert_nil @request.session[:return_to]
@request.expects(:referer).returns("/profile/redirect_to")
get :leave, :profile => community.identifier
assert_equal '/profile/redirect_to', @request.session[:return_to]
end
should 'store referer location when request join via get' do
community = Community.create!(:name => 'my test community')
login_as(profile.identifier)
assert_nil @request.session[:return_to]
@request.expects(:referer).returns("/profile/redirect_to").at_least_once
get :join, :profile => community.identifier
assert_equal '/profile/redirect_to', @request.session[:return_to]
end
should 'redirect to stored location after join community' do
community = Community.create!(:name => 'my test community')
@request.session[:return_to] = "/profile/#{community.identifier}/to_go"
login_as(profile.identifier)
post :join, :profile => community.identifier, :confirmation => '1'
assert_redirected_to "/profile/#{community.identifier}/to_go"
end
should 'store location before login when request join via get not logged' do
community = Community.create!(:name => 'my test community')
@request.expects(:referer).returns("/profile/#{community.identifier}")
get :join, :profile => community.identifier
assert_equal "/profile/#{community.identifier}", @request.session[:before_join]
end
should 'redirect to location before login after join community' do
community = Community.create!(:name => 'my test community')
@request.session[:return_to] = "/profile/#{community.identifier}/to_go"
login_as(profile.identifier)
post :join, :profile => community.identifier, :confirmation => '1'
assert_redirected_to "/profile/#{community.identifier}/to_go"
assert_nil @request.session[:before_join]
end
should 'show link to events in index' do
get :index, :profile => profile.identifier
assert_tag :tag => 'a', :attributes => { :href => "/profile/#{profile.identifier}/events" }
end
end