moat 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -0
- data/lib/moat.rb +2 -0
- data/lib/moat/rspec.rb +16 -2
- data/lib/moat/version.rb +1 -1
- data/spec/moat_spec.rb +27 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e99313f748a138e0c9e6f5899ad3e7df1f255d8c
|
4
|
+
data.tar.gz: 4c9638daa7e628b3ce83d5e154bdb002762e5e78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 580d3411e85435820dcdcfb4bfcd7ae0f3411744744b1291a6986d1d121bb4396b9c6231674a418d0cd53209d770c4a085a1f9990ecb45b6c31e9e8670e32739
|
7
|
+
data.tar.gz: 7a91973ffcf54b70b5fb05367f2439ad1a4faba44dd7232ed5f459c4923979762cc6100c8435286811050d4cfa2aa464d386d1adb072e3adb331df7d0df531c6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -319,6 +319,14 @@ describe ThingPolicy do
|
|
319
319
|
end
|
320
320
|
```
|
321
321
|
|
322
|
+
If a non-standard scope is required for filters, it can be overridden. It
|
323
|
+
defaults to the `all` relation for ActiveRecord models or a simple Array
|
324
|
+
otherwise.
|
325
|
+
|
326
|
+
```ruby
|
327
|
+
scope { resource.container }
|
328
|
+
```
|
329
|
+
|
322
330
|
## Ensure all policies have full test coverage
|
323
331
|
|
324
332
|
```ruby
|
data/lib/moat.rb
CHANGED
data/lib/moat/rspec.rb
CHANGED
@@ -138,7 +138,7 @@ module Moat
|
|
138
138
|
end
|
139
139
|
|
140
140
|
def permitted_through_filters(policy_class)
|
141
|
-
policy_instance = policy_class::Filter.new(public_send(role),
|
141
|
+
policy_instance = policy_class::Filter.new(public_send(role), policy_example_scope)
|
142
142
|
policy_filters.select do |filter|
|
143
143
|
policy_instance.public_send(filter).include?(policy_example_resource)
|
144
144
|
end
|
@@ -160,10 +160,15 @@ module Moat
|
|
160
160
|
alias_method :role, :roles
|
161
161
|
|
162
162
|
def resource(&block)
|
163
|
-
fail ArgumentError, "#
|
163
|
+
fail ArgumentError, "#{__method__} called without a block" unless block
|
164
164
|
let(:policy_example_resource) { instance_eval(&block) }
|
165
165
|
end
|
166
166
|
|
167
|
+
def scope(&block)
|
168
|
+
fail ArgumentError, "#{__method__} called without a block" unless block
|
169
|
+
let(:policy_example_scope) { instance_eval(&block) }
|
170
|
+
end
|
171
|
+
|
167
172
|
def policy_filters(*filters)
|
168
173
|
let(:policy_filters) { filters }
|
169
174
|
end
|
@@ -187,6 +192,15 @@ module Moat
|
|
187
192
|
let(:policy_example_resource) do
|
188
193
|
fail NotImplementedError, "A resource has not been defined"
|
189
194
|
end
|
195
|
+
|
196
|
+
# a scope that contains at least the resource
|
197
|
+
let(:policy_example_scope) do
|
198
|
+
if policy_example_resource.class.respond_to?(:all)
|
199
|
+
policy_example_resource.class.all
|
200
|
+
else
|
201
|
+
[policy_example_resource]
|
202
|
+
end
|
203
|
+
end
|
190
204
|
end
|
191
205
|
end
|
192
206
|
end
|
data/lib/moat/version.rb
CHANGED
data/spec/moat_spec.rb
CHANGED
@@ -113,25 +113,24 @@ describe Moat do
|
|
113
113
|
end
|
114
114
|
|
115
115
|
it "fails if a corresponding action can't be found" do
|
116
|
-
expect { moat_consumer.policy_filter([1, 2, 3], :invalid_action
|
116
|
+
expect { moat_consumer.policy_filter([1, 2, 3], :invalid_action) }.
|
117
117
|
to raise_error(Moat::ActionNotFoundError, "IntegerPolicy::Filter#invalid_action")
|
118
118
|
end
|
119
119
|
|
120
120
|
it "returns the value of applying a policy scope filter to the original scope" do
|
121
|
-
expect(moat_consumer.policy_filter([1, 2, 3, 4, 5]
|
121
|
+
expect(moat_consumer.policy_filter([1, 2, 3, 4, 5])).to eql([2, 4])
|
122
122
|
end
|
123
123
|
|
124
124
|
it "uses specified action" do
|
125
|
-
expect(moat_consumer.policy_filter([2, 3], :show
|
125
|
+
expect(moat_consumer.policy_filter([2, 3], :show)).to eql([3])
|
126
126
|
end
|
127
127
|
|
128
128
|
it "uses specified policy" do
|
129
|
-
expect(moat_consumer.policy_filter([2, 3], policy: OtherIntegerPolicy)).
|
130
|
-
to eql([3])
|
129
|
+
expect(moat_consumer.policy_filter([2, 3], policy: OtherIntegerPolicy)).to eql([3])
|
131
130
|
end
|
132
131
|
|
133
132
|
it "uses specified user" do
|
134
|
-
expect(moat_consumer.policy_filter([2, 3], user: "specified user"
|
133
|
+
expect(moat_consumer.policy_filter([2, 3], user: "specified user")).to eql([2, 3])
|
135
134
|
end
|
136
135
|
end
|
137
136
|
|
@@ -149,7 +148,7 @@ describe Moat do
|
|
149
148
|
end
|
150
149
|
|
151
150
|
it "fails if a corresponding action can't be found" do
|
152
|
-
expect { moat_consumer.authorize([1, 2, 3], :invalid_action
|
151
|
+
expect { moat_consumer.authorize([1, 2, 3], :invalid_action?) }.
|
153
152
|
to raise_error(Moat::ActionNotFoundError, "IntegerPolicy::Authorization#invalid_action?")
|
154
153
|
end
|
155
154
|
|
@@ -184,7 +183,7 @@ describe Moat do
|
|
184
183
|
end
|
185
184
|
context "policy_filter called" do
|
186
185
|
it "does not raise an exception" do
|
187
|
-
moat_consumer.policy_filter([1, 2]
|
186
|
+
moat_consumer.policy_filter([1, 2])
|
188
187
|
expect { moat_consumer.verify_policy_applied }.not_to raise_error
|
189
188
|
end
|
190
189
|
end
|
@@ -281,7 +280,7 @@ describe Moat do
|
|
281
280
|
end
|
282
281
|
|
283
282
|
it "infers a policy from an object's `model` method" do
|
284
|
-
class
|
283
|
+
class DefinesModel
|
285
284
|
def self.model
|
286
285
|
Fake
|
287
286
|
end
|
@@ -290,7 +289,25 @@ describe Moat do
|
|
290
289
|
[11, 12]
|
291
290
|
end
|
292
291
|
end
|
293
|
-
expect(moat_consumer.policy_filter(
|
292
|
+
expect(moat_consumer.policy_filter(DefinesModel)).to eql([11, 12])
|
293
|
+
end
|
294
|
+
|
295
|
+
it "infers a policy from when scope is a non-Array that implements to_ary" do
|
296
|
+
class IntContainer
|
297
|
+
def initialize(*numbers)
|
298
|
+
@numbers = numbers
|
299
|
+
end
|
300
|
+
|
301
|
+
def to_ary
|
302
|
+
@numbers
|
303
|
+
end
|
304
|
+
|
305
|
+
def select
|
306
|
+
@numbers.select { |n| yield(n) }
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
expect(moat_consumer.policy_filter(IntContainer.new(1, 2, 3, 4, 5))).to eql([2, 4])
|
294
311
|
end
|
295
312
|
end
|
296
313
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Poll Everywhere
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|