policy-assertions 0.1.1 → 0.2.0

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
- SHA1:
3
- metadata.gz: f24ccf11ad4ed8b8cfbc67593fdb47ee45acee40
4
- data.tar.gz: 1383189eb57f1a60a320658985f8ca7f1853289d
2
+ SHA256:
3
+ metadata.gz: 2e05d231a95ef387b2eacf9fa1cd51c5a920e3425562ec6d2047d8db91469c98
4
+ data.tar.gz: 21bd13d4d9146cef4e13de41b7c7a5c12114a916751f414e5c820ae1bb07e13e
5
5
  SHA512:
6
- metadata.gz: a5fc2eee22de0f4b9f5c0c6cb899035cef25322c09879613c83bde1b0c2e5ae943fe8f51b760223fe289889b628142e4235000fb62cceba3f5393728113d9338
7
- data.tar.gz: c3dc79ede4de1fd15a5c0f2979c7c615a21f6c1d047ae29717400624881d61b062737a5489cbdf737196d77876fab87bd9fe0e899e32d0f55338d016823d5245
6
+ metadata.gz: 8abbbb6535264993c6f11e71535e0fa5d4c96ff40e797b6ecfeb1f20e4ff9eaf94495554a6b897c47c6283d721f3215002903d083f1f3a52848f0f9a4e68cdf8
7
+ data.tar.gz: cd03f84dc8d8d447f69c3e6c15f44851da05caec8cfa7aca428051d39992398a454c2a58b6c214976c1fa2bf28af4182daf2abb3c1c7f1a701ce7b23157f7b6e
@@ -1,3 +1,6 @@
1
+ ## v0.2.0
2
+ * [Add support for testing policies that are not of type "#{record.class}Policy" #14](https://github.com/ProctorU/policy-assertions/pull/14) - Thanks @marceloeloelo!
3
+
1
4
  ## v0.1.0
2
5
  * [Adds support for arrays as actions in block tests](https://github.com/ProctorU/policy-assertions/pull/10)
3
6
  * [Adds support for `assert_not_permitted`](https://github.com/ProctorU/policy-assertions/pull/12)
@@ -17,7 +17,7 @@ module PolicyAssertions
17
17
  class Test < ActiveSupport::TestCase
18
18
  def assert_permit(user, record, *permissions)
19
19
  get_permissions(permissions.flatten).each do |permission|
20
- policy = Pundit.policy!(user, record)
20
+ policy = find_policy!(user, record)
21
21
  assert policy.public_send(permission),
22
22
  "Expected #{policy.class.name} to grant #{permission} "\
23
23
  "on #{record} for #{user} but it didn't"
@@ -26,7 +26,7 @@ module PolicyAssertions
26
26
 
27
27
  def refute_permit(user, record, *permissions)
28
28
  get_permissions(permissions.flatten).each do |permission|
29
- policy = Pundit.policy!(user, record)
29
+ policy = find_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"
@@ -35,7 +35,7 @@ module PolicyAssertions
35
35
  alias assert_not_permitted refute_permit
36
36
 
37
37
  def assert_strong_parameters(user, record, params_hash, allowed_params)
38
- policy = Pundit.policy!(user, record)
38
+ policy = find_policy!(user, record)
39
39
 
40
40
  param_key = find_param_key(record)
41
41
 
@@ -83,5 +83,10 @@ module PolicyAssertions
83
83
  caller[2][/`.*'/][1..-2]
84
84
  end
85
85
  end
86
+
87
+ def find_policy!(user, record)
88
+ described_policy = self.respond_to?(:described_class) ? described_class : nil
89
+ described_policy ? described_policy.new(user, record) : Pundit.policy!(user, record)
90
+ end
86
91
  end
87
92
  end
@@ -1,3 +1,3 @@
1
1
  module PolicyAssertions
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -7,10 +7,10 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "policy-assertions"
8
8
  spec.version = PolicyAssertions::VERSION
9
9
  spec.authors = ['ProctorU']
10
- spec.email = ['engineering@proctoru.com']
10
+ spec.email = ['engineering.team@proctoru.com']
11
11
  spec.summary = %q{Minitest assertions for Pundit policies.}
12
12
  spec.description = %q{Minitest assertions for Pundit policies.}
13
- spec.homepage = 'https://github.com/ksimmons/policy-assertions'
13
+ spec.homepage = 'https://github.com/proctoru/policy-assertions'
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.required_ruby_version = '>= 1.9.3'
@@ -123,6 +123,21 @@ class AssertionsTest < Minitest::Test
123
123
  test_runner.run
124
124
  assert test_runner.passed?
125
125
  end
126
+
127
+ def test_described_class
128
+ test_runner = custom_policy_class do
129
+ def described_class
130
+ CustomPolicy
131
+ end
132
+
133
+ def test_new
134
+ assert_permit User.new(100), Article.new(100)
135
+ end
136
+ end.new :test_new
137
+
138
+ test_runner.run
139
+ assert test_runner.passed?
140
+ end
126
141
  end
127
142
 
128
143
  class StrongParametersTest < Minitest::Test
@@ -15,6 +15,15 @@ def policy_class(&block)
15
15
  klass
16
16
  end
17
17
 
18
+ def custom_policy_class(&block)
19
+ klass = Class.new(PolicyAssertions::Test, &block)
20
+ def klass.name
21
+ 'CustomPolicyTest'
22
+ end
23
+
24
+ klass
25
+ end
26
+
18
27
  class User
19
28
  def self.policy_class
20
29
  PersonPolicy
@@ -93,6 +102,19 @@ class PersonPolicy
93
102
  end
94
103
  end
95
104
 
105
+ class CustomPolicy
106
+ attr_reader :user, :record
107
+
108
+ def initialize(user, record)
109
+ @user = user
110
+ @record = record
111
+ end
112
+
113
+ def new?
114
+ record.is_a?(Article)
115
+ end
116
+ end
117
+
96
118
  module Users
97
119
  class Session
98
120
  attr_accessor :id, :user_id
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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ProctorU
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -124,7 +124,7 @@ dependencies:
124
124
  version: 3.0.0
125
125
  description: Minitest assertions for Pundit policies.
126
126
  email:
127
- - engineering@proctoru.com
127
+ - engineering.team@proctoru.com
128
128
  executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
@@ -147,7 +147,7 @@ files:
147
147
  - test/lib/policy-assertions/version_test.rb
148
148
  - test/lib/policy_assertions_test.rb
149
149
  - test/test_helper.rb
150
- homepage: https://github.com/ksimmons/policy-assertions
150
+ homepage: https://github.com/proctoru/policy-assertions
151
151
  licenses:
152
152
  - MIT
153
153
  metadata: {}
@@ -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.6.8
170
+ rubygems_version: 2.7.6
171
171
  signing_key:
172
172
  specification_version: 4
173
173
  summary: Minitest assertions for Pundit policies.