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