pundit_assertions 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d5de5bdd6a032e18432811b7061fa42b20cb76b5e7a348177c825319ad4f672
4
- data.tar.gz: d766c710ac4bd35985bab3cc57b069fda3dc981e19e090cfb87b9e2bcb3f39cb
3
+ metadata.gz: 6561e3562c58620263a263edcdfb21b4dce057c09644cbdc1c97caaf3e470300
4
+ data.tar.gz: 285d5ce2043f5b758902a80954bfe9a490288a2cdd1bd9d79c3ba31afa7059da
5
5
  SHA512:
6
- metadata.gz: 96afe86a57e8fcab2a1ca56aebce5398b6d76f25cba3a11479786b4a562a1eb87be85ff59b465320f7218a4b2461588a926b2a0ef5ab70e9cc2c00634b9163b1
7
- data.tar.gz: 68238d5f429a88c664d1695e042596c6a4c30a33f726b0abe7473cd394880a30f4566cae42ec848c99b24a0a28a787d7496570aa29e1cbb48e43eeef5444d048
6
+ metadata.gz: 6c14b880b1d00d26eab0bbd62ea4297bb15f13f706c3801ba696971cd645b784dcc2e880dcf5b995e8e1c417ab4f1294232427029294f7c1e330d2b3db6dd05e
7
+ data.tar.gz: bd65653928dca71280d56e1b42b3fda0d7352fdbf3144b6747c70c016545c327f0d51b749ce42fb5cb4047b712dfe83f6ea5c618e1b69b1dbf85533aec960247
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.1] - 2026-09-15
4
+ * Fixed usage of `ActiveSupport::Deprecation`
5
+ * Fixed a missing alias method `assert_scope_not_includes`
6
+
7
+ ## [0.2.0] - 2026-09-07
8
+ ### Breaking changes
9
+ * `assert_permitted_attributes` has been renamed to `assert_has_permitted_attributes` for clarity. The old method name is deprecated and will be removed in a next MINOR release.
10
+
11
+ ### Improvements
12
+ * The messages of the validations have been customized. Before this, we often returned the message of the underlying assertion (ie. `assert_equal`, `assert_includes`, ...). We now have our own messages that should make it easier to understand the test failure.
13
+
3
14
  ## [0.1.0] - 2025-11-26
4
15
 
5
16
  - Initial release
