pundit 2.4.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -42
  3. data/README.md +31 -1
  4. data/lib/generators/pundit/install/install_generator.rb +3 -1
  5. data/lib/generators/pundit/policy/policy_generator.rb +3 -1
  6. data/lib/generators/rspec/policy_generator.rb +4 -1
  7. data/lib/generators/test_unit/policy_generator.rb +4 -1
  8. data/lib/pundit/authorization.rb +170 -77
  9. data/lib/pundit/cache_store/legacy_store.rb +10 -0
  10. data/lib/pundit/cache_store/null_store.rb +12 -0
  11. data/lib/pundit/cache_store.rb +24 -0
  12. data/lib/pundit/context.rb +89 -26
  13. data/lib/pundit/error.rb +71 -0
  14. data/lib/pundit/helper.rb +16 -0
  15. data/lib/pundit/policy_finder.rb +33 -1
  16. data/lib/pundit/railtie.rb +20 -0
  17. data/lib/pundit/rspec.rb +69 -6
  18. data/lib/pundit/version.rb +2 -1
  19. data/lib/pundit.rb +27 -61
  20. metadata +19 -179
  21. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -20
  22. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -26
  23. data/.github/PULL_REQUEST_TEMPLATE/gem_release_template.md +0 -8
  24. data/.github/pull_request_template.md +0 -9
  25. data/.github/workflows/main.yml +0 -112
  26. data/.github/workflows/push_gem.yml +0 -33
  27. data/.gitignore +0 -19
  28. data/.rubocop.yml +0 -63
  29. data/.yardopts +0 -1
  30. data/CODE_OF_CONDUCT.md +0 -28
  31. data/CONTRIBUTING.md +0 -31
  32. data/Gemfile +0 -8
  33. data/Rakefile +0 -20
  34. data/config/rubocop-rspec.yml +0 -5
  35. data/pundit.gemspec +0 -35
  36. data/spec/authorization_spec.rb +0 -274
  37. data/spec/dsl_spec.rb +0 -30
  38. data/spec/generators_spec.rb +0 -43
  39. data/spec/policies/post_policy_spec.rb +0 -49
  40. data/spec/policy_finder_spec.rb +0 -187
  41. data/spec/pundit_spec.rb +0 -448
  42. data/spec/spec_helper.rb +0 -352
  43. /data/lib/generators/pundit/install/templates/{application_policy.rb → application_policy.rb.tt} +0 -0
  44. /data/lib/generators/pundit/policy/templates/{policy.rb → policy.rb.tt} +0 -0
  45. /data/lib/generators/rspec/templates/{policy_spec.rb → policy_spec.rb.tt} +0 -0
  46. /data/lib/generators/test_unit/templates/{policy_test.rb → policy_test.rb.tt} +0 -0
