##// END OF EJS Templates
define constant of escaped "can't" for tests...
Toshi MARUYAMA -
r12505:5e94d388b5c2
parent child
Show More
@@ -1,480 +1,484
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 #require 'shoulda'
19 19 ENV["RAILS_ENV"] = "test"
20 20 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
21 21 require 'rails/test_help'
22 22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
23 23
24 24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
25 25 include ObjectHelpers
26 26
27 27 require 'awesome_nested_set/version'
28 28
29 29 class ActiveSupport::TestCase
30 30 include ActionDispatch::TestProcess
31 31
32 32 self.use_transactional_fixtures = true
33 33 self.use_instantiated_fixtures = false
34 34
35 ESCAPED_CANT='can't'
36 # Rails 4.0.2
37 #ESCAPED_CANT='can't'
38
35 39 def log_user(login, password)
36 40 User.anonymous
37 41 get "/login"
38 42 assert_equal nil, session[:user_id]
39 43 assert_response :success
40 44 assert_template "account/login"
41 45 post "/login", :username => login, :password => password
42 46 assert_equal login, User.find(session[:user_id]).login
43 47 end
44 48
45 49 def uploaded_test_file(name, mime)
46 50 fixture_file_upload("files/#{name}", mime, true)
47 51 end
48 52
49 53 def credentials(user, password=nil)
50 54 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
51 55 end
52 56
53 57 # Mock out a file
54 58 def self.mock_file
55 59 file = 'a_file.png'
56 60 file.stubs(:size).returns(32)
57 61 file.stubs(:original_filename).returns('a_file.png')
58 62 file.stubs(:content_type).returns('image/png')
59 63 file.stubs(:read).returns(false)
60 64 file
61 65 end
62 66
63 67 def mock_file
64 68 self.class.mock_file
65 69 end
66 70
67 71 def mock_file_with_options(options={})
68 72 file = ''
69 73 file.stubs(:size).returns(32)
70 74 original_filename = options[:original_filename] || nil
71 75 file.stubs(:original_filename).returns(original_filename)
72 76 content_type = options[:content_type] || nil
73 77 file.stubs(:content_type).returns(content_type)
74 78 file.stubs(:read).returns(false)
75 79 file
76 80 end
77 81
78 82 # Use a temporary directory for attachment related tests
79 83 def set_tmp_attachments_directory
80 84 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
81 85 unless File.directory?("#{Rails.root}/tmp/test/attachments")
82 86 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
83 87 end
84 88 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
85 89 end
86 90
87 91 def set_fixtures_attachments_directory
88 92 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
89 93 end
90 94
91 95 def with_settings(options, &block)
92 96 saved_settings = options.keys.inject({}) do |h, k|
93 97 h[k] = case Setting[k]
94 98 when Symbol, false, true, nil
95 99 Setting[k]
96 100 else
97 101 Setting[k].dup
98 102 end
99 103 h
100 104 end
101 105 options.each {|k, v| Setting[k] = v}
102 106 yield
103 107 ensure
104 108 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
105 109 end
106 110
107 111 # Yields the block with user as the current user
108 112 def with_current_user(user, &block)
109 113 saved_user = User.current
110 114 User.current = user
111 115 yield
112 116 ensure
113 117 User.current = saved_user
114 118 end
115 119
116 120 def change_user_password(login, new_password)
117 121 user = User.where(:login => login).first
118 122 user.password, user.password_confirmation = new_password, new_password
119 123 user.save!
120 124 end
121 125
122 126 def self.ldap_configured?
123 127 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
124 128 return @test_ldap.bind
125 129 rescue Exception => e
126 130 # LDAP is not listening
127 131 return nil
128 132 end
129 133
130 134 def self.convert_installed?
131 135 Redmine::Thumbnail.convert_available?
132 136 end
133 137
134 138 # Returns the path to the test +vendor+ repository
135 139 def self.repository_path(vendor)
136 140 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
137 141 end
138 142
139 143 # Returns the url of the subversion test repository
140 144 def self.subversion_repository_url
141 145 path = repository_path('subversion')
142 146 path = '/' + path unless path.starts_with?('/')
143 147 "file://#{path}"
144 148 end
145 149
146 150 # Returns true if the +vendor+ test repository is configured
147 151 def self.repository_configured?(vendor)
148 152 File.directory?(repository_path(vendor))
149 153 end
150 154
151 155 def repository_path_hash(arr)
152 156 hs = {}
153 157 hs[:path] = arr.join("/")
154 158 hs[:param] = arr.join("/")
155 159 hs
156 160 end
157 161
158 162 def assert_save(object)
159 163 saved = object.save
160 164 message = "#{object.class} could not be saved"
161 165 errors = object.errors.full_messages.map {|m| "- #{m}"}
162 166 message << ":\n#{errors.join("\n")}" if errors.any?
163 167 assert_equal true, saved, message
164 168 end
165 169
166 170 def assert_error_tag(options={})
167 171 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
168 172 end
169 173
170 174 def assert_include(expected, s, message=nil)
171 175 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
172 176 end
173 177
174 178 def assert_not_include(expected, s, message=nil)
175 179 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
176 180 end
177 181
178 182 def assert_select_in(text, *args, &block)
179 183 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
180 184 assert_select(d, *args, &block)
181 185 end
182 186
183 187 def assert_mail_body_match(expected, mail, message=nil)
184 188 if expected.is_a?(String)
185 189 assert_include expected, mail_body(mail), message
186 190 else
187 191 assert_match expected, mail_body(mail), message
188 192 end
189 193 end
190 194
191 195 def assert_mail_body_no_match(expected, mail, message=nil)
192 196 if expected.is_a?(String)
193 197 assert_not_include expected, mail_body(mail), message
194 198 else
195 199 assert_no_match expected, mail_body(mail), message
196 200 end
197 201 end
198 202
199 203 def mail_body(mail)
200 204 mail.parts.first.body.encoded
201 205 end
202 206
203 207 # awesome_nested_set new node lft and rgt value changed this refactor revision.
204 208 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61
205 209 # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line.
206 210 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273
207 211 # It seems correct behavior because of this line comment.
208 212 # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278
209 213 def new_issue_lft
210 214 ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1
211 215 end
212 216 end
213 217
214 218 module Redmine
215 219 module ApiTest
216 220 # Base class for API tests
217 221 class Base < ActionDispatch::IntegrationTest
218 222 # Test that a request allows the three types of API authentication
219 223 #
220 224 # * HTTP Basic with username and password
221 225 # * HTTP Basic with an api key for the username
222 226 # * Key based with the key=X parameter
223 227 #
224 228 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
225 229 # @param [String] url the request url
226 230 # @param [optional, Hash] parameters additional request parameters
227 231 # @param [optional, Hash] options additional options
228 232 # @option options [Symbol] :success_code Successful response code (:success)
229 233 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
230 234 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
231 235 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
232 236 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
233 237 should_allow_key_based_auth(http_method, url, parameters, options)
234 238 end
235 239
236 240 # Test that a request allows the username and password for HTTP BASIC
237 241 #
238 242 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
239 243 # @param [String] url the request url
240 244 # @param [optional, Hash] parameters additional request parameters
241 245 # @param [optional, Hash] options additional options
242 246 # @option options [Symbol] :success_code Successful response code (:success)
243 247 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
244 248 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
245 249 success_code = options[:success_code] || :success
246 250 failure_code = options[:failure_code] || :unauthorized
247 251
248 252 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
249 253 context "with a valid HTTP authentication" do
250 254 setup do
251 255 @user = User.generate! do |user|
252 256 user.admin = true
253 257 user.password = 'my_password'
254 258 end
255 259 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
256 260 end
257 261
258 262 should_respond_with success_code
259 263 should_respond_with_content_type_based_on_url(url)
260 264 should "login as the user" do
261 265 assert_equal @user, User.current
262 266 end
263 267 end
264 268
265 269 context "with an invalid HTTP authentication" do
266 270 setup do
267 271 @user = User.generate!
268 272 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
269 273 end
270 274
271 275 should_respond_with failure_code
272 276 should_respond_with_content_type_based_on_url(url)
273 277 should "not login as the user" do
274 278 assert_equal User.anonymous, User.current
275 279 end
276 280 end
277 281
278 282 context "without credentials" do
279 283 setup do
280 284 send(http_method, url, parameters)
281 285 end
282 286
283 287 should_respond_with failure_code
284 288 should_respond_with_content_type_based_on_url(url)
285 289 should "include_www_authenticate_header" do
286 290 assert @controller.response.headers.has_key?('WWW-Authenticate')
287 291 end
288 292 end
289 293 end
290 294 end
291 295
292 296 # Test that a request allows the API key with HTTP BASIC
293 297 #
294 298 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
295 299 # @param [String] url the request url
296 300 # @param [optional, Hash] parameters additional request parameters
297 301 # @param [optional, Hash] options additional options
298 302 # @option options [Symbol] :success_code Successful response code (:success)
299 303 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
300 304 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
301 305 success_code = options[:success_code] || :success
302 306 failure_code = options[:failure_code] || :unauthorized
303 307
304 308 context "should allow http basic auth with a key for #{http_method} #{url}" do
305 309 context "with a valid HTTP authentication using the API token" do
306 310 setup do
307 311 @user = User.generate! do |user|
308 312 user.admin = true
309 313 end
310 314 @token = Token.create!(:user => @user, :action => 'api')
311 315 send(http_method, url, parameters, credentials(@token.value, 'X'))
312 316 end
313 317 should_respond_with success_code
314 318 should_respond_with_content_type_based_on_url(url)
315 319 should_be_a_valid_response_string_based_on_url(url)
316 320 should "login as the user" do
317 321 assert_equal @user, User.current
318 322 end
319 323 end
320 324
321 325 context "with an invalid HTTP authentication" do
322 326 setup do
323 327 @user = User.generate!
324 328 @token = Token.create!(:user => @user, :action => 'feeds')
325 329 send(http_method, url, parameters, credentials(@token.value, 'X'))
326 330 end
327 331 should_respond_with failure_code
328 332 should_respond_with_content_type_based_on_url(url)
329 333 should "not login as the user" do
330 334 assert_equal User.anonymous, User.current
331 335 end
332 336 end
333 337 end
334 338 end
335 339
336 340 # Test that a request allows full key authentication
337 341 #
338 342 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
339 343 # @param [String] url the request url, without the key=ZXY parameter
340 344 # @param [optional, Hash] parameters additional request parameters
341 345 # @param [optional, Hash] options additional options
342 346 # @option options [Symbol] :success_code Successful response code (:success)
343 347 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
344 348 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
345 349 success_code = options[:success_code] || :success
346 350 failure_code = options[:failure_code] || :unauthorized
347 351
348 352 context "should allow key based auth using key=X for #{http_method} #{url}" do
349 353 context "with a valid api token" do
350 354 setup do
351 355 @user = User.generate! do |user|
352 356 user.admin = true
353 357 end
354 358 @token = Token.create!(:user => @user, :action => 'api')
355 359 # Simple url parse to add on ?key= or &key=
356 360 request_url = if url.match(/\?/)
357 361 url + "&key=#{@token.value}"
358 362 else
359 363 url + "?key=#{@token.value}"
360 364 end
361 365 send(http_method, request_url, parameters)
362 366 end
363 367 should_respond_with success_code
364 368 should_respond_with_content_type_based_on_url(url)
365 369 should_be_a_valid_response_string_based_on_url(url)
366 370 should "login as the user" do
367 371 assert_equal @user, User.current
368 372 end
369 373 end
370 374
371 375 context "with an invalid api token" do
372 376 setup do
373 377 @user = User.generate! do |user|
374 378 user.admin = true
375 379 end
376 380 @token = Token.create!(:user => @user, :action => 'feeds')
377 381 # Simple url parse to add on ?key= or &key=
378 382 request_url = if url.match(/\?/)
379 383 url + "&key=#{@token.value}"
380 384 else
381 385 url + "?key=#{@token.value}"
382 386 end
383 387 send(http_method, request_url, parameters)
384 388 end
385 389 should_respond_with failure_code
386 390 should_respond_with_content_type_based_on_url(url)
387 391 should "not login as the user" do
388 392 assert_equal User.anonymous, User.current
389 393 end
390 394 end
391 395 end
392 396
393 397 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
394 398 setup do
395 399 @user = User.generate! do |user|
396 400 user.admin = true
397 401 end
398 402 @token = Token.create!(:user => @user, :action => 'api')
399 403 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
400 404 end
401 405 should_respond_with success_code
402 406 should_respond_with_content_type_based_on_url(url)
403 407 should_be_a_valid_response_string_based_on_url(url)
404 408 should "login as the user" do
405 409 assert_equal @user, User.current
406 410 end
407 411 end
408 412 end
409 413
410 414 # Uses should_respond_with_content_type based on what's in the url:
411 415 #
412 416 # '/project/issues.xml' => should_respond_with_content_type :xml
413 417 # '/project/issues.json' => should_respond_with_content_type :json
414 418 #
415 419 # @param [String] url Request
416 420 def self.should_respond_with_content_type_based_on_url(url)
417 421 case
418 422 when url.match(/xml/i)
419 423 should "respond with XML" do
420 424 assert_equal 'application/xml', @response.content_type
421 425 end
422 426 when url.match(/json/i)
423 427 should "respond with JSON" do
424 428 assert_equal 'application/json', @response.content_type
425 429 end
426 430 else
427 431 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
428 432 end
429 433 end
430 434
431 435 # Uses the url to assert which format the response should be in
432 436 #
433 437 # '/project/issues.xml' => should_be_a_valid_xml_string
434 438 # '/project/issues.json' => should_be_a_valid_json_string
435 439 #
436 440 # @param [String] url Request
437 441 def self.should_be_a_valid_response_string_based_on_url(url)
438 442 case
439 443 when url.match(/xml/i)
440 444 should_be_a_valid_xml_string
441 445 when url.match(/json/i)
442 446 should_be_a_valid_json_string
443 447 else
444 448 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
445 449 end
446 450 end
447 451
448 452 # Checks that the response is a valid JSON string
449 453 def self.should_be_a_valid_json_string
450 454 should "be a valid JSON string (or empty)" do
451 455 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
452 456 end
453 457 end
454 458
455 459 # Checks that the response is a valid XML string
456 460 def self.should_be_a_valid_xml_string
457 461 should "be a valid XML string" do
458 462 assert REXML::Document.new(response.body)
459 463 end
460 464 end
461 465
462 466 def self.should_respond_with(status)
463 467 should "respond with #{status}" do
464 468 assert_response status
465 469 end
466 470 end
467 471 end
468 472 end
469 473 end
470 474
471 475 # URL helpers do not work with config.threadsafe!
472 476 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
473 477 ActionView::TestCase::TestController.instance_eval do
474 478 helper Rails.application.routes.url_helpers
475 479 end
476 480 ActionView::TestCase::TestController.class_eval do
477 481 def _routes
478 482 Rails.application.routes
479 483 end
480 484 end
General Comments 0
You need to be logged in to leave comments. Login now