cirandas.net

ref: master

vendor/plugins/access_control/test/test_helper.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
ENV["RAILS_ENV"] = "test"
require_relative '../../../../config/environment'

require 'test/unit'
require 'mocha'

# from Rails
require 'rails/test_help'

# load the database schema for the tests
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
load(File.dirname(__FILE__) + '/schema.rb')
# change the table names for the tests to not touch
Role.table_name = 'access_control_test_roles'
RoleAssignment.table_name = 'access_control_test_role_assignments'

# accessor example class to access some resources
class AccessControlTestAccessor < ActiveRecord::Base
  self.table_name = 'access_control_test_accessors'
  acts_as_accessor
  attr_accessible :name
  def cache_keys(arg)
    []
  end
  def blocks_to_expire_cache
    []
  end
end

# resource example class to be accessed by some accessor
class AccessControlTestResource < ActiveRecord::Base
  self.table_name = 'access_control_test_resources'
  acts_as_accessible
  PERMISSIONS[self.class.name] = {'bla' => N_('Bla')}

  attr_accessible :name
end

# controller to test protection
class AccessControlTestController < ApplicationController
  include PermissionCheck
  protect 'see_index', 'global', :user,  :only => :index
  protect 'do_some_stuff', :resource, :user, :only => :other_stuff
  protect ['permission1', 'permission2'], :resource, :user, :only => :stuff_with_multiple_permission

  def index
     render :text => 'test controller'
  end

  def other_stuff
    render :text => 'test stuff'
  end

  def stuff_with_multiple_permission
    render :text => 'multiple permission'
  end

protected
  def user
    AccessControlTestAccessor.find(params[:user]) if params[:user]
  end

  def resource
    AccessControlTestResource.find(params[:resource]) if params[:resource]
  end
end