pundit-matchers 1.7.0 → 1.8.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pundit/matchers/utils/all_actions/actions_matcher.rb +39 -0
- data/lib/pundit/matchers/utils/all_actions/error_message_formatter.rb +47 -0
- data/lib/pundit/matchers/utils/all_actions/forbidden_actions_error_formatter.rb +26 -0
- data/lib/pundit/matchers/utils/all_actions/forbidden_actions_matcher.rb +20 -0
- data/lib/pundit/matchers/utils/all_actions/permitted_actions_error_formatter.rb +26 -0
- data/lib/pundit/matchers/utils/all_actions/permitted_actions_matcher.rb +20 -0
- data/lib/pundit/matchers/utils/policy_info.rb +31 -0
- data/lib/pundit/matchers.rb +30 -0
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e82fc8f2d6caff46e84825de52194c6d349bb4e8b892b1d0cc2d89aeca3e098d
|
4
|
+
data.tar.gz: 4c3d25e6babeca67e96ba7a74f51c137a49f51fb78788ad6472f88fb72cb9990
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54ee9f992e7c90438a6613b98e3b9877b0510097e5711e2dc1f5aa3b550ceb8b40ffda1925547088f1da8bd2eeca1d195ae826127f427afb56dadff71375aa23
|
7
|
+
data.tar.gz: 4d6a0da772460ee2cbfe4721cbb7b14dc9fb9ef2d96335aabb88f3694b8a8bb7db49fd73eeb2b3b2105a4a3c89a3c3319dfef48258d4bb7ba51ffdfd7e80b99f
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pundit
|
4
|
+
module Matchers
|
5
|
+
module Utils
|
6
|
+
module AllActions
|
7
|
+
# Parent class for specific all_action matcher. Should not be used directly.
|
8
|
+
#
|
9
|
+
# Expects methods in child class:
|
10
|
+
# * actual_actions - list of actions which actually matches expected type.
|
11
|
+
class ActionsMatcher
|
12
|
+
attr_reader :policy_info
|
13
|
+
|
14
|
+
def initialize(policy)
|
15
|
+
@policy_info = PolicyInfo.new(policy)
|
16
|
+
end
|
17
|
+
|
18
|
+
def match?
|
19
|
+
missed_expected_actions.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
def missed_expected_actions
|
23
|
+
@missed_expected_actions ||= expected_actions - actual_actions
|
24
|
+
end
|
25
|
+
|
26
|
+
def policy
|
27
|
+
policy_info.policy
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def expected_actions
|
33
|
+
policy_info.actions
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pundit
|
4
|
+
module Matchers
|
5
|
+
module Utils
|
6
|
+
module AllActions
|
7
|
+
# Adds #message method which generates failed assertion message
|
8
|
+
# for *_all_actions matchers.
|
9
|
+
#
|
10
|
+
# Expects methods to be defined:
|
11
|
+
# * matcher - instance which has `AllActions::ActionsMatcher` as a parent class
|
12
|
+
# * expected_kind - string with expected actions type (can be "forbidden" or "permitted")
|
13
|
+
# * opposite_kind - string with oposite then expected actions type (can be "permitted" or "forbidden")
|
14
|
+
module ErrorMessageFormatter
|
15
|
+
def message
|
16
|
+
"#{policy_name} expected to have all actions #{expected_kind}, " \
|
17
|
+
"but #{mismatches_are(missed_expected_actions)} #{opposite_kind}"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :matcher, :expected_kind, :opposite_kind
|
23
|
+
|
24
|
+
def policy
|
25
|
+
matcher.policy
|
26
|
+
end
|
27
|
+
|
28
|
+
def missed_expected_actions
|
29
|
+
matcher.missed_expected_actions
|
30
|
+
end
|
31
|
+
|
32
|
+
def policy_name
|
33
|
+
policy.class.name
|
34
|
+
end
|
35
|
+
|
36
|
+
def mismatches_are(mismatches)
|
37
|
+
if mismatches.count == 1
|
38
|
+
"#{mismatches} is"
|
39
|
+
else
|
40
|
+
"#{mismatches} are"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'error_message_formatter'
|
4
|
+
|
5
|
+
module Pundit
|
6
|
+
module Matchers
|
7
|
+
module Utils
|
8
|
+
module AllActions
|
9
|
+
# Error message formatter for `forbid_all_actions` matcher.
|
10
|
+
class ForbiddenActionsErrorFormatter
|
11
|
+
include AllActions::ErrorMessageFormatter
|
12
|
+
|
13
|
+
def initialize(matcher)
|
14
|
+
@expected_kind = 'forbidden'
|
15
|
+
@opposite_kind = 'permitted'
|
16
|
+
@matcher = matcher
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :matcher, :expected_kind, :opposite_kind
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'actions_matcher'
|
4
|
+
|
5
|
+
module Pundit
|
6
|
+
module Matchers
|
7
|
+
module Utils
|
8
|
+
module AllActions
|
9
|
+
# Handles all the checks in `forbid_all_actions` matcher.
|
10
|
+
class ForbiddenActionsMatcher < AllActions::ActionsMatcher
|
11
|
+
private
|
12
|
+
|
13
|
+
def actual_actions
|
14
|
+
policy_info.forbidden_actions
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'error_message_formatter'
|
4
|
+
|
5
|
+
module Pundit
|
6
|
+
module Matchers
|
7
|
+
module Utils
|
8
|
+
module AllActions
|
9
|
+
# Error message formatter for `permit_all_actions` matcher.
|
10
|
+
class PermittedActionsErrorFormatter
|
11
|
+
include AllActions::ErrorMessageFormatter
|
12
|
+
|
13
|
+
def initialize(matcher)
|
14
|
+
@expected_kind = 'permitted'
|
15
|
+
@opposite_kind = 'forbidden'
|
16
|
+
@matcher = matcher
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :matcher, :expected_kind, :actual_kind
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'actions_matcher'
|
4
|
+
|
5
|
+
module Pundit
|
6
|
+
module Matchers
|
7
|
+
module Utils
|
8
|
+
module AllActions
|
9
|
+
# Handles all the checks in `permit_all_actions` matcher.
|
10
|
+
class PermittedActionsMatcher < AllActions::ActionsMatcher
|
11
|
+
private
|
12
|
+
|
13
|
+
def actual_actions
|
14
|
+
policy_info.permitted_actions
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pundit
|
4
|
+
module Matchers
|
5
|
+
module Utils
|
6
|
+
# Collects all details about given policy class.
|
7
|
+
class PolicyInfo
|
8
|
+
attr_reader :policy
|
9
|
+
|
10
|
+
def initialize(policy)
|
11
|
+
@policy = policy
|
12
|
+
end
|
13
|
+
|
14
|
+
def actions
|
15
|
+
@actions ||= begin
|
16
|
+
policy_methods = @policy.public_methods - Object.instance_methods
|
17
|
+
policy_methods.grep(/\?$/).map { |policy_method| policy_method.to_s.sub(/\?$/, '').to_sym }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def permitted_actions
|
22
|
+
@permitted_actions ||= actions.select { |action| policy.public_send("#{action}?") }
|
23
|
+
end
|
24
|
+
|
25
|
+
def forbidden_actions
|
26
|
+
actions - permitted_actions
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/pundit/matchers.rb
CHANGED
@@ -2,6 +2,12 @@ require 'rspec/core'
|
|
2
2
|
|
3
3
|
module Pundit
|
4
4
|
module Matchers
|
5
|
+
require_relative 'matchers/utils/policy_info'
|
6
|
+
require_relative 'matchers/utils/all_actions/forbidden_actions_error_formatter'
|
7
|
+
require_relative 'matchers/utils/all_actions/forbidden_actions_matcher'
|
8
|
+
require_relative 'matchers/utils/all_actions/permitted_actions_error_formatter'
|
9
|
+
require_relative 'matchers/utils/all_actions/permitted_actions_matcher'
|
10
|
+
|
5
11
|
class Configuration
|
6
12
|
attr_accessor :user_alias
|
7
13
|
|
@@ -364,6 +370,30 @@ module Pundit
|
|
364
370
|
.inspect + '.'
|
365
371
|
end
|
366
372
|
end
|
373
|
+
|
374
|
+
RSpec::Matchers.define :permit_all_actions do
|
375
|
+
match do |policy|
|
376
|
+
@matcher = Pundit::Matchers::Utils::AllActions::PermittedActionsMatcher.new(policy)
|
377
|
+
@matcher.match?
|
378
|
+
end
|
379
|
+
|
380
|
+
failure_message do
|
381
|
+
formatter = Pundit::Matchers::Utils::AllActions::PermittedActionsErrorFormatter.new(@matcher)
|
382
|
+
formatter.message
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
RSpec::Matchers.define :forbid_all_actions do
|
387
|
+
match do |policy|
|
388
|
+
@matcher = Pundit::Matchers::Utils::AllActions::ForbiddenActionsMatcher.new(policy)
|
389
|
+
@matcher.match?
|
390
|
+
end
|
391
|
+
|
392
|
+
failure_message do
|
393
|
+
formatter = Pundit::Matchers::Utils::AllActions::ForbiddenActionsErrorFormatter.new(@matcher)
|
394
|
+
formatter.message
|
395
|
+
end
|
396
|
+
end
|
367
397
|
end
|
368
398
|
|
369
399
|
if defined?(Pundit)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pundit-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Alley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -51,7 +51,14 @@ extensions: []
|
|
51
51
|
extra_rdoc_files: []
|
52
52
|
files:
|
53
53
|
- lib/pundit/matchers.rb
|
54
|
-
|
54
|
+
- lib/pundit/matchers/utils/all_actions/actions_matcher.rb
|
55
|
+
- lib/pundit/matchers/utils/all_actions/error_message_formatter.rb
|
56
|
+
- lib/pundit/matchers/utils/all_actions/forbidden_actions_error_formatter.rb
|
57
|
+
- lib/pundit/matchers/utils/all_actions/forbidden_actions_matcher.rb
|
58
|
+
- lib/pundit/matchers/utils/all_actions/permitted_actions_error_formatter.rb
|
59
|
+
- lib/pundit/matchers/utils/all_actions/permitted_actions_matcher.rb
|
60
|
+
- lib/pundit/matchers/utils/policy_info.rb
|
61
|
+
homepage: http://github.com/punditcommunity/pundit-matchers
|
55
62
|
licenses:
|
56
63
|
- MIT
|
57
64
|
metadata: {}
|
@@ -70,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
77
|
- !ruby/object:Gem::Version
|
71
78
|
version: '0'
|
72
79
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
80
|
+
rubygems_version: 3.3.7
|
74
81
|
signing_key:
|
75
82
|
specification_version: 4
|
76
83
|
summary: RSpec matchers for Pundit policies
|