##// END OF EJS Templates
Read attachment fixtures in binary mode....
Jean-Philippe Lang -
r4483:1c7079db3fb0
parent child
Show More
@@ -1,434 +1,434
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 ENV["RAILS_ENV"] = "test"
19 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 20 require 'test_help'
21 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23 23
24 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 25 include ObjectDaddyHelpers
26 26
27 27 class ActiveSupport::TestCase
28 28 # Transactional fixtures accelerate your tests by wrapping each test method
29 29 # in a transaction that's rolled back on completion. This ensures that the
30 30 # test database remains unchanged so your fixtures don't have to be reloaded
31 31 # between every test method. Fewer database queries means faster tests.
32 32 #
33 33 # Read Mike Clark's excellent walkthrough at
34 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 35 #
36 36 # Every Active Record database supports transactions except MyISAM tables
37 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 39 # is recommended.
40 40 self.use_transactional_fixtures = true
41 41
42 42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 43 # would need people(:david). If you don't want to migrate your existing
44 44 # test cases which use the @david style and don't mind the speed hit (each
45 45 # instantiated fixtures translates to a database query per test method),
46 46 # then set this back to true.
47 47 self.use_instantiated_fixtures = false
48 48
49 49 # Add more helper methods to be used by all tests here...
50 50
51 51 def log_user(login, password)
52 52 User.anonymous
53 53 get "/login"
54 54 assert_equal nil, session[:user_id]
55 55 assert_response :success
56 56 assert_template "account/login"
57 57 post "/login", :username => login, :password => password
58 58 assert_equal login, User.find(session[:user_id]).login
59 59 end
60 60
61 61 def uploaded_test_file(name, mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true)
63 63 end
64 64
65 65 # Mock out a file
66 66 def self.mock_file
67 67 file = 'a_file.png'
68 68 file.stubs(:size).returns(32)
69 69 file.stubs(:original_filename).returns('a_file.png')
70 70 file.stubs(:content_type).returns('image/png')
71 71 file.stubs(:read).returns(false)
72 72 file
73 73 end
74 74
75 75 def mock_file
76 76 self.class.mock_file
77 77 end
78 78
79 79 # Use a temporary directory for attachment related tests
80 80 def set_tmp_attachments_directory
81 81 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
82 82 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
83 83 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
84 84 end
85 85
86 86 def with_settings(options, &block)
87 87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
88 88 options.each {|k, v| Setting[k] = v}
89 89 yield
90 90 saved_settings.each {|k, v| Setting[k] = v}
91 91 end
92 92
93 93 def change_user_password(login, new_password)
94 94 user = User.first(:conditions => {:login => login})
95 95 user.password, user.password_confirmation = new_password, new_password
96 96 user.save!
97 97 end
98 98
99 99 def self.ldap_configured?
100 100 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
101 101 return @test_ldap.bind
102 102 rescue Exception => e
103 103 # LDAP is not listening
104 104 return nil
105 105 end
106 106
107 107 # Returns the path to the test +vendor+ repository
108 108 def self.repository_path(vendor)
109 109 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
110 110 end
111 111
112 112 # Returns true if the +vendor+ test repository is configured
113 113 def self.repository_configured?(vendor)
114 114 File.directory?(repository_path(vendor))
115 115 end
116 116
117 117 def assert_error_tag(options={})
118 118 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
119 119 end
120 120
121 121 # Shoulda macros
122 122 def self.should_render_404
123 123 should_respond_with :not_found
124 124 should_render_template 'common/error'
125 125 end
126 126
127 127 def self.should_have_before_filter(expected_method, options = {})
128 128 should_have_filter('before', expected_method, options)
129 129 end
130 130
131 131 def self.should_have_after_filter(expected_method, options = {})
132 132 should_have_filter('after', expected_method, options)
133 133 end
134 134
135 135 def self.should_have_filter(filter_type, expected_method, options)
136 136 description = "have #{filter_type}_filter :#{expected_method}"
137 137 description << " with #{options.inspect}" unless options.empty?
138 138
139 139 should description do
140 140 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
141 141 expected = klass.new(:filter, expected_method.to_sym, options)
142 142 assert_equal 1, @controller.class.filter_chain.select { |filter|
143 143 filter.method == expected.method && filter.kind == expected.kind &&
144 144 filter.options == expected.options && filter.class == expected.class
145 145 }.size
146 146 end
147 147 end
148 148
149 149 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
150 150 context "" do
151 151 setup do
152 152 if block_given?
153 153 instance_eval &block
154 154 else
155 155 @old_value = model.generate!
156 156 @new_value = model.generate!
157 157 end
158 158 end
159 159
160 160 should "use the new value's name" do
161 161 @detail = JournalDetail.generate!(:property => 'attr',
162 162 :old_value => @old_value.id,
163 163 :value => @new_value.id,
164 164 :prop_key => prop_key)
165 165
166 166 assert_match @new_value.name, show_detail(@detail, true)
167 167 end
168 168
169 169 should "use the old value's name" do
170 170 @detail = JournalDetail.generate!(:property => 'attr',
171 171 :old_value => @old_value.id,
172 172 :value => @new_value.id,
173 173 :prop_key => prop_key)
174 174
175 175 assert_match @old_value.name, show_detail(@detail, true)
176 176 end
177 177 end
178 178 end
179 179
180 180 def self.should_create_a_new_user(&block)
181 181 should "create a new user" do
182 182 user = instance_eval &block
183 183 assert user
184 184 assert_kind_of User, user
185 185 assert !user.new_record?
186 186 end
187 187 end
188 188
189 189 # Test that a request allows the three types of API authentication
190 190 #
191 191 # * HTTP Basic with username and password
192 192 # * HTTP Basic with an api key for the username
193 193 # * Key based with the key=X parameter
194 194 #
195 195 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
196 196 # @param [String] url the request url
197 197 # @param [optional, Hash] parameters additional request parameters
198 198 # @param [optional, Hash] options additional options
199 199 # @option options [Symbol] :success_code Successful response code (:success)
200 200 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
201 201 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
202 202 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
203 203 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
204 204 should_allow_key_based_auth(http_method, url, parameters, options)
205 205 end
206 206
207 207 # Test that a request allows the username and password for HTTP BASIC
208 208 #
209 209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
210 210 # @param [String] url the request url
211 211 # @param [optional, Hash] parameters additional request parameters
212 212 # @param [optional, Hash] options additional options
213 213 # @option options [Symbol] :success_code Successful response code (:success)
214 214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
215 215 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
216 216 success_code = options[:success_code] || :success
217 217 failure_code = options[:failure_code] || :unauthorized
218 218
219 219 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
220 220 context "with a valid HTTP authentication" do
221 221 setup do
222 222 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
223 223 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
224 224 send(http_method, url, parameters, {:authorization => @authorization})
225 225 end
226 226
227 227 should_respond_with success_code
228 228 should_respond_with_content_type_based_on_url(url)
229 229 should "login as the user" do
230 230 assert_equal @user, User.current
231 231 end
232 232 end
233 233
234 234 context "with an invalid HTTP authentication" do
235 235 setup do
236 236 @user = User.generate_with_protected!
237 237 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
238 238 send(http_method, url, parameters, {:authorization => @authorization})
239 239 end
240 240
241 241 should_respond_with failure_code
242 242 should_respond_with_content_type_based_on_url(url)
243 243 should "not login as the user" do
244 244 assert_equal User.anonymous, User.current
245 245 end
246 246 end
247 247
248 248 context "without credentials" do
249 249 setup do
250 250 send(http_method, url, parameters, {:authorization => ''})
251 251 end
252 252
253 253 should_respond_with failure_code
254 254 should_respond_with_content_type_based_on_url(url)
255 255 should "include_www_authenticate_header" do
256 256 assert @controller.response.headers.has_key?('WWW-Authenticate')
257 257 end
258 258 end
259 259 end
260 260
261 261 end
262 262
263 263 # Test that a request allows the API key with HTTP BASIC
264 264 #
265 265 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
266 266 # @param [String] url the request url
267 267 # @param [optional, Hash] parameters additional request parameters
268 268 # @param [optional, Hash] options additional options
269 269 # @option options [Symbol] :success_code Successful response code (:success)
270 270 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
271 271 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
272 272 success_code = options[:success_code] || :success
273 273 failure_code = options[:failure_code] || :unauthorized
274 274
275 275 context "should allow http basic auth with a key for #{http_method} #{url}" do
276 276 context "with a valid HTTP authentication using the API token" do
277 277 setup do
278 278 @user = User.generate_with_protected!(:admin => true)
279 279 @token = Token.generate!(:user => @user, :action => 'api')
280 280 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
281 281 send(http_method, url, parameters, {:authorization => @authorization})
282 282 end
283 283
284 284 should_respond_with success_code
285 285 should_respond_with_content_type_based_on_url(url)
286 286 should_be_a_valid_response_string_based_on_url(url)
287 287 should "login as the user" do
288 288 assert_equal @user, User.current
289 289 end
290 290 end
291 291
292 292 context "with an invalid HTTP authentication" do
293 293 setup do
294 294 @user = User.generate_with_protected!
295 295 @token = Token.generate!(:user => @user, :action => 'feeds')
296 296 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
297 297 send(http_method, url, parameters, {:authorization => @authorization})
298 298 end
299 299
300 300 should_respond_with failure_code
301 301 should_respond_with_content_type_based_on_url(url)
302 302 should "not login as the user" do
303 303 assert_equal User.anonymous, User.current
304 304 end
305 305 end
306 306 end
307 307 end
308 308
309 309 # Test that a request allows full key authentication
310 310 #
311 311 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
312 312 # @param [String] url the request url, without the key=ZXY parameter
313 313 # @param [optional, Hash] parameters additional request parameters
314 314 # @param [optional, Hash] options additional options
315 315 # @option options [Symbol] :success_code Successful response code (:success)
316 316 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
317 317 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
318 318 success_code = options[:success_code] || :success
319 319 failure_code = options[:failure_code] || :unauthorized
320 320
321 321 context "should allow key based auth using key=X for #{http_method} #{url}" do
322 322 context "with a valid api token" do
323 323 setup do
324 324 @user = User.generate_with_protected!(:admin => true)
325 325 @token = Token.generate!(:user => @user, :action => 'api')
326 326 # Simple url parse to add on ?key= or &key=
327 327 request_url = if url.match(/\?/)
328 328 url + "&key=#{@token.value}"
329 329 else
330 330 url + "?key=#{@token.value}"
331 331 end
332 332 send(http_method, request_url, parameters)
333 333 end
334 334
335 335 should_respond_with success_code
336 336 should_respond_with_content_type_based_on_url(url)
337 337 should_be_a_valid_response_string_based_on_url(url)
338 338 should "login as the user" do
339 339 assert_equal @user, User.current
340 340 end
341 341 end
342 342
343 343 context "with an invalid api token" do
344 344 setup do
345 345 @user = User.generate_with_protected!
346 346 @token = Token.generate!(:user => @user, :action => 'feeds')
347 347 # Simple url parse to add on ?key= or &key=
348 348 request_url = if url.match(/\?/)
349 349 url + "&key=#{@token.value}"
350 350 else
351 351 url + "?key=#{@token.value}"
352 352 end
353 353 send(http_method, request_url, parameters)
354 354 end
355 355
356 356 should_respond_with failure_code
357 357 should_respond_with_content_type_based_on_url(url)
358 358 should "not login as the user" do
359 359 assert_equal User.anonymous, User.current
360 360 end
361 361 end
362 362 end
363 363
364 364 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
365 365 setup do
366 366 @user = User.generate_with_protected!(:admin => true)
367 367 @token = Token.generate!(:user => @user, :action => 'api')
368 368 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
369 369 end
370 370
371 371 should_respond_with success_code
372 372 should_respond_with_content_type_based_on_url(url)
373 373 should_be_a_valid_response_string_based_on_url(url)
374 374 should "login as the user" do
375 375 assert_equal @user, User.current
376 376 end
377 377 end
378 378 end
379 379
380 380 # Uses should_respond_with_content_type based on what's in the url:
381 381 #
382 382 # '/project/issues.xml' => should_respond_with_content_type :xml
383 383 # '/project/issues.json' => should_respond_with_content_type :json
384 384 #
385 385 # @param [String] url Request
386 386 def self.should_respond_with_content_type_based_on_url(url)
387 387 case
388 388 when url.match(/xml/i)
389 389 should_respond_with_content_type :xml
390 390 when url.match(/json/i)
391 391 should_respond_with_content_type :json
392 392 else
393 393 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
394 394 end
395 395
396 396 end
397 397
398 398 # Uses the url to assert which format the response should be in
399 399 #
400 400 # '/project/issues.xml' => should_be_a_valid_xml_string
401 401 # '/project/issues.json' => should_be_a_valid_json_string
402 402 #
403 403 # @param [String] url Request
404 404 def self.should_be_a_valid_response_string_based_on_url(url)
405 405 case
406 406 when url.match(/xml/i)
407 407 should_be_a_valid_xml_string
408 408 when url.match(/json/i)
409 409 should_be_a_valid_json_string
410 410 else
411 411 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
412 412 end
413 413
414 414 end
415 415
416 416 # Checks that the response is a valid JSON string
417 417 def self.should_be_a_valid_json_string
418 418 should "be a valid JSON string (or empty)" do
419 419 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body))
420 420 end
421 421 end
422 422
423 423 # Checks that the response is a valid XML string
424 424 def self.should_be_a_valid_xml_string
425 425 should "be a valid XML string" do
426 426 assert REXML::Document.new(response.body)
427 427 end
428 428 end
429 429
430 430 end
431 431
432 432 # Simple module to "namespace" all of the API tests
433 433 module ApiTest
434 434 end
General Comments 0
You need to be logged in to leave comments. Login now