strongbolt 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +33 -0
  3. data/.gitignore +18 -0
  4. data/.rspec +1 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +130 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +182 -0
  11. data/Rakefile +1 -0
  12. data/app/assets/javascripts/strongbolt.js +1 -0
  13. data/app/assets/javascripts/strongbolt/role-capabilities.js +80 -0
  14. data/app/controllers/strongbolt/capabilities_controller.rb +77 -0
  15. data/app/controllers/strongbolt/roles_controller.rb +92 -0
  16. data/app/controllers/strongbolt/security_controller.rb +8 -0
  17. data/app/controllers/strongbolt/user_groups_controller.rb +76 -0
  18. data/app/controllers/strongbolt/user_groups_users_controller.rb +35 -0
  19. data/app/controllers/strongbolt_controller.rb +2 -0
  20. data/app/views/strongbolt/_menu.html.erb +13 -0
  21. data/app/views/strongbolt/capabilities/index.html.erb +53 -0
  22. data/app/views/strongbolt/capabilities/show.html.erb +53 -0
  23. data/app/views/strongbolt/roles/_capabilities.html.erb +47 -0
  24. data/app/views/strongbolt/roles/_capability.html.erb +21 -0
  25. data/app/views/strongbolt/roles/_form.html.erb +12 -0
  26. data/app/views/strongbolt/roles/edit.html.erb +14 -0
  27. data/app/views/strongbolt/roles/index.html.erb +54 -0
  28. data/app/views/strongbolt/roles/new.html.erb +11 -0
  29. data/app/views/strongbolt/roles/show.html.erb +52 -0
  30. data/app/views/strongbolt/user_groups/_form.html.erb +12 -0
  31. data/app/views/strongbolt/user_groups/edit.html.erb +14 -0
  32. data/app/views/strongbolt/user_groups/index.html.erb +46 -0
  33. data/app/views/strongbolt/user_groups/new.html.erb +13 -0
  34. data/app/views/strongbolt/user_groups/show.html.erb +88 -0
  35. data/lib/generators/strongbolt/fix_generator.rb +23 -0
  36. data/lib/generators/strongbolt/indexes_generator.rb +19 -0
  37. data/lib/generators/strongbolt/install_generator.rb +29 -0
  38. data/lib/generators/strongbolt/templates/fix.rb +5 -0
  39. data/lib/generators/strongbolt/templates/indexes.rb +21 -0
  40. data/lib/generators/strongbolt/templates/migration.rb +73 -0
  41. data/lib/generators/strongbolt/templates/strongbolt.rb +45 -0
  42. data/lib/generators/strongbolt/views_generator.rb +26 -0
  43. data/lib/strongbolt.rb +219 -0
  44. data/lib/strongbolt/base.rb +7 -0
  45. data/lib/strongbolt/bolted.rb +125 -0
  46. data/lib/strongbolt/bolted_controller.rb +297 -0
  47. data/lib/strongbolt/capabilities_role.rb +15 -0
  48. data/lib/strongbolt/capability.rb +165 -0
  49. data/lib/strongbolt/configuration.rb +111 -0
  50. data/lib/strongbolt/controllers/url_helpers.rb +37 -0
  51. data/lib/strongbolt/engine.rb +44 -0
  52. data/lib/strongbolt/errors.rb +38 -0
  53. data/lib/strongbolt/generators/migration.rb +35 -0
  54. data/lib/strongbolt/helpers.rb +18 -0
  55. data/lib/strongbolt/rails/routes.rb +20 -0
  56. data/lib/strongbolt/role.rb +46 -0
  57. data/lib/strongbolt/roles_user_group.rb +15 -0
  58. data/lib/strongbolt/rspec.rb +29 -0
  59. data/lib/strongbolt/rspec/user.rb +90 -0
  60. data/lib/strongbolt/tenantable.rb +304 -0
  61. data/lib/strongbolt/user_abilities.rb +292 -0
  62. data/lib/strongbolt/user_group.rb +24 -0
  63. data/lib/strongbolt/user_groups_user.rb +16 -0
  64. data/lib/strongbolt/users_tenant.rb +12 -0
  65. data/lib/strongbolt/version.rb +3 -0
  66. data/lib/tasks/strongbolt_tasks.rake +29 -0
  67. data/spec/controllers/strongbolt/capabilities_controller_spec.rb +254 -0
  68. data/spec/controllers/strongbolt/roles_controller_spec.rb +228 -0
  69. data/spec/controllers/strongbolt/user_groups_controller_spec.rb +216 -0
  70. data/spec/controllers/strongbolt/user_groups_users_controller_spec.rb +69 -0
  71. data/spec/controllers/without_authorization_controller_spec.rb +20 -0
  72. data/spec/dummy/.rspec +2 -0
  73. data/spec/dummy/README.rdoc +28 -0
  74. data/spec/dummy/Rakefile +6 -0
  75. data/spec/dummy/app/assets/images/.keep +0 -0
  76. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  77. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  78. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  79. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  80. data/spec/dummy/app/controllers/posts_controller.rb +18 -0
  81. data/spec/dummy/app/controllers/test_controller.rb +3 -0
  82. data/spec/dummy/app/controllers/without_authorization_controller.rb +5 -0
  83. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  84. data/spec/dummy/app/mailers/.keep +0 -0
  85. data/spec/dummy/app/models/.keep +0 -0
  86. data/spec/dummy/app/models/concerns/.keep +0 -0
  87. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  88. data/spec/dummy/bin/bundle +3 -0
  89. data/spec/dummy/bin/rails +4 -0
  90. data/spec/dummy/bin/rake +4 -0
  91. data/spec/dummy/config.ru +4 -0
  92. data/spec/dummy/config/application.rb +29 -0
  93. data/spec/dummy/config/boot.rb +5 -0
  94. data/spec/dummy/config/database.yml +25 -0
  95. data/spec/dummy/config/environment.rb +5 -0
  96. data/spec/dummy/config/environments/development.rb +37 -0
  97. data/spec/dummy/config/environments/production.rb +78 -0
  98. data/spec/dummy/config/environments/test.rb +39 -0
  99. data/spec/dummy/config/initializers/assets.rb +8 -0
  100. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  101. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  102. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  103. data/spec/dummy/config/initializers/inflections.rb +16 -0
  104. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  105. data/spec/dummy/config/initializers/session_store.rb +3 -0
  106. data/spec/dummy/config/initializers/strongbolt.rb +32 -0
  107. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  108. data/spec/dummy/config/locales/en.yml +23 -0
  109. data/spec/dummy/config/routes.rb +12 -0
  110. data/spec/dummy/config/secrets.yml +22 -0
  111. data/spec/dummy/db/development.sqlite3 +0 -0
  112. data/spec/dummy/db/migrate/20150630212236_create_strongbolt_tables.rb +54 -0
  113. data/spec/dummy/db/migrate/20150630212251_create_strongbolt_tables_indexes.rb +21 -0
  114. data/spec/dummy/db/schema.rb +84 -0
  115. data/spec/dummy/db/test.sqlite3 +0 -0
  116. data/spec/dummy/lib/assets/.keep +0 -0
  117. data/spec/dummy/public/404.html +67 -0
  118. data/spec/dummy/public/422.html +67 -0
  119. data/spec/dummy/public/500.html +66 -0
  120. data/spec/dummy/public/favicon.ico +0 -0
  121. data/spec/fabricators/capability_fabricator.rb +4 -0
  122. data/spec/fabricators/role_fabricator.rb +9 -0
  123. data/spec/fabricators/user_fabricator.rb +3 -0
  124. data/spec/fabricators/user_group_fabricator.rb +9 -0
  125. data/spec/fixtures/application.rb +28 -0
  126. data/spec/fixtures/controllers.rb +5 -0
  127. data/spec/spec_helper.rb +89 -0
  128. data/spec/strongbolt/bolted_controller_spec.rb +706 -0
  129. data/spec/strongbolt/bolted_spec.rb +136 -0
  130. data/spec/strongbolt/capability_spec.rb +251 -0
  131. data/spec/strongbolt/configuration_spec.rb +119 -0
  132. data/spec/strongbolt/controllers/url_helpers_spec.rb +34 -0
  133. data/spec/strongbolt/helpers_spec.rb +43 -0
  134. data/spec/strongbolt/role_spec.rb +90 -0
  135. data/spec/strongbolt/tenantable_spec.rb +281 -0
  136. data/spec/strongbolt/user_abilities_spec.rb +509 -0
  137. data/spec/strongbolt/user_group_spec.rb +37 -0
  138. data/spec/strongbolt/users_tenant_spec.rb +36 -0
  139. data/spec/strongbolt_spec.rb +274 -0
  140. data/spec/support/controller_macros.rb +11 -0
  141. data/spec/support/db_setup.rb +134 -0
  142. data/spec/support/helpers.rb +62 -0
  143. data/spec/support/transactional_specs.rb +17 -0
  144. data/strongbolt.gemspec +32 -0
  145. metadata +407 -0
