Class: DeclarativeAuthorization::Test::Helpers::RoleTestGenerator

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/declarative_authorization/test/helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(test_class, role) ⇒ RoleTestGenerator

Returns a new instance of RoleTestGenerator.



80
81
82
83
# File 'lib/declarative_authorization/test/helpers.rb', line 80

def initialize(test_class, role)
  @test_class = test_class
  @role = role
end

Instance Method Details

#allowed(options) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/declarative_authorization/test/helpers.rb', line 95

def allowed(options)
  return unless @test_class.run_assertion?(options)

  if options[:when]
    privilege(options[:when]) { allowed(options) }
  else
    Blockenspiel.invoke(Proc.new {allowed(options)}, PrivilegeTestGenerator.new(@test_class, @role, nil))
  end
end

#denied(options) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/declarative_authorization/test/helpers.rb', line 105

def denied(options)
  return unless @test_class.run_assertion?(options)

  if options[:when]
    privilege(options[:when]) { denied(options) }
  else
    Blockenspiel.invoke(Proc.new {denied(options)}, PrivilegeTestGenerator.new(@test_class, @role, nil))
  end
end

#privilege(privilege, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/declarative_authorization/test/helpers.rb', line 85

def privilege(privilege, &block)
  privileges = [privilege].flatten.uniq

  unless privileges.all? { |privilege| [:granted, :hidden, :read, :write, :write_without_delete].include?(privilege) }
    raise "Privilege (:when) must be :granted, :hidden, :read, :write_without_delete, or :write. Found #{privilege.inspect}."
  end

  Blockenspiel.invoke(block, PrivilegeTestGenerator.new(@test_class, @role, privileges))
end