authorizy 0.4.1 → 0.6.0

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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +25 -11
  4. data/lib/authorizy/config.rb +1 -1
  5. data/lib/authorizy/core.rb +6 -6
  6. data/lib/authorizy/expander.rb +7 -11
  7. data/lib/authorizy/extension.rb +9 -6
  8. data/lib/authorizy/rspec.rb +3 -3
  9. data/lib/authorizy/version.rb +1 -1
  10. metadata +8 -184
  11. data/spec/authorizy/base_cop/access_question_spec.rb +0 -10
  12. data/spec/authorizy/config/aliases_spec.rb +0 -13
  13. data/spec/authorizy/config/cop_spec.rb +0 -13
  14. data/spec/authorizy/config/current_user_spec.rb +0 -29
  15. data/spec/authorizy/config/denied_spec.rb +0 -51
  16. data/spec/authorizy/config/dependencies_spec.rb +0 -13
  17. data/spec/authorizy/config/field_spec.rb +0 -29
  18. data/spec/authorizy/config/initialize_spec.rb +0 -7
  19. data/spec/authorizy/config/redirect_url_spec.rb +0 -31
  20. data/spec/authorizy/cop/controller_spec.rb +0 -41
  21. data/spec/authorizy/cop/model_spec.rb +0 -16
  22. data/spec/authorizy/cop/namespaced_controller_spec.rb +0 -41
  23. data/spec/authorizy/core/access_spec.rb +0 -181
  24. data/spec/authorizy/expander/expand_spec.rb +0 -139
  25. data/spec/authorizy/extension/authorizy_question_spec.rb +0 -50
  26. data/spec/authorizy/extension/authorizy_spec.rb +0 -54
  27. data/spec/authorizy/rspec_spec.rb +0 -11
  28. data/spec/common_helper.rb +0 -13
  29. data/spec/spec_helper.rb +0 -29
  30. data/spec/support/application.rb +0 -8
  31. data/spec/support/common.rb +0 -13
  32. data/spec/support/controllers/admin/dummy_controller.rb +0 -13
  33. data/spec/support/controllers/dummy_controller.rb +0 -11
  34. data/spec/support/coverage.rb +0 -18
  35. data/spec/support/i18n.rb +0 -3
  36. data/spec/support/locales/en.yml +0 -3
  37. data/spec/support/models/authorizy_cop.rb +0 -31
  38. data/spec/support/models/empty_cop.rb +0 -4
  39. data/spec/support/models/user.rb +0 -4
  40. data/spec/support/routes.rb +0 -6
  41. data/spec/support/schema.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adf5a52d89eabb0dd6503d0f96fb44ae9d4268213a6da8b91fb758805db97371
4
- data.tar.gz: 01bcedd187623c4364c38d0c4f53b5a12ee1877f4426df44b21cc3b7446f6b5d
3
+ metadata.gz: d473bd8a20f491dd718805097a7060d9c3b792657beb535441bdab3d2520e65f
4
+ data.tar.gz: 8dff226b40401c2acb2b09252022ecbf55c512c93180b61ef72c5d1ac0256692
5
5
  SHA512:
6
- metadata.gz: 0623f322536c3a6de17848f3f6b3642d70041384e220d0417d028a045d59970c97dfa84e659dd6a95795fe30f2ed09bb7d6203cf0bc7b600fbea0e53ff63bec5
7
- data.tar.gz: 4e4862f37eb92ef4c0eb247ecd0fa0abe0652d6d750b282bb20908fadaf3dbaf54738ed51167cfa5784e1d158a441d349c75f9a460981c800def31df30b1d002
6
+ metadata.gz: 26519151ed8c405d60f11958cb07768b17b2fc9cd0ecc0335473f1b9cee81ea46cff277f7be4fdf9afb1d071e18b26d10a5d66f5954fcf3bff0df9d6dca817a3
7
+ data.tar.gz: 2ad9b69333e08dd84b0f3124619877af35bf7966c63776d2920ff9b42d4deaee493b515b49714fc8d928884dda2a51ff3f0abb871ede0fdc258a5258a1cd947f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ # v0.6.0
2
+
3
+ ## Updates
4
+
5
+ - Set `activesupport` as production dependency
6
+ - Update gemspec to not include specs into the gem
7
+
8
+ # v0.5.0
9
+
10
+ ## Fixes
11
+
12
+ - The `authorizy?` helper now accepts custom parameters;
13
+
1
14
  # v0.4.1
