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