pundit 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/pundit_spec.rb CHANGED
@@ -57,26 +57,51 @@ RSpec.describe Pundit do
57
57
  expect { Pundit.authorize(user, article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
58
58
  end
59
59
 
60
- it "raises an error with a query and action" do
60
+ it "raises an error with the policy, query and record" do
61
61
  # rubocop:disable Style/MultilineBlockChain
62
62
  expect do
63
63
  Pundit.authorize(user, post, :destroy?)
64
- end.to raise_error(Pundit::NotAuthorizedError, "not allowed to destroy? this Post") do |error|
64
+ end.to raise_error(Pundit::NotAuthorizedError, "not allowed to PostPolicy#destroy? this Post") do |error|
65
65
  expect(error.query).to eq :destroy?
66
66
  expect(error.record).to eq post
67
- expect(error.policy).to eq Pundit.policy(user, post)
67
+ expect(error.policy).to have_attributes(
68
+ user: user,
69
+ record: post
70
+ )
71
+ expect(error.policy).to be_a(PostPolicy)
68
72
  end
69
73
  # rubocop:enable Style/MultilineBlockChain
70
74
  end
71
75
 
72
- it "raises an error with a the record, query and action when the record is namespaced" do
76
+ it "raises an error with the policy, query and record when the record is namespaced" do
73
77
  # rubocop:disable Style/MultilineBlockChain
74
78
  expect do
75
79
  Pundit.authorize(user, [:project, :admin, comment], :destroy?)
76
- end.to raise_error(Pundit::NotAuthorizedError, "not allowed to destroy? this Comment") do |error|
80
+ end.to raise_error(Pundit::NotAuthorizedError,
81
+ "not allowed to Project::Admin::CommentPolicy#destroy? this Comment") do |error|
77
82
  expect(error.query).to eq :destroy?
78
83
  expect(error.record).to eq comment
79
- expect(error.policy).to eq Pundit.policy(user, [:project, :admin, 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)
80
105
  end
81
106
  # rubocop:enable Style/MultilineBlockChain
82
107
  end
@@ -399,22 +424,18 @@ RSpec.describe Pundit do
399
424
  it "includes Authorization module" do
400
425
  klass = Class.new
401
426
 
402
- ActiveSupport::Deprecation.silence do
427
+ expect do
403
428
  klass.include Pundit
404
- end
429
+ end.to output.to_stderr
405
430
 
406
431
  expect(klass).to include Pundit::Authorization
407
432
  end
408
433
 
409
434
  it "warns about deprecation" do
410
435
  klass = Class.new
411
- allow(ActiveSupport::Deprecation).to receive(:warn)
412
-
413
- ActiveSupport::Deprecation.silence do
436
+ expect do
414
437
  klass.include Pundit
415
- end
416
-
417
- expect(ActiveSupport::Deprecation).to have_received(:warn).with start_with("'include Pundit' is deprecated")
438
+ end.to output(a_string_starting_with("'include Pundit' is deprecated")).to_stderr
418
439
  end
419
440
  end
420
441
 
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "simplecov"
4
- SimpleCov.start do
5
- add_filter "/spec/"
3
+ if ENV["COVERAGE"]
4
+ require "simplecov"
5
+ SimpleCov.start do
6
+ add_filter "/spec/"
7
+ end
6
8
  end
7
9
 
8
10
  require "pundit"
@@ -16,13 +18,56 @@ require "active_support/core_ext"
16
18
  require "active_model/naming"
17
19
  require "action_controller/metal/strong_parameters"
18
20
 
19
- class PostPolicy < Struct.new(:user, :post)
20
- class Scope < Struct.new(:user, :scope)
21
+ module InstanceTracking
22
+ module ClassMethods
23
+ def instances
24
+ @instances || 0
25
+ end
26
+
27
+ attr_writer :instances
28
+ end
29
+
30
+ def self.prepended(other)
31
+ other.extend(ClassMethods)
32
+ end
33
+
34
+ def initialize(*args, **kwargs, &block)
35
+ self.class.instances += 1
36
+ super(*args, **kwargs, &block)
37
+ end
38
+ end
39
+
40
+ class BasePolicy
41
+ prepend InstanceTracking
42
+
43
+ class BaseScope
44
+ prepend InstanceTracking
45
+
46
+ def initialize(user, scope)
47
+ @user = user
48
+ @scope = scope
49
+ end
50
+
51
+ attr_reader :user, :scope
52
+ end
53
+
54
+ def initialize(user, record)
55
+ @user = user
56
+ @record = record
57
+ end
58
+
59
+ attr_reader :user, :record
60
+ end
61
+
62
+ class PostPolicy < BasePolicy
63
+ class Scope < BaseScope
21
64
  def resolve
22
65
  scope.published
23
66
  end
24
67
  end
25
68
 
69
+ alias post record
70
+
26
71
  def update?
27
72
  post.user == user
28
73
  end
@@ -48,7 +93,13 @@ class PostPolicy < Struct.new(:user, :post)
48
93
  end
49
94
  end
50
95
 
51
- class Post < Struct.new(:user)
96
+ class Post
97
+ def initialize(user = nil)
98
+ @user = user
99
+ end
100
+
101
+ attr_reader :user
102
+
52
103
  def self.published
53
104
  :published
54
105
  end
@@ -67,7 +118,7 @@ class Post < Struct.new(:user)
67
118
  end
68
119
 
69
120
  module Customer
70
- class Post < Post
121
+ class Post < ::Post
71
122
  def model_name
72
123
  OpenStruct.new(param_key: "customer_post")
73
124
  end
@@ -90,16 +141,18 @@ class CommentScope
90
141
  end
91
142
  end
92
143
 
93
- class CommentPolicy < Struct.new(:user, :comment)
94
- class Scope < Struct.new(:user, :scope)
144
+ class CommentPolicy < BasePolicy
145
+ class Scope < BaseScope
95
146
  def resolve
96
147
  CommentScope.new(scope)
97
148
  end
98
149
  end
150
+
151
+ alias comment record
99
152
  end
100
153
 
101
- class PublicationPolicy < Struct.new(:user, :publication)
102
- class Scope < Struct.new(:user, :scope)
154
+ class PublicationPolicy < BasePolicy
155
+ class Scope < BaseScope
103
156
  def resolve
104
157
  scope.published
105
158
  end
@@ -130,7 +183,9 @@ end
130
183
 
131
184
  class Article; end
132
185
 
133
- class BlogPolicy < Struct.new(:user, :blog); end
186
+ class BlogPolicy < BasePolicy
187
+ alias blog record
188
+ end
134
189
 
135
190
  class Blog; end
136
191
 
@@ -140,7 +195,7 @@ class ArtificialBlog < Blog
140
195
  end
141
196
  end
142
197
 
143
- class ArticleTagOtherNamePolicy < Struct.new(:user, :tag)
198
+ class ArticleTagOtherNamePolicy < BasePolicy
144
199
  def show?
145
200
  true
146
201
  end
@@ -148,6 +203,8 @@ class ArticleTagOtherNamePolicy < Struct.new(:user, :tag)
148
203
  def destroy?
149
204
  false
150
205
  end
206
+
207
+ alias tag record
151
208
  end
152
209
 
153
210
  class ArticleTag
@@ -156,33 +213,41 @@ class ArticleTag
156
213
  end
157
214
  end
158
215
 
159
- class CriteriaPolicy < Struct.new(:user, :criteria); end
216
+ class CriteriaPolicy < BasePolicy
217
+ alias criteria record
218
+ end
160
219
 
161
220
  module Project
162
- class CommentPolicy < Struct.new(:user, :comment)
163
- def update?
164
- true
165
- end
166
-
167
- class Scope < Struct.new(:user, :scope)
221
+ class CommentPolicy < BasePolicy
222
+ class Scope < BaseScope
168
223
  def resolve
169
224
  scope
170
225
  end
171
226
  end
227
+
228
+ def update?
229
+ true
230
+ end
231
+
232
+ alias comment record
172
233
  end
173
234
 
174
- class CriteriaPolicy < Struct.new(:user, :criteria); end
235
+ class CriteriaPolicy < BasePolicy
236
+ alias criteria record
237
+ end
175
238
 
176
- class PostPolicy < Struct.new(:user, :post)
177
- class Scope < Struct.new(:user, :scope)
239
+ class PostPolicy < BasePolicy
240
+ class Scope < BaseScope
178
241
  def resolve
179
242
  scope.read
180
243
  end
181
244
  end
245
+
246
+ alias post record
182
247
  end
183
248
 
184
249
  module Admin
185
- class CommentPolicy < Struct.new(:user, :comment)
250
+ class CommentPolicy < BasePolicy
186
251
  def update?
187
252
  true
188
253
  end
@@ -194,7 +259,7 @@ module Project
194
259
  end
195
260
  end
196
261
 
197
- class DenierPolicy < Struct.new(:user, :record)
262
+ class DenierPolicy < BasePolicy
198
263
  def update?
199
264
  false
200
265
  end
@@ -216,7 +281,7 @@ class Controller
216
281
  end
217
282
  end
218
283
 
219
- class NilClassPolicy < Struct.new(:user, :record)
284
+ class NilClassPolicy < BasePolicy
220
285
  class Scope
221
286
  def initialize(*)
222
287
  raise Pundit::NotDefinedError, "Cannot scope NilClass"
@@ -245,31 +310,43 @@ class Thread
245
310
  def self.all; end
246
311
  end
247
312
 
248
- class ThreadPolicy < Struct.new(:user, :thread)
249
- class Scope < Struct.new(:user, :scope)
313
+ class ThreadPolicy < BasePolicy
314
+ class Scope < BaseScope
250
315
  def resolve
251
- # deliberate wrong useage of the method
316
+ # deliberate wrong usage of the method
252
317
  scope.all(:unvalid, :parameters)
253
318
  end
254
319
  end
255
320
  end
256
321
 
257
- class PostFourFiveSix < Struct.new(:user); end
322
+ class PostFourFiveSix
323
+ def initialize(user)
324
+ @user = user
325
+ end
326
+
327
+ attr_reader(:user)
328
+ end
258
329
 
259
330
  class CommentFourFiveSix; extend ActiveModel::Naming; end
260
331
 
261
332
  module ProjectOneTwoThree
262
- class CommentFourFiveSixPolicy < Struct.new(:user, :post); end
333
+ class CommentFourFiveSixPolicy < BasePolicy; end
263
334
 
264
- class CriteriaFourFiveSixPolicy < Struct.new(:user, :criteria); end
335
+ class CriteriaFourFiveSixPolicy < BasePolicy; end
265
336
 
266
- class PostFourFiveSixPolicy < Struct.new(:user, :post); end
337
+ class PostFourFiveSixPolicy < BasePolicy; end
267
338
 
268
- class TagFourFiveSix < Struct.new(:user); end
339
+ class TagFourFiveSix
340
+ def initialize(user)
341
+ @user = user
342
+ end
343
+
344
+ attr_reader(:user)
345
+ end
269
346
 
270
- class TagFourFiveSixPolicy < Struct.new(:user, :tag); end
347
+ class TagFourFiveSixPolicy < BasePolicy; end
271
348
 
272
349
  class AvatarFourFiveSix; extend ActiveModel::Naming; end
273
350
 
274
- class AvatarFourFiveSixPolicy < Struct.new(:user, :avatar); end
351
+ class AvatarFourFiveSixPolicy < BasePolicy; end
275
352
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  - Varvet AB
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-11 00:00:00.000000000 Z
12
+ date: 2024-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -127,16 +127,16 @@ dependencies:
127
127
  name: rubocop
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
- - - '='
130
+ - - ">="
131
131
  - !ruby/object:Gem::Version
132
- version: 1.24.0
132
+ version: '0'
133
133
  type: :development
134
134
  prerelease: false
135
135
  version_requirements: !ruby/object:Gem::Requirement
136
136
  requirements:
137
- - - '='
137
+ - - ">="
138
138
  - !ruby/object:Gem::Version
139
- version: 1.24.0
139
+ version: '0'
140
140
  - !ruby/object:Gem::Dependency
141
141
  name: simplecov
142
142
  requirement: !ruby/object:Gem::Requirement
@@ -168,14 +168,19 @@ dependencies:
168
168
  description: Object oriented authorization for Rails applications
169
169
  email:
170
170
  - jonas.nicklas@gmail.com
171
- - dev@elabs.se
171
+ - info@varvet.com
172
172
  executables: []
173
173
  extensions: []
174
174
  extra_rdoc_files: []
175
175
  files:
176
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
177
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
178
+ - ".github/PULL_REQUEST_TEMPLATE/gem_release_template.md"
179
+ - ".github/pull_request_template.md"
180
+ - ".github/workflows/main.yml"
181
+ - ".github/workflows/push_gem.yml"
176
182
  - ".gitignore"
177
183
  - ".rubocop.yml"
178
- - ".travis.yml"
179
184
  - ".yardopts"
180
185
  - CHANGELOG.md
181
186
  - CODE_OF_CONDUCT.md
@@ -184,6 +189,8 @@ files:
184
189
  - LICENSE.txt
185
190
  - README.md
186
191
  - Rakefile
192
+ - SECURITY.md
193
+ - config/rubocop-rspec.yml
187
194
  - lib/generators/pundit/install/USAGE
188
195
  - lib/generators/pundit/install/install_generator.rb
189
196
  - lib/generators/pundit/install/templates/application_policy.rb
@@ -196,11 +203,15 @@ files:
196
203
  - lib/generators/test_unit/templates/policy_test.rb
197
204
  - lib/pundit.rb
198
205
  - lib/pundit/authorization.rb
206
+ - lib/pundit/cache_store/legacy_store.rb
207
+ - lib/pundit/cache_store/null_store.rb
208
+ - lib/pundit/context.rb
199
209
  - lib/pundit/policy_finder.rb
200
210
  - lib/pundit/rspec.rb
201
211
  - lib/pundit/version.rb
202
212
  - pundit.gemspec
203
213
  - spec/authorization_spec.rb
214
+ - spec/dsl_spec.rb
204
215
  - spec/generators_spec.rb
205
216
  - spec/policies/post_policy_spec.rb
206
217
  - spec/policy_finder_spec.rb
@@ -209,8 +220,9 @@ files:
209
220
  homepage: https://github.com/varvet/pundit
210
221
  licenses:
211
222
  - MIT
212
- metadata: {}
213
- post_install_message:
223
+ metadata:
224
+ rubygems_mfa_required: 'true'
225
+ post_install_message:
214
226
  rdoc_options: []
215
227
  require_paths:
216
228
  - lib
@@ -225,12 +237,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
237
  - !ruby/object:Gem::Version
226
238
  version: '0'
227
239
  requirements: []
228
- rubygems_version: 3.2.32
229
- signing_key:
240
+ rubygems_version: 3.5.11
241
+ signing_key:
230
242
  specification_version: 4
231
243
  summary: OO authorization for Rails
232
244
  test_files:
233
245
  - spec/authorization_spec.rb
246
+ - spec/dsl_spec.rb
234
247
  - spec/generators_spec.rb
235
248
  - spec/policies/post_policy_spec.rb
236
249
  - spec/policy_finder_spec.rb
data/.travis.yml DELETED
@@ -1,26 +0,0 @@
1
- language: ruby
2
- dist: focal
3
-
4
- matrix:
5
- include:
6
- - name: "RuboCop lint on pre-installed Ruby version"
7
- rvm: 2.7.1 # Pre-installed Ruby version
8
- before_install:
9
- - gem install bundler
10
- script: bundle exec rake rubocop # ONLY lint once, first
11
- - rvm: 2.6.7
12
- before_script:
13
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
- - chmod +x ./cc-test-reporter
15
- - ./cc-test-reporter before-build
16
- after_script:
17
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
18
- - rvm: 2.7.3
19
- - rvm: 3.0.1
20
- - rvm: 3.1.0
21
- - rvm: jruby-9.2.17.0
22
- env:
23
- - JRUBY_OPTS="--debug"
24
- - rvm: truffleruby-head
25
- allow_failures:
26
- - rvm: truffleruby-head