data/README.md CHANGED
@@ -5,19 +5,10 @@ A simple gem to make testing [Pundit](https://github.com/varvet/pundit) policies
5
5
  We deduce the relevant based on the name of the test class: We assume that `CommentPolicy` gets tested in `CommentPolicyTest`, `Admin::CommentPolicy` in `Admin::CommentPolicyTest`, ...
6
6
 
7
7
  ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
8
  Install the gem and add to the application's Gemfile by executing:
12
9
 
13
10
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
- ```
16
-
17
- If bundler is not being used to manage dependencies, install the gem by executing:
18
-
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
11
+ bundle add pundit_assertions
21
12
  ```
22
13
 
23
14
  You can include the test helpers in all tests, or include this in a specific file:
@@ -54,13 +45,13 @@ class CommentPolicyTest < ActiveSupport::TestCase
54
45
  end
55
46
 
56
47
  test 'should allow permitted attributes for user' do
57
- assert_permitted_attributes @user, @comment
48
+ assert_has_permitted_attributes @user, @comment
58
49
  assert_no_permitted_attributes nil, @comment
59
50
  end
60
51
 
61
52
  test 'should only allow private attribute on create' do
62
- assert_attribute_permitted @user, @comment, :private, :create
63
- assert_not_attribute_permitted @user, @comment, :private, :update
53
+ assert_attributes_permitted @user, @comment, :private, :create
54
+ assert_not_attributes_permitted @user, @comment, :private, :update
64
55
  end
65
56
 
66
57
  test 'should scope comments that belong to user' do
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PunditAssertions
4
+ module Deprecation # :nodoc:
5
+ ##
6
+ # Mark a method as deprecated and replace with a new method
7
+ def deprecate_method(old_name, new_name)
8
+ define_method(old_name) do |*args, **kwargs|
9
+ message = "#{old_name} is deprecated and will be removed in a future release. Use #{new_name} instead.\n"
10
+
11
+ # If we are in rails, we deprecate using rails' builtins, otherwise we use ruby's Warning
12
+ if defined?(ActiveSupport::Deprecation)
13
+ ActiveSupport::Deprecation.new('0.3.0', 'PunditAssertions').warn(message)
14
+ else
15
+ Warning.warn message, category: :deprecated
16
+ end
17
+ send(new_name, *args, **kwargs)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'deprecation'
4
+
3
5
  module PunditAssertions
4
6
  ##
5
7
  # A set of assertions to use in minitest
6
8
  #
7
9
  # This module should be included in your test class to use these assertions
8
10
  module TestHelpers
11
+ extend PunditAssertions::Deprecation
12
+
9
13
  ##
10
14
  # Assert whether a user is permitted to perform an action
11
15
  def assert_permitted(user, record, action)
12
- msg = "User #{user.inspect} should be permitted to #{action} #{record}, but isn't permitted"
16
+ msg = "Expected #{inspect_user(user)} to be permitted to #{action} #{record}"
13
17
 
14
18
  assert permitted?(user, record, action), msg
15
19
  end
@@ -17,7 +21,7 @@ module PunditAssertions
17
21
  ##
18
22
  # Assert whether a user is not permitted to perform an action
19
23
  def assert_not_permitted(user, record, action)
20
- msg = "User #{user.inspect} should NOT be permitted to #{action} #{record}, but is permitted"
24
+ msg = "Expected #{inspect_user(user)} to not be permitted to #{action} #{record}"
21
25
 
22
26
  refute permitted?(user, record, action), msg
23
27
  end
@@ -32,25 +36,41 @@ module PunditAssertions
32
36
 
33
37
  ##
34
38
  # Assert whether a user will have any attributes permitted
35
- def assert_permitted_attributes(user, record, action = nil)
36
- refute_empty permitted_attributes(user, record, action)
39
+ def assert_has_permitted_attributes(user, record, action = nil)
40
+ permitted = permitted_attributes(user, record, action)
41
+ for_action = action.nil? ? '' : " for #{action}"
42
+ message = "Expected #{inspect_user(user)} to have permitted attributes#{for_action}\n"
43
+
44
+ refute_nil permitted, message
45
+ refute_empty permitted, message
37
46
  end
38
47
 
48
+ deprecate_method :assert_permitted_attributes, :assert_has_permitted_attributes
49
+
39
50
  ##
40
51
  # Assert whether a user will have no attributes permitted
41
52
  def assert_no_permitted_attributes(user, record, action = nil)
42
- assert_nil permitted_attributes(user, record, action)
53
+ permitted = permitted_attributes(user, record, action)
54
+ for_action = action.nil? ? '' : " for #{action}"
55
+ permitted_message = "Attributes #{permitted.inspect} were permitted\n"
56
+ message = "Expected #{inspect_user(user)} to not have permitted attributes#{for_action}\n#{permitted_message}"
57
+
58
+ assert_nil permitted, message
43
59
  end
44
60
 
61
+ alias refute_permitted_attributes assert_no_permitted_attributes
62
+
45
63
  ##
46
- # Assert whether a user will have all of the specified attributes permitted
64
+ # Assert whether a user will have at least the specified attributes permitted
47
65
  # You can specify a specific action
48
66
  def assert_attributes_permitted(user, record, attributes, action = nil)
49
67
  attributes = [attributes] unless attributes.is_a?(Array)
50
68
  permitted = permitted_attributes(user, record, action)
51
69
  permitted = [] if permitted.nil?
70
+ for_action = action.nil? ? '' : " for #{action}"
52
71
 
53
- assert_empty attributes - permitted
72
+ assert_empty attributes - permitted,
73
+ "Expected #{inspect_user(user)} to have #{attributes} in permitted attributes#{for_action}"
54
74
  end
55
75
 
56
76
  ##
@@ -59,10 +79,14 @@ module PunditAssertions
59
79
  attributes = [attributes] unless attributes.is_a?(Array)
60
80
  permitted = permitted_attributes(user, record, action)
61
81
  permitted = [] if permitted.nil?
82
+ union = (attributes & permitted)
83
+ for_action = action.nil? ? '' : " for #{action}"
62
84
 
63
- assert_equal attributes.length, (attributes - permitted).length
85
+ assert_empty union, "Expected #{inspect_user(user)} to not have #{union} in permitted attributes#{for_action}"
64
86
  end
65
87
 
88
+ alias refute_attributes_permitted assert_not_attributes_permitted
89
+
66
90
  ##
67
91
  # Return the permitted attributes
68
92
  def permitted_attributes(user, record, action = nil)
@@ -75,51 +99,64 @@ module PunditAssertions
75
99
  policy.send(method)
76
100
  end
77
101
 
78
- ##
79
- # Return the scoped records for a user and a klass
80
- def scope(user, klass)
81
- result = scope_class.new(user, klass).resolve
82
-
83
- refute_nil result
84
- result
85
- end
86
-
87
102
  ##
88
103
  # Assert whether a user will have the specified records in scope
89
104
  def assert_scope_includes(user, *records)
90
105
  records.flatten.each do |record|
91
- assert_includes scope(user, record.class), record
106
+ assert_includes scope(user, record.class), record,
107
+ "Expected #{record} to be included in the scope for #{inspect_user(user)}"
92
108
  end
93
109
  end
94
110
 
95
111
  ##
96
112
  # Assert whether a user will not have the specified records in scope
97
- def assert_scope_not_includes(user, *records)
113
+ def assert_not_scope_includes(user, *records)
98
114
  records.flatten.each do |record|
99
- result = scope(user, record.class)
100
-
101
- assert result.nil? || !result.include?(record)
115
+ refute_includes scope(user, record.class), record,
116
+ "Expected #{record} to not be included in the scope for #{inspect_user(user)}"
102
117
  end
103
118
  end
104
119
 
105
- alias refute_scope_includes assert_scope_not_includes
120
+ alias refute_scope_includes assert_not_scope_includes
121
+ alias assert_scope_not_includes assert_not_scope_includes
106
122
 
107
123
  ##
108
124
  # Assert whether the scope for a user is empty
109
125
  def assert_scope_empty(user, klass)
110
- assert_empty scope(user, klass)
126
+ assert_empty scope(user, klass), "Expected scope for #{inspect_user(user)} to be empty"
111
127
  end
112
128
 
113
- # This assumes this test is happening inside ModelPolicyTest
129
+ ##
130
+ # Return the scoped records for a user and a klass
131
+ def scope(user, klass)
132
+ result = scope_class.new(user, klass).resolve
133
+
134
+ refute_nil result
135
+ result
136
+ end
137
+
138
+ ##
139
+ # Get the policy class based on the current test class
140
+ # This assumes this method is called inside `ModelPolicyTest` to get `ModelPolicy`
114
141
  def policy_class
115
142
  Object.const_get(self.class.to_s.gsub('Test', ''))
116
143
  end
117
144
 
118
- # This assumes this test is happening inside ModelPolicyTest or ModelPolicyTest::Scope
145
+ ##
146
+ # Get the policy scope class based on the current test class
147
+ # This assumes this method is called inside `ModelPolicyTest` or `ModelPolicyTest::Scope` to get `ModelPolicy::Scope`
119
148
  def scope_class
120
149
  klass = self.class.to_s.gsub('Test', '')
121
150
  klass << '::Scope' unless klass.match?(/::Scope$/)
122
151
  Object.const_get(klass)
123
152
  end
153
+
154
+ private
155
+
156
+ def inspect_user(user)
157
+ return 'nil' if user.nil?
158
+
159
+ user.class.name
160
+ end
124
161
  end
125
162
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PunditAssertions
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit_assertions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robbe Van Petegem
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-11-26 00:00:00.000000000 Z
11
- dependencies: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: minitest
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
12
26
  email:
13
27
  - git@robbevp.be
14
28
  executables: []
@@ -21,6 +35,7 @@ files:
21
35
  - README.md
22
36
  - Rakefile
23
37
  - lib/pundit_assertions.rb
38
+ - lib/pundit_assertions/deprecation.rb
24
39
  - lib/pundit_assertions/test_helpers.rb
25
40
  - lib/pundit_assertions/version.rb
26
41
  homepage: https://github.com/tree-company/pundit_assertions
@@ -45,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
60
  - !ruby/object:Gem::Version
46
61
  version: '0'
47
62
  requirements: []
48
- rubygems_version: 3.6.6
63
+ rubygems_version: 3.7.2
49
64
  specification_version: 4
50
65
  summary: A simple set of minitest assertion to help test pundit policies.
51
66
  test_files: []