@@ -0,0 +1,706 @@
1
+ require "spec_helper"
2
+
3
+ # We're testing BoltedController module through this one
4
+ describe PostsController, :type => :controller do
5
+
6
+ before(:all) do
7
+ define_model "Post"
8
+ @user = User.create!
9
+ end
10
+
11
+ let(:user) { @user }
12
+
13
+
14
+
15
+
16
+
17
+ #
18
+ # Setup a current user
19
+ #
20
+ def setup_session
21
+ allow_any_instance_of(PostsController).to receive(:current_user).and_return @user
22
+ end
23
+
24
+ #
25
+ # Performs the right query given the action
26
+ #
27
+ def perform action
28
+ case action
29
+ when :index, :new then get action
30
+ when :show, :edit then get action, id: 1
31
+ when :update then put :update, id: 1
32
+ when :create then post :create
33
+ when :destroy then delete :destroy, id: 1
34
+ end
35
+ end
36
+
37
+
38
+
39
+
40
+
41
+ #
42
+ # Helpers
43
+ #
44
+ describe "helpers" do
45
+ before { Strongbolt.current_user = User.create! }
46
+ after { Strongbolt.current_user = nil }
47
+
48
+ describe "can?" do
49
+ it "should respond to can?" do
50
+ expect(PostsController.new).to respond_to :can?
51
+ end
52
+
53
+ it "should call can? on current_user" do
54
+ expect(Strongbolt.current_user).to receive(:can?).with :find, User
55
+ PostsController.new.can? :find, User
56
+ end
57
+ end
58
+
59
+ describe "cannot?" do
60
+ it "should respond to cannot?" do
61
+ expect(PostsController.new).to respond_to :cannot?
62
+ end
63
+
64
+ it "should call can? on current_user" do
65
+ expect(Strongbolt.current_user).to receive(:cannot?).with :find, User
66
+ PostsController.new.cannot? :find, User
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+ #
79
+ # Before Filter, set current user
80
+ #
81
+ describe 'before_action' do
82
+
83
+ before do
84
+ PostsController.skip_after_action :unset_current_user
85
+ PostsController.skip_before_action :check_authorization
86
+ end
87
+ after do
88
+ PostsController.after_action :unset_current_user
89
+ PostsController.before_action :check_authorization
90
+ end
91
+
92
+ context 'when no user' do
93
+ before do
94
+ expect_any_instance_of(PostsController).to receive(:current_user)
95
+ .at_least(1).times.and_return nil
96
+ get :index
97
+ end
98
+
99
+ it "should set nil user" do
100
+ expect(Strongbolt.current_user).to be_nil
101
+ end
102
+
103
+ it "should have set $request" do
104
+ expect($request).to be_present
105
+ end
106
+ end
107
+
108
+ context "when user" do
109
+ let(:user) { User.new }
110
+
111
+ before do
112
+ expect_any_instance_of(PostsController).to receive(:current_user).and_return user
113
+ get :index
114
+ end
115
+
116
+ it "should set the user" do
117
+ expect(Strongbolt.current_user).to eq user
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ #
124
+ # After filter, unset current user
125
+ #
126
+ describe 'after_action' do
127
+
128
+ before do
129
+ PostsController.skip_before_action :check_authorization
130
+ end
131
+ after do
132
+ PostsController.before_action :check_authorization
133
+ end
134
+
135
+ context "when a user is set" do
136
+
137
+ before do
138
+ expect_any_instance_of(PostsController).to receive(:current_user)
139
+ .and_return @user
140
+ get :index
141
+ end
142
+
143
+ it "should have unsetted the user" do
144
+ expect(Strongbolt.current_user).to be_nil
145
+ end
146
+ end
147
+
148
+ end
149
+
150
+ #
151
+ # Catching Grant::Error and Strongbolt::Unauthorized
152
+ #
153
+ describe 'catching Grant::Error' do
154
+ context "when unauthorized method exists" do
155
+ before do
156
+ allow(controller).to receive :unauthorized
157
+ expect_any_instance_of(PostsController).to receive(:index)
158
+ .and_raise Strongbolt::Unauthorized
159
+ end
160
+
161
+ it "should call unauthorized" do
162
+ expect_any_instance_of(PostsController).to receive(:unauthorized)
163
+ get :index
164
+ end
165
+ end
166
+
167
+ context "when no unauthorized method" do
168
+ before do
169
+ expect_any_instance_of(PostsController).to receive(:index)
170
+ .and_raise Grant::Error.new "Error"
171
+ end
172
+
173
+ it "should call raise Strongbolt::Unauthorized" do
174
+ expect do
175
+ get :index
176
+ end.to raise_error Strongbolt::Unauthorized
177
+ end
178
+ end
179
+ end
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+ #
189
+ # Checking authorization on a high level
190
+ #
191
+ describe "checking authorization" do
192
+
193
+ #
194
+ # When no authorization unrelated errors
195
+ #
196
+
197
+ context "when no error" do
198
+
199
+ before(:all) do
200
+ # Model linked to the controller
201
+ define_model "Post" do
202
+ self.table_name = "models"
203
+ end
204
+ end
205
+
206
+ before do
207
+ # user is the current user of our app
208
+ allow_any_instance_of(PostsController).to receive(:current_user)
209
+ .and_return user
210
+ end
211
+
212
+
213
+ #
214
+ # Call the right CRUD operation
215
+ #
216
+ describe "calling the CRUD operations" do
217
+ {
218
+ :index => :find,
219
+ :show => :find,
220
+ :edit => :update,
221
+ :update => :update,
222
+ :new => :create,
223
+ :create => :create
224
+ }.each do |action, operation|
225
+ context "when calling #{action}" do
226
+ it "should call the operation" do
227
+ expect(user).to receive(:can?).with(operation, Post).and_return true
228
+ perform action
229
+ end
230
+ end
231
+ end # End checking calling right can
232
+ end
233
+
234
+
235
+ #
236
+ # When calling a custom action without CRUD associated
237
+ #
238
+ context "when calling unmapped action" do
239
+
240
+ it "should raise ActionNotConfigured" do
241
+ expect do
242
+ get :custom
243
+ end.to raise_error Strongbolt::ActionNotConfigured
244
+ end
245
+
246
+ end
247
+
248
+
249
+ #
250
+ # When not authorized
251
+ #
252
+ context "when not authorized" do
253
+ before do
254
+ expect(Strongbolt).to receive(:access_denied)
255
+ expect(user).to receive(:can?).and_return false
256
+ end
257
+
258
+ it "should raise Strongbolt::Unauthorized" do
259
+ expect do
260
+ get :index
261
+ end.to raise_error Strongbolt::Unauthorized
262
+ end
263
+ end
264
+
265
+ #
266
+ # When authorized
267
+ #
268
+ context "when authorized" do
269
+ before do
270
+ expect(user).to receive(:can?).and_return true
271
+ end
272
+
273
+ it "should not raise error" do
274
+ expect do
275
+ get :index
276
+ end.not_to raise_error
277
+ end
278
+ end
279
+
280
+ end # End when no error
281
+
282
+
283
+
284
+ #
285
+ # Getting model name from controller name
286
+ #
287
+ describe "model_for_authorization" do
288
+
289
+ after do
290
+ undefine "ItemsController", "Item", "Namespace::Item",
291
+ "Namespace::ItemsController"
292
+ end
293
+
294
+ context "when no module" do
295
+ before do
296
+ define_controller "ItemsController"
297
+ define_model "Item"
298
+ end
299
+
300
+ it "should return the right model" do
301
+ expect(ItemsController.model_for_authorization).to eq Item
302
+ end
303
+ end
304
+
305
+ context "when both have modules" do
306
+ before do
307
+ define_controller "Namespace::ItemsController"
308
+ define_model "Namespace::Item"
309
+ end
310
+
311
+ it "should return the right model" do
312
+ expect(Namespace::ItemsController.model_for_authorization).to eq Namespace::Item
313
+ end
314
+ end
315
+
316
+ context "when only controller has module" do
317
+ before do
318
+ define_controller "Namespace::ItemsController"
319
+ define_model "Item"
320
+ end
321
+
322
+ it "should return the right model" do
323
+ expect(Namespace::ItemsController.model_for_authorization).to eq Item
324
+ end
325
+ end
326
+
327
+ context "when only model has module" do
328
+ before do
329
+ define_controller "ItemsController"
330
+ define_model "Namespace::Item"
331
+ end
332
+
333
+ it "should raise error" do
334
+ expect do
335
+ ItemsController.model_for_authorization
336
+ end.to raise_error Strongbolt::ModelNotFound
337
+ end
338
+ end
339
+
340
+ context "when cannot find" do
341
+ before do
342
+ define_controller "ItemsController"
343
+ undefine_model "Item"
344
+ end
345
+
346
+ it "should return the right model" do
347
+ expect do
348
+ ItemsController.model_for_authorization
349
+ end.to raise_error Strongbolt::ModelNotFound
350
+ end
351
+ end
352
+ end
353
+
354
+
355
+
356
+ #
357
+ # When the controller doesn't have any model associated
358
+ #
359
+
360
+ context "when controller doesn't have model" do
361
+
362
+ before do
363
+ undefine_model "Post"
364
+ setup_session
365
+ end
366
+
367
+ it "should raise error" do
368
+ expect do
369
+ get :index
370
+ end.to raise_error
371
+ end
372
+
373
+ end # End when no model associated
374
+
375
+ #
376
+ # When no current user
377
+ #
378
+ context "when no current user" do
379
+ before do
380
+ expect(Strongbolt).to receive(:current_user).and_return nil
381
+ expect(Strongbolt).to receive(:logger).and_call_original
382
+ end
383
+
384
+ it "should not raise error" do
385
+ get :index
386
+ end
387
+ end
388
+
389
+ end # End describe authorizations
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+ #
398
+ # Setting a specific model for a controller
399
+ #
400
+ describe 'setting specific model' do
401
+
402
+ before do
403
+ define_model "Custom" do
404
+ self.table_name = "models"
405
+ end
406
+ end
407
+ after { PostsController.model_for_authorization = nil }
408
+
409
+ context "when given as a string" do
410
+
411
+ context "and not exists" do
412
+ it "should raise error" do
413
+ expect do
414
+ PostsController.model_for_authorization = "FEge"
415
+ end.to raise_error Strongbolt::ModelNotFound
416
+ end
417
+ end
418
+
419
+ context 'when exists' do
420
+ before { PostsController.model_for_authorization = "Custom" }
421
+
422
+ it "should set it" do
423
+ expect(PostsController.model_for_authorization).to eq Custom
424
+ end
425
+ end
426
+
427
+ end # End when given as a string
428
+
429
+ context "when given as a model" do
430
+ before { PostsController.model_for_authorization = Custom }
431
+
432
+ it "should set it" do
433
+ expect(PostsController.model_for_authorization).to eq Custom
434
+ end
435
+ end
436
+
437
+ end
438
+
439
+
440
+
441
+
442
+
443
+ #
444
+ # Fetching authorization model when not specified
445
+ #
446
+ describe "model_for_authorization" do
447
+
448
+ context "when model is infered from controller" do
449
+ before do
450
+ define_model "Post"
451
+ get :index
452
+ end
453
+
454
+ it "should return the model" do
455
+ expect(PostsController.model_for_authorization).to eq Post
456
+ end
457
+ end
458
+
459
+ context "when model cannot be infered" do
460
+ before do
461
+ undefine_model "Post"
462
+ end
463
+
464
+ it "should raise ModelNotFound" do
465
+ expect do
466
+ PostsController.model_for_authorization
467
+ end.to raise_error Strongbolt::ModelNotFound
468
+ end
469
+ end
470
+
471
+ end
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+ #
481
+ # Skipping controller authorization
482
+ #
483
+ describe 'skip_controller_authorization' do
484
+
485
+ after { PostsController.before_action :check_authorization }
486
+
487
+ context "when no argument" do
488
+
489
+ before { PostsController.skip_controller_authorization }
490
+
491
+ RESTFUL_ACTIONS.each do |action|
492
+ it "should not call check_authorization" do
493
+ expect_any_instance_of(PostsController).not_to receive(:check_authorization)
494
+ perform action
495
+ end
496
+ end
497
+
498
+ end
499
+
500
+ context 'with only argument' do
501
+
502
+ before { PostsController.skip_controller_authorization only: skipped_actions }
503
+
504
+ context "when 1 action" do
505
+
506
+ let(:skipped_actions) { :index }
507
+
508
+ RESTFUL_ACTIONS.each do |action|
509
+ it "should skip the right one - #{action}" do
510
+ if action == skipped_actions
511
+ expect_any_instance_of(PostsController).not_to receive(:check_authorization)
512
+ else
513
+ expect_any_instance_of(PostsController).to receive(:check_authorization)
514
+ end
515
+ perform action
516
+ end
517
+ end
518
+
519
+ end # End 1 action
520
+
521
+ context "when several actions" do
522
+
523
+ let(:skipped_actions) { [:show, :index] }
524
+
525
+ RESTFUL_ACTIONS.each do |action|
526
+ it "should skip the right ones - #{action}" do
527
+ if skipped_actions.include? action
528
+ expect_any_instance_of(PostsController).not_to receive(:check_authorization)
529
+ else
530
+ expect_any_instance_of(PostsController).to receive(:check_authorization)
531
+ end
532
+ perform action
533
+ end
534
+ end
535
+
536
+ end # End several actions
537
+
538
+ end # End when only argument
539
+
540
+ context "with except argument" do
541
+
542
+ before { PostsController.skip_controller_authorization except: preserved_actions }
543
+
544
+ context "when 1 action" do
545
+
546
+ let(:preserved_actions) { :index }
547
+
548
+ RESTFUL_ACTIONS.each do |action|
549
+ it "should preserve the right one - #{action}" do
550
+ if action == preserved_actions
551
+ expect_any_instance_of(PostsController).to receive(:check_authorization)
552
+ else
553
+ expect_any_instance_of(PostsController).not_to receive(:check_authorization)
554
+ end
555
+ perform action
556
+ end
557
+ end
558
+
559
+ end # End 1 action
560
+
561
+ context "when several actions" do
562
+
563
+ let(:preserved_actions) { [:show, :index] }
564
+
565
+ RESTFUL_ACTIONS.each do |action|
566
+ it "should preserve the right ones - #{action}" do
567
+ if preserved_actions.include? action
568
+ expect_any_instance_of(PostsController).to receive(:check_authorization)
569
+ else
570
+ expect_any_instance_of(PostsController).not_to receive(:check_authorization)
571
+ end
572
+ perform action
573
+ end
574
+ end
575
+
576
+ end # End several actions
577
+
578
+ end # End except argument
579
+
580
+ end # End skipping controller authorization
581
+
582
+
583
+
584
+
585
+ #
586
+ # Skip all authorizations checking
587
+ #
588
+ describe "skip_all_authorization" do
589
+ #
590
+ # The controller raiser error if grant enabled
591
+ #
592
+ before do
593
+ class PostsController
594
+ def index
595
+ raise Strongbolt::Unauthorized if Grant::Status.grant_enabled?
596
+ end
597
+ end
598
+ end
599
+ after do
600
+ class PostsController
601
+ def index(); end
602
+ end
603
+ end
604
+
605
+ it "should raise an error" do
606
+ expect do
607
+ get :index
608
+ end.to raise_error Strongbolt::Unauthorized
609
+ end
610
+
611
+ context "when skipping" do
612
+ before { PostsController.skip_all_authorization only: :index }
613
+ after do
614
+ PostsController.before_action :check_authorization
615
+ PostsController.skip_around_action :disable_authorization
616
+ end
617
+
618
+ it "should not raise error" do
619
+ expect do
620
+ get :index
621
+ end.not_to raise_error
622
+ end
623
+ end
624
+ end # End skipping all authorization
625
+
626
+
627
+
628
+
629
+
630
+ #
631
+ # Mapping custom action to CRUD operation
632
+ #
633
+ describe "authorize_as_" do
634
+ before do
635
+ setup_session
636
+ define_model "Post"
637
+ end
638
+
639
+ [:find, :update, :create, :destroy].each do |operation|
640
+ context "authorize_as_#{operation}" do
641
+ before do
642
+ PostsController.send "authorize_as_#{operation}", :custom, :other
643
+ end
644
+
645
+ it "should respond_to" do
646
+ expect(PostsController).to respond_to "authorize_as_#{operation}"
647
+ end
648
+
649
+ it "should call the proper operation" do
650
+ expect(user).to receive(:can?).with(operation, Post).and_return true
651
+ get :custom
652
+ end
653
+
654
+ end
655
+ end
656
+ end
657
+
658
+
659
+
660
+
661
+ #
662
+ # Render without authorization
663
+ #
664
+ describe "render_without_authorization" do
665
+
666
+ after { PostsController.render_with_authorization }
667
+
668
+ it "should have aliased render" do
669
+ expect(PostsController.new).to respond_to :_render
670
+ end
671
+
672
+ context "when no arg" do
673
+ before do
674
+ PostsController.render_without_authorization
675
+ expect(Strongbolt).not_to receive(:without_authorization)
676
+ end
677
+
678
+ it "should perform without auth when index" do
679
+ get :index
680
+ end
681
+
682
+ it "should perform without auth when show" do
683
+ get :show, id: 1
684
+ end
685
+ end
686
+
687
+ context "when 1 arg" do
688
+ before do
689
+ PostsController.render_without_authorization :index
690
+ end
691
+
692
+ it "should perform without auth when index" do
693
+ expect(Strongbolt).to receive(:without_authorization)
694
+ get :index
695
+ end
696
+
697
+ it "should not perform without auth when show" do
698
+ expect(Strongbolt).not_to receive(:without_authorization)
699
+ get :show, id: 1
700
+ end
701
+ end
702
+
703
+ end
704
+
705
+
706
+ end