Class: Hubbado::Policy::RspecMatchers::Deny::DenyMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hubbado/policy/rspec_matchers/deny.rb

Instance Method Summary collapse

Constructor Details

#initialize(action, *args, **kwargs) ⇒ DenyMatcher

Returns a new instance of DenyMatcher.



10
11
12
13
14
15
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 10

def initialize(action, *args, **kwargs)
  @action = action
  @args = args
  @kwargs = kwargs
  @reason = :denied
end

Instance Method Details

#descriptionObject



46
47
48
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 46

def description
  "#{@policy.class.name} deny #{@action} with #{@reason}"
end

#failure_messageObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 50

def failure_message
  if @incorrect_reason
    "Expected #{@policy.class.name} to deny #{@action}, with #{@reason} " \
      "but it was denied with #{@result.reason}"
  elsif @missing_translation
    "Translation missing for policy #{@policy.class.name} and #{@reason}"
  else
    "Expected #{@policy.class.name} to deny #{@action}, but it was permitted"
  end
end

#failure_message_when_negatedObject



61
62
63
64
65
66
67
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 61

def failure_message_when_negated
  if @missing_translation
    "Translation missing for policy #{@policy.class.name} and #{@reason}"
  else
    "Expected #{@policy.class.name} to permit #{@action}, but it was denied"
  end
end

#matches?(policy) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 22

def matches?(policy)
  @policy = policy
  @result = policy.send @action, *@args, **@kwargs

  unless @result.denied?
    @incorrect_repsonse = true
    return false
  end

  if @reason && @result.reason != @reason
    @incorrect_reason = true
    return false
  end

  begin
    @result.message
  rescue I18n::MissingTranslationData
    @missing_translation = true
    return false
  end

  true
end

#with(reason) ⇒ Object



17
18
19
20
# File 'lib/hubbado/policy/rspec_matchers/deny.rb', line 17

def with(reason)
  @reason = reason
  self
end