pundit 2.5.0 → 2.5.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/pundit/authorization.rb +18 -0
- data/lib/pundit/cache_store/legacy_store.rb +3 -0
- data/lib/pundit/cache_store/null_store.rb +3 -0
- data/lib/pundit/cache_store.rb +2 -0
- data/lib/pundit/context.rb +13 -0
- data/lib/pundit/error.rb +71 -0
- data/lib/pundit/helper.rb +16 -0
- data/lib/pundit/policy_finder.rb +11 -0
- data/lib/pundit/railtie.rb +1 -0
- data/lib/pundit/rspec.rb +2 -0
- data/lib/pundit/version.rb +1 -1
- data/lib/pundit.rb +10 -69
- metadata +4 -68
- data/.github/ISSUE_TEMPLATE/bug_report.md +0 -20
- data/.github/ISSUE_TEMPLATE/feature_request.md +0 -26
- data/.github/PULL_REQUEST_TEMPLATE/gem_release_template.md +0 -8
- data/.github/pull_request_template.md +0 -9
- data/.github/workflows/main.yml +0 -147
- data/.github/workflows/push_gem.yml +0 -33
- data/.gitignore +0 -19
- data/.rubocop.yml +0 -73
- data/.rubocop_ignore_git.yml +0 -7
- data/.yardopts +0 -1
- data/CODE_OF_CONDUCT.md +0 -28
- data/CONTRIBUTING.md +0 -31
- data/Gemfile +0 -28
- data/Rakefile +0 -21
- data/config/rubocop-rspec.yml +0 -5
- data/pundit.gemspec +0 -31
- data/spec/authorization_spec.rb +0 -331
- data/spec/generators_spec.rb +0 -43
- data/spec/policies/post_policy_spec.rb +0 -49
- data/spec/policy_finder_spec.rb +0 -191
- data/spec/pundit/helper_spec.rb +0 -18
- data/spec/pundit_spec.rb +0 -474
- data/spec/rspec_dsl_spec.rb +0 -81
- data/spec/simple_cov_check_action_formatter.rb +0 -79
- data/spec/spec_helper.rb +0 -35
- data/spec/support/lib/controller.rb +0 -38
- data/spec/support/lib/custom_cache.rb +0 -19
- data/spec/support/lib/instance_tracking.rb +0 -20
- data/spec/support/models/article.rb +0 -4
- data/spec/support/models/article_tag.rb +0 -7
- data/spec/support/models/artificial_blog.rb +0 -7
- data/spec/support/models/blog.rb +0 -4
- data/spec/support/models/comment.rb +0 -5
- data/spec/support/models/comment_four_five_six.rb +0 -5
- data/spec/support/models/comment_scope.rb +0 -13
- data/spec/support/models/comments_relation.rb +0 -15
- data/spec/support/models/customer/post.rb +0 -11
- data/spec/support/models/default_scope_contains_error.rb +0 -5
- data/spec/support/models/dummy_current_user.rb +0 -7
- data/spec/support/models/foo.rb +0 -4
- data/spec/support/models/post.rb +0 -25
- data/spec/support/models/post_four_five_six.rb +0 -9
- data/spec/support/models/project_one_two_three/avatar_four_five_six.rb +0 -7
- data/spec/support/models/project_one_two_three/tag_four_five_six.rb +0 -11
- data/spec/support/models/wiki.rb +0 -4
- data/spec/support/policies/article_tag_other_name_policy.rb +0 -13
- data/spec/support/policies/base_policy.rb +0 -23
- data/spec/support/policies/blog_policy.rb +0 -5
- data/spec/support/policies/comment_policy.rb +0 -11
- data/spec/support/policies/criteria_policy.rb +0 -5
- data/spec/support/policies/default_scope_contains_error_policy.rb +0 -10
- data/spec/support/policies/denier_policy.rb +0 -7
- data/spec/support/policies/dummy_current_user_policy.rb +0 -9
- data/spec/support/policies/nil_class_policy.rb +0 -17
- data/spec/support/policies/post_policy.rb +0 -36
- data/spec/support/policies/project/admin/comment_policy.rb +0 -15
- data/spec/support/policies/project/comment_policy.rb +0 -17
- data/spec/support/policies/project/criteria_policy.rb +0 -7
- data/spec/support/policies/project/post_policy.rb +0 -13
- data/spec/support/policies/project_one_two_three/avatar_four_five_six_policy.rb +0 -6
- data/spec/support/policies/project_one_two_three/comment_four_five_six_policy.rb +0 -6
- data/spec/support/policies/project_one_two_three/criteria_four_five_six_policy.rb +0 -6
- data/spec/support/policies/project_one_two_three/post_four_five_six_policy.rb +0 -6
- data/spec/support/policies/project_one_two_three/tag_four_five_six_policy.rb +0 -6
- data/spec/support/policies/publication_policy.rb +0 -13
- data/spec/support/policies/wiki_policy.rb +0 -8
data/spec/authorization_spec.rb
DELETED
@@ -1,331 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
require "action_controller/metal/strong_parameters"
|
5
|
-
|
6
|
-
describe Pundit::Authorization do
|
7
|
-
def to_params(*args, **kwargs, &block)
|
8
|
-
ActionController::Parameters.new(*args, **kwargs, &block)
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:controller) { Controller.new(user, "update", to_params({})) }
|
12
|
-
let(:user) { double("user") }
|
13
|
-
let(:post) { Post.new(user) }
|
14
|
-
let(:comment) { Comment.new }
|
15
|
-
let(:article) { Article.new }
|
16
|
-
let(:article_tag) { ArticleTag.new }
|
17
|
-
let(:wiki) { Wiki.new }
|
18
|
-
|
19
|
-
describe "#verify_authorized" do
|
20
|
-
it "does nothing when authorized" do
|
21
|
-
controller.authorize(post)
|
22
|
-
controller.verify_authorized
|
23
|
-
end
|
24
|
-
|
25
|
-
it "raises an exception when not authorized" do
|
26
|
-
expect { controller.verify_authorized }.to raise_error(Pundit::AuthorizationNotPerformedError)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe "#verify_policy_scoped" do
|
31
|
-
it "does nothing when policy_scope is used" do
|
32
|
-
controller.policy_scope(Post)
|
33
|
-
controller.verify_policy_scoped
|
34
|
-
end
|
35
|
-
|
36
|
-
it "raises an exception when policy_scope is not used" do
|
37
|
-
expect { controller.verify_policy_scoped }.to raise_error(Pundit::PolicyScopingNotPerformedError)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "#pundit_policy_authorized?" do
|
42
|
-
it "is true when authorized" do
|
43
|
-
controller.authorize(post)
|
44
|
-
expect(controller.pundit_policy_authorized?).to be true
|
45
|
-
end
|
46
|
-
|
47
|
-
it "is false when not authorized" do
|
48
|
-
expect(controller.pundit_policy_authorized?).to be false
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "#pundit_policy_scoped?" do
|
53
|
-
it "is true when policy_scope is used" do
|
54
|
-
controller.policy_scope(Post)
|
55
|
-
expect(controller.pundit_policy_scoped?).to be true
|
56
|
-
end
|
57
|
-
|
58
|
-
it "is false when policy scope is not used" do
|
59
|
-
expect(controller.pundit_policy_scoped?).to be false
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "#authorize" do
|
64
|
-
it "infers the policy name and authorizes based on it" do
|
65
|
-
expect(controller.authorize(post)).to be_truthy
|
66
|
-
end
|
67
|
-
|
68
|
-
it "returns the record on successful authorization" do
|
69
|
-
expect(controller.authorize(post)).to eq(post)
|
70
|
-
end
|
71
|
-
|
72
|
-
it "returns the record when passed record with namespace " do
|
73
|
-
expect(controller.authorize([:project, comment], :update?)).to eq(comment)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "returns the record when passed record with nested namespace " do
|
77
|
-
expect(controller.authorize([:project, :admin, comment], :update?)).to eq(comment)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "returns the policy name symbol when passed record with headless policy" do
|
81
|
-
expect(controller.authorize(:publication, :create?)).to eq(:publication)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "returns the class when passed record not a particular instance" do
|
85
|
-
expect(controller.authorize(Post, :show?)).to eq(Post)
|
86
|
-
end
|
87
|
-
|
88
|
-
it "can be given a different permission to check" do
|
89
|
-
expect(controller.authorize(post, :show?)).to be_truthy
|
90
|
-
expect { controller.authorize(post, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "can be given a different policy class" do
|
94
|
-
expect(controller.authorize(post, :create?, policy_class: PublicationPolicy)).to be_truthy
|
95
|
-
end
|
96
|
-
|
97
|
-
it "works with anonymous class policies" do
|
98
|
-
expect(controller.authorize(article_tag, :show?)).to be_truthy
|
99
|
-
expect { controller.authorize(article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "throws an exception when the permission check fails" do
|
103
|
-
expect { controller.authorize(Post.new) }.to raise_error(Pundit::NotAuthorizedError)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "throws an exception when a policy cannot be found" do
|
107
|
-
expect { controller.authorize(Article) }.to raise_error(Pundit::NotDefinedError)
|
108
|
-
end
|
109
|
-
|
110
|
-
it "caches the policy" do
|
111
|
-
expect(controller.policies[post]).to be_nil
|
112
|
-
controller.authorize(post)
|
113
|
-
expect(controller.policies[post]).not_to be_nil
|
114
|
-
end
|
115
|
-
|
116
|
-
it "raises an error when the given record is nil" do
|
117
|
-
expect { controller.authorize(nil, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
|
118
|
-
end
|
119
|
-
|
120
|
-
it "raises an error with a invalid policy constructor" do
|
121
|
-
expect { controller.authorize(wiki, :destroy?) }.to raise_error(Pundit::InvalidConstructorError)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
describe "#skip_authorization" do
|
126
|
-
it "disables authorization verification" do
|
127
|
-
controller.skip_authorization
|
128
|
-
expect { controller.verify_authorized }.not_to raise_error
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe "#skip_policy_scope" do
|
133
|
-
it "disables policy scope verification" do
|
134
|
-
controller.skip_policy_scope
|
135
|
-
expect { controller.verify_policy_scoped }.not_to raise_error
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe "#pundit_user" do
|
140
|
-
it "returns the same thing as current_user" do
|
141
|
-
expect(controller.pundit_user).to eq controller.current_user
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
describe "#policy" do
|
146
|
-
it "returns an instantiated policy" do
|
147
|
-
policy = controller.policy(post)
|
148
|
-
expect(policy.user).to eq user
|
149
|
-
expect(policy.post).to eq post
|
150
|
-
end
|
151
|
-
|
152
|
-
it "throws an exception if the given policy can't be found" do
|
153
|
-
expect { controller.policy(article) }.to raise_error(Pundit::NotDefinedError)
|
154
|
-
end
|
155
|
-
|
156
|
-
it "raises an error with a invalid policy constructor" do
|
157
|
-
expect { controller.policy(wiki) }.to raise_error(Pundit::InvalidConstructorError)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "allows policy to be injected" do
|
161
|
-
new_policy = double
|
162
|
-
controller.policies[post] = new_policy
|
163
|
-
|
164
|
-
expect(controller.policy(post)).to eq new_policy
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
describe "#policy_scope" do
|
169
|
-
it "returns an instantiated policy scope" do
|
170
|
-
expect(controller.policy_scope(Post)).to eq :published
|
171
|
-
end
|
172
|
-
|
173
|
-
it "allows policy scope class to be overridden" do
|
174
|
-
expect(controller.policy_scope(Post, policy_scope_class: PublicationPolicy::Scope)).to eq :published
|
175
|
-
end
|
176
|
-
|
177
|
-
it "throws an exception if the given policy can't be found" do
|
178
|
-
expect { controller.policy_scope(Article) }.to raise_error(Pundit::NotDefinedError)
|
179
|
-
end
|
180
|
-
|
181
|
-
it "raises an error with a invalid policy scope constructor" do
|
182
|
-
expect { controller.policy_scope(Wiki) }.to raise_error(Pundit::InvalidConstructorError)
|
183
|
-
end
|
184
|
-
|
185
|
-
it "allows policy_scope to be injected" do
|
186
|
-
new_scope = double
|
187
|
-
controller.policy_scopes[Post] = new_scope
|
188
|
-
|
189
|
-
expect(controller.policy_scope(Post)).to eq new_scope
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
describe "#permitted_attributes" do
|
194
|
-
it "checks policy for permitted attributes" do
|
195
|
-
params = to_params(
|
196
|
-
post: {
|
197
|
-
title: "Hello",
|
198
|
-
votes: 5,
|
199
|
-
admin: true
|
200
|
-
}
|
201
|
-
)
|
202
|
-
|
203
|
-
action = "update"
|
204
|
-
|
205
|
-
expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq(
|
206
|
-
"title" => "Hello",
|
207
|
-
"votes" => 5
|
208
|
-
)
|
209
|
-
expect(Controller.new(double, action, params).permitted_attributes(post).to_h).to eq("votes" => 5)
|
210
|
-
end
|
211
|
-
|
212
|
-
it "checks policy for permitted attributes for record of a ActiveModel type" do
|
213
|
-
customer_post = Customer::Post.new(user)
|
214
|
-
params = to_params(
|
215
|
-
customer_post: {
|
216
|
-
title: "Hello",
|
217
|
-
votes: 5,
|
218
|
-
admin: true
|
219
|
-
}
|
220
|
-
)
|
221
|
-
|
222
|
-
action = "update"
|
223
|
-
|
224
|
-
expect(Controller.new(user, action, params).permitted_attributes(customer_post).to_h).to eq(
|
225
|
-
"title" => "Hello",
|
226
|
-
"votes" => 5
|
227
|
-
)
|
228
|
-
expect(Controller.new(double, action, params).permitted_attributes(customer_post).to_h).to eq(
|
229
|
-
"votes" => 5
|
230
|
-
)
|
231
|
-
end
|
232
|
-
|
233
|
-
it "goes through the policy cache" do
|
234
|
-
params = to_params(post: { title: "Hello" })
|
235
|
-
user = double
|
236
|
-
post = Post.new(user)
|
237
|
-
controller = Controller.new(user, "update", params)
|
238
|
-
|
239
|
-
expect do
|
240
|
-
expect(controller.permitted_attributes(post)).to be_truthy
|
241
|
-
expect(controller.permitted_attributes(post)).to be_truthy
|
242
|
-
end.to change { PostPolicy.instances }.by(1)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
describe "#permitted_attributes_for_action" do
|
247
|
-
it "is checked if it is defined in the policy" do
|
248
|
-
params = to_params(
|
249
|
-
post: {
|
250
|
-
title: "Hello",
|
251
|
-
body: "blah",
|
252
|
-
votes: 5,
|
253
|
-
admin: true
|
254
|
-
}
|
255
|
-
)
|
256
|
-
|
257
|
-
action = "revise"
|
258
|
-
expect(Controller.new(user, action, params).permitted_attributes(post).to_h).to eq("body" => "blah")
|
259
|
-
end
|
260
|
-
|
261
|
-
it "can be explicitly set" do
|
262
|
-
params = to_params(
|
263
|
-
post: {
|
264
|
-
title: "Hello",
|
265
|
-
body: "blah",
|
266
|
-
votes: 5,
|
267
|
-
admin: true
|
268
|
-
}
|
269
|
-
)
|
270
|
-
|
271
|
-
action = "update"
|
272
|
-
expect(Controller.new(user, action, params).permitted_attributes(post, :revise).to_h).to eq("body" => "blah")
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
describe "#pundit_reset!" do
|
277
|
-
it "allows authorize to react to a user change" do
|
278
|
-
expect(controller.authorize(post)).to be_truthy
|
279
|
-
|
280
|
-
controller.current_user = double
|
281
|
-
controller.pundit_reset!
|
282
|
-
expect { controller.authorize(post) }.to raise_error(Pundit::NotAuthorizedError)
|
283
|
-
end
|
284
|
-
|
285
|
-
it "allows policy to react to a user change" do
|
286
|
-
expect(controller.policy(DummyCurrentUser).user).to be user
|
287
|
-
|
288
|
-
new_user = double("new user")
|
289
|
-
controller.current_user = new_user
|
290
|
-
controller.pundit_reset!
|
291
|
-
expect(controller.policy(DummyCurrentUser).user).to be new_user
|
292
|
-
end
|
293
|
-
|
294
|
-
it "allows policy scope to react to a user change" do
|
295
|
-
expect(controller.policy_scope(DummyCurrentUser)).to be user
|
296
|
-
|
297
|
-
new_user = double("new user")
|
298
|
-
controller.current_user = new_user
|
299
|
-
controller.pundit_reset!
|
300
|
-
expect(controller.policy_scope(DummyCurrentUser)).to be new_user
|
301
|
-
end
|
302
|
-
|
303
|
-
it "resets the pundit context" do
|
304
|
-
expect(controller.pundit.user).to be(user)
|
305
|
-
|
306
|
-
new_user = double
|
307
|
-
controller.current_user = new_user
|
308
|
-
expect { controller.pundit_reset! }.to change { controller.pundit.user }.from(user).to(new_user)
|
309
|
-
end
|
310
|
-
|
311
|
-
it "clears pundit_policy_authorized? flag" do
|
312
|
-
expect(controller.pundit_policy_authorized?).to be false
|
313
|
-
|
314
|
-
controller.skip_authorization
|
315
|
-
expect(controller.pundit_policy_authorized?).to be true
|
316
|
-
|
317
|
-
controller.pundit_reset!
|
318
|
-
expect(controller.pundit_policy_authorized?).to be false
|
319
|
-
end
|
320
|
-
|
321
|
-
it "clears pundit_policy_scoped? flag" do
|
322
|
-
expect(controller.pundit_policy_scoped?).to be false
|
323
|
-
|
324
|
-
controller.skip_policy_scope
|
325
|
-
expect(controller.pundit_policy_scoped?).to be true
|
326
|
-
|
327
|
-
controller.pundit_reset!
|
328
|
-
expect(controller.pundit_policy_scoped?).to be false
|
329
|
-
end
|
330
|
-
end
|
331
|
-
end
|
data/spec/generators_spec.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
require "tmpdir"
|
5
|
-
|
6
|
-
require "rails/generators"
|
7
|
-
require "generators/pundit/install/install_generator"
|
8
|
-
require "generators/pundit/policy/policy_generator"
|
9
|
-
|
10
|
-
RSpec.describe "generators" do
|
11
|
-
before(:all) do
|
12
|
-
@tmpdir = Dir.mktmpdir
|
13
|
-
|
14
|
-
Dir.chdir(@tmpdir) do
|
15
|
-
Pundit::Generators::InstallGenerator.new([], { quiet: true }).invoke_all
|
16
|
-
Pundit::Generators::PolicyGenerator.new(%w[Widget], { quiet: true }).invoke_all
|
17
|
-
|
18
|
-
require "./app/policies/application_policy"
|
19
|
-
require "./app/policies/widget_policy"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
after(:all) do
|
24
|
-
FileUtils.remove_entry(@tmpdir)
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "WidgetPolicy", type: :policy do
|
28
|
-
permissions :index?, :show?, :create?, :new?, :update?, :edit?, :destroy? do
|
29
|
-
it "has safe defaults" do
|
30
|
-
expect(WidgetPolicy).not_to permit(double("User"), double("Widget"))
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "WidgetPolicy::Scope" do
|
35
|
-
describe "#resolve" do
|
36
|
-
it "raises a descriptive error" do
|
37
|
-
scope = WidgetPolicy::Scope.new(double("User"), double("User.all"))
|
38
|
-
expect { scope.resolve }.to raise_error(NoMethodError, /WidgetPolicy::Scope/)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe PostPolicy do
|
6
|
-
let(:user) { double }
|
7
|
-
let(:own_post) { double(user: user) }
|
8
|
-
let(:other_post) { double(user: double) }
|
9
|
-
subject { described_class }
|
10
|
-
|
11
|
-
permissions :update?, :show? do
|
12
|
-
it "is successful when all permissions match" do
|
13
|
-
should permit(user, own_post)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "fails when any permissions do not match" do
|
17
|
-
expect do
|
18
|
-
should permit(user, other_post)
|
19
|
-
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "uses the default description if not overridden" do
|
23
|
-
expect(permit(user, own_post).description).to eq("permit #{user.inspect} and #{own_post.inspect}")
|
24
|
-
end
|
25
|
-
|
26
|
-
context "when the matcher description is overridden" do
|
27
|
-
after do
|
28
|
-
Pundit::RSpec::Matchers.description = nil
|
29
|
-
end
|
30
|
-
|
31
|
-
it "sets a custom matcher description with a Proc" do
|
32
|
-
allow(user).to receive(:role).and_return("default_role")
|
33
|
-
allow(own_post).to receive(:id).and_return(1)
|
34
|
-
|
35
|
-
Pundit::RSpec::Matchers.description = lambda { |user, record|
|
36
|
-
"permit user with role #{user.role} to access record with ID #{record.id}"
|
37
|
-
}
|
38
|
-
|
39
|
-
description = permit(user, own_post).description
|
40
|
-
expect(description).to eq("permit user with role default_role to access record with ID 1")
|
41
|
-
end
|
42
|
-
|
43
|
-
it "sets a custom matcher description with a string" do
|
44
|
-
Pundit::RSpec::Matchers.description = "permit user"
|
45
|
-
expect(permit(user, own_post).description).to eq("permit user")
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/spec/policy_finder_spec.rb
DELETED
@@ -1,191 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe Pundit::PolicyFinder do
|
6
|
-
let(:user) { double }
|
7
|
-
let(:post) { Post.new(user) }
|
8
|
-
let(:comment) { CommentFourFiveSix.new }
|
9
|
-
let(:article) { Article.new }
|
10
|
-
|
11
|
-
describe "SUFFIX" do
|
12
|
-
specify { expect(described_class::SUFFIX).to eq "Policy" }
|
13
|
-
specify { expect(Pundit::SUFFIX).to eq(described_class::SUFFIX) }
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "#scope" do
|
17
|
-
subject { described_class.new(post) }
|
18
|
-
|
19
|
-
it "returns a policy scope" do
|
20
|
-
expect(subject.scope).to eq PostPolicy::Scope
|
21
|
-
end
|
22
|
-
|
23
|
-
context "policy is nil" do
|
24
|
-
it "returns nil" do
|
25
|
-
allow(subject).to receive(:policy).and_return nil
|
26
|
-
expect(subject.scope).to eq nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "#policy" do
|
32
|
-
context "with an instance" do
|
33
|
-
it "returns the associated policy" do
|
34
|
-
object = described_class.new(post)
|
35
|
-
|
36
|
-
expect(object.policy).to eq PostPolicy
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "with an array of symbols" do
|
41
|
-
it "returns the associated namespaced policy" do
|
42
|
-
object = described_class.new(%i[project post])
|
43
|
-
|
44
|
-
expect(object.policy).to eq Project::PostPolicy
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "with an array of a symbol and an instance" do
|
49
|
-
it "returns the associated namespaced policy" do
|
50
|
-
object = described_class.new([:project, post])
|
51
|
-
|
52
|
-
expect(object.policy).to eq Project::PostPolicy
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "with an array of a symbol and a class with a specified policy class" do
|
57
|
-
it "returns the associated namespaced policy" do
|
58
|
-
object = described_class.new([:project, Customer::Post])
|
59
|
-
|
60
|
-
expect(object.policy).to eq Project::PostPolicy
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context "with an array of a symbol and a class with a specified model name" do
|
65
|
-
it "returns the associated namespaced policy" do
|
66
|
-
object = described_class.new([:project, CommentsRelation])
|
67
|
-
|
68
|
-
expect(object.policy).to eq Project::CommentPolicy
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
context "with a class" do
|
73
|
-
it "returns the associated policy" do
|
74
|
-
object = described_class.new(Post)
|
75
|
-
|
76
|
-
expect(object.policy).to eq PostPolicy
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context "with a class which has a specified policy class" do
|
81
|
-
it "returns the associated policy" do
|
82
|
-
object = described_class.new(Customer::Post)
|
83
|
-
|
84
|
-
expect(object.policy).to eq PostPolicy
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "with an instance which has a specified policy class" do
|
89
|
-
it "returns the associated policy" do
|
90
|
-
object = described_class.new(Customer::Post.new(user))
|
91
|
-
|
92
|
-
expect(object.policy).to eq PostPolicy
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context "with a class which has a specified model name" do
|
97
|
-
it "returns the associated policy" do
|
98
|
-
object = described_class.new(CommentsRelation)
|
99
|
-
|
100
|
-
expect(object.policy).to eq CommentPolicy
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
context "with an instance which has a specified policy class" do
|
105
|
-
it "returns the associated policy" do
|
106
|
-
object = described_class.new(CommentsRelation.new)
|
107
|
-
|
108
|
-
expect(object.policy).to eq CommentPolicy
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context "with nil" do
|
113
|
-
it "returns a NilClassPolicy" do
|
114
|
-
object = described_class.new(nil)
|
115
|
-
|
116
|
-
expect(object.policy).to eq NilClassPolicy
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "with a class that doesn't have an associated policy" do
|
121
|
-
it "returns nil" do
|
122
|
-
object = described_class.new(Foo)
|
123
|
-
|
124
|
-
expect(object.policy).to eq nil
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
describe "#scope!" do
|
130
|
-
context "@object is nil" do
|
131
|
-
subject { described_class.new(nil) }
|
132
|
-
|
133
|
-
it "returns the NilClass policy's scope class" do
|
134
|
-
expect(subject.scope!).to eq NilClassPolicy::Scope
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
context "@object is defined" do
|
139
|
-
subject { described_class.new(post) }
|
140
|
-
|
141
|
-
it "returns the scope" do
|
142
|
-
expect(subject.scope!).to eq PostPolicy::Scope
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
describe "#param_key" do
|
148
|
-
context "object responds to model_name" do
|
149
|
-
subject { described_class.new(comment) }
|
150
|
-
|
151
|
-
it "returns the param_key" do
|
152
|
-
expect(subject.object).to respond_to(:model_name)
|
153
|
-
expect(subject.param_key).to eq "comment_four_five_six"
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
context "object is a class" do
|
158
|
-
subject { described_class.new(Article) }
|
159
|
-
|
160
|
-
it "returns the param_key" do
|
161
|
-
expect(subject.object).not_to respond_to(:model_name)
|
162
|
-
expect(subject.object).to be_a Class
|
163
|
-
expect(subject.param_key).to eq "article"
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
context "object is an instance of a class" do
|
168
|
-
subject { described_class.new(article) }
|
169
|
-
|
170
|
-
it "returns the param_key" do
|
171
|
-
expect(subject.object).not_to respond_to(:model_name)
|
172
|
-
expect(subject.object).not_to be_a Class
|
173
|
-
expect(subject.object).to be_an_instance_of Article
|
174
|
-
|
175
|
-
expect(subject.param_key).to eq "article"
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
context "object is an array" do
|
180
|
-
subject { described_class.new([:project, article]) }
|
181
|
-
|
182
|
-
it "returns the param_key for the last element of the array" do
|
183
|
-
expect(subject.object).not_to respond_to(:model_name)
|
184
|
-
expect(subject.object).not_to be_a Class
|
185
|
-
expect(subject.object).to be_an_instance_of Array
|
186
|
-
|
187
|
-
expect(subject.param_key).to eq "article"
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
data/spec/pundit/helper_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe Pundit::Helper do
|
6
|
-
let(:user) { double }
|
7
|
-
let(:controller) { Controller.new(user, "update", double) }
|
8
|
-
let(:view) { Controller::View.new(controller) }
|
9
|
-
|
10
|
-
describe "#policy_scope" do
|
11
|
-
it "doesn't flip pundit_policy_scoped?" do
|
12
|
-
scoped = view.policy_scope(Post)
|
13
|
-
|
14
|
-
expect(scoped).to be(Post.published)
|
15
|
-
expect(controller).not_to be_pundit_policy_scoped
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|