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