authorizy 0.5.0 → 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 +7 -0
  3. data/README.md +2 -2
  4. data/lib/authorizy/config.rb +1 -1
  5. data/lib/authorizy/extension.rb +2 -1
  6. data/lib/authorizy/rspec.rb +3 -3
  7. data/lib/authorizy/version.rb +1 -1
  8. metadata +8 -188
  9. data/spec/authorizy/base_cop/access_question_spec.rb +0 -10
  10. data/spec/authorizy/config/aliases_spec.rb +0 -13
  11. data/spec/authorizy/config/cop_spec.rb +0 -13
  12. data/spec/authorizy/config/current_user_spec.rb +0 -29
  13. data/spec/authorizy/config/denied_spec.rb +0 -51
  14. data/spec/authorizy/config/dependencies_spec.rb +0 -13
  15. data/spec/authorizy/config/field_spec.rb +0 -29
  16. data/spec/authorizy/config/initialize_spec.rb +0 -7
  17. data/spec/authorizy/config/redirect_url_spec.rb +0 -31
  18. data/spec/authorizy/config_spec.rb +0 -7
  19. data/spec/authorizy/configure_spec.rb +0 -9
  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 -74
  26. data/spec/authorizy/extension/authorizy_spec.rb +0 -54
  27. data/spec/authorizy/rspec_spec.rb +0 -30
  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 -35
  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
