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