forked from theforeman/foreman
/
test_helper.rb
98 lines (80 loc) · 2.9 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
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
def logger
Rails.logger
end
class Test::Unit::TestCase
include RR::Adapters::TestUnit
end
def set_session_user
SETTINGS[:login] ? {:user => User.admin.id, :expires_at => 5.minutes.from_now} : {}
end
def as_user user
saved_user = User.current
User.current = users(user)
result = yield
User.current = saved_user
result
end
def as_admin &block
as_user :admin, &block
end
def setup_users
User.current = users :admin
user = User.find_by_login("one")
@request.session[:user] = user.id
@request.session[:expires_at] = 5.minutes.from_now
user.roles = [Role.find_by_name('Anonymous'), Role.find_by_name('Viewer')]
user.save!
end
def setup_user operation, type=""
@one = users(:one)
as_admin do
role = Role.find_or_create_by_name :name => "#{operation}_#{type}"
role.permissions = ["#{operation}_#{type}".to_sym]
@one.roles = [role]
@one.save!
end
User.current = @one
end
def unattended?
SETTINGS[:unattended].nil? or SETTINGS[:unattended]
end
def self.disable_orchestration
#This disables the DNS/DHCP orchestration
Host.any_instance.stubs(:boot_server).returns("boot_server")
Resolv::DNS.any_instance.stubs(:getname).returns("foo.fqdn")
Resolv::DNS.any_instance.stubs(:getaddress).returns("127.0.0.1")
Net::DNS::ARecord.any_instance.stubs(:conflicts).returns([])
Net::DNS::ARecord.any_instance.stubs(:conflicting?).returns(false)
Net::DNS::PTRRecord.any_instance.stubs(:conflicting?).returns(false)
Net::DNS::PTRRecord.any_instance.stubs(:conflicts).returns([])
Net::DHCP::Record.any_instance.stubs(:create).returns(true)
Net::DHCP::SparcRecord.any_instance.stubs(:create).returns(true)
Net::DHCP::Record.any_instance.stubs(:conflicting?).returns(false)
ProxyAPI::Puppet.any_instance.stubs(:environments).returns(["production"])
ProxyAPI::DHCP.any_instance.stubs(:unused_ip).returns('127.0.0.1')
end
def disable_orchestration
ActiveSupport::TestCase.disable_orchestration
end
end
class ActionController::TestCase
setup :setup_set_script_name, :set_api_user
def setup_set_script_name
@request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
end
def set_api_user
return unless self.class.to_s[/api/i]
@request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(users(:apiadmin).login, "secret")
end
end
Apipie.configuration.validate = false