comparison test/.svn/text-base/test_helper.rb.svn-base @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents 1d32c0a0efbf
children af80e5618e9b 8661b858af72
comparison
equal deleted inserted replaced
22:40f7cfd4df19 37:94944d00e43c
111 111
112 # Returns true if the +vendor+ test repository is configured 112 # Returns true if the +vendor+ test repository is configured
113 def self.repository_configured?(vendor) 113 def self.repository_configured?(vendor)
114 File.directory?(repository_path(vendor)) 114 File.directory?(repository_path(vendor))
115 end 115 end
116
117 def assert_error_tag(options={})
118 assert_tag({:tag => 'p', :attributes => { :id => 'errorExplanation' }}.merge(options))
119 end
116 120
117 # Shoulda macros 121 # Shoulda macros
118 def self.should_render_404 122 def self.should_render_404
119 should_respond_with :not_found 123 should_respond_with :not_found
120 should_render_template 'common/404' 124 should_render_template 'common/error'
121 end 125 end
122 126
123 def self.should_have_before_filter(expected_method, options = {}) 127 def self.should_have_before_filter(expected_method, options = {})
124 should_have_filter('before', expected_method, options) 128 should_have_filter('before', expected_method, options)
125 end 129 end
179 assert user 183 assert user
180 assert_kind_of User, user 184 assert_kind_of User, user
181 assert !user.new_record? 185 assert !user.new_record?
182 end 186 end
183 end 187 end
188
189 # Test that a request allows the three types of API authentication
190 #
191 # * HTTP Basic with username and password
192 # * HTTP Basic with an api key for the username
193 # * Key based with the key=X parameter
194 #
195 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
196 # @param [String] url the request url
197 # @param [optional, Hash] parameters additional request parameters
198 # @param [optional, Hash] options additional options
199 # @option options [Symbol] :success_code Successful response code (:success)
200 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
201 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
202 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
203 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
204 should_allow_key_based_auth(http_method, url, parameters, options)
205 end
206
207 # Test that a request allows the username and password for HTTP BASIC
208 #
209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
210 # @param [String] url the request url
211 # @param [optional, Hash] parameters additional request parameters
212 # @param [optional, Hash] options additional options
213 # @option options [Symbol] :success_code Successful response code (:success)
214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
215 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
216 success_code = options[:success_code] || :success
217 failure_code = options[:failure_code] || :unauthorized
218
219 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
220 context "with a valid HTTP authentication" do
221 setup do
222 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
223 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
224 send(http_method, url, parameters, {:authorization => @authorization})
225 end
226
227 should_respond_with success_code
228 should_respond_with_content_type_based_on_url(url)
229 should "login as the user" do
230 assert_equal @user, User.current
231 end
232 end
233
234 context "with an invalid HTTP authentication" do
235 setup do
236 @user = User.generate_with_protected!
237 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
238 send(http_method, url, parameters, {:authorization => @authorization})
239 end
240
241 should_respond_with failure_code
242 should_respond_with_content_type_based_on_url(url)
243 should "not login as the user" do
244 assert_equal User.anonymous, User.current
245 end
246 end
247
248 context "without credentials" do
249 setup do
250 send(http_method, url, parameters, {:authorization => ''})
251 end
252
253 should_respond_with failure_code
254 should_respond_with_content_type_based_on_url(url)
255 should "include_www_authenticate_header" do
256 assert @controller.response.headers.has_key?('WWW-Authenticate')
257 end
258 end
259 end
260
261 end
262
263 # Test that a request allows the API key with HTTP BASIC
264 #
265 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
266 # @param [String] url the request url
267 # @param [optional, Hash] parameters additional request parameters
268 # @param [optional, Hash] options additional options
269 # @option options [Symbol] :success_code Successful response code (:success)
270 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
271 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
272 success_code = options[:success_code] || :success
273 failure_code = options[:failure_code] || :unauthorized
274
275 context "should allow http basic auth with a key for #{http_method} #{url}" do
276 context "with a valid HTTP authentication using the API token" do
277 setup do
278 @user = User.generate_with_protected!(:admin => true)
279 @token = Token.generate!(:user => @user, :action => 'api')
280 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
281 send(http_method, url, parameters, {:authorization => @authorization})
282 end
283
284 should_respond_with success_code
285 should_respond_with_content_type_based_on_url(url)
286 should_be_a_valid_response_string_based_on_url(url)
287 should "login as the user" do
288 assert_equal @user, User.current
289 end
290 end
291
292 context "with an invalid HTTP authentication" do
293 setup do
294 @user = User.generate_with_protected!
295 @token = Token.generate!(:user => @user, :action => 'feeds')
296 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
297 send(http_method, url, parameters, {:authorization => @authorization})
298 end
299
300 should_respond_with failure_code
301 should_respond_with_content_type_based_on_url(url)
302 should "not login as the user" do
303 assert_equal User.anonymous, User.current
304 end
305 end
306 end
307 end
308
309 # Test that a request allows full key authentication
310 #
311 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
312 # @param [String] url the request url, without the key=ZXY parameter
313 # @param [optional, Hash] parameters additional request parameters
314 # @param [optional, Hash] options additional options
315 # @option options [Symbol] :success_code Successful response code (:success)
316 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
317 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
318 success_code = options[:success_code] || :success
319 failure_code = options[:failure_code] || :unauthorized
320
321 context "should allow key based auth using key=X for #{http_method} #{url}" do
322 context "with a valid api token" do
323 setup do
324 @user = User.generate_with_protected!(:admin => true)
325 @token = Token.generate!(:user => @user, :action => 'api')
326 # Simple url parse to add on ?key= or &key=
327 request_url = if url.match(/\?/)
328 url + "&key=#{@token.value}"
329 else
330 url + "?key=#{@token.value}"
331 end
332 send(http_method, request_url, parameters)
333 end
334
335 should_respond_with success_code
336 should_respond_with_content_type_based_on_url(url)
337 should_be_a_valid_response_string_based_on_url(url)
338 should "login as the user" do
339 assert_equal @user, User.current
340 end
341 end
342
343 context "with an invalid api token" do
344 setup do
345 @user = User.generate_with_protected!
346 @token = Token.generate!(:user => @user, :action => 'feeds')
347 # Simple url parse to add on ?key= or &key=
348 request_url = if url.match(/\?/)
349 url + "&key=#{@token.value}"
350 else
351 url + "?key=#{@token.value}"
352 end
353 send(http_method, request_url, parameters)
354 end
355
356 should_respond_with failure_code
357 should_respond_with_content_type_based_on_url(url)
358 should "not login as the user" do
359 assert_equal User.anonymous, User.current
360 end
361 end
362 end
363
364 end
365
366 # Uses should_respond_with_content_type based on what's in the url:
367 #
368 # '/project/issues.xml' => should_respond_with_content_type :xml
369 # '/project/issues.json' => should_respond_with_content_type :json
370 #
371 # @param [String] url Request
372 def self.should_respond_with_content_type_based_on_url(url)
373 case
374 when url.match(/xml/i)
375 should_respond_with_content_type :xml
376 when url.match(/json/i)
377 should_respond_with_content_type :json
378 else
379 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
380 end
381
382 end
383
384 # Uses the url to assert which format the response should be in
385 #
386 # '/project/issues.xml' => should_be_a_valid_xml_string
387 # '/project/issues.json' => should_be_a_valid_json_string
388 #
389 # @param [String] url Request
390 def self.should_be_a_valid_response_string_based_on_url(url)
391 case
392 when url.match(/xml/i)
393 should_be_a_valid_xml_string
394 when url.match(/json/i)
395 should_be_a_valid_json_string
396 else
397 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
398 end
399
400 end
401
402 # Checks that the response is a valid JSON string
403 def self.should_be_a_valid_json_string
404 should "be a valid JSON string (or empty)" do
405 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body))
406 end
407 end
408
409 # Checks that the response is a valid XML string
410 def self.should_be_a_valid_xml_string
411 should "be a valid XML string" do
412 assert REXML::Document.new(response.body)
413 end
414 end
415
184 end 416 end
417
418 # Simple module to "namespace" all of the API tests
419 module ApiTest
420 end