cirandas.net

ref: master

test/functional/application_controller_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
require_relative '../test_helper'

class ApplicationControllerTest < ActionController::TestCase
  all_fixtures

  def setup
    @controller = TestController.new
  end

  def test_detection_of_environment_by_host
    uses_host 'www.colivre.net'
    get :index

    assert_kind_of Environment, assigns(:environment)

    assert_kind_of Domain, assigns(:domain)
    assert_equal 'colivre.net', assigns(:domain).name

    assert_nil assigns(:profile)
  end

  def test_detect_profile_by_host
    uses_host 'www.jrh.net'
    get :index

    assert_kind_of Environment, assigns(:environment)

    assert_kind_of Domain, assigns(:domain)
    assert_equal 'jrh.net', assigns(:domain).name

    assert_kind_of Profile, assigns(:profile)
  end

  def test_unknown_domain_falls_back_to_default_environment
    uses_host 'veryunprobabledomain.com'

    get :index
    assert_kind_of Environment, assigns(:environment)
    assert assigns(:environment).is_default?
  end

  should 'detect the current environment' do
    default = Environment.default
    Environment.stubs(:default).returns(default)
    default.stubs(:top_url).returns('http://default.com/')

    current = fast_create(Environment, :name => 'test environment')
    current.domains.create!(:name => 'example.com')

    @request.env['HTTP_HOST'] = 'example.com'
    get :index

    assert_equal current, assigns(:environment)
  end


  def test_exist_environment_variable_to_helper_environment_identification
    get :index
    assert_not_nil assigns(:environment)
  end

  def test_get_against_post_only
    get :post_only
    assert_redirected_to :action => 'index'
  end
  def test_post_against_post_only
    post :post_only
    assert_response :success
    assert_tag :tag => 'span', :content => 'post_only'
  end

  def test_should_generate_help_box_when_passing_string
    get :help_with_string
    assert_tag({
      :tag => 'div',
      :attributes => { :class => 'help_box'},
      :descendant => {
        :tag => 'div',
        :attributes => { :class => 'help_message', :style => /display:\s+none/},
        :descendant => { :tag => 'div', :content => /my_help_message/ }
      }
    })
  end

  def test_should_generate_help_box_when_passing_block
    get :help_with_block
    assert_tag({
      :tag => 'div',
      :attributes => { :class => 'help_box'},
      :descendant => {
        :tag => 'div',
        :attributes => { :class => 'help_message', :style => /display:\s+none/},
        :descendant => { :tag => 'div', :content => /my_help_message/ }
      }
    })
  end

  def test_shouldnt_generate_help_box_markup_when_no_block_is_passed
    get :help_without_block
    assert_no_tag({
      :tag => 'div',
      :attributes => { :class => 'help_box'},
    })
  end

  should 'be able to not use design blocks' do

    class UsesBlocksTestController < ApplicationController
    end
    assert UsesBlocksTestController.new.send(:uses_design_blocks?)

    class DoesNotUsesBlocksTestController < ApplicationController
      no_design_blocks
    end
    refute DoesNotUsesBlocksTestController.new.send(:uses_design_blocks?)
  end

  should 'generate blocks' do
    get :index
    assert_tag :tag => 'div', :attributes => { :id => 'boxes', :class => 'boxes' }
  end

  should 'not generate blocks when told not to do so' do
    @controller.stubs(:uses_design_blocks?).returns(false)
    get :index
    assert_no_tag :tag => 'div', :attributes => { :id => 'boxes', :class => 'boxes'  }
  end

  should 'display only some categories in menu' do
    @controller.stubs(:get_layout).returns('application')
    c1 = Environment.default.categories.create!(:name => 'Category 1', :display_color => 'ffa500', :parent_id => nil, :display_in_menu => true )
    c2 = Environment.default.categories.create!(:name => 'Category 2', :display_color => nil, :parent_id => c1.id, :display_in_menu => true )
    get :index
    assert_tag :tag => 'a', :content => /Category 2/
  end

  should 'not display some categories in menu' do
    @controller.stubs(:get_layout).returns('application')
    c1 = Environment.default.categories.create!(:name => 'Category 1', :display_color => 'ffa500', :parent_id => nil, :display_in_menu => true)
    c2 = Environment.default.categories.create!(:name => 'Category 2', :display_color => nil, :parent_id => c1)
    get :index
    assert_no_tag :tag => 'a', :content => /Category 2/
  end

  should 'display dropdown for select language' do
    @controller.stubs(:get_layout).returns('application')
    Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once
    get :index, :lang => 'en'
    assert_tag :tag => 'option', :attributes => { :value => 'en', :selected => 'selected' }, :content => 'English'
    assert_no_tag :tag => 'option', :attributes => { :value => 'pt_BR', :selected => 'selected' }, :content => 'Português Brasileiro'
    assert_tag :tag => 'option', :attributes => { :value => 'pt_BR' }, :content => 'Português Brasileiro'
    assert_tag :tag => 'option', :attributes => { :value => 'fr' }, :content => 'Français'
    assert_tag :tag => 'option', :attributes => { :value => 'it' }, :content => 'Italiano'
  end

  should 'set and unset the current user' do
    testuser = create_user 'testuser'
    login_as 'testuser'
    User.expects(:current=).with do |user|
      user == testuser
    end.at_least_once
    User.expects(:current=).with(nil)
    get :index
  end

  should 'display link to webmail if enabled for system' do
    @controller.stubs(:get_layout).returns('application')
    login_as('ze')
    MailConf.expects(:enabled?).returns(true)
    MailConf.expects(:webmail_url).returns('http://web.mail/')

    get :index
    assert_tag :tag => 'div', :attributes => { :id => 'user_box' }, :descendant => { :tag => 'a', :attributes => { :href => 'http://web.mail/' } }
  end

  should 'not display link to webmail if not enabled for system' do
    @controller.stubs(:get_layout).returns('application')
    login_as('ze')
    MailConf.expects(:enabled?).returns(false)

    get :index
    assert_no_tag :tag => 'div', :attributes => { :id => 'user_box' }, :descendant => { :tag => 'a', :attributes => { :href => 'http://web.mail/' } }
  end

  should 'display search form with id' do
    @controller.stubs(:get_layout).returns('application-ng')
    get :index
    assert_tag :tag => 'form', :attributes => { :class => 'search_form clean', :id => 'top-search' }
  end

  should 'display theme test panel when testing theme' do
    @request.session[:user_theme] = 'my-test-theme'
    theme = mock
    profile = mock
    theme.expects(:owner).returns(profile).at_least_once
    profile.expects(:identifier).returns('testinguser').at_least_once
    Theme.expects(:find).with('my-test-theme').returns(theme).at_least_once
    get :index

    assert_tag :tag => 'div', :attributes => { :id => 'theme-test-panel' }, :descendant => {
      :tag => 'a', :attributes => { :href => '/myprofile/testinguser/profile_themes/edit/my-test-theme'}
    }
      #{ :tag => 'a', :attributes => { :href => '/myprofile/testinguser/themes/stop_test/my-test-theme'} }
  end

  should 'not display theme test panel in general' do
    @controller.stubs(:session).returns({})
    get :index
    assert_no_tag :tag => 'div', :attributes => { :id => 'theme-test-panel' }
  end

  should 'not display categories menu if categories feature disabled' do
    Environment.any_instance.stubs(:enabled?).with(anything).returns(true)
    c1 = Environment.default.categories.create!(:name => 'Category 1', :display_color => 'ffa500', :parent_id => nil, :display_in_menu => true )
    c2 = Environment.default.categories.create!(:name => 'Category 2', :display_color => nil, :parent_id => c1.id, :display_in_menu => true )
    get :index
    assert_no_tag :tag => 'a', :content => /Category 2/
  end

  should 'show name of article as title of page without environment' do
    p = create_user('test_user').person
    a = p.articles.create!(:name => 'test article')

    @controller.instance_variable_set('@profile', p)
    @controller.instance_variable_set('@page', a)

    get :index
    assert_tag 'title', :content => 'test article - ' + p.name
  end

  should 'diplay name of profile in the title without environment' do
    p = create_user('test_user').person
    p.name = 'Some Test User'
    p.save!
    @controller.instance_variable_set('@profile', p)

    get :index, :profile => p.identifier
    assert_tag 'title', :content => p.name
  end

  should 'display environment name in title when profile and page are not defined' do
    get :index
    assert_tag 'title', :content => assigns(:environment).name
  end

  should 'display menu links for my environment when logged in other environment' do
    @controller.stubs(:get_layout).returns('application')
    e = fast_create(Environment, :name => 'other_environment')
    e.domains << Domain.new(:name => 'other.environment')
    e.save!

    login_as(create_admin_user(e))
    uses_host 'other.environment'
    get :index
    assert_tag :tag => 'div', :attributes => {:id => 'user_menu_ul'}
    assert_tag tag: 'div', attributes: {id: 'user_menu_ul'}, descendant: {tag: 'a', attributes: { href: '/admin' }}
  end

  should 'add plugin items on user menu' do
    create_user 'testuser'
    login_as 'testuser'
    class Plugin1 < Noosfero::Plugin
      def user_menu_items(user)
        proc { content_tag('span', 'Plugin1')}
      end
    end
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([Plugin1.new])

    get :index
    assert_tag :tag => 'div', :attributes => {:id => 'user'}
    assert_tag tag: 'div', attributes: {id: 'user'}, descendant: {tag: 'span', content: 'Plugin1' }
  end

  should 'not display invisible blocks' do
    @controller.expects(:uses_design_blocks?).returns(true)
    p = create_user('test_user').person
    @controller.expects(:profile).at_least_once.returns(p)

    box = p.boxes.first
    invisible_block = fast_create(Block, :box_id => box.id)
    invisible_block.display = 'never'
    invisible_block.save
    visible_block = fast_create(Block, :box_id => box.id)
    visible_block.display = 'always'
    visible_block.save

    get :index, :profile => p.identifier
    assert_no_tag :tag => 'div', :attributes => {:id => 'block-' + invisible_block.id.to_s}
    assert_tag :tag => 'div', :attributes => {:id => 'block-' + visible_block.id.to_s}
  end

  should 'diplay name of environment in description' do
    get :index
    assert_tag :tag => 'meta', :attributes => { :name => 'description', :content => assigns(:environment).name }
  end

  should 'set html lang as the article language if an article is present and has a language' do
    p = create_user('test_user').person
    a = fast_create(Article, :name => 'test article', :language => 'fr', :profile_id => p.id )
    @controller.instance_variable_set('@page', a)
    FastGettext.stubs(:locale).returns('es')
    get :index
    assert_tag :html, :attributes => { :lang => 'fr' }
  end

  should 'set html lang as locale if no page present' do
    FastGettext.stubs(:locale).returns('es')
    get :index
    assert_tag :html, :attributes => { :lang => 'es' }
  end

  should 'set html lang as locale if page has no language' do
    p = create_user('test_user').person
    a = fast_create(Article, :name => 'test article', :language => nil, :profile_id => p.id )

    @controller.instance_variable_set('@page', a)
    FastGettext.stubs(:locale).returns('es')
    get :index
    assert_tag :html, :attributes => { :lang => 'es' }
  end

  should 'set Rails locale correctly' do
    @request.env['HTTP_ACCEPT_LANGUAGE'] = 'pt-BR,pt;q=0.8,en;q=0.6,en-US;q=0.4'
    get :index
    assert_equal 'pt', I18n.locale.to_s
  end

  should 'include stylesheets supplied by plugins' do
    class Plugin1 < Noosfero::Plugin
      def stylesheet?
        true
      end
    end
    plugin1_path = '/plugin1/style.css'

    class Plugin2 < Noosfero::Plugin
      def stylesheet?
        true
      end
    end
    plugin2_path = '/plugin2/style.css'

    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    environment = Environment.default
    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)

    get :index

    assert_tag tag: 'link', attributes: {href: /#{plugin1_path}/, rel: 'stylesheet'}
    assert_tag tag: 'link', attributes: {href: /#{plugin2_path}/, rel: 'stylesheet'}
  end

  should 'include javascripts supplied by plugins' do
    class Plugin1 < Noosfero::Plugin
      def js_files
        ['js1.js'.html_safe]
      end
    end

    js1 = 'js1.js'
    plugin1_path = '/plugin1/'+js1

    class Plugin2 < Noosfero::Plugin
      def js_files
        ['js2.js'.html_safe, 'js3.js'.html_safe]
      end
    end

    js2 = 'js2.js'
    js3 = 'js3.js'
    plugin2_path2 = '/plugin2/'+js2
    plugin2_path3 = '/plugin2/'+js3

    Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name])

    environment = Environment.default
    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)

    get :index

    assert_tag tag: 'script', attributes: {src: /#{plugin1_path}/}
    assert_tag tag: 'script', attributes: {src: /#{plugin2_path2}/}
    assert_tag tag: 'script', attributes: {src: /#{plugin2_path3}/}
  end

  should 'include content in the beginning of body supplied by plugins regardless it is a block or html code' do
    class TestBodyBeginning1Plugin < Noosfero::Plugin
      def body_beginning
        lambda {"<span id='plugin1'>This is [[plugin1]] speaking!</span>".html_safe}
      end
    end
    class TestBodyBeginning2Plugin < Noosfero::Plugin
      def body_beginning
        "<span id='plugin2'>This is Plugin2 speaking!</span>".html_safe
      end
    end

    Noosfero::Plugin.stubs(:all).returns([TestBodyBeginning1Plugin.name, TestBodyBeginning2Plugin.name])

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBodyBeginning1Plugin.new, TestBodyBeginning2Plugin.new])

    get :index

    assert_tag :tag => 'span', :content => 'This is [[plugin1]] speaking!', :attributes => {:id => 'plugin1'}
    assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'}
  end

  should 'include content in the ending of head supplied by plugins regardless it is a block or html code' do

    class TestHeadEnding1Plugin < Noosfero::Plugin
      def head_ending
        lambda {"<script>alert('This is [[plugin1]] speaking!')</script>".html_safe}
      end
    end
    class TestHeadEnding2Plugin < Noosfero::Plugin
      def head_ending
        "<style>This is Plugin2 speaking!</style>".html_safe
      end
    end

    Noosfero::Plugin.stubs(:all).returns([TestHeadEnding1Plugin.name, TestHeadEnding2Plugin.name])

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestHeadEnding1Plugin.new, TestHeadEnding2Plugin.new])

    get :index

    assert_tag :tag => 'script', :content => "alert('This is [[plugin1]] speaking!')"
    assert_tag :tag => 'style', :content => 'This is Plugin2 speaking!'
  end

  should 'not include jquery-validation language script if they do not exist' do
    Noosfero.stubs(:available_locales).returns(['bli'])
    get :index, :lang => 'bli'
    assert_no_tag :tag => 'script', :attributes => {:src => /messages_bli/}
    assert_no_tag :tag => 'script', :attributes => {:src => /methods_bli/}
  end

  should 'set access-control-allow-origin and method if configured' do
    e = Environment.default
    e.access_control_allow_origin = ['http://allowed']
    e.save!

    @request.headers["Origin"] = "http://allowed"
    get :index
    assert_response :success

    @request.headers["Origin"] = "http://other"
    get :index
    assert_response :success

    @request.headers["Origin"] = "http://other"
    e.restrict_to_access_control_origins = true
    e.save!
    get :index
    assert_response :forbidden
  end

  should 'register search_term occurrence on find_by_contents' do
    controller = ApplicationController.new
    controller.stubs(:environment).returns(Environment.default)
    assert_difference 'SearchTermOccurrence.count', 1 do
      controller.send(:find_by_contents, :people, Environment.default, Person, 'search_term', paginate_options={:page => 1}, options={})
      process_delayed_job_queue
    end
  end

  should 'allow plugin to propose search terms suggestions' do
    class SuggestionsPlugin < Noosfero::Plugin
      def find_suggestions(query, context, asset, options={:limit => 5})
        ['a', 'b', 'c']
      end
    end

    controller = ApplicationController.new
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SuggestionsPlugin.new])

    assert_equal ['a', 'b', 'c'], controller.send(:find_suggestions, 'random', Environment.default, 'random')
  end

  should 'redirect to login if environment is restrict to members' do
    Environment.default.enable(:restrict_to_members)
    get :index
    assert_redirected_to :controller => 'account', :action => 'login'
  end

  should 'override user when current is an admin' do
    user        = create_user
    other_user  = create_user
    environment = Environment.default
    login_as user.login
    @controller.stubs(:environment).returns(environment)

    get :index, override_user: other_user.id
    assert_equal user, assigns(:current_user)

    environment.add_admin user.person
    get :index, override_user: other_user.id
    assert_equal other_user, assigns(:current_user)
  end

  should 'do not allow member not included in whitelist to access an restricted environment' do
    user = create_user
    e = Environment.default
    e.enable(:restrict_to_members)
    e.members_whitelist_enabled = true
    e.save!
    login_as(user.login)
    get :index
    assert_response :forbidden
  end

  should 'allow member in whitelist to access an environment' do
    user = create_user
    e = Environment.default
    e.members_whitelist_enabled = true
    e.members_whitelist = "#{user.person.id}"
    e.save!
    login_as(user.login)
    get :index
    assert_response :success
  end

  should 'allow members to access an environment if whitelist is disabled' do
    user = create_user
    e = Environment.default
    e.members_whitelist_enabled = false
    e.save!
    login_as(user.login)
    get :index
    assert_response :success
  end

  should 'allow admin to access an environment if whitelist is enabled' do
    e = Environment.default
    e.members_whitelist_enabled = true
    e.save!
    login_as(create_admin_user(e))
    get :index
    assert_response :success
  end

  should 'not check whitelist members if the environment is not restrict to members' do
    e = Environment.default
    e.disable(:restrict_to_members)
    e.members_whitelist_enabled = true
    e.save!
    @controller.expects(:verify_members_whitelist).never
    login_as create_user.login
    get :index
    assert_response :success
  end

  should "redirect to 404 if profile is '~' and user is not logged in" do
    get :index, :profile => '~'
    assert_response :missing
  end

  should "redirect to action when profile is '~' " do
    login_as('ze')
    get :index, :profile => '~'
    assert_response 302
  end

  should "substitute '~' by current user and redirect properly " do
    login_as('ze')
    profile = Profile.where(:identifier => 'ze').first
    get :index, :profile => '~'
    assert_redirected_to :controller => 'test', :action => 'index', :profile => profile.identifier
  end

  should 'set session theme if a params theme is passed as parameter' do
    current_theme = 'my-test-theme'
    environment = Environment.default
    Theme.stubs(:system_themes).returns([Theme.new(current_theme)])
    environment.themes = [current_theme]
    environment.save!
    assert_nil @request.session[:theme]
    get :index, :theme => current_theme
    assert_equal current_theme, @request.session[:theme]
  end

  should 'set session theme only in environment available themes' do
    environment = Environment.default
    assert_nil @request.session[:theme]
    environment.stubs(:theme_ids).returns(['another_theme'])
    get :index, :theme => 'my-test-theme'
    assert_nil @request.session[:theme]
  end

  should 'unset session theme if not environment available themes is defined' do
    environment = Environment.default
    current_theme = 'my-test-theme'
    Theme.stubs(:system_themes).returns([Theme.new(current_theme)])
    environment.themes = [current_theme]
    environment.save!
    get :index, :theme => current_theme
    assert_equal current_theme, @request.session[:theme]

    get :index, :theme => 'another_theme'
    assert_nil @request.session[:theme]
  end

end