policy-assertions 0.0.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 +7 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +11 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +176 -0
- data/Rakefile +10 -0
- data/lib/policy_assertions/configuration.rb +9 -0
- data/lib/policy_assertions/errors.rb +16 -0
- data/lib/policy_assertions/version.rb +3 -0
- data/lib/policy_assertions.rb +81 -0
- data/policy-assertions.gemspec +32 -0
- data/test/.rubocop.yml +9 -0
- data/test/lib/policy-assertions/configuration_test.rb +17 -0
- data/test/lib/policy-assertions/version_test.rb +7 -0
- data/test/lib/policy_assertions_test.rb +172 -0
- data/test/test_helper.rb +66 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a38fcb854b7f7f60bbaacd50911686b294ec29f6
|
4
|
+
data.tar.gz: b7f5ecf2f114e7180bda034e1e1386aecc27db14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d410b2390857310401408497dacdfa4024ad8463606a03f89dfe202ff8b6deb6f79a3e50c589bd52d332d04b231fd17775b1fff4d9cb93dc4e95600af0bf7fa
|
7
|
+
data.tar.gz: 2d061e1749a933d9bae107741d66c4e25835be16f88c729b795ec62c41054ebfd8441e206d718dd643fd7d20b9a4caa0bd5a7fd008f2738b338cd2300570a84c
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Kevin Simmons
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# policy-assertions
|
2
|
+
[](https://travis-ci.org/ksimmons/policy-assertions)
|
3
|
+
|
4
|
+
Minitest test assertions for [Pundit](https://github.com/elabs/pundit) policies.
|
5
|
+
|
6
|
+
policy-assertions provides a test class for easy Pundit testing. The test class provides assertions and refutations for policies and strong parameters.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'policy-assertions'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install policy-assertions
|
23
|
+
|
24
|
+
**Add require policy\_assertions to test\_helper.rb**
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'policy_assertions'
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
policy-assertions is intended to make testing Pundit policies as simple as possible. The gem adds the following helpers:
|
33
|
+
|
34
|
+
- PolicyAssertions::Test class
|
35
|
+
- parses permissions to test from method name
|
36
|
+
- assert\_permit and refute\_permit methods
|
37
|
+
- assert\_strong\_parameters
|
38
|
+
|
39
|
+
The following code sample illustrates the intended use of this gem.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# The class is named after the policy to be tested.
|
43
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
44
|
+
|
45
|
+
# Test that the Article model allows index and show
|
46
|
+
# for any site visitor. nil is passed in for the user.
|
47
|
+
def test_index_and_show
|
48
|
+
assert_permit nil, Article
|
49
|
+
end
|
50
|
+
|
51
|
+
# Test that a site staff member is allowed access
|
52
|
+
# to new and create
|
53
|
+
def test_new_and_create
|
54
|
+
assert_permit users(:staff), Article
|
55
|
+
end
|
56
|
+
|
57
|
+
# Test that this user cannot delete this article
|
58
|
+
def test_destroy
|
59
|
+
refute_permit users(:regular), articles(:instructions)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Test a permission by passing in an array instead of
|
63
|
+
# defining it in the method name
|
64
|
+
def test_name_is_not_a_permission
|
65
|
+
refute_permit nil, Article, 'create?', 'new?'
|
66
|
+
end
|
67
|
+
|
68
|
+
# Test that a site staff member has access to the
|
69
|
+
# parameters defined in the params array.
|
70
|
+
# Site visitors should not have access to any Article attributes
|
71
|
+
def test_strong_parameters
|
72
|
+
params = [:title, :body, :tags]
|
73
|
+
assert_strong_parameters(users(:staff), Article,
|
74
|
+
article_attributes, params)
|
75
|
+
|
76
|
+
assert_strong_parameters(nil, Article, article_attributes, [])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Test method naming
|
82
|
+
policy-assertions can read the permissions to test from the method name. This will only work when using the minitest def test_name syntax, it does not work for the Rails test block helper.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# Good
|
86
|
+
# The create permission will be parsed from this method name
|
87
|
+
def test_create
|
88
|
+
end
|
89
|
+
|
90
|
+
# Good
|
91
|
+
# multiple permissions are defined in this method name
|
92
|
+
def test_show_and_index
|
93
|
+
end
|
94
|
+
|
95
|
+
# Not good
|
96
|
+
# The permission cannot be read from this block.
|
97
|
+
test 'create' do
|
98
|
+
# passing the permissions to the assert or refute is ok.
|
99
|
+
refute_permit nil, Article, 'create?', 'new?'
|
100
|
+
end
|
101
|
+
```
|
102
|
+
Define multiple permissions in a method name by separating the permissions using '\_and\_'.
|
103
|
+
|
104
|
+
See the configuration section for changing the separator value.
|
105
|
+
|
106
|
+
### assert\_permit and refute\_permit
|
107
|
+
These methods take the following parameters:
|
108
|
+
|
109
|
+
- User to authorize
|
110
|
+
- Model or instance to authorize
|
111
|
+
- Optional array of permissions. They should match the permission method name exactly.
|
112
|
+
|
113
|
+
#### Passing permissions to assert and refute
|
114
|
+
When permissions are passed to assert or refute the test method name is ignored and does not need to match a policy permission.
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
118
|
+
# this method name is not parsed since the permissions
|
119
|
+
# are passed into the method
|
120
|
+
def test_that_a_user_can_do_stuff
|
121
|
+
assert_permit nil, Article, 'show?', 'index?'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
126
|
+
### Using the rails test block helper
|
127
|
+
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
|
+
|
129
|
+
```ruby
|
130
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
131
|
+
test 'index?' do
|
132
|
+
assert_permit @user, Article, 'index?', 'show?'
|
133
|
+
end
|
134
|
+
|
135
|
+
# this will result in a
|
136
|
+
# PolicyAssertions::MissingBlockParameters error
|
137
|
+
test 'show?' do
|
138
|
+
assert_permit @user, Article
|
139
|
+
end
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
### Strong Parameters
|
144
|
+
Since Pundit offers a [permitted_attributes](https://github.com/elabs/pundit#strong-parameters) helper, policy-assertions provides an assert method for testing.
|
145
|
+
|
146
|
+
To use this assertion the test class **must** match an existing policy with 'Test' appended. If the class name does not match a policy a PolicyAssertions::InvalidClassName error is thrown. See the code sample below.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
# The class name matches the ArticlePolicy class.
|
150
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
151
|
+
# Test that a site staff member has access to the
|
152
|
+
# parameters defined in the params method.
|
153
|
+
# Site visitors should not have access to any Article attributes
|
154
|
+
def test_strong_parameters
|
155
|
+
params = [:title, :body, :tags]
|
156
|
+
assert_strong_parameters(users(:staff), Article,
|
157
|
+
article_attributes, params)
|
158
|
+
|
159
|
+
assert_strong_parameters(nil, Article, article_attributes, [])
|
160
|
+
end
|
161
|
+
end
|
162
|
+
```
|
163
|
+
## Configure
|
164
|
+
Use the following in your test helper to change the test definition permissions separator.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
PolicyAssertions.config.separator = '__separator__'
|
168
|
+
```
|
169
|
+
|
170
|
+
## Contributing
|
171
|
+
|
172
|
+
1. Fork it ( https://github.com/[my-github-username]/policy-assertions/fork )
|
173
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
174
|
+
3. Commit your changes with tests (`git commit -am 'Add some feature'`)
|
175
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
176
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module PolicyAssertions
|
2
|
+
class InvalidClassName < StandardError
|
3
|
+
def message
|
4
|
+
'The test class must be the same as a pundit policy class. ' \
|
5
|
+
'For example, RecordPolicyTest'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class MissingBlockParameters < StandardError
|
10
|
+
def message
|
11
|
+
'PolicyTest must pass the permissions into the assert if called ' \
|
12
|
+
'from the Rails test block method. Use def test_testname method ' \
|
13
|
+
'to have the permissions parsed automatically.'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/test'
|
3
|
+
require 'active_support'
|
4
|
+
require 'action_controller/metal/strong_parameters'
|
5
|
+
|
6
|
+
require 'policy_assertions/errors'
|
7
|
+
require 'policy_assertions/version'
|
8
|
+
require 'policy_assertions/configuration'
|
9
|
+
|
10
|
+
module PolicyAssertions
|
11
|
+
class Test < ActiveSupport::TestCase
|
12
|
+
def assert_permit(user, record, *permissions)
|
13
|
+
get_permissions(permissions).each do |permission|
|
14
|
+
policy = Pundit.policy!(user, record)
|
15
|
+
assert policy.public_send(permission),
|
16
|
+
"Expected #{policy.class.name} to grant #{permission} "\
|
17
|
+
"on #{record} for #{user} but it didn't"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def refute_permit(user, record, *permissions)
|
22
|
+
get_permissions(permissions).each do |permission|
|
23
|
+
policy = Pundit.policy!(user, record)
|
24
|
+
refute policy.public_send(permission),
|
25
|
+
"Expected #{policy.class.name} not to grant #{permission} "\
|
26
|
+
"on #{record} for #{user} but it did"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def assert_strong_parameters(user, record,
|
31
|
+
params_hash, allowed_params)
|
32
|
+
params = ActionController::Parameters.new(class_symbol => params_hash)
|
33
|
+
|
34
|
+
strong_params = params.require(class_symbol)
|
35
|
+
.permit(*get_strong_params(user, record)).keys
|
36
|
+
|
37
|
+
strong_params.each do |param|
|
38
|
+
assert_includes allowed_params,
|
39
|
+
param.to_sym,
|
40
|
+
"User #{user} should not be permitted to "\
|
41
|
+
"update parameter [#{param}]"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def class_symbol
|
48
|
+
@class_symbol ||= klass.to_s.gsub(/Policy/, '').downcase.to_sym
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_strong_params(user, record)
|
52
|
+
klass.new(user, record).permitted_attributes
|
53
|
+
end
|
54
|
+
|
55
|
+
def klass
|
56
|
+
@klass ||= self.class.name.demodulize.to_s.gsub(/Test/, '').constantize
|
57
|
+
rescue NameError
|
58
|
+
raise InvalidClassName
|
59
|
+
end
|
60
|
+
|
61
|
+
def get_permissions(permissions)
|
62
|
+
return permissions if permissions.present?
|
63
|
+
|
64
|
+
name = calling_method
|
65
|
+
|
66
|
+
fail(MissingBlockParameters) if name.start_with?('block')
|
67
|
+
|
68
|
+
# remove 'test_' and split
|
69
|
+
# append ? to the permission
|
70
|
+
name[5..-1].split(PolicyAssertions.config.separator).map { |a| "#{a}?" }
|
71
|
+
end
|
72
|
+
|
73
|
+
def calling_method
|
74
|
+
if PolicyAssertions.config.ruby_version > 1
|
75
|
+
caller_locations(3, 1)[0].label
|
76
|
+
else
|
77
|
+
caller[2][/`.*'/][1..-2]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'policy_assertions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "policy-assertions"
|
8
|
+
spec.version = PolicyAssertions::VERSION
|
9
|
+
spec.authors = ['Kevin Simmons']
|
10
|
+
spec.email = ['kevin.p.simmons@gmail.com']
|
11
|
+
spec.summary = %q{Minitest assertions for Pundit policies.}
|
12
|
+
spec.description = %q{Minitest assertions for Pundit policies.}
|
13
|
+
spec.homepage = 'https://github.com/ksimmons/policy-assertions'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 1.9.3'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5.6'
|
26
|
+
spec.add_development_dependency 'actionpack', '>= 3.0.0'
|
27
|
+
spec.add_development_dependency 'rack', '~>1.6.1'
|
28
|
+
spec.add_development_dependency 'rack-test', '~> 0.6.3'
|
29
|
+
|
30
|
+
spec.add_dependency 'pundit', '~> 1.0.0'
|
31
|
+
spec.add_dependency 'activesupport', '>= 3.0.0'
|
32
|
+
end
|
data/test/.rubocop.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConfigurationTest < Minitest::Test
|
4
|
+
def test_configuration_exists
|
5
|
+
refute_nil PolicyAssertions.config
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_seperator
|
9
|
+
assert_equal '_and_', PolicyAssertions.config.separator
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_ruby_version
|
13
|
+
refute_nil PolicyAssertions.config.ruby_version
|
14
|
+
|
15
|
+
assert PolicyAssertions.config.ruby_version > 0
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MultipleAssertionsTest < Minitest::Test
|
4
|
+
def test_assert_multiple_permissions_pass
|
5
|
+
test_runner = policy_class do
|
6
|
+
def test_index_and_create
|
7
|
+
assert_permit User.new, Article
|
8
|
+
end
|
9
|
+
end.new :test_index_and_create
|
10
|
+
|
11
|
+
test_runner.run
|
12
|
+
assert test_runner.passed?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_assert_multiple_permissions_fail
|
16
|
+
test_runner = policy_class do
|
17
|
+
def test_index_and_create
|
18
|
+
assert_permit nil, Article
|
19
|
+
end
|
20
|
+
end.new :test_index_and_create
|
21
|
+
|
22
|
+
test_runner.run
|
23
|
+
refute test_runner.passed?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_refute_multiple_permissions_pass
|
27
|
+
test_runner = policy_class do
|
28
|
+
def test_index_and_create
|
29
|
+
refute_permit nil, Article
|
30
|
+
end
|
31
|
+
end.new :test_index_and_create
|
32
|
+
|
33
|
+
test_runner.run
|
34
|
+
refute test_runner.passed?
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_refute_multiple_permissions_fail
|
38
|
+
test_runner = policy_class do
|
39
|
+
def test_index_and_create
|
40
|
+
refute_permit User.new, Article
|
41
|
+
end
|
42
|
+
end.new :test_index_and_create
|
43
|
+
|
44
|
+
test_runner.run
|
45
|
+
refute test_runner.passed?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class AssertionsTest < Minitest::Test
|
50
|
+
def test_long_permission_names
|
51
|
+
test_runner = policy_class do
|
52
|
+
def test_long_action_and_index
|
53
|
+
assert_permit nil, Article
|
54
|
+
end
|
55
|
+
end.new :test_long_action_and_index
|
56
|
+
|
57
|
+
test_runner.run
|
58
|
+
assert test_runner.passed?
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_permission_passed
|
62
|
+
test_runner = policy_class do
|
63
|
+
def test_create
|
64
|
+
assert_permit User.new, Article
|
65
|
+
end
|
66
|
+
end.new :test_create
|
67
|
+
|
68
|
+
test_runner.run
|
69
|
+
assert test_runner.passed?
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_permission_failed_expected_to_pass
|
73
|
+
test_runner = policy_class do
|
74
|
+
def test_create
|
75
|
+
assert_permit nil, Article
|
76
|
+
end
|
77
|
+
end.new :test_create
|
78
|
+
|
79
|
+
test_runner.run
|
80
|
+
refute test_runner.passed?
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_permission_passed_expected_to_fail
|
84
|
+
test_runner = policy_class do
|
85
|
+
def test_create
|
86
|
+
refute_permit User.new, Article
|
87
|
+
end
|
88
|
+
end.new :test_create
|
89
|
+
|
90
|
+
test_runner.run
|
91
|
+
refute test_runner.passed?
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_permission_failed
|
95
|
+
test_runner = policy_class do
|
96
|
+
def test_create
|
97
|
+
refute_permit nil, Article
|
98
|
+
end
|
99
|
+
end.new :test_create
|
100
|
+
|
101
|
+
test_runner.run
|
102
|
+
assert test_runner.passed?
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_destroy
|
106
|
+
test_runner = policy_class do
|
107
|
+
def test_destroy
|
108
|
+
assert_permit User.new(100), Article.new(100)
|
109
|
+
end
|
110
|
+
end.new :test_destroy
|
111
|
+
|
112
|
+
test_runner.run
|
113
|
+
assert test_runner.passed?
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class StrongParametersTest < Minitest::Test
|
118
|
+
def test_strong_parameters_pass
|
119
|
+
test_runner = policy_class do
|
120
|
+
def test_strong_parameters
|
121
|
+
allowed = [:user_id, :title, :description]
|
122
|
+
assert_strong_parameters User.new(1), Article, Article.params, allowed
|
123
|
+
end
|
124
|
+
end.new :test_strong_parameters
|
125
|
+
|
126
|
+
test_runner.run
|
127
|
+
assert test_runner.passed?
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_strong_parameters_fail
|
131
|
+
test_runner = policy_class do
|
132
|
+
def test_strong_parameters
|
133
|
+
assert_strong_parameters nil, Article, Article.params, [:user_id]
|
134
|
+
end
|
135
|
+
end.new :test_strong_parameters
|
136
|
+
|
137
|
+
test_runner.run
|
138
|
+
refute test_runner.passed?
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class InvalidClassNameTest
|
143
|
+
class FakePolicyTest < PolicyAssertions::Test
|
144
|
+
def test_strong_parameters
|
145
|
+
assert_raises(PolicyAssertions::InvalidClassName) do
|
146
|
+
assert_strong_parameters nil, Article, Article.params, [:user_id]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
class InvalidBlockParametersTest
|
153
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
154
|
+
test 'index?' do
|
155
|
+
assert_raises(PolicyAssertions::MissingBlockParameters) do
|
156
|
+
assert_permit nil, Article
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class ValidBlockParametersTest
|
163
|
+
class ArticlePolicyTest < PolicyAssertions::Test
|
164
|
+
test 'index?' do
|
165
|
+
assert_permit nil, Article, 'index?', 'long_action?'
|
166
|
+
end
|
167
|
+
|
168
|
+
test 'destroy?' do
|
169
|
+
refute_permit nil, Article, 'destroy?'
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/pride'
|
3
|
+
require 'policy_assertions'
|
4
|
+
require 'pundit'
|
5
|
+
|
6
|
+
ActiveSupport::TestCase.test_order = :random
|
7
|
+
|
8
|
+
# create a dynamic class using the passed in block
|
9
|
+
# and names it correctly
|
10
|
+
def policy_class(&block)
|
11
|
+
klass = Class.new(PolicyAssertions::Test, &block)
|
12
|
+
def klass.name
|
13
|
+
'ArticlePolicyTest'
|
14
|
+
end
|
15
|
+
|
16
|
+
klass
|
17
|
+
end
|
18
|
+
|
19
|
+
class User
|
20
|
+
attr_accessor :id
|
21
|
+
|
22
|
+
def initialize(id = nil)
|
23
|
+
@id = id if id
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Article
|
28
|
+
attr_accessor :user_id
|
29
|
+
|
30
|
+
def initialize(user_id)
|
31
|
+
@user_id = user_id
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.params
|
35
|
+
{ :user_id => 1, :title => 'title', :description => 'test description' }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class ArticlePolicy
|
40
|
+
attr_reader :user, :record
|
41
|
+
|
42
|
+
def initialize(user, record)
|
43
|
+
@user = user
|
44
|
+
@record = record
|
45
|
+
end
|
46
|
+
|
47
|
+
def index?
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
def long_action?
|
52
|
+
true
|
53
|
+
end
|
54
|
+
|
55
|
+
def create?
|
56
|
+
@user
|
57
|
+
end
|
58
|
+
|
59
|
+
def destroy?
|
60
|
+
@user && @user.id == @record.user_id
|
61
|
+
end
|
62
|
+
|
63
|
+
def permitted_attributes
|
64
|
+
(@user && @user.id == 1) ? [:user_id, :title, :description] : [:title]
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: policy-assertions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Simmons
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: actionpack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.6.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.6.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rack-test
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.6.3
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.6.3
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pundit
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.0.0
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.0.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activesupport
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.0.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 3.0.0
|
125
|
+
description: Minitest assertions for Pundit policies.
|
126
|
+
email:
|
127
|
+
- kevin.p.simmons@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rubocop.yml"
|
134
|
+
- ".travis.yml"
|
135
|
+
- CHANGELOG.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- lib/policy_assertions.rb
|
141
|
+
- lib/policy_assertions/configuration.rb
|
142
|
+
- lib/policy_assertions/errors.rb
|
143
|
+
- lib/policy_assertions/version.rb
|
144
|
+
- policy-assertions.gemspec
|
145
|
+
- test/.rubocop.yml
|
146
|
+
- test/lib/policy-assertions/configuration_test.rb
|
147
|
+
- test/lib/policy-assertions/version_test.rb
|
148
|
+
- test/lib/policy_assertions_test.rb
|
149
|
+
- test/test_helper.rb
|
150
|
+
homepage: https://github.com/ksimmons/policy-assertions
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: 1.9.3
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.4.1
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Minitest assertions for Pundit policies.
|
174
|
+
test_files:
|
175
|
+
- test/.rubocop.yml
|
176
|
+
- test/lib/policy-assertions/configuration_test.rb
|
177
|
+
- test/lib/policy-assertions/version_test.rb
|
178
|
+
- test/lib/policy_assertions_test.rb
|
179
|
+
- test/test_helper.rb
|
180
|
+
has_rdoc:
|