policy-assertions 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53e8739d031b758729afa0bbb35943b9435d82e0
4
- data.tar.gz: 7084ff252fda8db066873be079a1955320e3a52d
3
+ metadata.gz: de43c6a77ae8bc5ef2887f016bc04b7924842852
4
+ data.tar.gz: a68e538a28931ce1702a987c17ec56c433a132ee
5
5
  SHA512:
6
- metadata.gz: 3ea9753e8d7a4b09f8add5e23f9f4077769d53a85123175e035e25287680bf6ec64df9526fb2a2bb8137810aac93a94be497ced0bb8057f776cb9c12186cb5ac
7
- data.tar.gz: c90daf60ee58b1b60107c2bf76c18f7f373b3e3004027677969aa18556733856a549604e126fc83871e21fd68d041857ff99b4de11047811858812fb32c987b2
6
+ metadata.gz: d3958f8816690edb5148180e06b4f4245f98ff1f1a9e68e0643444a2793001f9f8d3b5fb91288a9f4f2fcbf74c85a2e4a70d4ff57addfc9a376ab5c3780991b7
7
+ data.tar.gz: 40c562a177f6387d8bff50d8aec2dbf838d46f5fb7399d96b0dbcc8a87a285266c9c166248cb9bb253d94adfdee376bd3a0d91cb02aaf44ab9846fbd88af6e42
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.0.3
2
+
3
+ * Compatibility with pundit 1.1.0
4
+
1
5
  ## v0.0.1
2
6
 
3
7
  * initial release
data/README.md CHANGED
@@ -143,10 +143,7 @@ end
143
143
  ### Strong Parameters
