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