permissioner 0.2.0.beta → 1.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/permissioner/matchers/exactly_allow_attributes.rb +4 -4
- data/lib/permissioner/matchers/exactly_allow_controllers.rb +2 -2
- data/lib/permissioner/matchers/exactly_allow_resources.rb +2 -2
- data/lib/permissioner/matchers/exactly_expect_actions.rb +2 -2
- data/lib/permissioner/version.rb +1 -1
- data/permissioner.gemspec +4 -4
- data/spec/permissioner/configurer_spec.rb +9 -9
- data/spec/permissioner/controller_additions_spec.rb +17 -17
- data/spec/permissioner/matchers/exactly_allow_attributes_spec.rb +22 -22
- data/spec/permissioner/matchers/exactly_allow_controllers_spec.rb +11 -11
- data/spec/permissioner/matchers/exactly_allow_resources_spec.rb +10 -10
- data/spec/permissioner/matchers/exactly_expect_actions_spec.rb +17 -17
- data/spec/permissioner/matchers_spec.rb +22 -22
- data/spec/permissioner/service_additions_spec.rb +76 -76
- metadata +23 -23
@@ -19,26 +19,26 @@ describe Permissioner::Matchers::ExactlyAllowResources do
|
|
19
19
|
|
20
20
|
it 'should return true if given resources exaclty allowed' do
|
21
21
|
matcher = create_matcher :user, :comment, :post
|
22
|
-
matcher.matches?(permission_service).
|
22
|
+
expect(matcher.matches?(permission_service)).to be_truthy
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should return false if at least one resource is not allowed' do
|
26
26
|
matcher = create_matcher :user, :comment, :account
|
27
|
-
matcher.matches?(permission_service).
|
27
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should return false if more resources allowed than expected' do
|
31
31
|
matcher = create_matcher :user, :comment
|
32
|
-
matcher.matches?(permission_service).
|
32
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should work if no resource is allowed' do
|
36
36
|
matcher = create_matcher :user, :comment
|
37
|
-
matcher.matches?(PermissionService.new).
|
37
|
+
expect(matcher.matches?(PermissionService.new)).to be_falsey
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
describe '#
|
41
|
+
describe '#failure_message' do
|
42
42
|
|
43
43
|
it 'should be available' do
|
44
44
|
matcher = create_matcher(:user, :comment)
|
@@ -48,30 +48,30 @@ describe Permissioner::Matchers::ExactlyAllowResources do
|
|
48
48
|
"[:comment, :post, :user] allowed"
|
49
49
|
#call is necessary because matches sets @permission_service
|
50
50
|
matcher.matches?(permission_service)
|
51
|
-
matcher.
|
51
|
+
expect(matcher.failure_message).to eq expected_messages
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should work if no controller allowed' do
|
55
55
|
matcher = create_matcher(:user, :comment)
|
56
56
|
#call is necessary because matches sets @permission_service
|
57
57
|
matcher.matches?((PermissionService.new))
|
58
|
-
matcher.
|
58
|
+
expect(matcher.failure_message).to be_kind_of(String)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
describe '#
|
62
|
+
describe '# failure_message_when_negated' do
|
63
63
|
|
64
64
|
it 'should be available' do
|
65
65
|
matcher = create_matcher(:user, :comment)
|
66
66
|
expected_messages =
|
67
67
|
"expected to exactly not allow resources \n" \
|
68
68
|
"[:comment, :user], but these resources are exactly allowed\n"
|
69
|
-
matcher.
|
69
|
+
expect(matcher. failure_message_when_negated).to eq expected_messages
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'should work if no controller allowed' do
|
73
73
|
matcher = create_matcher(:comment, :user)
|
74
|
-
matcher.
|
74
|
+
expect(matcher. failure_message_when_negated).to be_kind_of(String)
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
@@ -18,9 +18,9 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
18
18
|
|
19
19
|
it 'should transform expected_actions into string array' do
|
20
20
|
matcher = create_matcher [:controller, :action]
|
21
|
-
matcher.instance_variable_get(:@all_expected_actions).
|
21
|
+
expect(matcher.instance_variable_get(:@all_expected_actions)).to eq [['controller', ['action']]]
|
22
22
|
matcher = create_matcher [:controller, [:action_1, :action_2]]
|
23
|
-
matcher.instance_variable_get(:@all_expected_actions).
|
23
|
+
expect(matcher.instance_variable_get(:@all_expected_actions)).to eq [['controller', ['action_1', 'action_2']]]
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should raise an exception if multiple actions not stated as array' do
|
@@ -40,7 +40,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
40
40
|
[:users, [:show, :new, :create]],
|
41
41
|
[:posts, [:show, :edit, :update]]
|
42
42
|
)
|
43
|
-
matcher.matches?(permission_service).
|
43
|
+
expect(matcher.matches?(permission_service)).to be_truthy
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'should return true if for a given controller all actions are allowed and all is expected' do
|
@@ -50,7 +50,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
50
50
|
[:users, [:show, :new, :create]],
|
51
51
|
[:posts, [:show, :edit, :update]]
|
52
52
|
)
|
53
|
-
matcher.matches?(permission_service).
|
53
|
+
expect(matcher.matches?(permission_service)).to be_truthy
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -62,7 +62,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
62
62
|
[:users, [:show, :new, :create]],
|
63
63
|
[:accounts, [:show, :edit, :update]]
|
64
64
|
)
|
65
|
-
matcher.matches?(permission_service).
|
65
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'should return false if at least one controller is allowed but no expected' do
|
@@ -70,7 +70,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
70
70
|
[:comments, [:index, :show, :new, :create]],
|
71
71
|
[:users, [:show, :new, :create]]
|
72
72
|
)
|
73
|
-
matcher.matches?(permission_service).
|
73
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -82,7 +82,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
82
82
|
[:users, [:show, :new, :create]],
|
83
83
|
[:posts, [:show, :edit, :update]]
|
84
84
|
)
|
85
|
-
matcher.matches?(permission_service).
|
85
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'should return false if at least one action is not allowed but no expected' do
|
@@ -91,7 +91,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
91
91
|
[:users, [:show, :new, :create]],
|
92
92
|
[:posts, [:show, :edit, :update]]
|
93
93
|
)
|
94
|
-
matcher.matches?(permission_service).
|
94
|
+
expect(matcher.matches?(permission_service)).to be_falsey
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -101,11 +101,11 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
101
101
|
[:users, [:show, :new, :create]],
|
102
102
|
[:posts, [:show, :edit, :update]]
|
103
103
|
)
|
104
|
-
matcher.matches?(PermissionService.new).
|
104
|
+
expect(matcher.matches?(PermissionService.new)).to be_falsey
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
describe '#
|
108
|
+
describe '#failure_message' do
|
109
109
|
|
110
110
|
context 'if controllers did not match' do
|
111
111
|
|
@@ -120,7 +120,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
120
120
|
"[\"comments\", \"posts\", \"users\"]"
|
121
121
|
#call is necessary because matches sets @permission_service
|
122
122
|
matcher.matches?(permission_service)
|
123
|
-
matcher.
|
123
|
+
expect(matcher.failure_message).to eq expected_messages
|
124
124
|
end
|
125
125
|
|
126
126
|
it 'should work if no controller allowed' do
|
@@ -130,7 +130,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
130
130
|
)
|
131
131
|
#call is necessary because matches sets @permission_service
|
132
132
|
matcher.matches?(PermissionService.new)
|
133
|
-
matcher.
|
133
|
+
expect(matcher.failure_message).to be_kind_of(String)
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
@@ -152,7 +152,7 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
152
152
|
"[\"create\", \"new\", \"show\"]\n"
|
153
153
|
#call is necessary because matches sets @permission_service
|
154
154
|
matcher.matches?(permission_service)
|
155
|
-
matcher.
|
155
|
+
expect(matcher.failure_message).to eq expected_messages
|
156
156
|
end
|
157
157
|
|
158
158
|
|
@@ -164,24 +164,24 @@ describe Permissioner::Matchers::ExactlyExpectActions do
|
|
164
164
|
)
|
165
165
|
#call is necessary because matches sets @permission_service
|
166
166
|
matcher.matches?(PermissionService.new)
|
167
|
-
matcher.
|
167
|
+
expect(matcher.failure_message).to be_kind_of(String)
|
168
168
|
end
|
169
169
|
end
|
170
170
|
end
|
171
171
|
|
172
172
|
|
173
|
-
describe '#
|
173
|
+
describe '# failure_message_when_negated' do
|
174
174
|
|
175
175
|
it 'should be available' do
|
176
176
|
matcher = create_matcher
|
177
177
|
expected_messages = 'given actions are exactly match although this is not expected'
|
178
|
-
matcher.
|
178
|
+
expect(matcher. failure_message_when_negated).to eq expected_messages
|
179
179
|
end
|
180
180
|
|
181
181
|
it 'should be available' do
|
182
182
|
matcher = create_matcher
|
183
183
|
matcher.matches?(PermissionService.new)
|
184
|
-
matcher.
|
184
|
+
expect(matcher. failure_message_when_negated).to be_kind_of(String)
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
@@ -13,39 +13,39 @@ describe Permissioner::Matchers do
|
|
13
13
|
describe '#allow_action' do
|
14
14
|
|
15
15
|
it 'should delegate call to PermissionService#allow_action? and pass when is returns true' do
|
16
|
-
permission_service.
|
17
|
-
permission_service.
|
16
|
+
expect(permission_service).to receive(:allow_action?).with(:comments, :index, resource: 'resource', params: 'params').and_return(true)
|
17
|
+
expect(permission_service).to allow_action :comments, :index, resource: 'resource', params: 'params'
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'should fail when PermissionService#allow_action? returns false' do
|
21
|
-
permission_service.
|
22
|
-
permission_service.
|
21
|
+
expect(permission_service).to receive(:allow_action?).and_return(false)
|
22
|
+
expect(permission_service).not_to allow_action
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe '#allow_attribute' do
|
27
27
|
|
28
28
|
it 'should delegate call to PermissionService#allow_action? and pass when is returns true' do
|
29
|
-
permission_service.
|
30
|
-
permission_service.
|
29
|
+
expect(permission_service).to receive(:allow_attribute?).with(:comment, :user_id, :text).and_return(true)
|
30
|
+
expect(permission_service).to allow_attribute :comment, :user_id, :text
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'should fail when PermissionService#allow_action? returns false' do
|
34
|
-
permission_service.
|
35
|
-
permission_service.
|
34
|
+
expect(permission_service).to receive(:allow_attribute?).and_return(false)
|
35
|
+
expect(permission_service).not_to allow_attribute
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
describe '#pass_filters' do
|
40
40
|
|
41
41
|
it 'should delegate call to PermissionService#passed_filters? and pass when is returns true' do
|
42
|
-
permission_service.
|
43
|
-
permission_service.
|
42
|
+
expect(permission_service).to receive(:passed_filters?).with(:comment, :user_id, resource: 'resource', params: 'params').and_return(true)
|
43
|
+
expect(permission_service).to pass_filters :comment, :user_id, resource: 'resource', params: 'params'
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'should fail when PermissionService#passed_filters? returns false' do
|
47
|
-
permission_service.
|
48
|
-
permission_service.
|
47
|
+
expect(permission_service).to receive(:passed_filters?).and_return(false)
|
48
|
+
expect(permission_service).not_to pass_filters
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -53,8 +53,8 @@ describe Permissioner::Matchers do
|
|
53
53
|
|
54
54
|
it 'should return correctly instantiated instance of ExactlyExpectActions' do
|
55
55
|
expected_actions = [:controller_1, :actions_1], [:controller_2, :actions_2]
|
56
|
-
Permissioner::Matchers::ExactlyExpectActions.
|
57
|
-
matcher_helper.exactly_allow_actions(*expected_actions).
|
56
|
+
expect(Permissioner::Matchers::ExactlyExpectActions).to receive(:new).with(:@allowed_actions, *expected_actions).and_call_original
|
57
|
+
expect(matcher_helper.exactly_allow_actions(*expected_actions)).to be_kind_of(Permissioner::Matchers::ExactlyExpectActions)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -62,8 +62,8 @@ describe Permissioner::Matchers do
|
|
62
62
|
|
63
63
|
it 'should return correctly instantiated instance of ExactlyExpectActions' do
|
64
64
|
expected_actions = [:controller_1, :actions_1], [:controller_2, :actions_2]
|
65
|
-
Permissioner::Matchers::ExactlyExpectActions.
|
66
|
-
matcher_helper.exactly_have_filters_for(*expected_actions).
|
65
|
+
expect(Permissioner::Matchers::ExactlyExpectActions).to receive(:new).with(:@filters, *expected_actions).and_call_original
|
66
|
+
expect(matcher_helper.exactly_have_filters_for(*expected_actions)).to be_kind_of(Permissioner::Matchers::ExactlyExpectActions)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -71,24 +71,24 @@ describe Permissioner::Matchers do
|
|
71
71
|
|
72
72
|
it 'should return correctly instantiated instance of ExactlyAllowAttributes' do
|
73
73
|
expected_attributes = [:resource_1, :attribute_1], [:resource_2, :attribute_2]
|
74
|
-
Permissioner::Matchers::ExactlyAllowAttributes.
|
75
|
-
matcher_helper.exactly_allow_attributes(*expected_attributes).
|
74
|
+
expect(Permissioner::Matchers::ExactlyAllowAttributes).to receive(:new).with(*expected_attributes).and_call_original
|
75
|
+
expect(matcher_helper.exactly_allow_attributes(*expected_attributes)).to be_kind_of(Permissioner::Matchers::ExactlyAllowAttributes)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
79
|
describe '#exactly_allow_controllers' do
|
80
80
|
|
81
81
|
it 'should return correctly instantiated instance of ExactlyAllowControllers' do
|
82
|
-
Permissioner::Matchers::ExactlyAllowControllers.
|
83
|
-
matcher_helper.exactly_allow_controllers(:controller_1, :controller_2).
|
82
|
+
expect(Permissioner::Matchers::ExactlyAllowControllers).to receive(:new).with(:controller_1, :controller_2).and_call_original
|
83
|
+
expect(matcher_helper.exactly_allow_controllers(:controller_1, :controller_2)).to be_kind_of(Permissioner::Matchers::ExactlyAllowControllers)
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
describe '#exactly_allow_resources' do
|
88
88
|
|
89
89
|
it 'should return correctly instantiated instance of ExactlyAllowControllers' do
|
90
|
-
Permissioner::Matchers::ExactlyAllowResources.
|
91
|
-
matcher_helper.exactly_allow_resources(:resource_1, :resource_2).
|
90
|
+
expect(Permissioner::Matchers::ExactlyAllowResources).to receive(:new).with(:resource_1, :resource_2).and_call_original
|
91
|
+
expect(matcher_helper.exactly_allow_resources(:resource_1, :resource_2)).to be_kind_of(Permissioner::Matchers::ExactlyAllowResources)
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
@@ -13,13 +13,13 @@ describe Permissioner::ServiceAdditions do
|
|
13
13
|
describe '#initialize' do
|
14
14
|
|
15
15
|
it 'should call configure_permissions' do
|
16
|
-
permission_service_class.
|
16
|
+
expect_any_instance_of(permission_service_class).to receive(:configure_permissions).once
|
17
17
|
permission_service_class.new
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'should set current_user' do
|
21
21
|
permission_service = permission_service_class.new 'current_user'
|
22
|
-
permission_service.current_user.
|
22
|
+
expect(permission_service.current_user).to eq 'current_user'
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -29,21 +29,21 @@ describe Permissioner::ServiceAdditions do
|
|
29
29
|
|
30
30
|
it 'should return true if action is allowed' do
|
31
31
|
permission_service.allow_actions :comments, :index
|
32
|
-
permission_service.allow_action?(:comments, :index).
|
32
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should return true for every action if all actions of controller are allowed' do
|
36
36
|
permission_service.allow_actions :comments, :all
|
37
|
-
permission_service.allow_action?(:comments, :index).
|
37
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should return true if allow_all is true' do
|
41
41
|
permission_service.allow_all
|
42
|
-
permission_service.allow_action?(:comments, :index).
|
42
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should return false if actions is not allowed' do
|
46
|
-
permission_service.allow_action?(:comments, :index).
|
46
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_falsey
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -52,48 +52,48 @@ describe Permissioner::ServiceAdditions do
|
|
52
52
|
it 'should return true if action is allowed and all filters return true' do
|
53
53
|
permission_service.allow_actions :comments, :index, &Proc.new { true }
|
54
54
|
permission_service.add_filter :comments, :index, &Proc.new { true }
|
55
|
-
permission_service.allow_action?(:comments, :index).
|
55
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'should return true for every action if all actions of controller are allowed and filters return true' do
|
59
59
|
permission_service.allow_actions :comments, :all
|
60
60
|
permission_service.add_filter :comments, :index, &Proc.new { true }
|
61
|
-
permission_service.allow_action?(:comments, :index).
|
61
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'should return true if allow_all is true but filters return false' do
|
65
65
|
permission_service.allow_all
|
66
66
|
permission_service.allow_actions :comments, :index, &Proc.new { false }
|
67
67
|
permission_service.add_filter :comments, :index, &Proc.new { false }
|
68
|
-
permission_service.allow_action?(:comments, :index).
|
68
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_truthy
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'should return false if given action allowed but filters return false' do
|
72
72
|
permission_service.allow_actions :comments, :index, &Proc.new { false }
|
73
|
-
permission_service.allow_action?(:comments, :index).
|
73
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_falsey
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'should return false if given action allowed but at least one filter returns false' do
|
77
77
|
permission_service.allow_actions :comments, :index, &Proc.new { true }
|
78
78
|
permission_service.add_filter :comments, :index, &Proc.new { false }
|
79
|
-
permission_service.allow_action?(:comments, :index).
|
79
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_falsey
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'should return false for every action if all actions of controller are allowed but filters return false' do
|
83
83
|
permission_service.allow_actions :comments, :all
|
84
84
|
permission_service.add_filter :comments, :index, &Proc.new { false }
|
85
|
-
permission_service.allow_action?(:comments, :index).
|
85
|
+
expect(permission_service.allow_action?(:comments, :index)).to be_falsey
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
89
|
it 'should also except arguments as string' do
|
90
90
|
permission_service.allow_actions :comments, :index
|
91
|
-
permission_service.allow_action?('comments', 'index').
|
91
|
+
expect(permission_service.allow_action?('comments', 'index')).to be_truthy
|
92
92
|
end
|
93
93
|
|
94
94
|
it 'should pass arguments to passed_filters? if action is principally allowed' do
|
95
95
|
permission_service.allow_actions :comments, :index
|
96
|
-
permission_service.
|
96
|
+
expect(permission_service).to receive(:passed_filters?).with(:comments, :index, resource: :resource, params: :params)
|
97
97
|
permission_service.allow_action?(:comments, :index, resource: :resource, params: :params)
|
98
98
|
end
|
99
99
|
end
|
@@ -102,51 +102,51 @@ describe Permissioner::ServiceAdditions do
|
|
102
102
|
|
103
103
|
it 'should return true when all blocks for given controller and action returns true' do
|
104
104
|
permission_service.add_filter(:comments, :create, &Proc.new { true })
|
105
|
-
permission_service.passed_filters?(:comments, :create).
|
105
|
+
expect(permission_service.passed_filters?(:comments, :create)).to be_truthy
|
106
106
|
end
|
107
107
|
|
108
108
|
it 'should return true when no filters are added at all' do
|
109
|
-
permission_service.passed_filters?(:comments, :create).
|
109
|
+
expect(permission_service.passed_filters?(:comments, :create)).to be_truthy
|
110
110
|
end
|
111
111
|
|
112
112
|
it 'should return true when for given controller and action no filters has been added' do
|
113
113
|
permission_service.add_filter(:comments, :update, &Proc.new {})
|
114
|
-
permission_service.passed_filters?(:comments, :create).
|
114
|
+
expect(permission_service.passed_filters?(:comments, :create)).to be_truthy
|
115
115
|
end
|
116
116
|
|
117
117
|
it 'should return false when at least one block for given controller and action returns false' do
|
118
118
|
permission_service.add_filter(:comments, :create, &Proc.new { true })
|
119
119
|
permission_service.add_filter(:comments, :create, &Proc.new { false })
|
120
120
|
permission_service.add_filter(:comments, :create, &Proc.new { true })
|
121
|
-
permission_service.passed_filters?(:comments, :create).
|
121
|
+
expect(permission_service.passed_filters?(:comments, :create)).to be_falsey
|
122
122
|
end
|
123
123
|
|
124
124
|
it 'should pass resource to the given block' do
|
125
125
|
resource = Object.new
|
126
|
-
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| r.
|
126
|
+
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| expect(r).to be resource })
|
127
127
|
permission_service.passed_filters?(:comments, :create, resource: resource)
|
128
128
|
end
|
129
129
|
|
130
130
|
it 'should pass params to the given block' do
|
131
131
|
params = Object.new
|
132
|
-
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| p.
|
132
|
+
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| expect(p).to be params })
|
133
133
|
permission_service.passed_filters?(:comments, :create, params: params)
|
134
134
|
end
|
135
135
|
|
136
136
|
it 'should set params to {} if not given' do
|
137
|
-
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| p.
|
137
|
+
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| expect(p).to eq({}) })
|
138
138
|
permission_service.passed_filters?(:comments, :create)
|
139
139
|
end
|
140
140
|
|
141
141
|
it 'should set params to {} if nil' do
|
142
|
-
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| p.
|
142
|
+
permission_service.add_filter(:comments, :create, &Proc.new { |r, p| expect(p).to eq({}) })
|
143
143
|
permission_service.passed_filters?(:comments, :create, params: nil)
|
144
144
|
end
|
145
145
|
|
146
146
|
it 'should set default values for params and current resource' do
|
147
147
|
block = Proc.new do |current_resource, params|
|
148
|
-
current_resource.
|
149
|
-
params.
|
148
|
+
expect(current_resource).to be_nil
|
149
|
+
expect(params).to eq({})
|
150
150
|
end
|
151
151
|
permission_service.add_filter(:comments, :create, &block)
|
152
152
|
permission_service.passed_filters?(:comments, :create)
|
@@ -157,16 +157,16 @@ describe Permissioner::ServiceAdditions do
|
|
157
157
|
|
158
158
|
it 'should return true when @allow_all is true' do
|
159
159
|
permission_service.allow_all
|
160
|
-
permission_service.allow_attribute?(:comment, :user_id).
|
160
|
+
expect(permission_service.allow_attribute?(:comment, :user_id)).to be_truthy
|
161
161
|
end
|
162
162
|
|
163
163
|
it 'should return true when attribute is allowed' do
|
164
164
|
permission_service.allow_attributes(:comment, :user_id)
|
165
|
-
permission_service.allow_attribute?(:comment, :user_id).
|
165
|
+
expect(permission_service.allow_attribute?(:comment, :user_id)).to be_truthy
|
166
166
|
end
|
167
167
|
|
168
168
|
it 'should return false when attribute not allowed' do
|
169
|
-
permission_service.allow_attribute?(:comment, :user_id).
|
169
|
+
expect(permission_service.allow_attribute?(:comment, :user_id)).to be_falsey
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
@@ -176,7 +176,7 @@ describe Permissioner::ServiceAdditions do
|
|
176
176
|
|
177
177
|
it 'should call permit! on given params when @allow_all is true' do
|
178
178
|
params = double('params')
|
179
|
-
params.
|
179
|
+
expect(params).to receive(:permit!)
|
180
180
|
permission_service.allow_all
|
181
181
|
permission_service.permit_params!(params)
|
182
182
|
end
|
@@ -195,11 +195,11 @@ describe Permissioner::ServiceAdditions do
|
|
195
195
|
|
196
196
|
permission_service.permit_params!(params)
|
197
197
|
|
198
|
-
params[:comment].
|
199
|
-
params[:comment].
|
200
|
-
params[:comment].
|
201
|
-
params[:post].
|
202
|
-
params[:post].
|
198
|
+
expect(params[:comment]).to be_permitted
|
199
|
+
expect(params[:comment]).to include :user_id, :text
|
200
|
+
expect(params[:comment]).not_to include :date
|
201
|
+
expect(params[:post]).to be_permitted
|
202
|
+
expect(params[:post]).to include :title, :content
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
@@ -209,7 +209,7 @@ describe Permissioner::ServiceAdditions do
|
|
209
209
|
|
210
210
|
it 'should set @allow_all to true' do
|
211
211
|
permission_service.allow_all
|
212
|
-
permission_service.instance_variable_get(:@allow_all).
|
212
|
+
expect(permission_service.instance_variable_get(:@allow_all)).to be_truthy
|
213
213
|
end
|
214
214
|
end
|
215
215
|
|
@@ -220,24 +220,24 @@ describe Permissioner::ServiceAdditions do
|
|
220
220
|
it 'should add actions to @allowed_actions when one action is given' do
|
221
221
|
permission_service.allow_actions :comments, :index
|
222
222
|
allowed_actions = permission_service.instance_variable_get(:@allowed_actions)
|
223
|
-
allowed_actions.count.
|
224
|
-
allowed_actions[['comments', 'index']].
|
223
|
+
expect(allowed_actions.count).to eq 1
|
224
|
+
expect(allowed_actions[['comments', 'index']]).to be_truthy
|
225
225
|
end
|
226
226
|
|
227
227
|
it 'should add actions to @allowed_actions when multiple actions are given' do
|
228
228
|
permission_service.allow_actions([:comments, :users], [:index, :create])
|
229
229
|
allowed_actions = permission_service.instance_variable_get(:@allowed_actions)
|
230
|
-
allowed_actions.count.
|
231
|
-
allowed_actions[['comments', 'index']].
|
232
|
-
allowed_actions[['comments', 'create']].
|
233
|
-
allowed_actions[['users', 'index']].
|
234
|
-
allowed_actions[['users', 'create']].
|
230
|
+
expect(allowed_actions.count).to eq 4
|
231
|
+
expect(allowed_actions[['comments', 'index']]).to be_truthy
|
232
|
+
expect(allowed_actions[['comments', 'create']]).to be_truthy
|
233
|
+
expect(allowed_actions[['users', 'index']]).to be_truthy
|
234
|
+
expect(allowed_actions[['users', 'create']]).to be_truthy
|
235
235
|
end
|
236
236
|
|
237
237
|
it 'should not add any filters' do
|
238
238
|
permission_service.allow_actions :comments, :index
|
239
239
|
filters = permission_service.instance_variable_get(:@filters)
|
240
|
-
filters.count.
|
240
|
+
expect(filters.count).to eq 0
|
241
241
|
end
|
242
242
|
end
|
243
243
|
|
@@ -247,19 +247,19 @@ describe Permissioner::ServiceAdditions do
|
|
247
247
|
block = Proc.new {}
|
248
248
|
permission_service.allow_actions :comments, :index, &block
|
249
249
|
filters = permission_service.instance_variable_get(:@filters)
|
250
|
-
filters.count.
|
251
|
-
filters[['comments', 'index']].
|
250
|
+
expect(filters.count).to eq 1
|
251
|
+
expect(filters[['comments', 'index']]).to eq [block]
|
252
252
|
end
|
253
253
|
|
254
254
|
it 'should add block to @filters when multiple actions are given' do
|
255
255
|
block = Proc.new {}
|
256
256
|
permission_service.allow_actions([:comments, :users], [:index, :create], &block)
|
257
257
|
filters = permission_service.instance_variable_get(:@filters)
|
258
|
-
filters.count.
|
259
|
-
filters[['comments', 'index']].
|
260
|
-
filters[['comments', 'create']].
|
261
|
-
filters[['users', 'index']].
|
262
|
-
filters[['users', 'create']].
|
258
|
+
expect(filters.count).to eq 4
|
259
|
+
expect(filters[['comments', 'index']]).to eq [block]
|
260
|
+
expect(filters[['comments', 'create']]).to eq [block]
|
261
|
+
expect(filters[['users', 'index']]).to eq [block]
|
262
|
+
expect(filters[['users', 'create']]).to eq [block]
|
263
263
|
end
|
264
264
|
|
265
265
|
it 'should add multiple blocks to @filters fore every actions' do
|
@@ -268,15 +268,15 @@ describe Permissioner::ServiceAdditions do
|
|
268
268
|
permission_service.allow_actions :comments, :index, &block_1
|
269
269
|
permission_service.allow_actions :comments, :index, &block_2
|
270
270
|
filters = permission_service.instance_variable_get(:@filters)
|
271
|
-
filters.count.
|
272
|
-
filters[['comments', 'index']].
|
271
|
+
expect(filters.count).to eq 1
|
272
|
+
expect(filters[['comments', 'index']]).to eq [block_1, block_2]
|
273
273
|
end
|
274
274
|
|
275
275
|
it 'should add actions to @allowed_actions' do
|
276
276
|
permission_service.allow_actions :comments, :index, &Proc.new {}
|
277
277
|
allowed_actions = permission_service.instance_variable_get(:@allowed_actions)
|
278
|
-
allowed_actions.count.
|
279
|
-
allowed_actions[['comments', 'index']].
|
278
|
+
expect(allowed_actions.count).to eq 1
|
279
|
+
expect(allowed_actions[['comments', 'index']]).to be_truthy
|
280
280
|
end
|
281
281
|
|
282
282
|
it 'should raise error if block is given for all actions' do
|
@@ -296,23 +296,23 @@ describe Permissioner::ServiceAdditions do
|
|
296
296
|
it 'should add resource and attribute to @allowed_params' do
|
297
297
|
permission_service.allow_attributes :comment, :text
|
298
298
|
allowed_params = permission_service.instance_variable_get(:@allowed_attributes)
|
299
|
-
allowed_params.count.
|
300
|
-
allowed_params[:comment].
|
299
|
+
expect(allowed_params.count).to eq 1
|
300
|
+
expect(allowed_params[:comment]).to eq [:text]
|
301
301
|
end
|
302
302
|
|
303
303
|
it 'should add resource and attributes to @allowed_params if multiple given' do
|
304
304
|
permission_service.allow_attributes [:comment, :post], [:user, :text]
|
305
305
|
allowed_params = permission_service.instance_variable_get(:@allowed_attributes)
|
306
|
-
allowed_params.count.
|
307
|
-
allowed_params[:comment].
|
308
|
-
allowed_params[:post].
|
306
|
+
expect(allowed_params.count).to eq 2
|
307
|
+
expect(allowed_params[:comment]).to eq [:user, :text]
|
308
|
+
expect(allowed_params[:post]).to eq [:user, :text]
|
309
309
|
end
|
310
310
|
|
311
311
|
it 'should add resource and attributes to @allowed_params if attributes is a Hash' do
|
312
312
|
permission_service.allow_attributes :comment, {attributes: [:user, :text]}
|
313
313
|
allowed_params = permission_service.instance_variable_get(:@allowed_attributes)
|
314
|
-
allowed_params.count.
|
315
|
-
allowed_params[:comment].
|
314
|
+
expect(allowed_params.count).to eq 1
|
315
|
+
expect(allowed_params[:comment]).to eq [{attributes: [:user, :text]}]
|
316
316
|
end
|
317
317
|
end
|
318
318
|
|
@@ -322,17 +322,17 @@ describe Permissioner::ServiceAdditions do
|
|
322
322
|
block = Proc.new {}
|
323
323
|
permission_service.add_filter(:comments, :create, &block)
|
324
324
|
filter_list = permission_service.instance_variable_get(:@filters)[['comments', 'create']]
|
325
|
-
filter_list.count.
|
326
|
-
filter_list.
|
325
|
+
expect(filter_list.count).to eq 1
|
326
|
+
expect(filter_list).to include block
|
327
327
|
end
|
328
328
|
|
329
329
|
it 'should add given block to @filters addressed by controller and action when multiple given' do
|
330
330
|
block = Proc.new {}
|
331
331
|
permission_service.add_filter([:comments, :posts], [:create, :update], &block)
|
332
|
-
permission_service.instance_variable_get(:@filters)[['comments', 'create']].
|
333
|
-
permission_service.instance_variable_get(:@filters)[['comments', 'update']].
|
334
|
-
permission_service.instance_variable_get(:@filters)[['posts', 'create']].
|
335
|
-
permission_service.instance_variable_get(:@filters)[['posts', 'update']].
|
332
|
+
expect(permission_service.instance_variable_get(:@filters)[['comments', 'create']]).to include block
|
333
|
+
expect(permission_service.instance_variable_get(:@filters)[['comments', 'update']]).to include block
|
334
|
+
expect(permission_service.instance_variable_get(:@filters)[['posts', 'create']]).to include block
|
335
|
+
expect(permission_service.instance_variable_get(:@filters)[['posts', 'update']]).to include block
|
336
336
|
end
|
337
337
|
|
338
338
|
it 'should add multiple blocks to @filters addressed by controller and action' do
|
@@ -341,9 +341,9 @@ describe Permissioner::ServiceAdditions do
|
|
341
341
|
permission_service.add_filter(:comments, :create, &block_1)
|
342
342
|
permission_service.add_filter(:comments, :create, &block_2)
|
343
343
|
filter_list = permission_service.instance_variable_get(:@filters)[['comments', 'create']]
|
344
|
-
filter_list.count.
|
345
|
-
filter_list.
|
346
|
-
filter_list.
|
344
|
+
expect(filter_list.count).to eq 2
|
345
|
+
expect(filter_list).to include block_1
|
346
|
+
expect(filter_list).to include block_2
|
347
347
|
end
|
348
348
|
|
349
349
|
it 'should rails exception when no block given' do
|
@@ -370,14 +370,14 @@ describe Permissioner::ServiceAdditions do
|
|
370
370
|
it 'should delete filters for actions if one controller and action are given' do
|
371
371
|
permission_service.clear_filters :users, :edit
|
372
372
|
filters = permission_service.instance_variable_get(:@filters)
|
373
|
-
filters.count.
|
374
|
-
filters.
|
373
|
+
expect(filters.count).to eq 3
|
374
|
+
expect(filters).not_to have_key([:users, :edit])
|
375
375
|
end
|
376
376
|
|
377
377
|
it 'should delete filters for actions if multiple controllers and actions are given' do
|
378
378
|
permission_service.clear_filters [:users, :comments], [:edit, :update]
|
379
379
|
filters = permission_service.instance_variable_get(:@filters)
|
380
|
-
filters.
|
380
|
+
expect(filters).to be_empty
|
381
381
|
end
|
382
382
|
|
383
383
|
end
|
@@ -386,18 +386,18 @@ describe Permissioner::ServiceAdditions do
|
|
386
386
|
|
387
387
|
it 'should set @filters to nil' do
|
388
388
|
permission_service.add_filter(:comments, :create, &Proc.new {})
|
389
|
-
permission_service.instance_variable_get(:@filters).
|
389
|
+
expect(permission_service.instance_variable_get(:@filters)).not_to be_nil
|
390
390
|
permission_service.clear_all_filters
|
391
|
-
permission_service.instance_variable_get(:@filters).
|
391
|
+
expect(permission_service.instance_variable_get(:@filters)).to be_nil
|
392
392
|
end
|
393
393
|
end
|
394
394
|
|
395
395
|
describe '#configure' do
|
396
396
|
|
397
397
|
it 'should create new instance of given configurer class' do
|
398
|
-
permission_service.
|
398
|
+
allow(permission_service).to receive(:current_user).and_return('current_user')
|
399
399
|
configurer_class = double('permission_configurer')
|
400
|
-
configurer_class.
|
400
|
+
expect(configurer_class).to receive(:new).with(permission_service, 'current_user')
|
401
401
|
permission_service.configure(configurer_class)
|
402
402
|
end
|
403
403
|
end
|