Mercurial > hg > soundsoftware-site
comparison test/functional/welcome_controller_test.rb @ 1526:404aa68d4227
Merge from live branch
author | Chris Cannam |
---|---|
date | Thu, 11 Sep 2014 12:46:20 +0100 |
parents | dffacf8a6908 |
children |
comparison
equal
deleted
inserted
replaced
1493:a5f2bdf3b486 | 1526:404aa68d4227 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2014 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
14 # You should have received a copy of the GNU General Public License | 14 # You should have received a copy of the GNU General Public License |
15 # along with this program; if not, write to the Free Software | 15 # along with this program; if not, write to the Free Software |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 require File.expand_path('../../test_helper', __FILE__) | 18 require File.expand_path('../../test_helper', __FILE__) |
19 require 'welcome_controller' | |
20 | |
21 # Re-raise errors caught by the controller. | |
22 class WelcomeController; def rescue_action(e) raise e end; end | |
23 | 19 |
24 class WelcomeControllerTest < ActionController::TestCase | 20 class WelcomeControllerTest < ActionController::TestCase |
25 fixtures :projects, :news, :users, :members | 21 fixtures :projects, :news, :users, :members |
26 | 22 |
27 def setup | 23 def setup |
28 @controller = WelcomeController.new | |
29 @request = ActionController::TestRequest.new | |
30 @response = ActionController::TestResponse.new | |
31 User.current = nil | 24 User.current = nil |
32 end | 25 end |
33 | 26 |
34 def test_index | 27 def test_index |
35 get :index | 28 get :index |
36 assert_response :success | 29 assert_response :success |
37 assert_template 'index' | 30 assert_template 'index' |
38 assert_not_nil assigns(:news) | 31 assert_not_nil assigns(:news) |
39 assert_not_nil assigns(:projects) | 32 assert_not_nil assigns(:projects) |
40 assert !assigns(:projects).include?(Project.find(:first, :conditions => {:is_public => false})) | 33 assert !assigns(:projects).include?(Project.where(:is_public => false).first) |
41 end | 34 end |
42 | 35 |
43 def test_browser_language | 36 def test_browser_language |
44 Setting.default_language = 'en' | 37 Setting.default_language = 'en' |
45 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | 38 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' |
57 def test_browser_language_alternate_not_valid | 50 def test_browser_language_alternate_not_valid |
58 Setting.default_language = 'en' | 51 Setting.default_language = 'en' |
59 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr-CA' | 52 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr-CA' |
60 get :index | 53 get :index |
61 assert_equal :fr, @controller.current_language | 54 assert_equal :fr, @controller.current_language |
55 end | |
56 | |
57 def test_browser_language_should_be_ignored_with_force_default_language_for_anonymous | |
58 Setting.default_language = 'en' | |
59 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
60 with_settings :force_default_language_for_anonymous => '1' do | |
61 get :index | |
62 assert_equal :en, @controller.current_language | |
63 end | |
64 end | |
65 | |
66 def test_user_language_should_be_used | |
67 Setting.default_language = 'fi' | |
68 user = User.find(2).update_attribute :language, 'it' | |
69 @request.session[:user_id] = 2 | |
70 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
71 get :index | |
72 assert_equal :it, @controller.current_language | |
73 end | |
74 | |
75 def test_user_language_should_be_ignored_if_force_default_language_for_loggedin | |
76 Setting.default_language = 'fi' | |
77 user = User.find(2).update_attribute :language, 'it' | |
78 @request.session[:user_id] = 2 | |
79 @request.env['HTTP_ACCEPT_LANGUAGE'] = 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' | |
80 with_settings :force_default_language_for_loggedin => '1' do | |
81 get :index | |
82 assert_equal :fi, @controller.current_language | |
83 end | |
62 end | 84 end |
63 | 85 |
64 def test_robots | 86 def test_robots |
65 get :robots | 87 get :robots |
66 assert_response :success | 88 assert_response :success |
90 assert_no_tag 'script', | 112 assert_no_tag 'script', |
91 :attributes => {:type => "text/javascript"}, | 113 :attributes => {:type => "text/javascript"}, |
92 :content => %r{warnLeavingUnsaved} | 114 :content => %r{warnLeavingUnsaved} |
93 end | 115 end |
94 | 116 |
117 def test_logout_link_should_post | |
118 @request.session[:user_id] = 2 | |
119 | |
120 get :index | |
121 assert_select 'a[href=/logout][data-method=post]', :text => 'Sign out' | |
122 end | |
123 | |
95 def test_call_hook_mixed_in | 124 def test_call_hook_mixed_in |
96 assert @controller.respond_to?(:call_hook) | 125 assert @controller.respond_to?(:call_hook) |
97 end | 126 end |
98 | 127 |
99 def test_project_jump_box_should_escape_names_once | 128 def test_project_jump_box_should_escape_names_once |
104 assert_select "#header select" do | 133 assert_select "#header select" do |
105 assert_select "option", :text => 'Foo & Bar' | 134 assert_select "option", :text => 'Foo & Bar' |
106 end | 135 end |
107 end | 136 end |
108 | 137 |
109 context "test_api_offset_and_limit" do | 138 def test_api_offset_and_limit_without_params |
110 context "without params" do | 139 assert_equal [0, 25], @controller.api_offset_and_limit({}) |
111 should "return 0, 25" do | 140 end |
112 assert_equal [0, 25], @controller.api_offset_and_limit({}) | |
113 end | |
114 end | |
115 | 141 |
116 context "with limit" do | 142 def test_api_offset_and_limit_with_limit |
117 should "return 0, limit" do | 143 assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30}) |
118 assert_equal [0, 30], @controller.api_offset_and_limit({:limit => 30}) | 144 assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120}) |
119 end | 145 assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10}) |
146 end | |
120 | 147 |
121 should "not exceed 100" do | 148 def test_api_offset_and_limit_with_offset |
122 assert_equal [0, 100], @controller.api_offset_and_limit({:limit => 120}) | 149 assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10}) |
123 end | 150 assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10}) |
151 end | |
124 | 152 |
125 should "not be negative" do | 153 def test_api_offset_and_limit_with_offset_and_limit |
126 assert_equal [0, 25], @controller.api_offset_and_limit({:limit => -10}) | 154 assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50}) |
127 end | 155 end |
128 end | |
129 | 156 |
130 context "with offset" do | 157 def test_api_offset_and_limit_with_page |
131 should "return offset, 25" do | 158 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1}) |
132 assert_equal [10, 25], @controller.api_offset_and_limit({:offset => 10}) | 159 assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3}) |
133 end | 160 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0}) |
161 assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2}) | |
162 end | |
134 | 163 |
135 should "not be negative" do | 164 def test_api_offset_and_limit_with_page_and_limit |
136 assert_equal [0, 25], @controller.api_offset_and_limit({:offset => -10}) | 165 assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100}) |
137 end | 166 assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100}) |
167 end | |
138 | 168 |
139 context "and limit" do | 169 def test_unhautorized_exception_with_anonymous_should_redirect_to_login |
140 should "return offset, limit" do | 170 WelcomeController.any_instance.stubs(:index).raises(::Unauthorized) |
141 assert_equal [10, 50], @controller.api_offset_and_limit({:offset => 10, :limit => 50}) | |
142 end | |
143 end | |
144 end | |
145 | 171 |
146 context "with page" do | 172 get :index |
147 should "return offset, 25" do | 173 assert_response 302 |
148 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 1}) | 174 assert_redirected_to('/login?back_url='+CGI.escape('http://test.host/')) |
149 assert_equal [50, 25], @controller.api_offset_and_limit({:page => 3}) | 175 end |
150 end | |
151 | 176 |
152 should "not be negative" do | 177 def test_unhautorized_exception_with_anonymous_and_xmlhttprequest_should_respond_with_401_to_anonymous |
153 assert_equal [0, 25], @controller.api_offset_and_limit({:page => 0}) | 178 WelcomeController.any_instance.stubs(:index).raises(::Unauthorized) |
154 assert_equal [0, 25], @controller.api_offset_and_limit({:page => -2}) | |
155 end | |
156 | 179 |
157 context "and limit" do | 180 @request.env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" |
158 should "return offset, limit" do | 181 get :index |
159 assert_equal [0, 100], @controller.api_offset_and_limit({:page => 1, :limit => 100}) | 182 assert_response 401 |
160 assert_equal [200, 100], @controller.api_offset_and_limit({:page => 3, :limit => 100}) | |
161 end | |
162 end | |
163 end | |
164 end | 183 end |
165 end | 184 end |