2
15
 
3
16
  ## Fixes
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Authorizy
2
2
 
3
- [![CI](https://github.com/wbotelhos/authorizy/workflows/CI/badge.svg)](https://github.com/wbotelhos/authorizy/actions)
3
+ [![Tests](https://github.com/wbotelhos/authorizy/workflows/Tests/badge.svg)](https://github.com/wbotelhos/authorizy/actions?query=workflow:Tests)
4
4
  [![Gem Version](https://badge.fury.io/rb/authorizy.svg)](https://badge.fury.io/rb/authorizy)
5
- [![Maintainability](https://api.codeclimate.com/v1/badges/f312587b4f126bb13e85/maintainability)](https://codeclimate.com/github/wbotelhos/authorizy/maintainability)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/22ac7790d35a7c24410e/maintainability)](https://codeclimate.com/github/wbotelhos/authorizy/maintainability)
6
6
  [![Coverage](https://codecov.io/gh/wbotelhos/authorizy/branch/main/graph/badge.svg)](https://codecov.io/gh/wbotelhos/authorizy)
7
- [![Sponsor](https://img.shields.io/badge/sponsor-%3C3-green)](https://www.patreon.com/wbotelhos)
7
+ [![Sponsor](https://img.shields.io/badge/sponsor-%3C3-green)](https://github.com/sponsors/wbotelhos)
8
8
 
9
9
  A JSON based Authorization.
10
10
 
@@ -22,7 +22,7 @@ Run the following task to create Authorizy migration and initialize.
22
22
  rails g authorizy:install
23
23
  ```
24
24
 
25
- Then execute the migration to adds the column `authorizy` to your `users` table.
25
+ Then execute the migration to add the column `authorizy` to your `users` table.
26
26
 
27
27
  ```sh
28
28
  rake db:migrate
@@ -208,15 +208,29 @@ Using on view:
208
208
  <% end %>
209
209
  ```
210
210
 
211
+ Usually, we use the helper to check DB permission, not the runtime permission using the Cop file, although you can do it. Just remember that the parameters will be related to the current page, not the action you're protecting.
212
+
211
213
  Using on jBuilder view:
212
214
 
213
215
  ```ruby
214
- json.create_link new_users_url if authorizy?(:users, :create)
216
+ if authorizy?(:users, :create)
217
+ link_to('Create', new_users_url)
218
+ end
219
+ ```
220
+
221
+ But if you want to simulate the access on that resource you can manually provide the same parameters dispatched when you normally access that resource:
222
+
223
+ ```ruby
224
+ if authorizy?(:users, :create, params: { role: 'admin' })
225
+ link_to('Create', new_users_url(role: 'admin'))
226
+ end
215
227
  ```
216
228
 
229
+ Now you're providing the same parameters used in runtime when the user accesses the link, so now, we can check the "future" access and prevent or allow it before happens.
230
+
217
231
  # Specs
218
232
 
219
- To test some routes you'll need to give or not permission to the user, for that you have to ways, where the first is give permission to the user via session:
233
+ To test some routes you'll need to give or not permission to the user, for that you have two ways, where the first is the user via session:
220
234
 
221
235
  ```ruby
222
236
  before do
@@ -238,7 +252,7 @@ end
238
252
 
239
253
  ## Checks
240
254
 
241
- We have a couple of check, here is the order:
255
+ We have a couple of checks, here is the order:
242
256
 
243
257
  1. `Authorizy::BaseCop#access?`;
244
258
  2. `session[:permissions]`;
@@ -247,15 +261,15 @@ We have a couple of check, here is the order:
247
261
 
248
262
  ## Performance
249
263
 
250
- If you have few permissions, you can save the permissions in the session and avoid hit database many times, but if you have a couple of them, maybe it's a good idea save it in some place like [Redis](https://redis.io).
264
+ If you have few permissions, you can save the permissions in the session and avoid hitting the database many times, but if you have a couple of them, maybe it's a good idea to save them in some place like [Redis](https://redis.io).
251
265
 
252
266
  ## Management
253
267
 
254
- It's a good idea you keep your permissions in the database, so the customer can change it dynamic. You can load all permissions when the user is logged and cache it later. For cache expiration, you can trigger a refresh everytime that the permissions change.
268
+ It's a good idea you keep your permissions in the database, so the customer can change it dynamically. You can load all permissions when the user is logged in and cache it later. For cache expiration, you can trigger a refresh every time that the permissions change.
255
269
 
256
270
  ## Database Structure
257
271
 
258
- Inside database you can use the following relation to dynamicly change your permissions:
272
+ Inside the database, you can use the following relation to dynamically change your permissions:
259
273
 
260
274
  ```ruby
261
275
  plans -> plans_permissions <- permissions
@@ -269,7 +283,7 @@ plans -> plans_permissions <- permissions
269
283
 
270
284
  ## RSpec
271
285
 
272
- You can test you app passing through all authorizy layers:
286
+ You can test your app by passing through all Authorizy layers:
273
287
 
274
288
  ```ruby
275
289
  user = User.create!(permission: { permissions: [[:users, :create]] })
@@ -14,7 +14,7 @@ module Authorizy
14
14
 
15
15
  return context.render(json: { message: info }, status: 403) if context.request.xhr?
16
16
 
17
- context.redirect_to(redirect_url.call(context), info: info)
17
+ context.redirect_to(redirect_url.call(context), info:)
18
18
  }
19
19
 
20
20
  @dependencies = {}
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Authorizy
4
4
  class Core
5
- def initialize(user, params, session, cop:)
5
+ def initialize(user, params, session, cop: nil)
6
6
  @cop = cop
7
7
  @params = params
8
8
  @session = session
@@ -12,13 +12,13 @@ module Authorizy
12
12
  def access?
13
13
  return false if @user.blank?
14
14
 
15
- return true if @cop.access? ||
16
- session_permissions.any? { |tuple| route_match?(tuple) } ||
17
- user_permissions.any? { |tuple| route_match?(tuple) }
15
+ return true if @cop&.access?
16
+ return true if session_permissions.any? { |tuple| route_match?(tuple) }
17
+ return true if user_permissions.any? { |tuple| route_match?(tuple) }
18
18
 
19
- return @cop.public_send(cop_controller) == true if @cop.respond_to?(cop_controller)
19
+ return false unless @cop.respond_to?(cop_controller)
20
20
 
21
- false
21
+ @cop.public_send(cop_controller) == true
22
22
  end
23
23
 
24
24
  private
@@ -19,7 +19,7 @@ module Authorizy
19
19
  end
20
20
  end
21
21
 
22
- actions = [default_aliases[action]].flatten.compact
22
+ actions = [aliases[action]].flatten.compact
23
23
 
24
24
  next if actions.blank?
25
25
 
@@ -34,7 +34,12 @@ module Authorizy
34
34
  private
35
35
 
36
36
  def aliases
37
- Authorizy.config.aliases.stringify_keys
37
+ default = {
38
+ 'create' => 'new',
39
+ 'update' => 'edit',
40
+ }
41
+
42
+ default.merge(Authorizy.config.aliases.stringify_keys)
38
43
  end
39
44
 
40
45
  def controller_dependency(controller, action)
@@ -44,15 +49,6 @@ module Authorizy
44
49
  permissions.map { |c, a| [c.to_s, a.to_s] }
45
50
  end
46
51
 
47
- def default_aliases
48
- {
49
- 'create' => 'new',
50
- 'edit' => 'update',
51
- 'new' => 'create',
52
- 'update' => 'edit',
53
- }.merge(aliases)
54
- end
55
-
56
52
  def dependencies
57
53
  Authorizy.config.dependencies.deep_stringify_keys
58
54
  end
@@ -13,21 +13,24 @@ module Authorizy
13
13
  Authorizy.config.denied.call(self)
14
14
  end
15
15
 
16
- def authorizy?(controller, action)
16
+ def authorizy?(controller, action, custom_params: {})
17
17
  params['controller'] = controller
18
18
  params['action'] = action
19
19
 
20
- Authorizy::Core.new(authorizy_user, params, session, cop: authorizy_cop).access?
20
+ parameters = params.merge(custom_params)
21
+ cop = authorizy_cop(parameters)
22
+
23
+ Authorizy::Core.new(authorizy_user, parameters, session, cop:).access?
21
24
  end
22
25
 
23
26
  private
24
27
 
25
- def authorizy_user
26
- Authorizy.config.current_user.call(self)
28
+ def authorizy_cop(parameters = params)
29
+ Authorizy.config.cop.new(authorizy_user, parameters, session)
27
30
  end
28
31
 
29
- def authorizy_cop
30
- Authorizy.config.cop.new(authorizy_user, params, session)
32
+ def authorizy_user
33
+ Authorizy.config.current_user.call(self)
31
34
  end
32
35
  end
33
36
  end
@@ -4,13 +4,13 @@ require 'rspec/expectations'
4
4
 
5
5
  RSpec::Matchers.define :be_authorized do |controller, action, params: {}, session: {}|
6
6
  match do |user|
7
- parameters = params.merge(controller: controller, action: action)
7
+ parameters = params.merge(controller:, action:)
8
8
 
9
9
  access?(user, parameters, session)
10
10
  end
11
11
 
12
12
  match_when_negated do |user|
13
- parameters = params.merge(controller: controller, action: action)
13
+ parameters = params.merge(controller:, action:)
14
14
 
15
15
  !access?(user, parameters, session)
16
16
  end
@@ -28,7 +28,7 @@ RSpec::Matchers.define :be_authorized do |controller, action, params: {}, sessio
28
28
  def access?(user, params, session)
29
29
  cop = Authorizy.config.cop.new(user, params, session)
30
30
 
31
- Authorizy::Core.new(user, params, session, cop: cop).access?
31
+ Authorizy::Core.new(user, params, session, cop:).access?
32
32
  end
33
33
 
34
34
  def maybe_params_or_session(message, params, session)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Authorizy
4
- VERSION = '0.4.1'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,135 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authorizy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Washington Botelho
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-09-12 00:00:00.000000000 Z
10
+ date: 2025-04-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: activerecord
13
+ name: activesupport
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: codecov
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: pg
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: pry-byebug
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rake
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rspec-rails
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: rubocop-performance
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: rubocop-rails
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: rubocop-rspec
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
19
+ type: :runtime
133
20
  prerelease: false
134
21
  version_requirements: !ruby/object:Gem::Requirement
135
22
  requirements:
@@ -159,42 +46,11 @@ files:
159
46
  - lib/generators/authorizy/install_generator.rb
160
47
  - lib/generators/authorizy/templates/config/initializers/authorizy.rb
161
48
  - lib/generators/authorizy/templates/db/migrate/add_authorizy_on_users.rb
162
- - spec/authorizy/base_cop/access_question_spec.rb
163
- - spec/authorizy/config/aliases_spec.rb
164
- - spec/authorizy/config/cop_spec.rb
165
- - spec/authorizy/config/current_user_spec.rb
166
- - spec/authorizy/config/denied_spec.rb
167
- - spec/authorizy/config/dependencies_spec.rb
168
- - spec/authorizy/config/field_spec.rb
169
- - spec/authorizy/config/initialize_spec.rb
170
- - spec/authorizy/config/redirect_url_spec.rb
171
- - spec/authorizy/cop/controller_spec.rb
172
- - spec/authorizy/cop/model_spec.rb
173
- - spec/authorizy/cop/namespaced_controller_spec.rb
174
- - spec/authorizy/core/access_spec.rb
175
- - spec/authorizy/expander/expand_spec.rb
176
- - spec/authorizy/extension/authorizy_question_spec.rb
177
- - spec/authorizy/extension/authorizy_spec.rb
178
- - spec/authorizy/rspec_spec.rb
179
- - spec/common_helper.rb
180
- - spec/spec_helper.rb
181
- - spec/support/application.rb
182
- - spec/support/common.rb
183
- - spec/support/controllers/admin/dummy_controller.rb
184
- - spec/support/controllers/dummy_controller.rb
185
- - spec/support/coverage.rb
186
- - spec/support/i18n.rb
187
- - spec/support/locales/en.yml
188
- - spec/support/models/authorizy_cop.rb
189
- - spec/support/models/empty_cop.rb
190
- - spec/support/models/user.rb
191
- - spec/support/routes.rb
192
- - spec/support/schema.rb
193
49
  homepage: https://github.com/wbotelhos/authorizy
194
50
  licenses:
195
51
  - MIT
196
- metadata: {}
197
- post_install_message:
52
+ metadata:
53
+ rubygems_mfa_required: 'true'
198
54
  rdoc_options: []
199
55
  require_paths:
200
56
  - lib
@@ -209,39 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
209
65
  - !ruby/object:Gem::Version
210
66
  version: '0'
211
67
  requirements: []
212
- rubygems_version: 3.2.22
213
- signing_key:
68
+ rubygems_version: 3.6.6
214
69
  specification_version: 4
215
70
  summary: A JSON based Authorization.
216
- test_files:
217
- - spec/authorizy/base_cop/access_question_spec.rb
218
- - spec/authorizy/config/aliases_spec.rb
219
- - spec/authorizy/config/cop_spec.rb
220
- - spec/authorizy/config/current_user_spec.rb
221
- - spec/authorizy/config/denied_spec.rb
222
- - spec/authorizy/config/dependencies_spec.rb
223
- - spec/authorizy/config/field_spec.rb
224
- - spec/authorizy/config/initialize_spec.rb
225
- - spec/authorizy/config/redirect_url_spec.rb
226
- - spec/authorizy/cop/controller_spec.rb
227
- - spec/authorizy/cop/model_spec.rb
228
- - spec/authorizy/cop/namespaced_controller_spec.rb
229
- - spec/authorizy/core/access_spec.rb
230
- - spec/authorizy/expander/expand_spec.rb
231
- - spec/authorizy/extension/authorizy_question_spec.rb
232
- - spec/authorizy/extension/authorizy_spec.rb
233
- - spec/authorizy/rspec_spec.rb
234
- - spec/common_helper.rb
235
- - spec/spec_helper.rb
236
- - spec/support/application.rb
237
- - spec/support/common.rb
238
- - spec/support/controllers/admin/dummy_controller.rb
239
- - spec/support/controllers/dummy_controller.rb
240
- - spec/support/coverage.rb
241
- - spec/support/i18n.rb
242
- - spec/support/locales/en.yml
243
- - spec/support/models/authorizy_cop.rb
244
- - spec/support/models/empty_cop.rb
245
- - spec/support/models/user.rb
246
- - spec/support/routes.rb
247
- - spec/support/schema.rb
71
+ test_files: []
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::BaseCop, '#access?' do
4
- let!(:params) { { 'controller' => 'controller', 'action' => 'action' } }
5
- let(:cop) { described_class.new('current_user', params, 'session') }
6
-
7
- it 'returns false as default' do
8
- expect(cop.access?).to be(false)
9
- end
10
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#aliases' do
4
- let!(:config) { described_class.new }
5
-
6
- it 'has default value and can receive a new one' do
7
- expect(config.aliases).to eq({})
8
-
9
- config.aliases = 'value'
10
-
11
- expect(config.aliases).to eq('value')
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#cop' do
4
- let!(:config) { described_class.new }
5
-
6
- it 'has default value and can receive a new one' do
7
- expect(config.cop).to eq(Authorizy::BaseCop)
8
-
9
- config.cop = 'value'
10
-
11
- expect(config.cop).to eq('value')
12
- end
13
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#current_user' do
4
- let!(:config) { described_class.new }
5
-
6
- context 'when uses default value' do
7
- context 'when context responds to current_user' do
8
- let!(:context) { OpenStruct.new(current_user: 'user') }
9
-
10
- it 'is called' do
11
- expect(config.current_user.call(context)).to eq('user')
12
- end
13
- end
14
-
15
- context 'when context does not respond to current_user' do
16
- let!(:context) { 'context' }
17
-
18
- it { expect(config.current_user.call(context)).to be(nil) }
19
- end
20
- end
21
-
22
- context 'when uses custom value' do
23
- it 'executes what you want' do
24
- config.current_user = ->(context) { context[:value] }
25
-
26
- expect(config.current_user.call({ value: 'value' })).to eq('value')
27
- end
28
- end
29
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#denied' do
4
- let!(:config) { described_class.new }
5
-
6
- context 'with default denied callback' do
7
- context 'when is a xhr request' do
8
- let!(:context) do
9
- double('context',
10
- params: { controller: 'users', action: 'index' },
11
- request: OpenStruct.new(xhr?: true)
12
- )
13
- end
14
-
15
- it 'renders' do
16
- allow(context).to receive(:render)
17
-
18
- config.denied.call(context)
19
-
20
- expect(context).to have_received(:render).with(json: { message: 'Action denied for users#index' }, status: 403)
21
- end
22
- end
23
-
24
- context 'when is not a xhr request' do
25
- let!(:context) do
26
- double('context',
27
- params: { controller: 'users', action: 'index' },
28
- request: OpenStruct.new(xhr?: false),
29
- root_url: 'root_url'
30
- )
31
- end
32
-
33
- it 'redirects' do
34
- allow(context).to receive(:redirect_to)
35
- allow(context).to receive(:respond_to?).with(:root_url).and_return(true)
36
-
37
- config.denied.call(context)
38
-
39
- expect(context).to have_received(:redirect_to).with('root_url', info: 'Action denied for users#index')
40
- end
41
- end
42
- end
43
-
44
- context 'with custom denied callback' do
45
- it 'calls the callback' do
46
- config.denied = ->(context) { context[:key] }
47
-
48
- expect(config.denied.call(key: :value)).to eq(:value)
49
- end
50
- end
51
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#dependencies' do
4
- let!(:config) { described_class.new }
5
-
6
- it 'has default value and can receive a new one' do
7
- expect(config.dependencies).to eq({})
8
-
9
- config.dependencies = 'value'
10
-
11
- expect(config.dependencies).to eq('value')
12
- end
13
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config, '#field' do
4
- let!(:config) { described_class.new }
5
-
6
- context 'when uses default value' do
7
- context 'when current_user responds to authorizy' do
8
- let!(:current_user) { OpenStruct.new(authorizy: { permissions: [%i[users index]] }) }
9
-
10
- it 'is called' do
11
- expect(config.field.call(current_user)).to eq(permissions: [%i[users index]])
12
- end
13
- end
14
-
15
- context 'when current_user does not respond to field' do
16
- let!(:current_user) { nil }
17
-
18
- it { expect(config.field.call(current_user)).to eq({}) }
19
- end
20
- end
21
-
22
- context 'when uses custom value' do
23
- it 'executes what you want' do
24
- config.field = ->(current_user) { current_user[:value] }
25
-
26
- expect(config.field.call({ value: 'value' })).to eq('value')
27
- end
28
- end
29
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Config do
4
- it 'starts with a default cop' do
5
- expect(described_class.new.cop).to eq(Authorizy::BaseCop)
6
- end
7
- end