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