@@ -1,223 +1,215 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'account_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class AccountController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class AccountControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :users, :roles |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = AccountController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_login_should_redirect_to_back_url_param |
|
35 | 35 | # request.uri is "test.host" in test environment |
|
36 | 36 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.host%2Fissues%2Fshow%2F1' |
|
37 | 37 | assert_redirected_to '/issues/show/1' |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def test_login_should_not_redirect_to_another_host |
|
41 | 41 | post :login, :username => 'jsmith', :password => 'jsmith', :back_url => 'http%3A%2F%2Ftest.foo%2Ffake' |
|
42 | 42 | assert_redirected_to '/my/page' |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_login_with_wrong_password |
|
46 | 46 | post :login, :username => 'admin', :password => 'bad' |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'login' |
|
49 | 49 | assert_tag 'div', |
|
50 | 50 | :attributes => { :class => "flash error" }, |
|
51 | 51 | :content => /Invalid user or password/ |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | if Object.const_defined?(:OpenID) |
|
55 | 55 | |
|
56 | 56 | def test_login_with_openid_for_existing_user |
|
57 | 57 | Setting.self_registration = '3' |
|
58 | 58 | Setting.openid = '1' |
|
59 | 59 | existing_user = User.new(:firstname => 'Cool', |
|
60 | 60 | :lastname => 'User', |
|
61 | 61 | :mail => 'user@somedomain.com', |
|
62 | 62 | :identity_url => 'http://openid.example.com/good_user') |
|
63 | 63 | existing_user.login = 'cool_user' |
|
64 | 64 | assert existing_user.save! |
|
65 | 65 | |
|
66 | 66 | post :login, :openid_url => existing_user.identity_url |
|
67 | 67 | assert_redirected_to '/my/page' |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def test_login_with_invalid_openid_provider |
|
71 | 71 | Setting.self_registration = '0' |
|
72 | 72 | Setting.openid = '1' |
|
73 | 73 | post :login, :openid_url => 'http;//openid.example.com/good_user' |
|
74 | 74 | assert_redirected_to home_url |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def test_login_with_openid_for_existing_non_active_user |
|
78 | 78 | Setting.self_registration = '2' |
|
79 | 79 | Setting.openid = '1' |
|
80 | 80 | existing_user = User.new(:firstname => 'Cool', |
|
81 | 81 | :lastname => 'User', |
|
82 | 82 | :mail => 'user@somedomain.com', |
|
83 | 83 | :identity_url => 'http://openid.example.com/good_user', |
|
84 | 84 | :status => User::STATUS_REGISTERED) |
|
85 | 85 | existing_user.login = 'cool_user' |
|
86 | 86 | assert existing_user.save! |
|
87 | 87 | |
|
88 | 88 | post :login, :openid_url => existing_user.identity_url |
|
89 | 89 | assert_redirected_to '/login' |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def test_login_with_openid_with_new_user_created |
|
93 | 93 | Setting.self_registration = '3' |
|
94 | 94 | Setting.openid = '1' |
|
95 | 95 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
96 | 96 | assert_redirected_to '/my/account' |
|
97 | 97 | user = User.find_by_login('cool_user') |
|
98 | 98 | assert user |
|
99 | 99 | assert_equal 'Cool', user.firstname |
|
100 | 100 | assert_equal 'User', user.lastname |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def test_login_with_openid_with_new_user_and_self_registration_off |
|
104 | 104 | Setting.self_registration = '0' |
|
105 | 105 | Setting.openid = '1' |
|
106 | 106 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
107 | 107 | assert_redirected_to home_url |
|
108 | 108 | user = User.find_by_login('cool_user') |
|
109 | 109 | assert ! user |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def test_login_with_openid_with_new_user_created_with_email_activation_should_have_a_token |
|
113 | 113 | Setting.self_registration = '1' |
|
114 | 114 | Setting.openid = '1' |
|
115 | 115 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
116 | 116 | assert_redirected_to '/login' |
|
117 | 117 | user = User.find_by_login('cool_user') |
|
118 | 118 | assert user |
|
119 | 119 | |
|
120 | 120 | token = Token.find_by_user_id_and_action(user.id, 'register') |
|
121 | 121 | assert token |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def test_login_with_openid_with_new_user_created_with_manual_activation |
|
125 | 125 | Setting.self_registration = '2' |
|
126 | 126 | Setting.openid = '1' |
|
127 | 127 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
128 | 128 | assert_redirected_to '/login' |
|
129 | 129 | user = User.find_by_login('cool_user') |
|
130 | 130 | assert user |
|
131 | 131 | assert_equal User::STATUS_REGISTERED, user.status |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | def test_login_with_openid_with_new_user_with_conflict_should_register |
|
135 | 135 | Setting.self_registration = '3' |
|
136 | 136 | Setting.openid = '1' |
|
137 | 137 | existing_user = User.new(:firstname => 'Cool', :lastname => 'User', :mail => 'user@somedomain.com') |
|
138 | 138 | existing_user.login = 'cool_user' |
|
139 | 139 | assert existing_user.save! |
|
140 | 140 | |
|
141 | 141 | post :login, :openid_url => 'http://openid.example.com/good_user' |
|
142 | 142 | assert_response :success |
|
143 | 143 | assert_template 'register' |
|
144 | 144 | assert assigns(:user) |
|
145 | 145 | assert_equal 'http://openid.example.com/good_user', assigns(:user)[:identity_url] |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def test_setting_openid_should_return_true_when_set_to_true |
|
149 | 149 | Setting.openid = '1' |
|
150 | 150 | assert_equal true, Setting.openid? |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | else |
|
154 | 154 | puts "Skipping openid tests." |
|
155 | 155 | end |
|
156 | 156 | |
|
157 | 157 | def test_logout |
|
158 | 158 | @request.session[:user_id] = 2 |
|
159 | 159 | get :logout |
|
160 | 160 | assert_redirected_to '/' |
|
161 | 161 | assert_nil @request.session[:user_id] |
|
162 | 162 | end |
|
163 | 163 | |
|
164 | context "GET #register" do | |
|
165 |
|
|
|
166 | setup do | |
|
167 | Setting.self_registration = '3' | |
|
168 | get :register | |
|
169 | end | |
|
170 | ||
|
171 | should_respond_with :success | |
|
172 | should_render_template :register | |
|
173 | should_assign_to :user | |
|
164 | def test_get_register_with_registration_on | |
|
165 | with_settings :self_registration => '3' do | |
|
166 | get :register | |
|
167 | assert_response :success | |
|
168 | assert_template 'register' | |
|
169 | assert_not_nil assigns(:user) | |
|
174 | 170 | end |
|
171 | end | |
|
175 | 172 | |
|
176 | context "with self registration off" do | |
|
177 | setup do | |
|
178 | Setting.self_registration = '0' | |
|
179 | get :register | |
|
180 | end | |
|
181 | ||
|
182 | should_redirect_to('/') { home_url } | |
|
173 | def test_get_register_with_registration_off_should_redirect | |
|
174 | with_settings :self_registration => '0' do | |
|
175 | get :register | |
|
176 | assert_redirected_to '/' | |
|
183 | 177 | end |
|
184 | 178 | end |
|
185 | 179 | |
|
186 | 180 | # See integration/account_test.rb for the full test |
|
187 | context "POST #register" do | |
|
188 |
|
|
|
189 | setup do | |
|
190 | Setting.self_registration = '3' | |
|
181 | def test_post_register_with_registration_on | |
|
182 | with_settings :self_registration => '3' do | |
|
183 | assert_difference 'User.count' do | |
|
191 | 184 | post :register, :user => { |
|
192 | 185 | :login => 'register', |
|
193 | 186 | :password => 'test', |
|
194 | 187 | :password_confirmation => 'test', |
|
195 | 188 | :firstname => 'John', |
|
196 | 189 | :lastname => 'Doe', |
|
197 | 190 | :mail => 'register@example.com' |
|
198 | 191 | } |
|
192 | assert_redirected_to '/my/account' | |
|
199 | 193 | end |
|
200 | ||
|
201 | should_respond_with :redirect | |
|
202 | should_assign_to :user | |
|
203 | should_redirect_to('my page') { {:controller => 'my', :action => 'account'} } | |
|
204 | ||
|
205 | should_create_a_new_user { User.last(:conditions => {:login => 'register'}) } | |
|
206 | ||
|
207 | should 'set the user status to active' do | |
|
208 | user = User.last(:conditions => {:login => 'register'}) | |
|
209 | assert user | |
|
210 | assert_equal User::STATUS_ACTIVE, user.status | |
|
211 | end | |
|
194 | user = User.first(:order => 'id DESC') | |
|
195 | assert_equal 'register', user.login | |
|
196 | assert user.active? | |
|
212 | 197 | end |
|
213 | ||
|
214 | context "with self registration off" do | |
|
215 | setup do | |
|
216 |
|
|
|
217 | post :register | |
|
198 | end | |
|
199 | ||
|
200 | def test_post_register_with_registration_off_should_redirect | |
|
201 | with_settings :self_registration => '0' do | |
|
202 | assert_no_difference 'User.count' do | |
|
203 | post :register, :user => { | |
|
204 | :login => 'register', | |
|
205 | :password => 'test', | |
|
206 | :password_confirmation => 'test', | |
|
207 | :firstname => 'John', | |
|
208 | :lastname => 'Doe', | |
|
209 | :mail => 'register@example.com' | |
|
210 | } | |
|
211 | assert_redirected_to '/' | |
|
218 | 212 | end |
|
219 | ||
|
220 | should_redirect_to('/') { home_url } | |
|
221 | 213 | end |
|
222 | 214 | end |
|
223 | 215 | end |
@@ -1,464 +1,455 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s |
|
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 | 62 | ActionController::TestUploadedFile.new( |
|
63 | 63 | ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true) |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | # Mock out a file |
|
67 | 67 | def self.mock_file |
|
68 | 68 | file = 'a_file.png' |
|
69 | 69 | file.stubs(:size).returns(32) |
|
70 | 70 | file.stubs(:original_filename).returns('a_file.png') |
|
71 | 71 | file.stubs(:content_type).returns('image/png') |
|
72 | 72 | file.stubs(:read).returns(false) |
|
73 | 73 | file |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def mock_file |
|
77 | 77 | self.class.mock_file |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def mock_file_with_options(options={}) |
|
81 | 81 | file = '' |
|
82 | 82 | file.stubs(:size).returns(32) |
|
83 | 83 | original_filename = options[:original_filename] || nil |
|
84 | 84 | file.stubs(:original_filename).returns(original_filename) |
|
85 | 85 | content_type = options[:content_type] || nil |
|
86 | 86 | file.stubs(:content_type).returns(content_type) |
|
87 | 87 | file.stubs(:read).returns(false) |
|
88 | 88 | file |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | # Use a temporary directory for attachment related tests |
|
92 | 92 | def set_tmp_attachments_directory |
|
93 | 93 | Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") |
|
94 | 94 | unless File.directory?("#{Rails.root}/tmp/test/attachments") |
|
95 | 95 | Dir.mkdir "#{Rails.root}/tmp/test/attachments" |
|
96 | 96 | end |
|
97 | 97 | Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | def set_fixtures_attachments_directory |
|
101 | 101 | Attachment.storage_path = "#{Rails.root}/test/fixtures/files" |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | def with_settings(options, &block) |
|
105 | 105 | saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h} |
|
106 | 106 | options.each {|k, v| Setting[k] = v} |
|
107 | 107 | yield |
|
108 | 108 | ensure |
|
109 | 109 | saved_settings.each {|k, v| Setting[k] = v} if saved_settings |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def change_user_password(login, new_password) |
|
113 | 113 | user = User.first(:conditions => {:login => login}) |
|
114 | 114 | user.password, user.password_confirmation = new_password, new_password |
|
115 | 115 | user.save! |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | def self.ldap_configured? |
|
119 | 119 | @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) |
|
120 | 120 | return @test_ldap.bind |
|
121 | 121 | rescue Exception => e |
|
122 | 122 | # LDAP is not listening |
|
123 | 123 | return nil |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | # Returns the path to the test +vendor+ repository |
|
127 | 127 | def self.repository_path(vendor) |
|
128 | 128 | Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s |
|
129 | 129 | end |
|
130 | 130 | |
|
131 | 131 | # Returns the url of the subversion test repository |
|
132 | 132 | def self.subversion_repository_url |
|
133 | 133 | path = repository_path('subversion') |
|
134 | 134 | path = '/' + path unless path.starts_with?('/') |
|
135 | 135 | "file://#{path}" |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | # Returns true if the +vendor+ test repository is configured |
|
139 | 139 | def self.repository_configured?(vendor) |
|
140 | 140 | File.directory?(repository_path(vendor)) |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | def assert_error_tag(options={}) |
|
144 | 144 | assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options)) |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | def assert_include(expected, s) |
|
148 | 148 | assert s.include?(expected), "\"#{expected}\" not found in \"#{s}\"" |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | # Shoulda macros |
|
152 | 152 | def self.should_render_404 |
|
153 | 153 | should_respond_with :not_found |
|
154 | 154 | should_render_template 'common/error' |
|
155 | 155 | end |
|
156 | 156 | |
|
157 | 157 | def self.should_have_before_filter(expected_method, options = {}) |
|
158 | 158 | should_have_filter('before', expected_method, options) |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | def self.should_have_after_filter(expected_method, options = {}) |
|
162 | 162 | should_have_filter('after', expected_method, options) |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | def self.should_have_filter(filter_type, expected_method, options) |
|
166 | 166 | description = "have #{filter_type}_filter :#{expected_method}" |
|
167 | 167 | description << " with #{options.inspect}" unless options.empty? |
|
168 | 168 | |
|
169 | 169 | should description do |
|
170 | 170 | klass = "action_controller/filters/#{filter_type}_filter".classify.constantize |
|
171 | 171 | expected = klass.new(:filter, expected_method.to_sym, options) |
|
172 | 172 | assert_equal 1, @controller.class.filter_chain.select { |filter| |
|
173 | 173 | filter.method == expected.method && filter.kind == expected.kind && |
|
174 | 174 | filter.options == expected.options && filter.class == expected.class |
|
175 | 175 | }.size |
|
176 | 176 | end |
|
177 | 177 | end |
|
178 | 178 | |
|
179 | 179 | def self.should_show_the_old_and_new_values_for(prop_key, model, &block) |
|
180 | 180 | context "" do |
|
181 | 181 | setup do |
|
182 | 182 | if block_given? |
|
183 | 183 | instance_eval &block |
|
184 | 184 | else |
|
185 | 185 | @old_value = model.generate! |
|
186 | 186 | @new_value = model.generate! |
|
187 | 187 | end |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | should "use the new value's name" do |
|
191 | 191 | @detail = JournalDetail.generate!(:property => 'attr', |
|
192 | 192 | :old_value => @old_value.id, |
|
193 | 193 | :value => @new_value.id, |
|
194 | 194 | :prop_key => prop_key) |
|
195 | 195 | |
|
196 | 196 | assert_match @new_value.name, show_detail(@detail, true) |
|
197 | 197 | end |
|
198 | 198 | |
|
199 | 199 | should "use the old value's name" do |
|
200 | 200 | @detail = JournalDetail.generate!(:property => 'attr', |
|
201 | 201 | :old_value => @old_value.id, |
|
202 | 202 | :value => @new_value.id, |
|
203 | 203 | :prop_key => prop_key) |
|
204 | 204 | |
|
205 | 205 | assert_match @old_value.name, show_detail(@detail, true) |
|
206 | 206 | end |
|
207 | 207 | end |
|
208 | 208 | end |
|
209 | 209 | |
|
210 | def self.should_create_a_new_user(&block) | |
|
211 | should "create a new user" do | |
|
212 | user = instance_eval &block | |
|
213 | assert user | |
|
214 | assert_kind_of User, user | |
|
215 | assert !user.new_record? | |
|
216 | end | |
|
217 | end | |
|
218 | ||
|
219 | 210 | # Test that a request allows the three types of API authentication |
|
220 | 211 | # |
|
221 | 212 | # * HTTP Basic with username and password |
|
222 | 213 | # * HTTP Basic with an api key for the username |
|
223 | 214 | # * Key based with the key=X parameter |
|
224 | 215 | # |
|
225 | 216 | # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) |
|
226 | 217 | # @param [String] url the request url |
|
227 | 218 | # @param [optional, Hash] parameters additional request parameters |
|
228 | 219 | # @param [optional, Hash] options additional options |
|
229 | 220 | # @option options [Symbol] :success_code Successful response code (:success) |
|
230 | 221 | # @option options [Symbol] :failure_code Failure response code (:unauthorized) |
|
231 | 222 | def self.should_allow_api_authentication(http_method, url, parameters={}, options={}) |
|
232 | 223 | should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options) |
|
233 | 224 | should_allow_http_basic_auth_with_key(http_method, url, parameters, options) |
|
234 | 225 | should_allow_key_based_auth(http_method, url, parameters, options) |
|
235 | 226 | end |
|
236 | 227 | |
|
237 | 228 | # Test that a request allows the username and password for HTTP BASIC |
|
238 | 229 | # |
|
239 | 230 | # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) |
|
240 | 231 | # @param [String] url the request url |
|
241 | 232 | # @param [optional, Hash] parameters additional request parameters |
|
242 | 233 | # @param [optional, Hash] options additional options |
|
243 | 234 | # @option options [Symbol] :success_code Successful response code (:success) |
|
244 | 235 | # @option options [Symbol] :failure_code Failure response code (:unauthorized) |
|
245 | 236 | def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={}) |
|
246 | 237 | success_code = options[:success_code] || :success |
|
247 | 238 | failure_code = options[:failure_code] || :unauthorized |
|
248 | 239 | |
|
249 | 240 | context "should allow http basic auth using a username and password for #{http_method} #{url}" do |
|
250 | 241 | context "with a valid HTTP authentication" do |
|
251 | 242 | setup do |
|
252 | 243 | @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project |
|
253 | 244 | @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password') |
|
254 | 245 | send(http_method, url, parameters, {:authorization => @authorization}) |
|
255 | 246 | end |
|
256 | 247 | |
|
257 | 248 | should_respond_with success_code |
|
258 | 249 | should_respond_with_content_type_based_on_url(url) |
|
259 | 250 | should "login as the user" do |
|
260 | 251 | assert_equal @user, User.current |
|
261 | 252 | end |
|
262 | 253 | end |
|
263 | 254 | |
|
264 | 255 | context "with an invalid HTTP authentication" do |
|
265 | 256 | setup do |
|
266 | 257 | @user = User.generate_with_protected! |
|
267 | 258 | @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password') |
|
268 | 259 | send(http_method, url, parameters, {:authorization => @authorization}) |
|
269 | 260 | end |
|
270 | 261 | |
|
271 | 262 | should_respond_with failure_code |
|
272 | 263 | should_respond_with_content_type_based_on_url(url) |
|
273 | 264 | should "not login as the user" do |
|
274 | 265 | assert_equal User.anonymous, User.current |
|
275 | 266 | end |
|
276 | 267 | end |
|
277 | 268 | |
|
278 | 269 | context "without credentials" do |
|
279 | 270 | setup do |
|
280 | 271 | send(http_method, url, parameters, {:authorization => ''}) |
|
281 | 272 | end |
|
282 | 273 | |
|
283 | 274 | should_respond_with failure_code |
|
284 | 275 | should_respond_with_content_type_based_on_url(url) |
|
285 | 276 | should "include_www_authenticate_header" do |
|
286 | 277 | assert @controller.response.headers.has_key?('WWW-Authenticate') |
|
287 | 278 | end |
|
288 | 279 | end |
|
289 | 280 | end |
|
290 | 281 | |
|
291 | 282 | end |
|
292 | 283 | |
|
293 | 284 | # Test that a request allows the API key with HTTP BASIC |
|
294 | 285 | # |
|
295 | 286 | # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) |
|
296 | 287 | # @param [String] url the request url |
|
297 | 288 | # @param [optional, Hash] parameters additional request parameters |
|
298 | 289 | # @param [optional, Hash] options additional options |
|
299 | 290 | # @option options [Symbol] :success_code Successful response code (:success) |
|
300 | 291 | # @option options [Symbol] :failure_code Failure response code (:unauthorized) |
|
301 | 292 | def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={}) |
|
302 | 293 | success_code = options[:success_code] || :success |
|
303 | 294 | failure_code = options[:failure_code] || :unauthorized |
|
304 | 295 | |
|
305 | 296 | context "should allow http basic auth with a key for #{http_method} #{url}" do |
|
306 | 297 | context "with a valid HTTP authentication using the API token" do |
|
307 | 298 | setup do |
|
308 | 299 | @user = User.generate_with_protected!(:admin => true) |
|
309 | 300 | @token = Token.generate!(:user => @user, :action => 'api') |
|
310 | 301 | @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X') |
|
311 | 302 | send(http_method, url, parameters, {:authorization => @authorization}) |
|
312 | 303 | end |
|
313 | 304 | |
|
314 | 305 | should_respond_with success_code |
|
315 | 306 | should_respond_with_content_type_based_on_url(url) |
|
316 | 307 | should_be_a_valid_response_string_based_on_url(url) |
|
317 | 308 | should "login as the user" do |
|
318 | 309 | assert_equal @user, User.current |
|
319 | 310 | end |
|
320 | 311 | end |
|
321 | 312 | |
|
322 | 313 | context "with an invalid HTTP authentication" do |
|
323 | 314 | setup do |
|
324 | 315 | @user = User.generate_with_protected! |
|
325 | 316 | @token = Token.generate!(:user => @user, :action => 'feeds') |
|
326 | 317 | @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X') |
|
327 | 318 | send(http_method, url, parameters, {:authorization => @authorization}) |
|
328 | 319 | end |
|
329 | 320 | |
|
330 | 321 | should_respond_with failure_code |
|
331 | 322 | should_respond_with_content_type_based_on_url(url) |
|
332 | 323 | should "not login as the user" do |
|
333 | 324 | assert_equal User.anonymous, User.current |
|
334 | 325 | end |
|
335 | 326 | end |
|
336 | 327 | end |
|
337 | 328 | end |
|
338 | 329 | |
|
339 | 330 | # Test that a request allows full key authentication |
|
340 | 331 | # |
|
341 | 332 | # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) |
|
342 | 333 | # @param [String] url the request url, without the key=ZXY parameter |
|
343 | 334 | # @param [optional, Hash] parameters additional request parameters |
|
344 | 335 | # @param [optional, Hash] options additional options |
|
345 | 336 | # @option options [Symbol] :success_code Successful response code (:success) |
|
346 | 337 | # @option options [Symbol] :failure_code Failure response code (:unauthorized) |
|
347 | 338 | def self.should_allow_key_based_auth(http_method, url, parameters={}, options={}) |
|
348 | 339 | success_code = options[:success_code] || :success |
|
349 | 340 | failure_code = options[:failure_code] || :unauthorized |
|
350 | 341 | |
|
351 | 342 | context "should allow key based auth using key=X for #{http_method} #{url}" do |
|
352 | 343 | context "with a valid api token" do |
|
353 | 344 | setup do |
|
354 | 345 | @user = User.generate_with_protected!(:admin => true) |
|
355 | 346 | @token = Token.generate!(:user => @user, :action => 'api') |
|
356 | 347 | # Simple url parse to add on ?key= or &key= |
|
357 | 348 | request_url = if url.match(/\?/) |
|
358 | 349 | url + "&key=#{@token.value}" |
|
359 | 350 | else |
|
360 | 351 | url + "?key=#{@token.value}" |
|
361 | 352 | end |
|
362 | 353 | send(http_method, request_url, parameters) |
|
363 | 354 | end |
|
364 | 355 | |
|
365 | 356 | should_respond_with success_code |
|
366 | 357 | should_respond_with_content_type_based_on_url(url) |
|
367 | 358 | should_be_a_valid_response_string_based_on_url(url) |
|
368 | 359 | should "login as the user" do |
|
369 | 360 | assert_equal @user, User.current |
|
370 | 361 | end |
|
371 | 362 | end |
|
372 | 363 | |
|
373 | 364 | context "with an invalid api token" do |
|
374 | 365 | setup do |
|
375 | 366 | @user = User.generate_with_protected! |
|
376 | 367 | @token = Token.generate!(:user => @user, :action => 'feeds') |
|
377 | 368 | # Simple url parse to add on ?key= or &key= |
|
378 | 369 | request_url = if url.match(/\?/) |
|
379 | 370 | url + "&key=#{@token.value}" |
|
380 | 371 | else |
|
381 | 372 | url + "?key=#{@token.value}" |
|
382 | 373 | end |
|
383 | 374 | send(http_method, request_url, parameters) |
|
384 | 375 | end |
|
385 | 376 | |
|
386 | 377 | should_respond_with failure_code |
|
387 | 378 | should_respond_with_content_type_based_on_url(url) |
|
388 | 379 | should "not login as the user" do |
|
389 | 380 | assert_equal User.anonymous, User.current |
|
390 | 381 | end |
|
391 | 382 | end |
|
392 | 383 | end |
|
393 | 384 | |
|
394 | 385 | context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do |
|
395 | 386 | setup do |
|
396 | 387 | @user = User.generate_with_protected!(:admin => true) |
|
397 | 388 | @token = Token.generate!(:user => @user, :action => 'api') |
|
398 | 389 | send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s}) |
|
399 | 390 | end |
|
400 | 391 | |
|
401 | 392 | should_respond_with success_code |
|
402 | 393 | should_respond_with_content_type_based_on_url(url) |
|
403 | 394 | should_be_a_valid_response_string_based_on_url(url) |
|
404 | 395 | should "login as the user" do |
|
405 | 396 | assert_equal @user, User.current |
|
406 | 397 | end |
|
407 | 398 | end |
|
408 | 399 | end |
|
409 | 400 | |
|
410 | 401 | # Uses should_respond_with_content_type based on what's in the url: |
|
411 | 402 | # |
|
412 | 403 | # '/project/issues.xml' => should_respond_with_content_type :xml |
|
413 | 404 | # '/project/issues.json' => should_respond_with_content_type :json |
|
414 | 405 | # |
|
415 | 406 | # @param [String] url Request |
|
416 | 407 | def self.should_respond_with_content_type_based_on_url(url) |
|
417 | 408 | case |
|
418 | 409 | when url.match(/xml/i) |
|
419 | 410 | should_respond_with_content_type :xml |
|
420 | 411 | when url.match(/json/i) |
|
421 | 412 | should_respond_with_content_type :json |
|
422 | 413 | else |
|
423 | 414 | raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}" |
|
424 | 415 | end |
|
425 | 416 | |
|
426 | 417 | end |
|
427 | 418 | |
|
428 | 419 | # Uses the url to assert which format the response should be in |
|
429 | 420 | # |
|
430 | 421 | # '/project/issues.xml' => should_be_a_valid_xml_string |
|
431 | 422 | # '/project/issues.json' => should_be_a_valid_json_string |
|
432 | 423 | # |
|
433 | 424 | # @param [String] url Request |
|
434 | 425 | def self.should_be_a_valid_response_string_based_on_url(url) |
|
435 | 426 | case |
|
436 | 427 | when url.match(/xml/i) |
|
437 | 428 | should_be_a_valid_xml_string |
|
438 | 429 | when url.match(/json/i) |
|
439 | 430 | should_be_a_valid_json_string |
|
440 | 431 | else |
|
441 | 432 | raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}" |
|
442 | 433 | end |
|
443 | 434 | |
|
444 | 435 | end |
|
445 | 436 | |
|
446 | 437 | # Checks that the response is a valid JSON string |
|
447 | 438 | def self.should_be_a_valid_json_string |
|
448 | 439 | should "be a valid JSON string (or empty)" do |
|
449 | 440 | assert(response.body.blank? || ActiveSupport::JSON.decode(response.body)) |
|
450 | 441 | end |
|
451 | 442 | end |
|
452 | 443 | |
|
453 | 444 | # Checks that the response is a valid XML string |
|
454 | 445 | def self.should_be_a_valid_xml_string |
|
455 | 446 | should "be a valid XML string" do |
|
456 | 447 | assert REXML::Document.new(response.body) |
|
457 | 448 | end |
|
458 | 449 | end |
|
459 | 450 | |
|
460 | 451 | end |
|
461 | 452 | |
|
462 | 453 | # Simple module to "namespace" all of the API tests |
|
463 | 454 | module ApiTest |
|
464 | 455 | end |
General Comments 0
You need to be logged in to leave comments.
Login now