@@ -1,181 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Core, '#access?' do
4
- context 'when cop#access? returns true' do
5
- let!(:cop) { OpenStruct.new(access?: true) }
6
- let!(:current_user) { User.new }
7
- let!(:params) { { action: 'any', controller: 'any' } }
8
- let!(:session) { {} }
9
-
10
- it 'is authorized based in the cop response' do
11
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(true)
12
- end
13
- end
14
-
15
- context 'when permissions is in the current user' do
16
- let!(:cop) { OpenStruct.new(access?: false) }
17
- let!(:current_user) { User.new(authorizy: { permissions: [%w[controller create]] }) }
18
- let!(:params) { { controller: 'controller', action: 'create' } }
19
- let!(:session) { {} }
20
-
21
- it 'is authorized based on the user permissions' do
22
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(true)
23
- end
24
- end
25
-
26
- context 'when session has no permission nor the user' do
27
- let!(:cop) { OpenStruct.new(access?: false) }
28
- let!(:current_user) { User.new }
29
- let!(:params) { { controller: 'match', action: 'create' } }
30
- let!(:session) { {} }
31
-
32
- it 'does not authorize' do
33
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
34
- end
35
- end
36
-
37
- context 'when cop does not respond to controller' do
38
- let!(:cop) { instance_double('Authorizy::BaseCop', access?: false) }
39
- let!(:current_user) { User.new }
40
- let!(:params) { { action: 'create', controller: 'missing' } }
41
- let!(:session) { {} }
42
-
43
- it 'does not authorize via cop' do
44
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
45
- end
46
- end
47
-
48
- context 'when cop responds to controller' do
49
- let!(:current_user) { User.new }
50
- let!(:params) { { controller: 'admin/controller', action: 'create' } }
51
- let!(:session) { {} }
52
-
53
- context 'when cop does not release the access' do
54
- let!(:cop) do
55
- Class.new(Authorizy::BaseCop) do
56
- def access?
57
- false
58
- end
59
-
60
- def admin__controller
61
- false
62
- end
63
- end.new(current_user, params, session)
64
- end
65
-
66
- it 'is not authorized by cop' do
67
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
68
- end
69
- end
70
-
71
- context 'when cop releases the access' do
72
- let!(:cop) do
73
- Class.new(Authorizy::BaseCop) do
74
- def access?
75
- false
76
- end
77
-
78
- def admin__controller
79
- true
80
- end
81
- end.new(current_user, params, session)
82
- end
83
-
84
- it 'is authorized by the cop' do
85
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(true)
86
- end
87
- end
88
-
89
- context 'when cop return nil' do
90
- let!(:cop) do
91
- Class.new(Authorizy::BaseCop) do
92
- def access?
93
- false
94
- end
95
-
96
- def admin__controller
97
- nil
98
- end
99
- end.new(current_user, params, session)
100
- end
101
-
102
- it 'is converted to false' do
103
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
104
- end
105
- end
106
-
107
- context 'when cop return empty' do
108
- let!(:cop) do
109
- Class.new(Authorizy::BaseCop) do
110
- def access?
111
- false
112
- end
113
-
114
- def admin__controller
115
- ''
116
- end
117
- end.new(current_user, params, session)
118
- end
119
-
120
- it 'is converted to false' do
121
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
122
- end
123
- end
124
-
125
- context 'when cop return nothing' do
126
- let!(:cop) do
127
- Class.new(Authorizy::BaseCop) do
128
- def access?
129
- false
130
- end
131
-
132
- def admin__controller; end
133
- end.new(current_user, params, session)
134
- end
135
-
136
- it 'is converted to false' do
137
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
138
- end
139
- end
140
-
141
- context 'when cop return true as string' do
142
- let!(:cop) do
143
- Class.new(Authorizy::BaseCop) do
144
- def access?
145
- false
146
- end
147
-
148
- def admin__controller
149
- 'true'
150
- end
151
- end.new(current_user, params, session)
152
- end
153
-
154
- it 'is converted to false' do
155
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
156
- end
157
- end
158
- end
159
-
160
- context 'when user has the controller permission but not action' do
161
- let!(:cop) { instance_double('Authorizy::BaseCop', access?: false) }
162
- let!(:current_user) { User.new }
163
- let!(:params) { { controller: 'controller', action: 'action' } }
164
- let!(:session) { { permissions: [%w[controller miss]] } }
165
-
166
- it 'is not authorized' do
167
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
168
- end
169
- end
170
-
171
- context 'when user has the action permission but not controller' do
172
- let!(:cop) { instance_double('Authorizy::BaseCop', access?: false) }
173
- let!(:current_user) { User.new }
174
- let!(:params) { { controller: 'controller', action: 'action' } }
175
- let!(:session) { { permissions: [%w[miss action]] } }
176
-
177
- it 'is not authorized' do
178
- expect(described_class.new(current_user, params, session, cop: cop).access?).to be(false)
179
- end
180
- end
181
- end
@@ -1,139 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Authorizy::Expander, '#expand' do
4
- subject(:expander) { described_class.new }
5
-
6
- context 'when permissions is blank' do
7
- let(:permissions) { [] }
8
-
9
- it 'returns an empty permissions' do
10
- expect(expander.expand(permissions)).to eq []
11
- end
12
- end
13
-
14
- context 'when permissions is given' do
15
- context 'when data is symbol' do
16
- let(:permissions) do
17
- [
18
- %i[controller create],
19
- %i[controller update],
20
- ]
21
- end
22
-
23
- it 'maps the default actions aliases' do
24
- expect(expander.expand(permissions)).to match_array [
25
- %w[controller create],
26
- %w[controller edit],
27
- %w[controller new],
28
- %w[controller update],
29
- ]
30
- end
31
- end
32
-
33
- context 'when data is string' do
34
- let(:permissions) do
35
- [
36
- %w[controller create],
37
- %w[controller update],
38
- ]
39
- end
40
-
41
- it 'maps the default actions aliases' do
42
- expect(expander.expand(permissions)).to match_array [
43
- %w[controller create],
44
- %w[controller edit],
45
- %w[controller new],
46
- %w[controller update],
47
- ]
48
- end
49
- end
50
- end
51
-
52
- context 'when a dependencies is given' do
53
- context 'when keys and values are strings' do
54
- let(:dependencies) { { 'controller' => { 'action' => [%w[controller_2 action_2]] } } }
55
- let!(:permissions) { [%w[controller action]] }
56
-
57
- it 'addes the dependencies permissions' do
58
- config_mock(dependencies: dependencies) do
59
- expect(expander.expand(permissions)).to match_array [
60
- %w[controller action],
61
- %w[controller_2 action_2],
62
- ]
63
- end
64
- end
65
- end
66
-
67
- context 'when keys and values are symbol' do
68
- let(:dependencies) { { controller: { action: [%i[controller_2 action_2]] } } }
69
- let!(:permissions) { [%w[controller action]] }
70
-
71
- it 'addes the dependencies permissions' do
72
- config_mock(dependencies: dependencies) do
73
- expect(expander.expand(permissions)).to match_array [
74
- %w[controller action],
75
- %w[controller_2 action_2],
76
- ]
77
- end
78
- end
79
- end
80
- end
81
-
82
- context 'when aliases is given' do
83
- let!(:permissions) { [%w[controller action]] }
84
-
85
- context 'when key and values are strings' do
86
- let(:aliases) { { 'action' => 'action_2' } }
87
-
88
- it 'maps the action with the current controller' do
89
- config_mock(aliases: aliases) do
90
- expect(expander.expand(permissions)).to match_array [
91
- %w[controller action],
92
- %w[controller action_2],
93
- ]
94
- end
95
- end
96
- end
97
-
98
- context 'when key and values are symbols' do
99
- let(:aliases) { { action: :action_2 } }
100
-
101
- it 'maps the action with the current controller' do
102
- config_mock(aliases: aliases) do
103
- expect(expander.expand(permissions)).to match_array [
104
- %w[controller action],
105
- %w[controller action_2],
106
- ]
107
- end
108
- end
109
- end
110
-
111
- context 'when key and values are array of strings' do
112
- let(:aliases) { { action: %w[action_2 action_3] } }
113
-
114
- it 'maps the actions with the current controller' do
115
- config_mock(aliases: aliases) do
116
- expect(expander.expand(permissions)).to match_array [
117
- %w[controller action],
118
- %w[controller action_2],
119
- %w[controller action_3],
120
- ]
121
- end
122
- end
123
- end
124
-
125
- context 'when key and values are array of symbols' do
126
- let(:aliases) { { action: %i[action_2 action_3] } }
127
-
128
- it 'maps the actions with the current controller' do
129
- config_mock(aliases: aliases) do
130
- expect(expander.expand(permissions)).to match_array [
131
- %w[controller action],
132
- %w[controller action_2],
133
- %w[controller action_3],
134
- ]
135
- end
136
- end
137
- end
138
- end
139
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'support/controllers/dummy_controller'
4
- require 'support/models/authorizy_cop'
5
-
6
- RSpec.describe DummyController, '#authorizy?', type: :controller do
7
- context 'when config returns no current user' do
8
- it 'returns false' do
9
- config_mock(current_user: nil) do
10
- expect(controller.helpers.authorizy?('controller', 'action')).to be(false)
11
- end
12
- end
13
- end
14
-
15
- context 'when config returns current user' do
16
- let!(:config) { Authorizy.config }
17
- let!(:user) { User.new }
18
-
19
- before { allow(Authorizy).to receive(:config).and_return(config) }
20
-
21
- context 'when authorizy returns false' do
22
- let!(:core) { instance_double('Authorizy::Core', access?: false) }
23
- let!(:parameters) { ActionController::Parameters.new(controller: 'controller', action: 'action') }
24
-
25
- it 'returns false' do
26
- allow(Authorizy::Core).to receive(:new)
27
- .with(user, parameters, session, cop: config.cop)
28
- .and_return(core)
29
-
30
- config_mock(current_user: user) do
31
- expect(controller.helpers.authorizy?('controller', 'action')).to be(false)
32
- end
33
- end
34
- end
35
-
36
- context 'when authorizy returns true' do
37
- let!(:core) { instance_double('Authorizy::Core', access?: true) }
38
- let!(:parameters) { ActionController::Parameters.new(controller: 'controller', action: 'action') }
39
-
40
- it 'returns true' do
41
- allow(Authorizy::Core).to receive(:new)
42
- .with(user, parameters, session, cop: config.cop)
43
- .and_return(core)
44
-
45
- config_mock(current_user: user) do
46
- expect(controller.helpers.authorizy?('controller', 'action')).to be(true)
47
- end
48
- end
49
- end
50
-
51
- context 'when custom params is provided' do
52
- let!(:core) { instance_double('Authorizy::Core', access?: true) }
53
- let!(:parameters) { ActionController::Parameters.new(controller: 'controller', action: 'action', key: 'value') }
54
-
55
- it 'forwards to core' do
56
- expect(Authorizy::Core).to receive(:new)
57
- .with(user, parameters, session, cop: config.cop)
58
- .and_return(core)
59
-
60
- config_mock(current_user: user) do
61
- controller.helpers.authorizy?('controller', 'action', custom_params: { key: 'value' })
62
- end
63
- end
64
- end
65
-
66
- context 'when custom params is provided' do
67
- it 'forwards to cop' do
68
- config_mock(cop: AuthorizyCop, current_user: user) do
69
- controller.helpers.authorizy?('custom_params', 'action', custom_params: { custom: 'true' })
70
- end
71
- end
72
- end
73
- end
74
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'support/controllers/dummy_controller'
4
-
5
- RSpec.describe DummyController, '#authorizy', type: :controller do
6
- let!(:parameters) { ActionController::Parameters.new(key: 'value', controller: 'dummy', action: 'action') }
7
- let!(:user) { nil }
8
-
9
- context 'when user has access' do
10
- let!(:authorizy_core) { instance_double('Authorizy::Core', access?: true) }
11
-
12
- before do
13
- allow(Authorizy::Core).to receive(:new)
14
- .with(user, parameters, session, cop: Authorizy.config.cop)
15
- .and_return(authorizy_core)
16
- end
17
-
18
- context 'when is a xhr request' do
19
- it 'receives the default values and do not denied the access' do
20
- get :action, xhr: true, params: { key: 'value' }
21
-
22
- expect(response.body).to eq('{"message":"authorized"}')
23
- expect(response.status).to be(200)
24
- end
25
- end
26
-
27
- context 'when is a html request' do
28
- it 'receives the default values and do not denied the access' do
29
- get :action, params: { key: 'value' }
30
-
31
- expect(response.body).to eq('{"message":"authorized"}')
32
- expect(response.status).to be(200)
33
- end
34
- end
35
- end
36
-
37
- context 'when user has no access' do
38
- let!(:authorizy_core) { instance_double('Authorizy::Core', access?: false) }
39
-
40
- before do
41
- allow(Authorizy::Core).to receive(:new)
42
- .with(user, parameters, session, cop: Authorizy.config.cop)
43
- .and_return(authorizy_core)
44
- end
45
-
46
- it 'calls denied callback' do
47
- allow(Authorizy.config.denied).to receive(:call)
48
-
49
- get :action, xhr: true, params: { key: 'value' }
50
-
51
- expect(Authorizy.config.denied).to have_received(:call).with(subject)
52
- end
53
- end
54
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'authorizy/rspec'
4
- require 'support/models/authorizy_cop'
5
-
6
- RSpec.describe RSpec::Matchers, '#be_authorized' do
7
- it 'builds the correct description' do
8
- matcher = be_authorized('controller', 'action', params: { params: true }, session: { session: true })
9
-
10
- expect(matcher.description).to eq %(
11
- be authorized "controller", "action", and {:params=>{:params=>true}, :session=>{:session=>true}}
12
- ).squish
13
- end
14
-
15
- it 'has the positive question helper method' do
16
- user = User.new
17
-
18
- config_mock(cop: AuthorizyCop, current_user: user) do
19
- expect(user).to be_authorized('dummy', 'any', params: { access: 'true' })
20
- end
21
- end
22
-
23
- it 'has the negative question helper method' do
24
- user = User.new
25
-
26
- config_mock(cop: AuthorizyCop, current_user: user) do
27
- expect(user).not_to be_authorized('dummy', 'any', params: { access: 'false' })
28
- end
29
- end
30
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV['RAILS_ENV'] ||= 'test'
4
-
5
- require 'support/coverage'
6
-
7
- require 'support/application'
8
- require 'support/common'
9
- require 'support/i18n'
10
- require 'support/routes'
11
- require 'support/schema'
12
- require 'authorizy'
13
- require 'pry-byebug'
data/spec/spec_helper.rb DELETED
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- def config_mock(aliases: nil, cop: nil, current_user: nil, dependencies: nil, redirect_url: nil)
4
- backup = {
5
- aliases: Authorizy.config.aliases,
6
- cop: Authorizy.config.cop,
7
- current_user: Authorizy.config.current_user,
8
- dependencies: Authorizy.config.dependencies,
9
- redirect_url: Authorizy.config.redirect_url,
10
- }
11
-
12
- Authorizy.configure do |config|
13
- config.aliases = aliases if aliases
14
- config.cop = cop if cop
15
- config.current_user = ->(_context) { current_user } if current_user
16
- config.dependencies = dependencies if dependencies
17
- config.redirect_url = ->(_context) { redirect_url } if redirect_url
18
- end
19
-
20
- yield
21
- ensure
22
- Authorizy.configure do |config|
23
- config.aliases = backup[:aliases]
24
- config.cop = backup[:cop]
25
- config.current_user = backup[:current_user]
26
- config.dependencies = backup[:dependencies]
27
- config.redirect_url = backup[:redirect_url]
28
- end
29
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/railtie'
4
-
5
- module Authorizy
6
- class Application < Rails::Application
7
- end
8
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rspec/rails'
4
-
5
- RSpec.configure do |config|
6
- config.disable_monkey_patching!
7
-
8
- config.mock_with(:rspec) do |mocks|
9
- mocks.verify_partial_doubles = true
10
- end
11
-
12
- config.order = :random
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Admin
4
- class DummyController < ActionController::Base
5
- include Authorizy::Extension
6
-
7
- before_action :authorizy
8
-
9
- def action
10
- render json: { message: 'authorized' }
11
- end
12
- end
13
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class DummyController < ActionController::Base
4
- include Authorizy::Extension
5
-
6
- before_action :authorizy
7
-
8
- def action
9
- render json: { message: 'authorized' }
10
- end
11
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV['COVERAGE'] == 'true'
4
- require 'simplecov'
5
- require 'codecov'
6
-
7
- SimpleCov.formatter = SimpleCov::Formatter::Codecov
8
-
9
- SimpleCov.minimum_coverage(ENV.fetch('MINIMUM_COVERAGE', 80).to_i)
10
-
11
- SimpleCov.start('rails') do
12
- add_filter [
13
- '/lib/generators',
14
- '/vendor',
15
- '/lib/authorizy/version.rb',
16
- ]
17
- end
18
- end
data/spec/support/i18n.rb DELETED
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- I18n.load_path << Rails.root.join('spec/support/locales/en.yml')
@@ -1,3 +0,0 @@
1
- en:
2
- authorizy:
3
- denied: "Action denied for %{controller}#%{action}"
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class AuthorizyCop < Authorizy::BaseCop
4
- def admin__dummy
5
- params[:admin] == 'true'
6
- end
7
-
8
- def custom_params
9
- params[:custom] == 'true'
10
- end
11
-
12
- def dummy
13
- params[:access] == 'true'
14
- end
15
-
16
- def fetch_action
17
- action
18
- end
19
-
20
- def fetch_controller
21
- controller
22
- end
23
-
24
- def fetch_current_user
25
- current_user
26
- end
27
-
28
- def fetch_params
29
- params
30
- end
31
-
32
- def fetch_session
33
- session
34
- end
35
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class EmptyCop < Authorizy::BaseCop
4
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class User < ActiveRecord::Base
4
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Rails.application.routes.draw do
4
- get :action, to: 'admin/dummy#action'
5
- get :action, to: 'dummy#action'
6
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record'
4
- require 'support/models/user'
5
-
6
- ActiveRecord::Base.establish_connection(
7
- adapter: 'postgresql',
8
- host: 'localhost',
9
- username: 'postgres'
10
- )
11
-
12
- ActiveRecord::Base.connection.execute('DROP DATABASE IF EXISTS authorizy_test;')
13
- ActiveRecord::Base.connection.execute('CREATE DATABASE authorizy_test;')
14
- ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS users;')
15
-
16
- ActiveRecord::Schema.define(version: 1) do
17
- enable_extension 'plpgsql'
18
-
19
- create_table :users do |t|
20
- t.jsonb 'authorizy', default: {}
21
- end
22
- end