forked from stffn/declarative_authorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.rb
154 lines (129 loc) · 3.91 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'test/unit'
require 'pathname'
unless defined?(RAILS_ROOT)
RAILS_ROOT = ENV['RAILS_ROOT'] ?
ENV['RAILS_ROOT'] + "" :
File.join(File.dirname(__FILE__), %w{.. .. .. ..})
end
unless defined?(ActiveRecord)
if File.directory? RAILS_ROOT + '/config'
puts 'Using config/boot.rb'
ENV['RAILS_ENV'] = 'test'
require File.join(RAILS_ROOT, 'config', 'environment.rb')
else
# simply use installed gems if available
version_requirement = ENV['RAILS_VERSION'] ? "= #{ENV['RAILS_VERSION']}" : "> 2.1.0"
puts "Using Rails from RubyGems (#{version_requirement || "default"})"
require 'rubygems'
%w{actionpack activerecord activesupport rails}.each do |gem_name|
gem gem_name, version_requirement
end
end
unless defined?(Rails) # needs to be explicit in Rails < 3
%w(action_pack action_controller active_record active_support initializer).each {|f| require f}
end
end
DA_ROOT = Pathname.new(File.expand_path("..", File.dirname(__FILE__)))
require DA_ROOT + File.join(%w{lib declarative_authorization rails_legacy})
require DA_ROOT + File.join(%w{lib declarative_authorization authorization})
require DA_ROOT + File.join(%w{lib declarative_authorization in_controller})
require DA_ROOT + File.join(%w{lib declarative_authorization maintenance})
begin
require 'ruby-debug'
rescue MissingSourceFile; end
class MockDataObject
def initialize (attrs = {})
attrs.each do |key, value|
instance_variable_set(:"@#{key}", value)
self.class.class_eval do
attr_reader key
end
end
end
def self.descends_from_active_record?
true
end
def self.table_name
name.tableize
end
def self.name
"Mock"
end
def self.find(*args)
raise StandardError, "Couldn't find #{self.name} with id #{args[0].inspect}" unless args[0]
new :id => args[0]
end
end
class MockUser < MockDataObject
def initialize (*roles)
options = roles.last.is_a?(::Hash) ? roles.pop : {}
super({:role_symbols => roles, :login => hash}.merge(options))
end
def initialize_copy (other)
@role_symbols = @role_symbols.clone
end
end
class MocksController < ActionController::Base
attr_accessor :current_user
attr_writer :authorization_engine
def authorized?
!!@authorized
end
def self.define_action_methods (*methods)
methods.each do |method|
define_method method do
@authorized = true
render :text => 'nothing'
end
end
end
def self.define_resource_actions
define_action_methods :index, :show, :edit, :update, :new, :create, :destroy
end
def logger (*args)
Class.new do
def warn(*args)
#p args
end
alias_method :info, :warn
alias_method :debug, :warn
def warn?; end
alias_method :info?, :warn?
alias_method :debug?, :warn?
end.new
end
end
if Rails.version < "3"
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
else
#Rails::Application.routes.draw do
Rails.application.routes.draw do
match '/name/spaced_things(/:action)' => 'name/spaced_things'
match '/deep/name_spaced/things(/:action)' => 'deep/name_spaced/things'
match '/:controller(/:action(/:id))'
end
end
ActionController::Base.send :include, Authorization::AuthorizationInController
if Rails.version < "3"
require "action_controller/test_process"
end
class Test::Unit::TestCase
include Authorization::TestHelper
def request! (user, action, reader, params = {})
action = action.to_sym if action.is_a?(String)
@controller.current_user = user
@controller.authorization_engine = Authorization::Engine.new(reader)
((params.delete(:clear) || []) + [:@authorized]).each do |var|
@controller.instance_variable_set(var, nil)
end
get action, params
end
unless Rails.version < "3"
def setup
#@routes = Rails::Application.routes
@routes = Rails.application.routes
end
end
end