pundit-matchers 1.8.2 → 1.8.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
  SHA256:
3
- metadata.gz: 394ccd7f7a4ca7d409dac69d53c230e37e8a003b33b3d1bf35ba023b04a51e38
4
- data.tar.gz: cdbfed8c6543e5210b7e69e57b15d4b7e87d1d4bcf0fa0545f602267147c6cce
3
+ metadata.gz: e7ac5ee799eb2a2f3064a84b07e309ff249e2f0cd8a98895da2922ee09c0ce39
4
+ data.tar.gz: b8de8ec8ecece032ee5144733ff9d7225430b61b0ef983758d4204370e9ff582
5
5
  SHA512:
6
- metadata.gz: 88f9827d0cc96d6376cab9956bf4707be9753ec507ab026289ba3662032eeebdc6922b5a5c39661897c9e5e52a520f49abde3d4b438353c4345cc796e3d939ff
7
- data.tar.gz: 19816084889873bc6dd31d60dc4568533856faadb072ff36b10f1ca673e57aa686a30a7f088b54820a05f6ca7ac42e9c6182b1ab9929407843deba2b1e489ef5
6
+ metadata.gz: 1370ebade318121ba43fada2826d6796af2b3c0529fb93440557929175b6fd52f5d2bb65cc5ca692158f3351ab105eee685202c0660e415f47d64bb723743855
7
+ data.tar.gz: 1805f3272434c2a26e49e407db6ce45f8c4b8a95445e023d32bb2f710281b26eefcabb933a4afeb3a6707a861b3cf103d625b5442365441c7acb2ee8245eb02e
@@ -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
@@ -2,11 +2,11 @@ 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'
5
+ require './matchers/utils/policy_info'
6
+ require './matchers/utils/all_actions/forbidden_actions_error_formatter'
7
+ require './matchers/utils/all_actions/forbidden_actions_matcher'
8
+ require './matchers/utils/all_actions/permitted_actions_error_formatter'
9
+ require './matchers/utils/all_actions/permitted_actions_matcher'
10
10
 
11
11
  class Configuration
12
12
  attr_accessor :user_alias
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.2
4
+ version: 1.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Alley
@@ -51,6 +51,13 @@ extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
53
  - lib/pundit/matchers.rb
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
54
61
  homepage: http://github.com/punditcommunity/pundit-matchers
55
62
  licenses:
56
63
  - MIT