pundit_assertions 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +4 -13
- data/lib/pundit_assertions/deprecation.rb +21 -0
- data/lib/pundit_assertions/test_helpers.rb +62 -26
- data/lib/pundit_assertions/version.rb +1 -1
- metadata +19 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ecbd42aea5a12ef1a0110fca059d8dd42cbe219c9e0dfb734b8adbf433c85c2b
|
|
4
|
+
data.tar.gz: 71387d99a57d37b1404ec0e966640567a942df7fa9d75f982e5d337fdf42ef58
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 15a5aee469a6048e4f169d0242e3ff6efb99ab0178861517bd20209b500b2606238ee02aa38f0a4365b9377cb31c0f7895c964ea611f2fc450a928fc64e3d1d5
|
|
7
|
+
data.tar.gz: c4eed4e2c6cd92cf809ba24f1dc55033d59872a5945f0d1f1d40daa396adc1ce93e6474c1d1d0c758b10e42e09a16f4b93d9f9b2ef4c1a6e41722eed58a2e5bb
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.2.0] - 2026-09-07
|
|
4
|
+
### Breaking changes
|
|
5
|
+
* `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.
|
|
6
|
+
|
|
7
|
+
### Improvements
|
|
8
|
+
* 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.
|
|
9
|
+
|
|
3
10
|
## [0.1.0] - 2025-11-26
|
|
4
11
|
|
|
5
12
|
- 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
|
|
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
|
-
|
|
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
|
-
|
|
63
|
-
|
|
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.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 = "
|
|
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 = "
|
|
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
|
|
36
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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,63 @@ 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
|
|
113
|
+
def assert_not_scope_includes(user, *records)
|
|
98
114
|
records.flatten.each do |record|
|
|
99
|
-
|
|
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
|
|
120
|
+
alias refute_scope_includes assert_not_scope_includes
|
|
106
121
|
|
|
107
122
|
##
|
|
108
123
|
# Assert whether the scope for a user is empty
|
|
109
124
|
def assert_scope_empty(user, klass)
|
|
110
|
-
assert_empty scope(user, klass)
|
|
125
|
+
assert_empty scope(user, klass), "Expected scope for #{inspect_user(user)} to be empty"
|
|
111
126
|
end
|
|
112
127
|
|
|
113
|
-
|
|
128
|
+
##
|
|
129
|
+
# Return the scoped records for a user and a klass
|
|
130
|
+
def scope(user, klass)
|
|
131
|
+
result = scope_class.new(user, klass).resolve
|
|
132
|
+
|
|
133
|
+
refute_nil result
|
|
134
|
+
result
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
##
|
|
138
|
+
# Get the policy class based on the current test class
|
|
139
|
+
# This assumes this method is called inside `ModelPolicyTest` to get `ModelPolicy`
|
|
114
140
|
def policy_class
|
|
115
141
|
Object.const_get(self.class.to_s.gsub('Test', ''))
|
|
116
142
|
end
|
|
117
143
|
|
|
118
|
-
|
|
144
|
+
##
|
|
145
|
+
# Get the policy scope class based on the current test class
|
|
146
|
+
# This assumes this method is called inside `ModelPolicyTest` or `ModelPolicyTest::Scope` to get `ModelPolicy::Scope`
|
|
119
147
|
def scope_class
|
|
120
148
|
klass = self.class.to_s.gsub('Test', '')
|
|
121
149
|
klass << '::Scope' unless klass.match?(/::Scope$/)
|
|
122
150
|
Object.const_get(klass)
|
|
123
151
|
end
|
|
152
|
+
|
|
153
|
+
private
|
|
154
|
+
|
|
155
|
+
def inspect_user(user)
|
|
156
|
+
return 'nil' if user.nil?
|
|
157
|
+
|
|
158
|
+
user.class.name
|
|
159
|
+
end
|
|
124
160
|
end
|
|
125
161
|
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.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robbe Van Petegem
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
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.
|
|
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: []
|