144
144
  Since Pundit offers a [permitted_attributes](https://github.com/elabs/pundit#strong-parameters) helper, policy-assertions provides an assert method for testing.
145
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
146
  ```ruby
149
- # The class name matches the ArticlePolicy class.
150
147
  class ArticlePolicyTest < PolicyAssertions::Test
151
148
  # Test that a site staff member has access to the
152
149
  # parameters defined in the params method.
@@ -33,16 +33,18 @@ module PolicyAssertions
33
33
  end
34
34
  end
35
35
 
36
- def assert_strong_parameters(user, record,
37
- params_hash, allowed_params)
38
- params = ActionController::Parameters.new(class_symbol => params_hash)
36
+ def assert_strong_parameters(user, record, params_hash, allowed_params)
37
+ policy = Pundit.policy!(user, record)
39
38
 
40
- strong_params = params.require(class_symbol)
41
- .permit(*get_strong_params(user, record)).keys
39
+ param_key = find_param_key(record)
40
+
41
+ params = ActionController::Parameters.new(param_key => params_hash)
42
+
43
+ strong_params = params.require(param_key)
44
+ .permit(*policy.permitted_attributes).keys
42
45
 
43
46
  strong_params.each do |param|
44
- assert_includes allowed_params,
45
- param.to_sym,
47
+ assert_includes allowed_params, param.to_sym,
46
48
  "User #{user} should not be permitted to "\
47
49
  "update parameter [#{param}]"
48
50
  end
@@ -50,18 +52,15 @@ module PolicyAssertions
50
52
 
51
53
  private
52
54
 
53
- def class_symbol
54
- @class_symbol ||= klass.to_s.gsub(/Policy/, '').downcase.to_sym
55
- end
56
-
57
- def get_strong_params(user, record)
58
- klass.new(user, record).permitted_attributes
59
- end
60
-
61
- def klass
62
- @klass ||= self.class.name.demodulize.to_s.gsub(/Test/, '').constantize
63
- rescue NameError
64
- raise InvalidClassName
55
+ # borrowed from Pundit::PolicyFinder
56
+ def find_param_key(record)
57
+ if record.respond_to?(:model_name)
58
+ record.model_name.param_key.to_s
59
+ elsif record.is_a?(Class)
60
+ record.to_s.demodulize.underscore
61
+ else
62
+ record.class.to_s.demodulize.underscore
63
+ end
65
64
  end
66
65
 
67
66
  def get_permissions(permissions)
@@ -1,11 +1,4 @@
1
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
2
  class MissingBlockParameters < StandardError
10
3
  def message
11
4
  'PolicyTest must pass the permissions into the assert if called ' \
@@ -1,3 +1,3 @@
1
1
  module PolicyAssertions
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'minitest', '~> 5.6'
26
26
  spec.add_development_dependency 'actionpack', '>= 3.0.0'
27
- spec.add_development_dependency 'rack', '~>1.6.1'
27
+ spec.add_development_dependency 'rack', '~> 1.6.1'
28
28
  spec.add_development_dependency 'rack-test', '~> 0.6.3'
29
29
 
30
- spec.add_dependency 'pundit', '~> 1.0.0'
30
+ spec.add_dependency 'pundit', '>= 1.0.0'
31
31
  spec.add_dependency 'activesupport', '>= 3.0.0'
32
32
  end
@@ -139,12 +139,11 @@ class StrongParametersTest < Minitest::Test
139
139
  end
140
140
  end
141
141
 
142
- class InvalidClassNameTest
142
+ class DifferentClassNameTest
143
143
  class FakePolicyTest < PolicyAssertions::Test
144
144
  def test_strong_parameters
145
- assert_raises(PolicyAssertions::InvalidClassName) do
146
- assert_strong_parameters nil, Article, Article.params, [:user_id]
147
- end
145
+ allowed = [:user_id, :title, :description]
146
+ assert_strong_parameters User.new(1), Article, Article.params, allowed
148
147
  end
149
148
  end
150
149
  end
@@ -159,6 +158,39 @@ class InvalidBlockParametersTest
159
158
  end
160
159
  end
161
160
 
161
+ class DefinedPolicyClassTest
162
+ class PersonPolicyTest < PolicyAssertions::Test
163
+ def test_create_and_destroy
164
+ assert_permit User.new(100), User.new(101)
165
+ end
166
+
167
+ def test_strong_parameters
168
+ assert_strong_parameters nil, User, User.params, [:user_id, :name]
169
+ end
170
+ end
171
+ end
172
+
173
+ # rubocop:disable Style/ClassAndModuleChildren:
174
+ class ModularizedPolicyClassTest
175
+ class Users::SessionPolicyTest < PolicyAssertions::Test
176
+ def test_create_and_destroy
177
+ assert_permit User.new(100), Users::Session.new(100)
178
+ end
179
+
180
+ def test_destroy
181
+ refute_permit User.new(100), Users::Session.new(101)
182
+ refute_permit nil, Users::Session.new
183
+ end
184
+
185
+ def test_strong_parameters
186
+ assert_strong_parameters User.new,
187
+ Users::Session,
188
+ Users::Session.params,
189
+ [:id, :user_id]
190
+ end
191
+ end
192
+ end
193
+
162
194
  class ValidBlockParametersTest
163
195
  class ArticlePolicyTest < PolicyAssertions::Test
164
196
  test 'index?' do
data/test/test_helper.rb CHANGED
@@ -16,11 +16,19 @@ def policy_class(&block)
16
16
  end
17
17
 
18
18
  class User
19
+ def self.policy_class
20
+ PersonPolicy
21
+ end
22
+
19
23
  attr_accessor :id
20
24
 
21
25
  def initialize(id = nil)
22
26
  @id = id if id
23
27
  end
28
+
29
+ def self.params
30
+ { :user_id => 1, :name => 'name', :role => 'admin' }
31
+ end
24
32
  end
25
33
 
26
34
  class Article
@@ -63,3 +71,67 @@ class ArticlePolicy
63
71
  (@user && @user.id == 1) ? [:user_id, :title, :description] : [:title]
64
72
  end
65
73
  end
74
+
75
+ class PersonPolicy
76
+ attr_reader :user, :record
77
+
78
+ def initialize(user, record)
79
+ @user = user
80
+ @record = record
81
+ end
82
+
83
+ def create?
84
+ true
85
+ end
86
+
87
+ def destroy?
88
+ @user
89
+ end
90
+
91
+ def permitted_attributes
92
+ (@user && @user.id == 1) ? [:user_id, :name, :role] : [:user_id, :name]
93
+ end
94
+ end
95
+
96
+ module Users
97
+ class Session
98
+ attr_accessor :id, :user_id
99
+
100
+ def initialize(user_id = nil)
101
+ @id = random_id
102
+ @user_id = user_id || random_id
103
+ end
104
+
105
+ def self.params
106
+ { :id => @id, :user_id => @user_id, :name => 'session_name' }
107
+ end
108
+
109
+ private
110
+
111
+ def random_id
112
+ 100 + Random.rand(1000)
113
+ end
114
+ end
115
+
116
+ class SessionPolicy
117
+ attr_reader :user, :record
118
+
119
+ def initialize(user, record)
120
+ @user = user
121
+ @record = record
122
+ end
123
+
124
+ def create?
125
+ @user
126
+ end
127
+
128
+ def destroy?
129
+ @user && @user.id == record.user_id
130
+ end
131
+
132
+ def permitted_attributes
133
+ return [] unless @user
134
+ @user.id == 1 ? [:id, :user_id, :name] : [:id, :user_id]
135
+ end
136
+ end
137
+ 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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Simmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,14 +98,14 @@ dependencies:
98
98
  name: pundit
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: 1.0.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: 1.0.0
111
111
  - !ruby/object:Gem::Dependency