ccls-common_lib 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +116 -0
  3. data/lib/ccls-common_lib.rb +1 -0
  4. data/lib/common_lib.rb +37 -0
  5. data/lib/common_lib/action_controller_extension.rb +8 -0
  6. data/lib/common_lib/action_controller_extension/accessible_via_protocol.rb +405 -0
  7. data/lib/common_lib/action_controller_extension/accessible_via_user.rb +605 -0
  8. data/lib/common_lib/action_controller_extension/routing.rb +20 -0
  9. data/lib/common_lib/action_controller_extension/test_case.rb +22 -0
  10. data/lib/common_lib/action_view_extension.rb +3 -0
  11. data/lib/common_lib/action_view_extension/base.rb +310 -0
  12. data/lib/common_lib/action_view_extension/form_builder.rb +197 -0
  13. data/lib/common_lib/active_model.rb +4 -0
  14. data/lib/common_lib/active_model/errors.rb +16 -0
  15. data/lib/common_lib/active_model/validations/absence.rb +78 -0
  16. data/lib/common_lib/active_model/validations/complete_date.rb +138 -0
  17. data/lib/common_lib/active_model/validations/past_date.rb +121 -0
  18. data/lib/common_lib/active_record.rb +1 -0
  19. data/lib/common_lib/active_record/base.rb +129 -0
  20. data/lib/common_lib/active_support_extension.rb +12 -0
  21. data/lib/common_lib/active_support_extension/assertions.rb +108 -0
  22. data/lib/common_lib/active_support_extension/associations.rb +154 -0
  23. data/lib/common_lib/active_support_extension/attributes.rb +296 -0
  24. data/lib/common_lib/active_support_extension/pending.rb +115 -0
  25. data/lib/common_lib/active_support_extension/test_case.rb +203 -0
  26. data/lib/common_lib/railtie.rb +48 -0
  27. data/lib/common_lib/ruby.rb +8 -0
  28. data/lib/common_lib/ruby/array.rb +128 -0
  29. data/lib/common_lib/ruby/fixnum.rb +5 -0
  30. data/lib/common_lib/ruby/hash.rb +51 -0
  31. data/lib/common_lib/ruby/integer.rb +11 -0
  32. data/lib/common_lib/ruby/nil_class.rb +13 -0
  33. data/lib/common_lib/ruby/numeric.rb +0 -0
  34. data/lib/common_lib/ruby/object.rb +53 -0
  35. data/lib/common_lib/ruby/string.rb +20 -0
  36. data/lib/common_lib/translation_table.rb +129 -0
  37. data/lib/tasks/common_lib.rake +10 -0
  38. data/lib/tasks/csv.rake +0 -0
  39. data/lib/tasks/database.rake +229 -0
  40. data/lib/tasks/rcov.rake +52 -0
  41. data/vendor/assets/javascripts/common_lib.js +77 -0
  42. data/vendor/assets/stylesheets/common_lib.css +71 -0
  43. metadata +84 -0
