Chris@441
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@1115
|
18 #require 'shoulda'
|
Chris@0
|
19 ENV["RAILS_ENV"] = "test"
|
Chris@0
|
20 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
Chris@1115
|
21 require 'rails/test_help'
|
Chris@909
|
22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
|
Chris@0
|
23
|
Chris@1115
|
24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
|
Chris@1115
|
25 include ObjectHelpers
|
Chris@0
|
26
|
Chris@1517
|
27 require 'awesome_nested_set/version'
|
Chris@1517
|
28
|
Chris@0
|
29 class ActiveSupport::TestCase
|
Chris@1115
|
30 include ActionDispatch::TestProcess
|
Chris@1464
|
31
|
Chris@0
|
32 self.use_transactional_fixtures = true
|
Chris@0
|
33 self.use_instantiated_fixtures = false
|
Chris@0
|
34
|
Chris@1517
|
35 ESCAPED_CANT = 'can't'
|
Chris@1517
|
36 ESCAPED_UCANT = 'Can't'
|
Chris@1517
|
37 # Rails 4.0.2
|
Chris@1517
|
38 #ESCAPED_CANT = 'can't'
|
Chris@1517
|
39 #ESCAPED_UCANT = 'Can't'
|
Chris@1517
|
40
|
Chris@0
|
41 def log_user(login, password)
|
Chris@0
|
42 User.anonymous
|
Chris@0
|
43 get "/login"
|
Chris@0
|
44 assert_equal nil, session[:user_id]
|
Chris@0
|
45 assert_response :success
|
Chris@0
|
46 assert_template "account/login"
|
Chris@0
|
47 post "/login", :username => login, :password => password
|
Chris@0
|
48 assert_equal login, User.find(session[:user_id]).login
|
Chris@0
|
49 end
|
Chris@909
|
50
|
Chris@0
|
51 def uploaded_test_file(name, mime)
|
Chris@1115
|
52 fixture_file_upload("files/#{name}", mime, true)
|
Chris@1115
|
53 end
|
Chris@1115
|
54
|
Chris@1115
|
55 def credentials(user, password=nil)
|
Chris@1115
|
56 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
|
Chris@0
|
57 end
|
Chris@0
|
58
|
Chris@0
|
59 # Mock out a file
|
Chris@14
|
60 def self.mock_file
|
Chris@0
|
61 file = 'a_file.png'
|
Chris@0
|
62 file.stubs(:size).returns(32)
|
Chris@0
|
63 file.stubs(:original_filename).returns('a_file.png')
|
Chris@0
|
64 file.stubs(:content_type).returns('image/png')
|
Chris@0
|
65 file.stubs(:read).returns(false)
|
Chris@0
|
66 file
|
Chris@0
|
67 end
|
Chris@14
|
68
|
Chris@14
|
69 def mock_file
|
Chris@14
|
70 self.class.mock_file
|
Chris@14
|
71 end
|
Chris@14
|
72
|
Chris@909
|
73 def mock_file_with_options(options={})
|
Chris@909
|
74 file = ''
|
Chris@909
|
75 file.stubs(:size).returns(32)
|
Chris@909
|
76 original_filename = options[:original_filename] || nil
|
Chris@909
|
77 file.stubs(:original_filename).returns(original_filename)
|
Chris@909
|
78 content_type = options[:content_type] || nil
|
Chris@909
|
79 file.stubs(:content_type).returns(content_type)
|
Chris@909
|
80 file.stubs(:read).returns(false)
|
Chris@909
|
81 file
|
Chris@909
|
82 end
|
Chris@909
|
83
|
Chris@0
|
84 # Use a temporary directory for attachment related tests
|
Chris@0
|
85 def set_tmp_attachments_directory
|
Chris@909
|
86 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
|
Chris@909
|
87 unless File.directory?("#{Rails.root}/tmp/test/attachments")
|
Chris@909
|
88 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
|
Chris@909
|
89 end
|
Chris@909
|
90 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
|
Chris@0
|
91 end
|
Chris@909
|
92
|
Chris@1115
|
93 def set_fixtures_attachments_directory
|
Chris@1115
|
94 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
|
Chris@1115
|
95 end
|
Chris@1115
|
96
|
Chris@0
|
97 def with_settings(options, &block)
|
Chris@1464
|
98 saved_settings = options.keys.inject({}) do |h, k|
|
Chris@1464
|
99 h[k] = case Setting[k]
|
Chris@1464
|
100 when Symbol, false, true, nil
|
Chris@1464
|
101 Setting[k]
|
Chris@1464
|
102 else
|
Chris@1464
|
103 Setting[k].dup
|
Chris@1464
|
104 end
|
Chris@1464
|
105 h
|
Chris@1464
|
106 end
|
Chris@0
|
107 options.each {|k, v| Setting[k] = v}
|
Chris@0
|
108 yield
|
Chris@441
|
109 ensure
|
Chris@909
|
110 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
|
Chris@0
|
111 end
|
Chris@0
|
112
|
Chris@1115
|
113 # Yields the block with user as the current user
|
Chris@1115
|
114 def with_current_user(user, &block)
|
Chris@1115
|
115 saved_user = User.current
|
Chris@1115
|
116 User.current = user
|
Chris@1115
|
117 yield
|
Chris@1115
|
118 ensure
|
Chris@1115
|
119 User.current = saved_user
|
Chris@1115
|
120 end
|
Chris@1115
|
121
|
Chris@1494
|
122 def with_locale(locale, &block)
|
Chris@1494
|
123 saved_localed = ::I18n.locale
|
Chris@1494
|
124 ::I18n.locale = locale
|
Chris@1494
|
125 yield
|
Chris@1494
|
126 ensure
|
Chris@1494
|
127 ::I18n.locale = saved_localed
|
Chris@1494
|
128 end
|
Chris@1494
|
129
|
Chris@14
|
130 def change_user_password(login, new_password)
|
Chris@1464
|
131 user = User.where(:login => login).first
|
Chris@14
|
132 user.password, user.password_confirmation = new_password, new_password
|
Chris@14
|
133 user.save!
|
Chris@14
|
134 end
|
Chris@14
|
135
|
Chris@0
|
136 def self.ldap_configured?
|
Chris@0
|
137 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
|
Chris@0
|
138 return @test_ldap.bind
|
Chris@0
|
139 rescue Exception => e
|
Chris@0
|
140 # LDAP is not listening
|
Chris@0
|
141 return nil
|
Chris@0
|
142 end
|
Chris@909
|
143
|
Chris@1115
|
144 def self.convert_installed?
|
Chris@1115
|
145 Redmine::Thumbnail.convert_available?
|
Chris@1115
|
146 end
|
Chris@1115
|
147
|
Chris@0
|
148 # Returns the path to the test +vendor+ repository
|
Chris@0
|
149 def self.repository_path(vendor)
|
Chris@909
|
150 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
|
Chris@0
|
151 end
|
Chris@909
|
152
|
Chris@441
|
153 # Returns the url of the subversion test repository
|
Chris@441
|
154 def self.subversion_repository_url
|
Chris@441
|
155 path = repository_path('subversion')
|
Chris@441
|
156 path = '/' + path unless path.starts_with?('/')
|
Chris@441
|
157 "file://#{path}"
|
Chris@441
|
158 end
|
Chris@909
|
159
|
Chris@0
|
160 # Returns true if the +vendor+ test repository is configured
|
Chris@0
|
161 def self.repository_configured?(vendor)
|
Chris@0
|
162 File.directory?(repository_path(vendor))
|
Chris@0
|
163 end
|
Chris@909
|
164
|
Chris@1115
|
165 def repository_path_hash(arr)
|
Chris@1115
|
166 hs = {}
|
Chris@1115
|
167 hs[:path] = arr.join("/")
|
Chris@1115
|
168 hs[:param] = arr.join("/")
|
Chris@1115
|
169 hs
|
Chris@1115
|
170 end
|
Chris@1115
|
171
|
Chris@1115
|
172 def assert_save(object)
|
Chris@1115
|
173 saved = object.save
|
Chris@1115
|
174 message = "#{object.class} could not be saved"
|
Chris@1115
|
175 errors = object.errors.full_messages.map {|m| "- #{m}"}
|
Chris@1115
|
176 message << ":\n#{errors.join("\n")}" if errors.any?
|
Chris@1115
|
177 assert_equal true, saved, message
|
Chris@1115
|
178 end
|
Chris@1115
|
179
|
chris@37
|
180 def assert_error_tag(options={})
|
Chris@119
|
181 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
|
chris@37
|
182 end
|
Chris@0
|
183
|
Chris@1115
|
184 def assert_include(expected, s, message=nil)
|
Chris@1115
|
185 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
|
Chris@1115
|
186 end
|
Chris@1115
|
187
|
Chris@1464
|
188 def assert_not_include(expected, s, message=nil)
|
Chris@1464
|
189 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
|
Chris@1115
|
190 end
|
Chris@1115
|
191
|
Chris@1115
|
192 def assert_select_in(text, *args, &block)
|
Chris@1115
|
193 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
|
Chris@1115
|
194 assert_select(d, *args, &block)
|
Chris@1115
|
195 end
|
Chris@1115
|
196
|
Chris@1464
|
197 def assert_mail_body_match(expected, mail, message=nil)
|
Chris@1115
|
198 if expected.is_a?(String)
|
Chris@1464
|
199 assert_include expected, mail_body(mail), message
|
Chris@1115
|
200 else
|
Chris@1464
|
201 assert_match expected, mail_body(mail), message
|
Chris@1115
|
202 end
|
Chris@1115
|
203 end
|
Chris@1115
|
204
|
Chris@1464
|
205 def assert_mail_body_no_match(expected, mail, message=nil)
|
Chris@1115
|
206 if expected.is_a?(String)
|
Chris@1464
|
207 assert_not_include expected, mail_body(mail), message
|
Chris@1115
|
208 else
|
Chris@1464
|
209 assert_no_match expected, mail_body(mail), message
|
Chris@1115
|
210 end
|
Chris@1115
|
211 end
|
Chris@1115
|
212
|
Chris@1115
|
213 def mail_body(mail)
|
Chris@1115
|
214 mail.parts.first.body.encoded
|
Chris@909
|
215 end
|
Chris@1517
|
216
|
Chris@1517
|
217 # awesome_nested_set new node lft and rgt value changed this refactor revision.
|
Chris@1517
|
218 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61
|
Chris@1517
|
219 # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line.
|
Chris@1517
|
220 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273
|
Chris@1517
|
221 # It seems correct behavior because of this line comment.
|
Chris@1517
|
222 # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278
|
Chris@1517
|
223 def new_issue_lft
|
Chris@1517
|
224 ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1
|
Chris@1517
|
225 end
|
Chris@1464
|
226 end
|
Chris@909
|
227
|
Chris@1464
|
228 module Redmine
|
Chris@1464
|
229 module ApiTest
|
Chris@1464
|
230 # Base class for API tests
|
Chris@1464
|
231 class Base < ActionDispatch::IntegrationTest
|
Chris@1464
|
232 # Test that a request allows the three types of API authentication
|
Chris@1464
|
233 #
|
Chris@1464
|
234 # * HTTP Basic with username and password
|
Chris@1464
|
235 # * HTTP Basic with an api key for the username
|
Chris@1464
|
236 # * Key based with the key=X parameter
|
Chris@1464
|
237 #
|
Chris@1464
|
238 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
Chris@1464
|
239 # @param [String] url the request url
|
Chris@1464
|
240 # @param [optional, Hash] parameters additional request parameters
|
Chris@1464
|
241 # @param [optional, Hash] options additional options
|
Chris@1464
|
242 # @option options [Symbol] :success_code Successful response code (:success)
|
Chris@1464
|
243 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
Chris@1464
|
244 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
|
Chris@1464
|
245 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
|
Chris@1464
|
246 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
|
Chris@1464
|
247 should_allow_key_based_auth(http_method, url, parameters, options)
|
Chris@1464
|
248 end
|
Chris@1464
|
249
|
Chris@1464
|
250 # Test that a request allows the username and password for HTTP BASIC
|
Chris@1464
|
251 #
|
Chris@1464
|
252 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
Chris@1464
|
253 # @param [String] url the request url
|
Chris@1464
|
254 # @param [optional, Hash] parameters additional request parameters
|
Chris@1464
|
255 # @param [optional, Hash] options additional options
|
Chris@1464
|
256 # @option options [Symbol] :success_code Successful response code (:success)
|
Chris@1464
|
257 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
Chris@1464
|
258 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
|
Chris@1464
|
259 success_code = options[:success_code] || :success
|
Chris@1464
|
260 failure_code = options[:failure_code] || :unauthorized
|
Chris@1464
|
261
|
Chris@1464
|
262 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
|
Chris@1464
|
263 context "with a valid HTTP authentication" do
|
Chris@1464
|
264 setup do
|
Chris@1464
|
265 @user = User.generate! do |user|
|
Chris@1464
|
266 user.admin = true
|
Chris@1464
|
267 user.password = 'my_password'
|
Chris@1464
|
268 end
|
Chris@1464
|
269 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
|
Chris@1464
|
270 end
|
Chris@1464
|
271
|
Chris@1464
|
272 should_respond_with success_code
|
Chris@1464
|
273 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
274 should "login as the user" do
|
Chris@1464
|
275 assert_equal @user, User.current
|
Chris@1464
|
276 end
|
Chris@1115
|
277 end
|
Chris@1464
|
278
|
Chris@1464
|
279 context "with an invalid HTTP authentication" do
|
Chris@1464
|
280 setup do
|
Chris@1464
|
281 @user = User.generate!
|
Chris@1464
|
282 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
|
Chris@1464
|
283 end
|
Chris@1464
|
284
|
Chris@1464
|
285 should_respond_with failure_code
|
Chris@1464
|
286 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
287 should "not login as the user" do
|
Chris@1464
|
288 assert_equal User.anonymous, User.current
|
Chris@1464
|
289 end
|
Chris@1464
|
290 end
|
Chris@1464
|
291
|
Chris@1464
|
292 context "without credentials" do
|
Chris@1464
|
293 setup do
|
Chris@1464
|
294 send(http_method, url, parameters)
|
Chris@1464
|
295 end
|
Chris@1464
|
296
|
Chris@1464
|
297 should_respond_with failure_code
|
Chris@1464
|
298 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
299 should "include_www_authenticate_header" do
|
Chris@1464
|
300 assert @controller.response.headers.has_key?('WWW-Authenticate')
|
Chris@1464
|
301 end
|
Chris@1464
|
302 end
|
chris@37
|
303 end
|
chris@37
|
304 end
|
Chris@1464
|
305
|
Chris@1464
|
306 # Test that a request allows the API key with HTTP BASIC
|
Chris@1464
|
307 #
|
Chris@1464
|
308 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
Chris@1464
|
309 # @param [String] url the request url
|
Chris@1464
|
310 # @param [optional, Hash] parameters additional request parameters
|
Chris@1464
|
311 # @param [optional, Hash] options additional options
|
Chris@1464
|
312 # @option options [Symbol] :success_code Successful response code (:success)
|
Chris@1464
|
313 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
Chris@1464
|
314 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
|
Chris@1464
|
315 success_code = options[:success_code] || :success
|
Chris@1464
|
316 failure_code = options[:failure_code] || :unauthorized
|
Chris@1464
|
317
|
Chris@1464
|
318 context "should allow http basic auth with a key for #{http_method} #{url}" do
|
Chris@1464
|
319 context "with a valid HTTP authentication using the API token" do
|
Chris@1464
|
320 setup do
|
Chris@1464
|
321 @user = User.generate! do |user|
|
Chris@1464
|
322 user.admin = true
|
Chris@1464
|
323 end
|
Chris@1464
|
324 @token = Token.create!(:user => @user, :action => 'api')
|
Chris@1464
|
325 send(http_method, url, parameters, credentials(@token.value, 'X'))
|
Chris@1464
|
326 end
|
Chris@1464
|
327 should_respond_with success_code
|
Chris@1464
|
328 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
329 should_be_a_valid_response_string_based_on_url(url)
|
Chris@1464
|
330 should "login as the user" do
|
Chris@1464
|
331 assert_equal @user, User.current
|
Chris@1464
|
332 end
|
Chris@1464
|
333 end
|
Chris@1464
|
334
|
Chris@1464
|
335 context "with an invalid HTTP authentication" do
|
Chris@1464
|
336 setup do
|
Chris@1464
|
337 @user = User.generate!
|
Chris@1464
|
338 @token = Token.create!(:user => @user, :action => 'feeds')
|
Chris@1464
|
339 send(http_method, url, parameters, credentials(@token.value, 'X'))
|
Chris@1464
|
340 end
|
Chris@1464
|
341 should_respond_with failure_code
|
Chris@1464
|
342 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
343 should "not login as the user" do
|
Chris@1464
|
344 assert_equal User.anonymous, User.current
|
Chris@1464
|
345 end
|
Chris@1464
|
346 end
|
chris@37
|
347 end
|
chris@37
|
348 end
|
Chris@1464
|
349
|
Chris@1464
|
350 # Test that a request allows full key authentication
|
Chris@1464
|
351 #
|
Chris@1464
|
352 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
Chris@1464
|
353 # @param [String] url the request url, without the key=ZXY parameter
|
Chris@1464
|
354 # @param [optional, Hash] parameters additional request parameters
|
Chris@1464
|
355 # @param [optional, Hash] options additional options
|
Chris@1464
|
356 # @option options [Symbol] :success_code Successful response code (:success)
|
Chris@1464
|
357 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
Chris@1464
|
358 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
|
Chris@1464
|
359 success_code = options[:success_code] || :success
|
Chris@1464
|
360 failure_code = options[:failure_code] || :unauthorized
|
Chris@1464
|
361
|
Chris@1464
|
362 context "should allow key based auth using key=X for #{http_method} #{url}" do
|
Chris@1464
|
363 context "with a valid api token" do
|
Chris@1464
|
364 setup do
|
Chris@1464
|
365 @user = User.generate! do |user|
|
Chris@1464
|
366 user.admin = true
|
Chris@1464
|
367 end
|
Chris@1464
|
368 @token = Token.create!(:user => @user, :action => 'api')
|
Chris@1464
|
369 # Simple url parse to add on ?key= or &key=
|
Chris@1464
|
370 request_url = if url.match(/\?/)
|
Chris@1464
|
371 url + "&key=#{@token.value}"
|
Chris@1464
|
372 else
|
Chris@1464
|
373 url + "?key=#{@token.value}"
|
Chris@1464
|
374 end
|
Chris@1464
|
375 send(http_method, request_url, parameters)
|
Chris@1464
|
376 end
|
Chris@1464
|
377 should_respond_with success_code
|
Chris@1464
|
378 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
379 should_be_a_valid_response_string_based_on_url(url)
|
Chris@1464
|
380 should "login as the user" do
|
Chris@1464
|
381 assert_equal @user, User.current
|
Chris@1464
|
382 end
|
Chris@1464
|
383 end
|
Chris@1464
|
384
|
Chris@1464
|
385 context "with an invalid api token" do
|
Chris@1464
|
386 setup do
|
Chris@1464
|
387 @user = User.generate! do |user|
|
Chris@1464
|
388 user.admin = true
|
Chris@1464
|
389 end
|
Chris@1464
|
390 @token = Token.create!(:user => @user, :action => 'feeds')
|
Chris@1464
|
391 # Simple url parse to add on ?key= or &key=
|
Chris@1464
|
392 request_url = if url.match(/\?/)
|
Chris@1464
|
393 url + "&key=#{@token.value}"
|
Chris@1464
|
394 else
|
Chris@1464
|
395 url + "?key=#{@token.value}"
|
Chris@1464
|
396 end
|
Chris@1464
|
397 send(http_method, request_url, parameters)
|
Chris@1464
|
398 end
|
Chris@1464
|
399 should_respond_with failure_code
|
Chris@1464
|
400 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
401 should "not login as the user" do
|
Chris@1464
|
402 assert_equal User.anonymous, User.current
|
Chris@1464
|
403 end
|
Chris@1464
|
404 end
|
chris@37
|
405 end
|
Chris@1464
|
406
|
Chris@1464
|
407 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
|
Chris@1464
|
408 setup do
|
Chris@1464
|
409 @user = User.generate! do |user|
|
Chris@1464
|
410 user.admin = true
|
Chris@1464
|
411 end
|
Chris@1464
|
412 @token = Token.create!(:user => @user, :action => 'api')
|
Chris@1464
|
413 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
|
Chris@1464
|
414 end
|
Chris@1464
|
415 should_respond_with success_code
|
Chris@1464
|
416 should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
417 should_be_a_valid_response_string_based_on_url(url)
|
Chris@1464
|
418 should "login as the user" do
|
Chris@1464
|
419 assert_equal @user, User.current
|
Chris@1464
|
420 end
|
Chris@1464
|
421 end
|
Chris@1464
|
422 end
|
Chris@1464
|
423
|
Chris@1464
|
424 # Uses should_respond_with_content_type based on what's in the url:
|
Chris@1464
|
425 #
|
Chris@1464
|
426 # '/project/issues.xml' => should_respond_with_content_type :xml
|
Chris@1464
|
427 # '/project/issues.json' => should_respond_with_content_type :json
|
Chris@1464
|
428 #
|
Chris@1464
|
429 # @param [String] url Request
|
Chris@1464
|
430 def self.should_respond_with_content_type_based_on_url(url)
|
Chris@1464
|
431 case
|
Chris@1464
|
432 when url.match(/xml/i)
|
Chris@1464
|
433 should "respond with XML" do
|
Chris@1464
|
434 assert_equal 'application/xml', @response.content_type
|
Chris@1464
|
435 end
|
Chris@1464
|
436 when url.match(/json/i)
|
Chris@1464
|
437 should "respond with JSON" do
|
Chris@1464
|
438 assert_equal 'application/json', @response.content_type
|
Chris@1464
|
439 end
|
Chris@1464
|
440 else
|
Chris@1464
|
441 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
|
Chris@1464
|
442 end
|
Chris@1464
|
443 end
|
Chris@1464
|
444
|
Chris@1464
|
445 # Uses the url to assert which format the response should be in
|
Chris@1464
|
446 #
|
Chris@1464
|
447 # '/project/issues.xml' => should_be_a_valid_xml_string
|
Chris@1464
|
448 # '/project/issues.json' => should_be_a_valid_json_string
|
Chris@1464
|
449 #
|
Chris@1464
|
450 # @param [String] url Request
|
Chris@1464
|
451 def self.should_be_a_valid_response_string_based_on_url(url)
|
Chris@1464
|
452 case
|
Chris@1464
|
453 when url.match(/xml/i)
|
Chris@1464
|
454 should_be_a_valid_xml_string
|
Chris@1464
|
455 when url.match(/json/i)
|
Chris@1464
|
456 should_be_a_valid_json_string
|
Chris@1464
|
457 else
|
Chris@1464
|
458 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
|
Chris@1464
|
459 end
|
Chris@1464
|
460 end
|
Chris@1464
|
461
|
Chris@1464
|
462 # Checks that the response is a valid JSON string
|
Chris@1464
|
463 def self.should_be_a_valid_json_string
|
Chris@1464
|
464 should "be a valid JSON string (or empty)" do
|
Chris@1464
|
465 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
|
Chris@1464
|
466 end
|
Chris@1464
|
467 end
|
Chris@1464
|
468
|
Chris@1464
|
469 # Checks that the response is a valid XML string
|
Chris@1464
|
470 def self.should_be_a_valid_xml_string
|
Chris@1464
|
471 should "be a valid XML string" do
|
Chris@1464
|
472 assert REXML::Document.new(response.body)
|
Chris@1464
|
473 end
|
Chris@1464
|
474 end
|
Chris@1464
|
475
|
Chris@1464
|
476 def self.should_respond_with(status)
|
Chris@1464
|
477 should "respond with #{status}" do
|
Chris@1464
|
478 assert_response status
|
chris@37
|
479 end
|
chris@37
|
480 end
|
chris@37
|
481 end
|
chris@37
|
482 end
|
Chris@1464
|
483 end
|
chris@37
|
484
|
Chris@1464
|
485 # URL helpers do not work with config.threadsafe!
|
Chris@1464
|
486 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
|
Chris@1464
|
487 ActionView::TestCase::TestController.instance_eval do
|
Chris@1464
|
488 helper Rails.application.routes.url_helpers
|
Chris@1464
|
489 end
|
Chris@1464
|
490 ActionView::TestCase::TestController.class_eval do
|
Chris@1464
|
491 def _routes
|
Chris@1464
|
492 Rails.application.routes
|
Chris@1115
|
493 end
|
Chris@0
|
494 end
|