comparison test/test_helper.rb @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents 753f1380d6bc
children 5e80956cc792
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
57 post "/login", :username => login, :password => password 57 post "/login", :username => login, :password => password
58 assert_equal login, User.find(session[:user_id]).login 58 assert_equal login, User.find(session[:user_id]).login
59 end 59 end
60 60
61 def uploaded_test_file(name, mime) 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 end 63 end
64 64
65 # Mock out a file 65 # Mock out a file
66 def self.mock_file 66 def self.mock_file
67 file = 'a_file.png' 67 file = 'a_file.png'
85 85
86 def with_settings(options, &block) 86 def with_settings(options, &block)
87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h} 87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
88 options.each {|k, v| Setting[k] = v} 88 options.each {|k, v| Setting[k] = v}
89 yield 89 yield
90 ensure
90 saved_settings.each {|k, v| Setting[k] = v} 91 saved_settings.each {|k, v| Setting[k] = v}
91 end 92 end
92 93
93 def change_user_password(login, new_password) 94 def change_user_password(login, new_password)
94 user = User.first(:conditions => {:login => login}) 95 user = User.first(:conditions => {:login => login})
107 # Returns the path to the test +vendor+ repository 108 # Returns the path to the test +vendor+ repository
108 def self.repository_path(vendor) 109 def self.repository_path(vendor)
109 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository") 110 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
110 end 111 end
111 112
113 # Returns the url of the subversion test repository
114 def self.subversion_repository_url
115 path = repository_path('subversion')
116 path = '/' + path unless path.starts_with?('/')
117 "file://#{path}"
118 end
119
112 # Returns true if the +vendor+ test repository is configured 120 # Returns true if the +vendor+ test repository is configured
113 def self.repository_configured?(vendor) 121 def self.repository_configured?(vendor)
114 File.directory?(repository_path(vendor)) 122 File.directory?(repository_path(vendor))
115 end 123 end
116 124
117 def assert_error_tag(options={}) 125 def assert_error_tag(options={})
118 assert_tag({:tag => 'p', :attributes => { :id => 'errorExplanation' }}.merge(options)) 126 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
119 end 127 end
120 128
121 # Shoulda macros 129 # Shoulda macros
122 def self.should_render_404 130 def self.should_render_404
123 should_respond_with :not_found 131 should_respond_with :not_found
359 assert_equal User.anonymous, User.current 367 assert_equal User.anonymous, User.current
360 end 368 end
361 end 369 end
362 end 370 end
363 371
372 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
373 setup do
374 @user = User.generate_with_protected!(:admin => true)
375 @token = Token.generate!(:user => @user, :action => 'api')
376 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
377 end
378
379 should_respond_with success_code
380 should_respond_with_content_type_based_on_url(url)
381 should_be_a_valid_response_string_based_on_url(url)
382 should "login as the user" do
383 assert_equal @user, User.current
384 end
385 end
364 end 386 end
365 387
366 # Uses should_respond_with_content_type based on what's in the url: 388 # Uses should_respond_with_content_type based on what's in the url:
367 # 389 #
368 # '/project/issues.xml' => should_respond_with_content_type :xml 390 # '/project/issues.xml' => should_respond_with_content_type :xml
400 end 422 end
401 423
402 # Checks that the response is a valid JSON string 424 # Checks that the response is a valid JSON string
403 def self.should_be_a_valid_json_string 425 def self.should_be_a_valid_json_string
404 should "be a valid JSON string (or empty)" do 426 should "be a valid JSON string (or empty)" do
405 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body)) 427 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
406 end 428 end
407 end 429 end
408 430
409 # Checks that the response is a valid XML string 431 # Checks that the response is a valid XML string
410 def self.should_be_a_valid_xml_string 432 def self.should_be_a_valid_xml_string