@@ -0,0 +1,605 @@
1
+ module CommonLib::ActionControllerExtension::AccessibleViaUser
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ # This needs to be static and not dynamic or the multiple
8
+ # calls that would create it would overwrite each other.
9
+ def nawil_redirection(options={})
10
+ if options[:redirect]
11
+ send(options[:redirect])
12
+ else
13
+ root_path
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ def awil_title(options={})
20
+ "with #{options[:login]} login#{options[:suffix]}"
21
+ end
22
+
23
+ def assert_access_with_login(*actions)
24
+ user_options = actions.extract_options!
25
+
26
+ options = {}
27
+ # ruby 193 uses symbols
28
+ if ( self.constants.include?(:ASSERT_ACCESS_OPTIONS) )
29
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
30
+ # elsif ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
31
+ # options.merge!(self::ASSERT_ACCESS_OPTIONS)
32
+ end
33
+ options.merge!(user_options)
34
+ actions += options[:actions]||[]
35
+
36
+ m_key = options[:model].try(:underscore).try(:to_sym)
37
+
38
+ logins = Array(options[:logins]||options[:login])
39
+ logins.each do |login|
40
+ # options[:login] is set for the title,
41
+ # but "login_as send(login)" as options[:login]
42
+ # will be the last in the array at runtime.
43
+ options[:login] = login
44
+
45
+ test "#{brand}should get new #{awil_title(options)}" do
46
+ login_as send(login)
47
+ args = options[:new] || {}
48
+ send(:get,:new,args)
49
+ assert_response :success
50
+ assert_template 'new'
51
+ assert assigns(m_key), "#{m_key} was not assigned."
52
+ assert_nil flash[:error], "flash[:error] was not nil"
53
+ end if actions.include?(:new) || options.keys.include?(:new)
54
+
55
+ test "#{brand}should post create #{awil_title(options)}" do
56
+ login_as send(login)
57
+ args = if options[:create]
58
+ options[:create]
59
+ elsif options[:attributes_for_create]
60
+ {m_key => send(options[:attributes_for_create])}
61
+ else
62
+ {}
63
+ end
64
+ assert_difference("#{options[:model]}.count",1) do
65
+ send(:post,:create,args)
66
+ end
67
+ assert_response :redirect
68
+ assert_nil flash[:error], "flash[:error] was not nil"
69
+ end if actions.include?(:create) || options.keys.include?(:create)
70
+
71
+ test "#{brand}should NOT post create #{awil_title(options)} and #{m_key} save fails" do
72
+ login_as send(login)
73
+ # AFTER login as it may create this resource with the user
74
+ options[:model].constantize.any_instance.stubs(:create_or_update).returns(false)
75
+ args = if options[:create]
76
+ options[:create]
77
+ elsif options[:attributes_for_create]
78
+ {m_key => send(options[:attributes_for_create])}
79
+ else
80
+ {}
81
+ end
82
+ assert_difference("#{options[:model]}.count",0) do
83
+ send(:post,:create,args)
84
+ end
85
+ assert assigns(m_key)
86
+ assert_response :success
87
+ assert_template 'new'
88
+ assert_not_nil flash[:error]
89
+ end if( actions.include?(:create) || options.keys.include?(:create) ) && !options[:skip_create_failure]
90
+
91
+ test "#{brand}should NOT post create #{awil_title(options)} and invalid #{m_key}" do
92
+ login_as send(login)
93
+ # AFTER login as it may create this resource with the user
94
+ options[:model].constantize.any_instance.stubs(:valid?).returns(false)
95
+ args = if options[:create]
96
+ options[:create]
97
+ elsif options[:attributes_for_create]
98
+ {m_key => send(options[:attributes_for_create])}
99
+ else
100
+ {}
101
+ end
102
+ assert_difference("#{options[:model]}.count",0) do
103
+ send(:post,:create,args)
104
+ end
105
+ assert assigns(m_key)
106
+ assert_response :success
107
+ assert_template 'new'
108
+ assert_not_nil flash[:error]
109
+ end if( actions.include?(:create) || options.keys.include?(:create) ) && !options[:skip_create_failure]
110
+
111
+ test "#{brand}should get edit #{awil_title(options)}" do
112
+ login_as send(login)
113
+ args={}
114
+ if options[:method_for_create]
115
+ obj = send(options[:method_for_create])
116
+ args[:id] = obj.id
117
+ end
118
+ send(:get,:edit, args)
119
+ assert_response :success
120
+ assert_template 'edit'
121
+ assert assigns(m_key), "#{m_key} was not assigned."
122
+ assert_nil flash[:error], "flash[:error] was not nil"
123
+ end if actions.include?(:edit) || options.keys.include?(:edit)
124
+
125
+ test "#{brand}should NOT get edit #{awil_title(options)} and an invalid id" do
126
+ login_as send(login)
127
+ get :edit, :id => 0
128
+ assert_not_nil flash[:error], "flash[:error] was nil"
129
+ assert_response :redirect
130
+ end if( actions.include?(:edit) || options.keys.include?(:edit) ) && !options[:skip_edit_failure]
131
+
132
+ test "#{brand}should put update #{awil_title(options)}" do
133
+ login_as send(login)
134
+ args={}
135
+ if options[:method_for_create] && options[:attributes_for_create]
136
+ obj = send(options[:method_for_create],
137
+ :updated_at => ( Time.now - 2.days ) )
138
+ args[:id] = obj.id
139
+ args[m_key] = send(options[:attributes_for_create])
140
+ end
141
+ before = obj.updated_at if obj
142
+ # sleep 1 if obj # if updated too quickly, updated_at won't change
143
+ send(:put,:update, args)
144
+ after = obj.reload.updated_at if obj
145
+ assert_not_equal( before.to_i,after.to_i, "updated_at did not change." ) if obj
146
+ assert_response :redirect
147
+ assert_nil flash[:error], "flash[:error] was not nil"
148
+ end if actions.include?(:update) || options.keys.include?(:update)
149
+
150
+ test "#{brand}should NOT put update #{awil_title(options)} and #{m_key} save fails" do
151
+ login_as send(login)
152
+ args=options[:update]||{}
153
+ if options[:method_for_create] && options[:attributes_for_create]
154
+ obj = send(options[:method_for_create],
155
+ :updated_at => ( Time.now - 2.days ) )
156
+ args[:id] = obj.id
157
+ args[m_key] = send(options[:attributes_for_create])
158
+ end
159
+ before = obj.updated_at if obj
160
+ # sleep 1 if obj # if updated too quickly, updated_at won't change
161
+ options[:model].constantize.any_instance.stubs(:create_or_update).returns(false)
162
+ send(:put,:update, args)
163
+ after = obj.reload.updated_at if obj
164
+ assert_equal( before.to_s(:db), after.to_s(:db), "updated_at changed." ) if obj
165
+ assert_not_nil flash[:error], "flash[:error] was nil"
166
+ unless options[:no_redirect_check]
167
+ assert_response :success
168
+ assert_template 'edit'
169
+ end
170
+ end if( actions.include?(:update) || options.keys.include?(:update) ) && !options[:skip_update_failure]
171
+
172
+ test "#{brand}should NOT put update #{awil_title(options)} and invalid #{m_key}" do
173
+ login_as send(login)
174
+ args=options[:update]||{}
175
+ if options[:method_for_create] && options[:attributes_for_create]
176
+ obj = send(options[:method_for_create],
177
+ :updated_at => ( Time.now - 2.days ) )
178
+ args[:id] = obj.id
179
+ args[m_key] = send(options[:attributes_for_create])
180
+ end
181
+ before = obj.updated_at if obj
182
+ # sleep 1 if obj # if updated too quickly, updated_at won't change
183
+ options[:model].constantize.any_instance.stubs(:valid?).returns(false)
184
+ send(:put,:update, args)
185
+ after = obj.reload.updated_at if obj
186
+ assert_equal( before.to_s(:db), after.to_s(:db), "updated_at changed." ) if obj
187
+ assert_not_nil flash[:error], "flash[:error] was nil"
188
+ unless options[:no_redirect_check]
189
+ assert_response :success
190
+ assert_template 'edit'
191
+ end
192
+ end if( actions.include?(:update) || options.keys.include?(:update) ) && !options[:skip_update_failure]
193
+
194
+ test "#{brand}should NOT put update #{awil_title(options)} and an invalid id" do
195
+ login_as send(login)
196
+ put :update, :id => 0
197
+ assert_not_nil flash[:error], "flash[:error] was nil"
198
+ assert_response :redirect
199
+ end if( actions.include?(:update) || options.keys.include?(:update) ) && !options[:skip_update_failure]
200
+
201
+ test "#{brand}should get show #{awil_title(options)}" do
202
+ login_as send(login)
203
+ args={}
204
+ if options[:method_for_create]
205
+ obj = send(options[:method_for_create])
206
+ args[:id] = obj.id
207
+ end
208
+ send(:get,:show, args)
209
+ assert_response :success
210
+ assert_template 'show'
211
+ assert assigns(m_key), "#{m_key} was not assigned."
212
+ assert_nil flash[:error], "flash[:error] was not nil"
213
+ end if actions.include?(:show) || options.keys.include?(:show)
214
+
215
+ test "#{brand}should NOT get show #{awil_title(options)} and an invalid id" do
216
+ login_as send(login)
217
+ get :show, :id => 0
218
+ assert_not_nil flash[:error], "flash[:error] was nil"
219
+ assert_response :redirect
220
+ end if( actions.include?(:show) || options.keys.include?(:show) ) && !options[:skip_show_failure]
221
+
222
+ test "#{brand}should delete destroy #{awil_title(options)}" do
223
+ login_as send(login)
224
+ args={}
225
+ if options[:method_for_create]
226
+ obj = send(options[:method_for_create])
227
+ args[:id] = obj.id
228
+ end
229
+ assert_difference("#{options[:model]}.count",-1) do
230
+ send(:delete,:destroy,args)
231
+ end
232
+ assert_response :redirect
233
+ assert assigns(m_key), "#{m_key} was not assigned."
234
+ assert_nil flash[:error], "flash[:error] was not nil"
235
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
236
+
237
+ test "#{brand}should NOT delete destroy #{awil_title(options)} and an invalid id" do
238
+ login_as send(login)
239
+ delete :destroy, :id => 0
240
+ assert_not_nil flash[:error], "flash[:error] was nil"
241
+ assert_response :redirect
242
+ end if( actions.include?(:destroy) || options.keys.include?(:destroy) ) && !options[:skip_destroy_failure]
243
+
244
+ test "#{brand}should get index #{awil_title(options)}" do
245
+ login_as send(login)
246
+ get :index
247
+ assert_response :success
248
+ assert_template 'index'
249
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym)),
250
+ "#{m_key.try(:to_s).try(:pluralize).try(:to_sym)} was not assigned."
251
+ assert_nil flash[:error], "flash[:error] was not nil"
252
+ end if actions.include?(:index) || options.keys.include?(:index)
253
+
254
+ test "#{brand}should get index #{awil_title(options)} and items" do
255
+ send(options[:before]) if !options[:before].blank?
256
+ login_as send(login)
257
+ 3.times{ send(options[:method_for_create]) } if !options[:method_for_create].blank?
258
+ get :index
259
+ assert_response :success
260
+ assert_template 'index'
261
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym)),
262
+ "#{m_key.try(:to_s).try(:pluralize).try(:to_sym)} was not assigned."
263
+ assert_nil flash[:error], "flash[:error] was not nil"
264
+ end if actions.include?(:index) || options.keys.include?(:index)
265
+
266
+ end # logins.each
267
+ end
268
+
269
+
270
+ # I can't imagine a whole lot of use for this one.
271
+
272
+ def assert_access_without_login(*actions)
273
+ user_options = actions.extract_options!
274
+
275
+ options = {}
276
+ if ( self.constants.include?(:ASSERT_ACCESS_OPTIONS) )
277
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
278
+ # elsif ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
279
+ # options.merge!(self::ASSERT_ACCESS_OPTIONS)
280
+ end
281
+ options.merge!(user_options)
282
+ actions += options[:actions]||[]
283
+
284
+ m_key = options[:model].try(:underscore).try(:to_sym)
285
+
286
+ test "#{brand}AWoL should get new without login" do
287
+ get :new
288
+ assert_response :success
289
+ assert_template 'new'
290
+ assert assigns(m_key), "#{m_key} was not assigned."
291
+ assert_nil flash[:error], "flash[:error] was not nil"
292
+ end if actions.include?(:new) || options.keys.include?(:new)
293
+
294
+ test "#{brand}AWoL should post create without login" do
295
+ args = if options[:create]
296
+ options[:create]
297
+ elsif options[:attributes_for_create]
298
+ {m_key => send(options[:attributes_for_create])}
299
+ else
300
+ {}
301
+ end
302
+ assert_difference("#{options[:model]}.count",1) do
303
+ send(:post,:create,args)
304
+ end
305
+ assert_response :redirect
306
+ assert_nil flash[:error], "flash[:error] was not nil"
307
+ end if actions.include?(:create) || options.keys.include?(:create)
308
+
309
+ # test "should NOT get edit without login" do
310
+ # args=[]
311
+ # if options[:factory]
312
+ # obj = Factory(options[:factory])
313
+ # args.push(:id => obj.id)
314
+ # end
315
+ # send(:get,:edit, *args)
316
+ # assert_redirected_to_login
317
+ # end if actions.include?(:edit) || options.keys.include?(:edit)
318
+ #
319
+ # test "should NOT put update without login" do
320
+ # args={}
321
+ # if options[:factory]
322
+ # obj = Factory(options[:factory])
323
+ # args[:id] = obj.id
324
+ # args[options[:factory]] = Factory.attributes_for(options[:factory])
325
+ # end
326
+ # send(:put,:update, args)
327
+ # assert_redirected_to_login
328
+ # end if actions.include?(:update) || options.keys.include?(:update)
329
+
330
+ test "#{brand}AWoL should get show without login" do
331
+ args={}
332
+ if options[:method_for_create]
333
+ obj = send(options[:method_for_create])
334
+ args[:id] = obj.id
335
+ end
336
+ send(:get,:show, args)
337
+ assert_response :success
338
+ assert_template 'show'
339
+ assert assigns(m_key), "#{m_key} was not assigned."
340
+ assert_nil flash[:error], "flash[:error] was not nil"
341
+ end if actions.include?(:show) || options.keys.include?(:show)
342
+
343
+ # test "should NOT delete destroy without login" do
344
+ # args=[]
345
+ # if options[:factory]
346
+ # obj = Factory(options[:factory])
347
+ # args.push(:id => obj.id)
348
+ # end
349
+ # assert_no_difference("#{options[:model]}.count") do
350
+ # send(:delete,:destroy,*args)
351
+ # end
352
+ # assert_redirected_to_login
353
+ # end if actions.include?(:destroy) || options.keys.include?(:destroy)
354
+ #
355
+ # test "should NOT get index without login" do
356
+ # get :index
357
+ # assert_redirected_to_login
358
+ # end if actions.include?(:index) || options.keys.include?(:index)
359
+
360
+ test "#{brand}should get index without login" do
361
+ get :index
362
+ assert_response :success
363
+ assert_template 'index'
364
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym)),
365
+ "#{m_key.try(:to_s).try(:pluralize).try(:to_sym)} was not assigned."
366
+ assert_nil flash[:error], "flash[:error] was not nil"
367
+ end if actions.include?(:index) || options.keys.include?(:index)
368
+
369
+ test "#{brand}should get index without login and items" do
370
+ send(options[:before]) if !options[:before].blank?
371
+ 3.times{ send(options[:method_for_create]) } if !options[:method_for_create].blank?
372
+ get :index
373
+ assert_response :success
374
+ assert_template 'index'
375
+ assert assigns(m_key.try(:to_s).try(:pluralize).try(:to_sym)),
376
+ "#{m_key.try(:to_s).try(:pluralize).try(:to_sym)} was not assigned."
377
+ assert_nil flash[:error], "flash[:error] was not nil"
378
+ end if actions.include?(:index) || options.keys.include?(:index)
379
+
380
+ end
381
+
382
+ def nawil_title(options={})
383
+ "with #{options[:login]} login#{options[:suffix]}"
384
+ end
385
+
386
+ def assert_no_access_with_login(*actions)
387
+ user_options = actions.extract_options!
388
+
389
+ options = {}
390
+ if ( self.constants.include?(:ASSERT_ACCESS_OPTIONS) )
391
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
392
+ # elsif ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
393
+ # options.merge!(self::ASSERT_ACCESS_OPTIONS)
394
+ end
395
+ options.merge!(user_options)
396
+ actions += options[:actions]||[]
397
+
398
+ m_key = options[:model].try(:underscore).try(:to_sym)
399
+
400
+ logins = Array(options[:logins]||options[:login])
401
+ logins.each do |login|
402
+ # options[:login] is set for the title,
403
+ # but "login_as send(login)" as options[:login]
404
+ # will be the last in the array at runtime.
405
+ options[:login] = login
406
+
407
+ test "#{brand}should NOT get new #{nawil_title(options)}" do
408
+ login_as send(login)
409
+ args = options[:new]||{}
410
+ send(:get,:new,args)
411
+ assert_not_nil flash[:error], "flash[:error] was nil"
412
+ assert_response :redirect
413
+ unless options[:no_redirect_check]
414
+ assert_redirected_to nawil_redirection(options)
415
+ end
416
+ end if actions.include?(:new) || options.keys.include?(:new)
417
+
418
+ test "#{brand}should NOT post create #{nawil_title(options)}" do
419
+ login_as send(login)
420
+ args = if options[:create]
421
+ options[:create]
422
+ elsif options[:attributes_for_create]
423
+ {m_key => send(options[:attributes_for_create])}
424
+ else
425
+ {}
426
+ end
427
+ assert_no_difference("#{options[:model]}.count") do
428
+ send(:post,:create,args)
429
+ end
430
+ assert_not_nil flash[:error], "flash[:error] was nil"
431
+ assert_response :redirect
432
+ unless options[:no_redirect_check]
433
+ assert_redirected_to nawil_redirection(options)
434
+ end
435
+ end if actions.include?(:create) || options.keys.include?(:create)
436
+
437
+ test "#{brand}should NOT get edit #{nawil_title(options)}" do
438
+ login_as send(login)
439
+ args=options[:edit]||{}
440
+ if options[:method_for_create]
441
+ obj = send(options[:method_for_create])
442
+ args[:id] = obj.id
443
+ end
444
+ send(:get,:edit, args)
445
+ assert_not_nil flash[:error], "flash[:error] was nil"
446
+ assert_response :redirect
447
+ unless options[:no_redirect_check]
448
+ assert_redirected_to nawil_redirection(options)
449
+ end
450
+ end if actions.include?(:edit) || options.keys.include?(:edit)
451
+
452
+ test "#{brand}should NOT put update #{nawil_title(options)}" do
453
+ login_as send(login)
454
+ args=options[:update]||{}
455
+ if options[:method_for_create] && options[:attributes_for_create]
456
+ obj = send(options[:method_for_create])
457
+ args[:id] = obj.id
458
+ args[m_key] = send(options[:attributes_for_create])
459
+ end
460
+ before = obj.updated_at if obj
461
+ send(:put,:update, args)
462
+ after = obj.reload.updated_at if obj
463
+ assert_equal( before.to_s(:db), after.to_s(:db), "updated_at changed." ) if obj
464
+ assert_not_nil flash[:error], "flash[:error] was nil"
465
+ assert_response :redirect
466
+ unless options[:no_redirect_check]
467
+ assert_redirected_to nawil_redirection(options)
468
+ end
469
+ end if actions.include?(:update) || options.keys.include?(:update)
470
+
471
+ test "#{brand}should NOT get show #{nawil_title(options)}" do
472
+ login_as send(login)
473
+ args=options[:show]||{}
474
+ if options[:method_for_create]
475
+ obj = send(options[:method_for_create])
476
+ args[:id] = obj.id
477
+ end
478
+ send(:get,:show, args)
479
+ assert_not_nil flash[:error], "flash[:error] was nil"
480
+ assert_response :redirect
481
+ unless options[:no_redirect_check]
482
+ assert_redirected_to nawil_redirection(options)
483
+ end
484
+ end if actions.include?(:show) || options.keys.include?(:show)
485
+
486
+ test "#{brand}should NOT delete destroy #{nawil_title(options)}" do
487
+ login_as send(login)
488
+ args=options[:destroy]||{}
489
+ if options[:method_for_create]
490
+ obj = send(options[:method_for_create])
491
+ args[:id] = obj.id
492
+ end
493
+ assert_no_difference("#{options[:model]}.count") do
494
+ send(:delete,:destroy,args)
495
+ end
496
+ assert_not_nil flash[:error], "flash[:error] was nil"
497
+ assert_response :redirect
498
+ unless options[:no_redirect_check]
499
+ assert_redirected_to nawil_redirection(options)
500
+ end
501
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
502
+
503
+ test "#{brand}should NOT get index #{nawil_title(options)}" do
504
+ login_as send(login)
505
+ get :index
506
+ assert_not_nil flash[:error], "flash[:error] was nil"
507
+ assert_response :redirect
508
+ unless options[:no_redirect_check]
509
+ assert_redirected_to nawil_redirection(options)
510
+ end
511
+ end if actions.include?(:index) || options.keys.include?(:index)
512
+
513
+ end # logins.each
514
+ end
515
+
516
+ def assert_no_access_without_login(*actions)
517
+ user_options = actions.extract_options!
518
+
519
+ options = {}
520
+ if ( self.constants.include?(:ASSERT_ACCESS_OPTIONS) )
521
+ options.merge!(self::ASSERT_ACCESS_OPTIONS)
522
+ # elsif ( self.constants.include?('ASSERT_ACCESS_OPTIONS') )
523
+ # options.merge!(self::ASSERT_ACCESS_OPTIONS)
524
+ end
525
+ options.merge!(user_options)
526
+ actions += options[:actions]||[]
527
+
528
+ m_key = options[:model].try(:underscore).try(:to_sym)
529
+
530
+ test "#{brand}should NOT get new without login" do
531
+ get :new
532
+ assert_redirected_to_login
533
+ end if actions.include?(:new) || options.keys.include?(:new)
534
+
535
+ test "#{brand}should NOT post create without login" do
536
+ args = if options[:create]
537
+ options[:create]
538
+ elsif options[:attributes_for_create]
539
+ {m_key => send(options[:attributes_for_create])}
540
+ else
541
+ {}
542
+ end
543
+ assert_no_difference("#{options[:model]}.count") do
544
+ send(:post,:create,args)
545
+ end
546
+ assert_redirected_to_login
547
+ end if actions.include?(:create) || options.keys.include?(:create)
548
+
549
+ test "#{brand}should NOT get edit without login" do
550
+ args={}
551
+ if options[:method_for_create]
552
+ obj = send(options[:method_for_create])
553
+ args[:id] = obj.id
554
+ end
555
+ send(:get,:edit, args)
556
+ assert_redirected_to_login
557
+ end if actions.include?(:edit) || options.keys.include?(:edit)
558
+
559
+ test "#{brand}should NOT put update without login" do
560
+ args={}
561
+ if options[:method_for_create] && options[:attributes_for_create]
562
+ obj = send(options[:method_for_create])
563
+ args[:id] = obj.id
564
+ args[m_key] = send(options[:attributes_for_create])
565
+ end
566
+ before = obj.updated_at if obj
567
+ send(:put,:update, args)
568
+ after = obj.reload.updated_at if obj
569
+ assert_equal( before.to_s(:db), after.to_s(:db) ) if obj
570
+ assert_redirected_to_login
571
+ end if actions.include?(:update) || options.keys.include?(:update)
572
+
573
+ test "#{brand}should NOT get show without login" do
574
+ args={}
575
+ if options[:method_for_create]
576
+ obj = send(options[:method_for_create])
577
+ args[:id] = obj.id
578
+ end
579
+ send(:get,:show, args)
580
+ assert_redirected_to_login
581
+ end if actions.include?(:show) || options.keys.include?(:show)
582
+
583
+ test "#{brand}should NOT delete destroy without login" do
584
+ args={}
585
+ if options[:method_for_create]
586
+ obj = send(options[:method_for_create])
587
+ args[:id] = obj.id
588
+ end
589
+ assert_no_difference("#{options[:model]}.count") do
590
+ send(:delete,:destroy,args)
591
+ end
592
+ assert_redirected_to_login
593
+ end if actions.include?(:destroy) || options.keys.include?(:destroy)
594
+
595
+ test "#{brand}should NOT get index without login" do
596
+ get :index
597
+ assert_redirected_to_login
598
+ end if actions.include?(:index) || options.keys.include?(:index)
599
+
600
+ end
601
+
602
+ end # module ClassMethods
603
+ end # module CommonLib::ActionControllerExtension::AccessibleViaProtocol
604
+ ActionController::TestCase.send(:include,
605
+ CommonLib::ActionControllerExtension::AccessibleViaUser)