policy-assertions 0.0.3 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +73 -19
- data/circle.yml +13 -0
- data/lib/policy_assertions.rb +3 -2
- data/lib/policy_assertions/version.rb +1 -1
- data/policy-assertions.gemspec +2 -2
- data/test/lib/policy_assertions_test.rb +23 -0
- metadata +6 -7
- data/.travis.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f24ccf11ad4ed8b8cfbc67593fdb47ee45acee40
|
4
|
+
data.tar.gz: 1383189eb57f1a60a320658985f8ca7f1853289d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5fc2eee22de0f4b9f5c0c6cb899035cef25322c09879613c83bde1b0c2e5ae943fe8f51b760223fe289889b628142e4235000fb62cceba3f5393728113d9338
|
7
|
+
data.tar.gz: c3dc79ede4de1fd15a5c0f2979c7c615a21f6c1d047ae29717400624881d61b062737a5489cbdf737196d77876fab87bd9fe0e899e32d0f55338d016823d5245
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## v0.1.0
|
2
|
+
* [Adds support for arrays as actions in block tests](https://github.com/ProctorU/policy-assertions/pull/10)
|
3
|
+
* [Adds support for `assert_not_permitted`](https://github.com/ProctorU/policy-assertions/pull/12)
|
4
|
+
* [Readme updates](https://github.com/ProctorU/policy-assertions/pull/11)
|
5
|
+
* Transferred ownership from @ksimmons to @proctoru
|
6
|
+
|
1
7
|
## v0.0.3
|
2
8
|
|
3
9
|
* Compatibility with pundit 1.1.0
|
data/README.md
CHANGED
@@ -1,10 +1,20 @@
|
|
1
1
|
# policy-assertions
|
2
|
-
|
2
|
+
|
3
|
+
[![Build Status](https://circleci.com/gh/ProctorU/policy-assertions.svg?style=shield&circle-token=7084a829c9e63b415f59e627d9e4ee90db7d2afa)](https://circleci.com/gh/ProctorU/policy-assertions) [![Gem Version](https://badge.fury.io/rb/policy-assertions.svg)](https://badge.fury.io/rb/policy-assertions)
|
3
4
|
|
4
5
|
Minitest test assertions for [Pundit](https://github.com/elabs/pundit) policies.
|
5
6
|
|
6
7
|
policy-assertions provides a test class for easy Pundit testing. The test class provides assertions and refutations for policies and strong parameters.
|
7
8
|
|
9
|
+
## Table of contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Available test methods](#test-method-naming)
|
14
|
+
* [Configuration](#configuration)
|
15
|
+
* [Developing](#developing)
|
16
|
+
* [Credits](#credits)
|
17
|
+
|
8
18
|
## Installation
|
9
19
|
|
10
20
|
Add this line to your application's Gemfile:
|
@@ -21,7 +31,7 @@ Or install it yourself as:
|
|
21
31
|
|
22
32
|
$ gem install policy-assertions
|
23
33
|
|
24
|
-
**Add require
|
34
|
+
**Add require policy_assertions to test_helper.rb**
|
25
35
|
|
26
36
|
```ruby
|
27
37
|
require 'policy_assertions'
|
@@ -31,10 +41,10 @@ require 'policy_assertions'
|
|
31
41
|
|
32
42
|
policy-assertions is intended to make testing Pundit policies as simple as possible. The gem adds the following helpers:
|
33
43
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
44
|
+
* PolicyAssertions::Test class
|
45
|
+
* parses permissions to test from method name
|
46
|
+
* assert_permit and refute_permit methods
|
47
|
+
* assert_strong_parameters
|
38
48
|
|
39
49
|
The following code sample illustrates the intended use of this gem.
|
40
50
|
|
@@ -57,6 +67,8 @@ class ArticlePolicyTest < PolicyAssertions::Test
|
|
57
67
|
# Test that this user cannot delete this article
|
58
68
|
def test_destroy
|
59
69
|
refute_permit users(:regular), articles(:instructions)
|
70
|
+
# Alternate method name
|
71
|
+
asssert_not_permitted users(:regular), articles(:instructions)
|
60
72
|
end
|
61
73
|
|
62
74
|
# Test a permission by passing in an array instead of
|
@@ -78,8 +90,19 @@ class ArticlePolicyTest < PolicyAssertions::Test
|
|
78
90
|
end
|
79
91
|
```
|
80
92
|
|
81
|
-
|
82
|
-
|
93
|
+
If policies are namespaced, the invocation of the class name should follow the same syntax as Pundit.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# Test that the Organizations::Article model allows index and show
|
97
|
+
# for any site visitor. nil is passed in for the user.
|
98
|
+
def test_index_and_show
|
99
|
+
assert_permit nil, [:organizations, Article]
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
## Test method naming
|
104
|
+
|
105
|
+
policy-assertions can read the permissions to test from the method name. This will only work when using the minitest def test_name syntax. When using the block syntax, you must explicitly pass the permission names.
|
83
106
|
|
84
107
|
```ruby
|
85
108
|
# Good
|
@@ -92,26 +115,28 @@ end
|
|
92
115
|
def test_show_and_index
|
93
116
|
end
|
94
117
|
|
95
|
-
#
|
96
|
-
# The permission cannot be read
|
118
|
+
# Good block syntax
|
119
|
+
# The permission cannot be automatically read, so you must pass the policy names directly.
|
97
120
|
test 'create' do
|
98
|
-
# passing the permissions to the assert or refute is ok.
|
99
121
|
refute_permit nil, Article, 'create?', 'new?'
|
100
122
|
end
|
101
123
|
```
|
124
|
+
|
102
125
|
Define multiple permissions in a method name by separating the permissions using '\_and\_'.
|
103
126
|
|
104
127
|
See the configuration section for changing the separator value.
|
105
128
|
|
106
|
-
###
|
129
|
+
### assert_permit and refute_permit
|
130
|
+
|
107
131
|
These methods take the following parameters:
|
108
132
|
|
109
|
-
|
110
|
-
|
111
|
-
|
133
|
+
* User to authorize
|
134
|
+
* Model or instance to authorize
|
135
|
+
* Optional array of permissions. They should match the permission method name exactly.
|
112
136
|
|
113
137
|
#### Passing permissions to assert and refute
|
114
|
-
|
138
|
+
|
139
|
+
When permissions are passed to assert or refute, the test method name is ignored and does not need to match a policy permission.
|
115
140
|
|
116
141
|
```ruby
|
117
142
|
class ArticlePolicyTest < PolicyAssertions::Test
|
@@ -124,7 +149,8 @@ end
|
|
124
149
|
```
|
125
150
|
|
126
151
|
### Using the rails test block helper
|
127
|
-
|
152
|
+
|
153
|
+
policy-assertions will work with the rails test block helper but it cannot parse the permissions. If a test block is used and the permissions are not passed to the `assert` and `refute` methods, a PolicyAssertions::MissingBlockParameters error will be thrown.
|
128
154
|
|
129
155
|
```ruby
|
130
156
|
class ArticlePolicyTest < PolicyAssertions::Test
|
@@ -132,6 +158,11 @@ class ArticlePolicyTest < PolicyAssertions::Test
|
|
132
158
|
assert_permit @user, Article, 'index?', 'show?'
|
133
159
|
end
|
134
160
|
|
161
|
+
# Actions can also be passed as an array
|
162
|
+
test 'index?' do
|
163
|
+
assert_permit @user, Article, %w(index? show?)
|
164
|
+
end
|
165
|
+
|
135
166
|
# this will result in a
|
136
167
|
# PolicyAssertions::MissingBlockParameters error
|
137
168
|
test 'show?' do
|
@@ -141,6 +172,7 @@ end
|
|
141
172
|
```
|
142
173
|
|
143
174
|
### Strong Parameters
|
175
|
+
|
144
176
|
Since Pundit offers a [permitted_attributes](https://github.com/elabs/pundit#strong-parameters) helper, policy-assertions provides an assert method for testing.
|
145
177
|
|
146
178
|
```ruby
|
@@ -157,17 +189,39 @@ class ArticlePolicyTest < PolicyAssertions::Test
|
|
157
189
|
end
|
158
190
|
end
|
159
191
|
```
|
160
|
-
|
192
|
+
|
193
|
+
## Configuration
|
194
|
+
|
161
195
|
Use the following in your test helper to change the test definition permissions separator.
|
162
196
|
|
163
197
|
```ruby
|
164
198
|
PolicyAssertions.config.separator = '__separator__'
|
165
199
|
```
|
166
200
|
|
167
|
-
##
|
201
|
+
## Developing
|
168
202
|
|
169
203
|
1. Fork it ( https://github.com/[my-github-username]/policy-assertions/fork )
|
170
204
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
171
205
|
3. Commit your changes with tests (`git commit -am 'Add some feature'`)
|
172
206
|
4. Push to the branch (`git push origin my-new-feature`)
|
173
207
|
5. Create a new Pull Request
|
208
|
+
|
209
|
+
## Credits
|
210
|
+
|
211
|
+
Policy-assertions is maintained and funded by [ProctorU](https://twitter.com/ProctorU),
|
212
|
+
a simple online proctoring service that allows you to take exams or
|
213
|
+
certification tests at home.
|
214
|
+
|
215
|
+
We'd like to thank [@ksimmons](https://github.com/ksimmons) for being the original creator of policy-assertions and allowing us to maintain the project.
|
216
|
+
|
217
|
+
<br>
|
218
|
+
|
219
|
+
<p align="center">
|
220
|
+
<a href="https://twitter.com/ProctorUEng">
|
221
|
+
<img src="https://s3-us-west-2.amazonaws.com/dev-team-resources/procki-eyes.svg" width=108 height=72>
|
222
|
+
</a>
|
223
|
+
|
224
|
+
<h3 align="center">
|
225
|
+
<a href="https://twitter.com/ProctorUEng">ProctorU Engineering & Design</a>
|
226
|
+
</h3>
|
227
|
+
</p>
|
data/circle.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
dependencies:
|
2
|
+
override:
|
3
|
+
- 'rvm-exec 2.1.10 bundle install'
|
4
|
+
- 'rvm-exec 2.2.8 bundle install'
|
5
|
+
- 'rvm-exec 2.3.5 bundle install'
|
6
|
+
- 'rvm-exec 2.4.2 bundle install'
|
7
|
+
|
8
|
+
test:
|
9
|
+
override:
|
10
|
+
- 'rvm-exec 2.1.10 bundle exec rake'
|
11
|
+
- 'rvm-exec 2.2.8 bundle exec rake'
|
12
|
+
- 'rvm-exec 2.3.5 bundle exec rake'
|
13
|
+
- 'rvm-exec 2.4.2 bundle exec rake'
|
data/lib/policy_assertions.rb
CHANGED
@@ -16,7 +16,7 @@ require 'policy_assertions/configuration'
|
|
16
16
|
module PolicyAssertions
|
17
17
|
class Test < ActiveSupport::TestCase
|
18
18
|
def assert_permit(user, record, *permissions)
|
19
|
-
get_permissions(permissions).each do |permission|
|
19
|
+
get_permissions(permissions.flatten).each do |permission|
|
20
20
|
policy = Pundit.policy!(user, record)
|
21
21
|
assert policy.public_send(permission),
|
22
22
|
"Expected #{policy.class.name} to grant #{permission} "\
|
@@ -25,13 +25,14 @@ module PolicyAssertions
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def refute_permit(user, record, *permissions)
|
28
|
-
get_permissions(permissions).each do |permission|
|
28
|
+
get_permissions(permissions.flatten).each do |permission|
|
29
29
|
policy = Pundit.policy!(user, record)
|
30
30
|
refute policy.public_send(permission),
|
31
31
|
"Expected #{policy.class.name} not to grant #{permission} "\
|
32
32
|
"on #{record} for #{user} but it did"
|
33
33
|
end
|
34
34
|
end
|
35
|
+
alias assert_not_permitted refute_permit
|
35
36
|
|
36
37
|
def assert_strong_parameters(user, record, params_hash, allowed_params)
|
37
38
|
policy = Pundit.policy!(user, record)
|
data/policy-assertions.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'policy_assertions/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "policy-assertions"
|
8
8
|
spec.version = PolicyAssertions::VERSION
|
9
|
-
spec.authors = ['
|
10
|
-
spec.email = ['
|
9
|
+
spec.authors = ['ProctorU']
|
10
|
+
spec.email = ['engineering@proctoru.com']
|
11
11
|
spec.summary = %q{Minitest assertions for Pundit policies.}
|
12
12
|
spec.description = %q{Minitest assertions for Pundit policies.}
|
13
13
|
spec.homepage = 'https://github.com/ksimmons/policy-assertions'
|
@@ -102,6 +102,17 @@ class AssertionsTest < Minitest::Test
|
|
102
102
|
assert test_runner.passed?
|
103
103
|
end
|
104
104
|
|
105
|
+
def test_permission_failed_as_assert_not_permitted
|
106
|
+
test_runner = policy_class do
|
107
|
+
def test_create
|
108
|
+
assert_not_permitted nil, Article
|
109
|
+
end
|
110
|
+
end.new :test_create
|
111
|
+
|
112
|
+
test_runner.run
|
113
|
+
assert test_runner.passed?
|
114
|
+
end
|
115
|
+
|
105
116
|
def test_destroy
|
106
117
|
test_runner = policy_class do
|
107
118
|
def test_destroy
|
@@ -197,8 +208,20 @@ class ValidBlockParametersTest
|
|
197
208
|
assert_permit nil, Article, 'index?', 'long_action?'
|
198
209
|
end
|
199
210
|
|
211
|
+
test 'assert_permit index? as array' do
|
212
|
+
assert_permit nil, Article, %w(index? long_action?)
|
213
|
+
end
|
214
|
+
|
200
215
|
test 'destroy?' do
|
201
216
|
refute_permit nil, Article, 'destroy?'
|
202
217
|
end
|
218
|
+
|
219
|
+
test 'destroy? with assert_not_permitted' do
|
220
|
+
assert_not_permitted nil, Article, 'destroy?'
|
221
|
+
end
|
222
|
+
|
223
|
+
test 'refute_permit destroy? as array' do
|
224
|
+
refute_permit nil, Article, %w(destroy?)
|
225
|
+
end
|
203
226
|
end
|
204
227
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: policy-assertions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- ProctorU
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -124,19 +124,19 @@ dependencies:
|
|
124
124
|
version: 3.0.0
|
125
125
|
description: Minitest assertions for Pundit policies.
|
126
126
|
email:
|
127
|
-
-
|
127
|
+
- engineering@proctoru.com
|
128
128
|
executables: []
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
133
|
- ".rubocop.yml"
|
134
|
-
- ".travis.yml"
|
135
134
|
- CHANGELOG.md
|
136
135
|
- Gemfile
|
137
136
|
- LICENSE.txt
|
138
137
|
- README.md
|
139
138
|
- Rakefile
|
139
|
+
- circle.yml
|
140
140
|
- lib/policy_assertions.rb
|
141
141
|
- lib/policy_assertions/configuration.rb
|
142
142
|
- lib/policy_assertions/errors.rb
|
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
version: '0'
|
168
168
|
requirements: []
|
169
169
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.
|
170
|
+
rubygems_version: 2.6.8
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Minitest assertions for Pundit policies.
|
@@ -177,4 +177,3 @@ test_files:
|
|
177
177
|
- test/lib/policy-assertions/version_test.rb
|
178
178
|
- test/lib/policy_assertions_test.rb
|
179
179
|
- test/test_helper.rb
|
180
|
-
has_rdoc:
|
data/.travis.yml
DELETED