@@ -1,187 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- class Foo; end
6
- RSpec.describe Pundit::PolicyFinder do
7
- let(:user) { double }
8
- let(:post) { Post.new(user) }
9
- let(:comment) { CommentFourFiveSix.new }
10
- let(:article) { Article.new }
11
-
12
- describe "#scope" do
13
- subject { described_class.new(post) }
14
-
15
- it "returns a policy scope" do
16
- expect(subject.scope).to eq PostPolicy::Scope
17
- end
18
-
19
- context "policy is nil" do
20
- it "returns nil" do
21
- allow(subject).to receive(:policy).and_return nil
22
- expect(subject.scope).to eq nil
23
- end
24
- end
25
- end
26
-
27
- describe "#policy" do
28
- context "with an instance" do
29
- it "returns the associated policy" do
30
- object = described_class.new(post)
31
-
32
- expect(object.policy).to eq PostPolicy
33
- end
34
- end
35
-
36
- context "with an array of symbols" do
37
- it "returns the associated namespaced policy" do
38
- object = described_class.new(%i[project post])
39
-
40
- expect(object.policy).to eq Project::PostPolicy
41
- end
42
- end
43
-
44
- context "with an array of a symbol and an instance" do
45
- it "returns the associated namespaced policy" do
46
- object = described_class.new([:project, post])
47
-
48
- expect(object.policy).to eq Project::PostPolicy
49
- end
50
- end
51
-
52
- context "with an array of a symbol and a class with a specified policy class" do
53
- it "returns the associated namespaced policy" do
54
- object = described_class.new([:project, Customer::Post])
55
-
56
- expect(object.policy).to eq Project::PostPolicy
57
- end
58
- end
59
-
60
- context "with an array of a symbol and a class with a specified model name" do
61
- it "returns the associated namespaced policy" do
62
- object = described_class.new([:project, CommentsRelation])
63
-
64
- expect(object.policy).to eq Project::CommentPolicy
65
- end
66
- end
67
-
68
- context "with a class" do
69
- it "returns the associated policy" do
70
- object = described_class.new(Post)
71
-
72
- expect(object.policy).to eq PostPolicy
73
- end
74
- end
75
-
76
- context "with a class which has a specified policy class" do
77
- it "returns the associated policy" do
78
- object = described_class.new(Customer::Post)
79
-
80
- expect(object.policy).to eq PostPolicy
81
- end
82
- end
83
-
84
- context "with an instance which has a specified policy class" do
85
- it "returns the associated policy" do
86
- object = described_class.new(Customer::Post.new(user))
87
-
88
- expect(object.policy).to eq PostPolicy
89
- end
90
- end
91
-
92
- context "with a class which has a specified model name" do
93
- it "returns the associated policy" do
94
- object = described_class.new(CommentsRelation)
95
-
96
- expect(object.policy).to eq CommentPolicy
97
- end
98
- end
99
-
100
- context "with an instance which has a specified policy class" do
101
- it "returns the associated policy" do
102
- object = described_class.new(CommentsRelation.new)
103
-
104
- expect(object.policy).to eq CommentPolicy
105
- end
106
- end
107
-
108
- context "with nil" do
109
- it "returns a NilClassPolicy" do
110
- object = described_class.new(nil)
111
-
112
- expect(object.policy).to eq NilClassPolicy
113
- end
114
- end
115
-
116
- context "with a class that doesn't have an associated policy" do
117
- it "returns nil" do
118
- object = described_class.new(Foo)
119
-
120
- expect(object.policy).to eq nil
121
- end
122
- end
123
- end
124
-
125
- describe "#scope!" do
126
- context "@object is nil" do
127
- subject { described_class.new(nil) }
128
-
129
- it "returns the NilClass policy's scope class" do
130
- expect(subject.scope!).to eq NilClassPolicy::Scope
131
- end
132
- end
133
-
134
- context "@object is defined" do
135
- subject { described_class.new(post) }
136
-
137
- it "returns the scope" do
138
- expect(subject.scope!).to eq PostPolicy::Scope
139
- end
140
- end
141
- end
142
-
143
- describe "#param_key" do
144
- context "object responds to model_name" do
145
- subject { described_class.new(comment) }
146
-
147
- it "returns the param_key" do
148
- expect(subject.object).to respond_to(:model_name)
149
- expect(subject.param_key).to eq "comment_four_five_six"
150
- end
151
- end
152
-
153
- context "object is a class" do
154
- subject { described_class.new(Article) }
155
-
156
- it "returns the param_key" do
157
- expect(subject.object).not_to respond_to(:model_name)
158
- expect(subject.object).to be_a Class
159
- expect(subject.param_key).to eq "article"
160
- end
161
- end
162
-
163
- context "object is an instance of a class" do
164
- subject { described_class.new(article) }
165
-
166
- it "returns the param_key" do
167
- expect(subject.object).not_to respond_to(:model_name)
168
- expect(subject.object).not_to be_a Class
169
- expect(subject.object).to be_an_instance_of Article
170
-
171
- expect(subject.param_key).to eq "article"
172
- end
173
- end
174
-
175
- context "object is an array" do
176
- subject { described_class.new([:project, article]) }
177
-
178
- it "returns the param_key for the last element of the array" do
179
- expect(subject.object).not_to respond_to(:model_name)
180
- expect(subject.object).not_to be_a Class
181
- expect(subject.object).to be_an_instance_of Array
182
-
183
- expect(subject.param_key).to eq "article"
184
- end
185
- end
186
- end
187
- end
data/spec/pundit_spec.rb DELETED
@@ -1,448 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Pundit do
6
- let(:user) { double }
7
- let(:post) { Post.new(user) }
8
- let(:customer_post) { Customer::Post.new(user) }
9
- let(:post_four_five_six) { PostFourFiveSix.new(user) }
10
- let(:comment) { Comment.new }
11
- let(:comment_four_five_six) { CommentFourFiveSix.new }
12
- let(:article) { Article.new }
13
- let(:artificial_blog) { ArtificialBlog.new }
14
- let(:article_tag) { ArticleTag.new }
15
- let(:comments_relation) { CommentsRelation.new(empty: false) }
16
- let(:empty_comments_relation) { CommentsRelation.new(empty: true) }
17
- let(:tag_four_five_six) { ProjectOneTwoThree::TagFourFiveSix.new(user) }
18
- let(:avatar_four_five_six) { ProjectOneTwoThree::AvatarFourFiveSix.new }
19
- let(:wiki) { Wiki.new }
20
-
21
- describe ".authorize" do
22
- it "infers the policy and authorizes based on it" do
23
- expect(Pundit.authorize(user, post, :update?)).to be_truthy
24
- end
25
-
26
- it "returns the record on successful authorization" do
27
- expect(Pundit.authorize(user, post, :update?)).to eq(post)
28
- end
29
-
30
- it "returns the record when passed record with namespace " do
31
- expect(Pundit.authorize(user, [:project, comment], :update?)).to eq(comment)
32
- end
33
-
34
- it "returns the record when passed record with nested namespace " do
35
- expect(Pundit.authorize(user, [:project, :admin, comment], :update?)).to eq(comment)
36
- end
37
-
38
- it "returns the policy name symbol when passed record with headless policy" do
39
- expect(Pundit.authorize(user, :publication, :create?)).to eq(:publication)
40
- end
41
-
42
- it "returns the class when passed record not a particular instance" do
43
- expect(Pundit.authorize(user, Post, :show?)).to eq(Post)
44
- end
45
-
46
- it "can be given a different policy class" do
47
- expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
48
- end
49
-
50
- it "can be given a different policy class using namespaces" do
51
- expect(PublicationPolicy).to receive(:new).with(user, comment).and_call_original
52
- expect(Pundit.authorize(user, [:project, comment], :create?, policy_class: PublicationPolicy)).to be_truthy
53
- end
54
-
55
- it "works with anonymous class policies" do
56
- expect(Pundit.authorize(user, article_tag, :show?)).to be_truthy
57
- expect { Pundit.authorize(user, article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
58
- end
59
-
60
- it "raises an error with the policy, query and record" do
61
- # rubocop:disable Style/MultilineBlockChain
62
- expect do
63
- Pundit.authorize(user, post, :destroy?)
64
- end.to raise_error(Pundit::NotAuthorizedError, "not allowed to PostPolicy#destroy? this Post") do |error|
65
- expect(error.query).to eq :destroy?
66
- expect(error.record).to eq post
67
- expect(error.policy).to have_attributes(
68
- user: user,
69
- record: post
70
- )
71
- expect(error.policy).to be_a(PostPolicy)
72
- end
73
- # rubocop:enable Style/MultilineBlockChain
74
- end
75
-
76
- it "raises an error with the policy, query and record when the record is namespaced" do
77
- # rubocop:disable Style/MultilineBlockChain
78
- expect do
79
- Pundit.authorize(user, [:project, :admin, comment], :destroy?)
80
- end.to raise_error(Pundit::NotAuthorizedError,
81
- "not allowed to Project::Admin::CommentPolicy#destroy? this Comment") do |error|
82
- expect(error.query).to eq :destroy?
83
- expect(error.record).to eq comment
84
- expect(error.policy).to have_attributes(
85
- user: user,
86
- record: comment
87
- )
88
- expect(error.policy).to be_a(Project::Admin::CommentPolicy)
89
- end
90
- # rubocop:enable Style/MultilineBlockChain
91
- end
92
-
93
- it "raises an error with the policy, query and the class name when a Class is given" do
94
- # rubocop:disable Style/MultilineBlockChain
95
- expect do
96
- Pundit.authorize(user, Post, :destroy?)
97
- end.to raise_error(Pundit::NotAuthorizedError, "not allowed to PostPolicy#destroy? Post") do |error|
98
- expect(error.query).to eq :destroy?
99
- expect(error.record).to eq Post
100
- expect(error.policy).to have_attributes(
101
- user: user,
102
- record: Post
103
- )
104
- expect(error.policy).to be_a(PostPolicy)
105
- end
106
- # rubocop:enable Style/MultilineBlockChain
107
- end
108
-
109
- it "raises an error with a invalid policy constructor" do
110
- expect do
111
- Pundit.authorize(user, wiki, :update?)
112
- end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
113
- end
114
- end
115
-
116
- describe ".policy_scope" do
117
- it "returns an instantiated policy scope given a plain model class" do
118
- expect(Pundit.policy_scope(user, Post)).to eq :published
119
- end
120
-
121
- it "returns an instantiated policy scope given an active model class" do
122
- expect(Pundit.policy_scope(user, Comment)).to eq CommentScope.new(Comment)
123
- end
124
-
125
- it "returns an instantiated policy scope given an active record relation" do
126
- expect(Pundit.policy_scope(user, comments_relation)).to eq CommentScope.new(comments_relation)
127
- end
128
-
129
- it "returns an instantiated policy scope given an empty active record relation" do
130
- expect(Pundit.policy_scope(user, empty_comments_relation)).to eq CommentScope.new(empty_comments_relation)
131
- end
132
-
133
- it "returns an instantiated policy scope given an array of a symbol and plain model class" do
134
- expect(Pundit.policy_scope(user, [:project, Post])).to eq :read
135
- end
136
-
137
- it "returns an instantiated policy scope given an array of a symbol and active model class" do
138
- expect(Pundit.policy_scope(user, [:project, Comment])).to eq Comment
139
- end
140
-
141
- it "returns nil if the given policy scope can't be found" do
142
- expect(Pundit.policy_scope(user, Article)).to be_nil
143
- end
144
-
145
- it "raises an exception if nil object given" do
146
- expect { Pundit.policy_scope(user, nil) }.to raise_error(Pundit::NotDefinedError)
147
- end
148
-
149
- it "raises an error with a invalid policy scope constructor" do
150
- expect do
151
- Pundit.policy_scope(user, Wiki)
152
- end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy::Scope> constructor is called")
153
- end
154
-
155
- it "raises an original error with a policy scope that contains error" do
156
- expect do
157
- Pundit.policy_scope(user, Thread)
158
- end.to raise_error(ArgumentError)
159
- end
160
- end
161
-
162
- describe ".policy_scope!" do
163
- it "returns an instantiated policy scope given a plain model class" do
164
- expect(Pundit.policy_scope!(user, Post)).to eq :published
165
- end
166
-
167
- it "returns an instantiated policy scope given an active model class" do
168
- expect(Pundit.policy_scope!(user, Comment)).to eq CommentScope.new(Comment)
169
- end
170
-
171
- it "throws an exception if the given policy scope can't be found" do
172
- expect { Pundit.policy_scope!(user, Article) }.to raise_error(Pundit::NotDefinedError)
173
- end
174
-
175
- it "throws an exception if the given policy scope can't be found" do
176
- expect { Pundit.policy_scope!(user, ArticleTag) }.to raise_error(Pundit::NotDefinedError)
177
- end
178
-
179
- it "throws an exception if the given policy scope is nil" do
180
- expect do
181
- Pundit.policy_scope!(user, nil)
182
- end.to raise_error(Pundit::NotDefinedError, "Cannot scope NilClass")
183
- end
184
-
185
- it "returns an instantiated policy scope given an array of a symbol and plain model class" do
186
- expect(Pundit.policy_scope!(user, [:project, Post])).to eq :read
187
- end
188
-
189
- it "returns an instantiated policy scope given an array of a symbol and active model class" do
190
- expect(Pundit.policy_scope!(user, [:project, Comment])).to eq Comment
191
- end
192
-
193
- it "raises an error with a invalid policy scope constructor" do
194
- expect do
195
- Pundit.policy_scope(user, Wiki)
196
- end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy::Scope> constructor is called")
197
- end
198
- end
199
-
200
- describe ".policy" do
201
- it "returns an instantiated policy given a plain model instance" do
202
- policy = Pundit.policy(user, post)
203
- expect(policy.user).to eq user
204
- expect(policy.post).to eq post
205
- end
206
-
207
- it "returns an instantiated policy given an active model instance" do
208
- policy = Pundit.policy(user, comment)
209
- expect(policy.user).to eq user
210
- expect(policy.comment).to eq comment
211
- end
212
-
213
- it "returns an instantiated policy given a plain model class" do
214
- policy = Pundit.policy(user, Post)
215
- expect(policy.user).to eq user
216
- expect(policy.post).to eq Post
217
- end
218
-
219
- it "returns an instantiated policy given an active model class" do
220
- policy = Pundit.policy(user, Comment)
221
- expect(policy.user).to eq user
222
- expect(policy.comment).to eq Comment
223
- end
224
-
225
- it "returns an instantiated policy given a symbol" do
226
- policy = Pundit.policy(user, :criteria)
227
- expect(policy.class).to eq CriteriaPolicy
228
- expect(policy.user).to eq user
229
- expect(policy.criteria).to eq :criteria
230
- end
231
-
232
- it "returns an instantiated policy given an array of symbols" do
233
- policy = Pundit.policy(user, %i[project criteria])
234
- expect(policy.class).to eq Project::CriteriaPolicy
235
- expect(policy.user).to eq user
236
- expect(policy.criteria).to eq :criteria
237
- end
238
-
239
- it "returns an instantiated policy given an array of a symbol and plain model instance" do
240
- policy = Pundit.policy(user, [:project, post])
241
- expect(policy.class).to eq Project::PostPolicy
242
- expect(policy.user).to eq user
243
- expect(policy.post).to eq post
244
- end
245
-
246
- it "returns an instantiated policy given an array of a symbol and a model instance with policy_class override" do
247
- policy = Pundit.policy(user, [:project, customer_post])
248
- expect(policy.class).to eq Project::PostPolicy
249
- expect(policy.user).to eq user
250
- expect(policy.post).to eq customer_post
251
- end
252
-
253
- it "returns an instantiated policy given an array of a symbol and an active model instance" do
254
- policy = Pundit.policy(user, [:project, comment])
255
- expect(policy.class).to eq Project::CommentPolicy
256
- expect(policy.user).to eq user
257
- expect(policy.comment).to eq comment
258
- end
259
-
260
- it "returns an instantiated policy given an array of a symbol and a plain model class" do
261
- policy = Pundit.policy(user, [:project, Post])
262
- expect(policy.class).to eq Project::PostPolicy
263
- expect(policy.user).to eq user
264
- expect(policy.post).to eq Post
265
- end
266
-
267
- it "raises an error with a invalid policy constructor" do
268
- expect do
269
- Pundit.policy(user, Wiki)
270
- end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
271
- end
272
-
273
- it "returns an instantiated policy given an array of a symbol and an active model class" do
274
- policy = Pundit.policy(user, [:project, Comment])
275
- expect(policy.class).to eq Project::CommentPolicy
276
- expect(policy.user).to eq user
277
- expect(policy.comment).to eq Comment
278
- end
279
-
280
- it "returns an instantiated policy given an array of a symbol and a class with policy_class override" do
281
- policy = Pundit.policy(user, [:project, Customer::Post])
282
- expect(policy.class).to eq Project::PostPolicy
283
- expect(policy.user).to eq user
284
- expect(policy.post).to eq Customer::Post
285
- end
286
-
287
- it "returns correct policy class for an array of a multi-word symbols" do
288
- policy = Pundit.policy(user, %i[project_one_two_three criteria_four_five_six])
289
- expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy
290
- end
291
-
292
- it "returns correct policy class for an array of a multi-word symbol and a multi-word plain model instance" do
293
- policy = Pundit.policy(user, [:project_one_two_three, post_four_five_six])
294
- expect(policy.class).to eq ProjectOneTwoThree::PostFourFiveSixPolicy
295
- end
296
-
297
- it "returns correct policy class for an array of a multi-word symbol and a multi-word active model instance" do
298
- policy = Pundit.policy(user, [:project_one_two_three, comment_four_five_six])
299
- expect(policy.class).to eq ProjectOneTwoThree::CommentFourFiveSixPolicy
300
- end
301
-
302
- it "returns correct policy class for an array of a multi-word symbol and a multi-word plain model class" do
303
- policy = Pundit.policy(user, [:project_one_two_three, PostFourFiveSix])
304
- expect(policy.class).to eq ProjectOneTwoThree::PostFourFiveSixPolicy
305
- end
306
-
307
- it "returns correct policy class for an array of a multi-word symbol and a multi-word active model class" do
308
- policy = Pundit.policy(user, [:project_one_two_three, CommentFourFiveSix])
309
- expect(policy.class).to eq ProjectOneTwoThree::CommentFourFiveSixPolicy
310
- end
311
-
312
- it "returns correct policy class for a multi-word scoped plain model class" do
313
- policy = Pundit.policy(user, ProjectOneTwoThree::TagFourFiveSix)
314
- expect(policy.class).to eq ProjectOneTwoThree::TagFourFiveSixPolicy
315
- end
316
-
317
- it "returns correct policy class for a multi-word scoped plain model instance" do
318
- policy = Pundit.policy(user, tag_four_five_six)
319
- expect(policy.class).to eq ProjectOneTwoThree::TagFourFiveSixPolicy
320
- end
321
-
322
- it "returns correct policy class for a multi-word scoped active model class" do
323
- policy = Pundit.policy(user, ProjectOneTwoThree::AvatarFourFiveSix)
324
- expect(policy.class).to eq ProjectOneTwoThree::AvatarFourFiveSixPolicy
325
- end
326
-
327
- it "returns correct policy class for a multi-word scoped active model instance" do
328
- policy = Pundit.policy(user, avatar_four_five_six)
329
- expect(policy.class).to eq ProjectOneTwoThree::AvatarFourFiveSixPolicy
330
- end
331
-
332
- it "returns nil if the given policy can't be found" do
333
- expect(Pundit.policy(user, article)).to be_nil
334
- expect(Pundit.policy(user, Article)).to be_nil
335
- end
336
-
337
- it "returns the specified NilClassPolicy for nil" do
338
- expect(Pundit.policy(user, nil)).to be_a NilClassPolicy
339
- end
340
-
341
- describe "with .policy_class set on the model" do
342
- it "returns an instantiated policy given a plain model instance" do
343
- policy = Pundit.policy(user, artificial_blog)
344
- expect(policy.user).to eq user
345
- expect(policy.blog).to eq artificial_blog
346
- end
347
-
348
- it "returns an instantiated policy given a plain model class" do
349
- policy = Pundit.policy(user, ArtificialBlog)
350
- expect(policy.user).to eq user
351
- expect(policy.blog).to eq ArtificialBlog
352
- end
353
-
354
- it "returns an instantiated policy given a plain model instance providing an anonymous class" do
355
- policy = Pundit.policy(user, article_tag)
356
- expect(policy.user).to eq user
357
- expect(policy.tag).to eq article_tag
358
- end
359
-
360
- it "returns an instantiated policy given a plain model class providing an anonymous class" do
361
- policy = Pundit.policy(user, ArticleTag)
362
- expect(policy.user).to eq user
363
- expect(policy.tag).to eq ArticleTag
364
- end
365
- end
366
- end
367
-
368
- describe ".policy!" do
369
- it "returns an instantiated policy given a plain model instance" do
370
- policy = Pundit.policy!(user, post)
371
- expect(policy.user).to eq user
372
- expect(policy.post).to eq post
373
- end
374
-
375
- it "returns an instantiated policy given an active model instance" do
376
- policy = Pundit.policy!(user, comment)
377
- expect(policy.user).to eq user
378
- expect(policy.comment).to eq comment
379
- end
380
-
381
- it "returns an instantiated policy given a plain model class" do
382
- policy = Pundit.policy!(user, Post)
383
- expect(policy.user).to eq user
384
- expect(policy.post).to eq Post
385
- end
386
-
387
- it "returns an instantiated policy given an active model class" do
388
- policy = Pundit.policy!(user, Comment)
389
- expect(policy.user).to eq user
390
- expect(policy.comment).to eq Comment
391
- end
392
-
393
- it "returns an instantiated policy given a symbol" do
394
- policy = Pundit.policy!(user, :criteria)
395
- expect(policy.class).to eq CriteriaPolicy
396
- expect(policy.user).to eq user
397
- expect(policy.criteria).to eq :criteria
398
- end
399
-
400
- it "returns an instantiated policy given an array of symbols" do
401
- policy = Pundit.policy!(user, %i[project criteria])
402
- expect(policy.class).to eq Project::CriteriaPolicy
403
- expect(policy.user).to eq user
404
- expect(policy.criteria).to eq :criteria
405
- end
406
-
407
- it "throws an exception if the given policy can't be found" do
408
- expect { Pundit.policy!(user, article) }.to raise_error(Pundit::NotDefinedError)
409
- expect { Pundit.policy!(user, Article) }.to raise_error(Pundit::NotDefinedError)
410
- end
411
-
412
- it "returns the specified NilClassPolicy for nil" do
413
- expect(Pundit.policy!(user, nil)).to be_a NilClassPolicy
414
- end
415
-
416
- it "raises an error with a invalid policy constructor" do
417
- expect do
418
- Pundit.policy(user, Wiki)
419
- end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
420
- end
421
- end
422
-
423
- describe ".included" do
424
- it "includes Authorization module" do
425
- klass = Class.new
426
-
427
- expect do
428
- klass.include Pundit
429
- end.to output.to_stderr
430
-
431
- expect(klass).to include Pundit::Authorization
432
- end
433
-
434
- it "warns about deprecation" do
435
- klass = Class.new
436
- expect do
437
- klass.include Pundit
438
- end.to output(a_string_starting_with("'include Pundit' is deprecated")).to_stderr
439
- end
440
- end
441
-
442
- describe "Pundit::NotAuthorizedError" do
443
- it "can be initialized with a string as message" do
444
- error = Pundit::NotAuthorizedError.new("must be logged in")
445
- expect(error.message).to eq "must be logged in"
446
- end
447
- end
448
- end