Class: DeclarativeAuthorization::Test::Helpers::PrivilegeTestGenerator

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, privileges) ⇒ PrivilegeTestGenerator

Returns a new instance of PrivilegeTestGenerator.



19
20
21
22
23
# File 'lib/declarative_authorization/test/helpers.rb', line 19

def initialize(test_class, role, privileges)
  @test_class = test_class
  @role = role
  @privileges = [privileges].flatten
end

Instance Method Details

#allowed(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/declarative_authorization/test/helpers.rb', line 25

def allowed(options)
  role, privileges, actions, params_name = extract_options(options)

  actions.each do |action|
    privileges.each do |privilege|
      @test_class.send(:define_method, "test_#{action}__access_allowed__#{role}_role__#{privilege ? "#{privilege}_permissions__" : ""}with_#{params_name || 'no_params'}") do
        priv_param = (privilege == :hidden ? nil : privilege)
        if forbidden_with_role_and_privilege?(action, role, priv_param, params_name, options)
          flunk "The '#{action}' action #{params_name ? "with '#{params_name}' parameters " : ''}should be accessible for users with #{privilege ? "'#{privilege}' permissions for " : ""}the '#{role}' role."
        end
      end
    end
  end
end

#denied(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/declarative_authorization/test/helpers.rb', line 40

def denied(options)
  role, privileges, actions, params_name = extract_options(options)

  actions.each do |action|
    privileges.each do |privilege|
      @test_class.send(:define_method, "test_#{action}__access_denied__#{role}_role__#{privilege ? "#{privilege}_permissions__" : ""}with_#{params_name || 'no_params'}") do
        priv_param = (privilege == :hidden ? nil : privilege)
        unless forbidden_with_role_and_privilege?(action, role, priv_param, params_name, options)
          flunk "The '#{action}' action #{params_name ? "with '#{params_name}' parameters " : ''}should NOT be accessible for users with #{privilege ? "'#{privilege}' permissions for " : ""}the '#{role}' role."
        end
      end
    end
  end
end