pundit-matchers 2.3.0 → 3.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pundit/matchers/actions_matcher.rb +41 -0
  3. data/lib/pundit/matchers/attributes_matcher.rb +71 -0
  4. data/lib/pundit/matchers/base_matcher.rb +27 -0
  5. data/lib/pundit/matchers/forbid_all_actions_matcher.rb +43 -0
  6. data/lib/pundit/matchers/forbid_only_actions_matcher.rb +60 -0
  7. data/lib/pundit/matchers/permit_actions_matcher.rb +69 -0
  8. data/lib/pundit/matchers/permit_all_actions_matcher.rb +43 -0
  9. data/lib/pundit/matchers/permit_attributes_matcher.rb +65 -0
  10. data/lib/pundit/matchers/permit_only_actions_matcher.rb +60 -0
  11. data/lib/pundit/matchers/utils/policy_info.rb +67 -6
  12. data/lib/pundit/matchers.rb +120 -364
  13. metadata +16 -39
  14. data/lib/pundit/matchers/utils/all_actions/actions_matcher.rb +0 -39
  15. data/lib/pundit/matchers/utils/all_actions/error_message_formatter.rb +0 -47
  16. data/lib/pundit/matchers/utils/all_actions/forbidden_actions_error_formatter.rb +0 -26
  17. data/lib/pundit/matchers/utils/all_actions/forbidden_actions_matcher.rb +0 -20
  18. data/lib/pundit/matchers/utils/all_actions/permitted_actions_error_formatter.rb +0 -26
  19. data/lib/pundit/matchers/utils/all_actions/permitted_actions_matcher.rb +0 -20
  20. data/lib/pundit/matchers/utils/only_actions/actions_matcher.rb +0 -39
  21. data/lib/pundit/matchers/utils/only_actions/error_message_formatter.rb +0 -54
  22. data/lib/pundit/matchers/utils/only_actions/forbidden_actions_error_formatter.rb +0 -26
  23. data/lib/pundit/matchers/utils/only_actions/forbidden_actions_matcher.rb +0 -20
  24. data/lib/pundit/matchers/utils/only_actions/permitted_actions_error_formatter.rb +0 -26
  25. data/lib/pundit/matchers/utils/only_actions/permitted_actions_matcher.rb +0 -20
@@ -1,47 +0,0 @@
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
@@ -1,26 +0,0 @@
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
@@ -1,20 +0,0 @@
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
@@ -1,26 +0,0 @@
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, :opposite_kind
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,20 +0,0 @@
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
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pundit
4
- module Matchers
5
- module Utils
6
- module OnlyActions
7
- # Parent class for specific only_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, :expected_actions
13
-
14
- def initialize(policy, expected_actions)
15
- @policy_info = PolicyInfo.new(policy)
16
- @expected_actions = expected_actions
17
- end
18
-
19
- def match?
20
- missed_expected_actions.empty? &&
21
- actual_actions.sort == expected_actions.sort
22
- end
23
-
24
- def unexpected_actions
25
- @unexpected_actions ||= actual_actions - expected_actions
26
- end
27
-
28
- def missed_expected_actions
29
- @missed_expected_actions ||= expected_actions - actual_actions
30
- end
31
-
32
- def policy
33
- policy_info.policy
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pundit
4
- module Matchers
5
- module Utils
6
- module OnlyActions
7
- # Adds #message method which generates failed assertion message
8
- # for *_only_actions matchers.
9
- #
10
- # Expects methods to be defined:
11
- # * matcher - instance which has `OnlyActions::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 only actions #{matcher.expected_actions} #{expected_kind}, but " \
17
- "#{unless missed_expected_actions.empty?
18
- "#{mismatches_are(missed_expected_actions)} #{opposite_kind} and "
19
- end}" \
20
- "#{mismatches_are(unexpected_actions)} #{expected_kind} too"
21
- end
22
-
23
- private
24
-
25
- attr_reader :matcher, :expected_kind, :opposite_kind
26
-
27
- def policy
28
- matcher.policy
29
- end
30
-
31
- def unexpected_actions
32
- matcher.unexpected_actions
33
- end
34
-
35
- def missed_expected_actions
36
- matcher.missed_expected_actions
37
- end
38
-
39
- def policy_name
40
- policy.class.name
41
- end
42
-
43
- def mismatches_are(mismatches)
44
- if mismatches.count == 1
45
- "#{mismatches} is"
46
- else
47
- "#{mismatches} are"
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'error_message_formatter'
4
-
5
- module Pundit
6
- module Matchers
7
- module Utils
8
- module OnlyActions
9
- # Error message formatter for `forbid_only_actions` matcher.
10
- class ForbiddenActionsErrorFormatter
11
- include OnlyActions::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
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'actions_matcher'
4
-
5
- module Pundit
6
- module Matchers
7
- module Utils
8
- module OnlyActions
9
- # Handles all the checks in `forbid_only_actions` matcher.
10
- class ForbiddenActionsMatcher < OnlyActions::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
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'error_message_formatter'
4
-
5
- module Pundit
6
- module Matchers
7
- module Utils
8
- module OnlyActions
9
- # Error message formatter for `permit_only_actions` matcher.
10
- class PermittedActionsErrorFormatter
11
- include OnlyActions::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, :opposite_kind
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'actions_matcher'
4
-
5
- module Pundit
6
- module Matchers
7
- module Utils
8
- module OnlyActions
9
- # Handles all the checks in `permit_only_actions` matcher.
10
- class PermittedActionsMatcher < OnlyActions::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