forked from theforeman/foreman
/
test_helper.rb
229 lines (189 loc) · 7.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
require 'rubygems'
require 'spork'
# $LOAD_PATH required for testdrb party of spork-minitest
$LOAD_PATH << "test"
unless RUBY_VERSION =~ /^1\.8/
require 'simplecov'
SimpleCov.start 'rails' do
add_group 'API', 'app/controllers/api'
end
end
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/autorun"
require 'capybara/rails'
require 'factory_girl_rails'
# Turn of Apipie validation for tests
Apipie.configuration.validate = false
# To prevent Postgres' errors "permission denied: "RI_ConstraintTrigger"
if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
ActiveRecord::Migration.execute "SET CONSTRAINTS ALL DEFERRED;"
end
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
set_fixture_class({ :hosts => Host::Base })
set_fixture_class :nics => Nic::BMC
# for backwards compatibility to between Minitest syntax
alias_method :assert_not, :refute
alias_method :assert_no_match, :refute_match
alias_method :assert_not_nil, :refute_nil
alias_method :assert_not_equal, :refute_equal
alias_method :assert_raise, :assert_raises
class <<self
alias_method :test, :it
end
# Add more helper methods to be used by all tests here...
def logger
Rails.logger
end
class MiniTest::Unit::TestCase
include RR::Adapters::MiniTest
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
# Transactional fixtures do not work with Selenium tests, because Capybara
# uses a separate server thread, which the transactions would be hidden
# from. We hence use DatabaseCleaner to truncate our test database.
DatabaseCleaner.strategy = :truncation
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Stop ActiveRecord from wrapping tests in transactions
self.use_transactional_fixtures = false
def assert_index_page(index_path,title_text,new_link_text = nil,has_search = true,has_pagination = true)
visit index_path
assert page.has_selector?('h1', :text => title_text), "#{title_text} was expected in the <h1> tag, but was not found"
(assert find_link(new_link_text).visible?, "#{new_link_text} is not visible") if new_link_text
(assert find_button('Search').visible?, "Search button is not visible") if has_search
(assert has_content?("Displaying"), "Pagination 'Display ...' does not appear") if has_pagination
end
def assert_new_button(index_path,new_link_text,new_path)
visit index_path
click_link new_link_text
assert_equal new_path, current_path, "new path #{new_path} was expected but it was #{current_path}"
end
def assert_submit_button(redirect_path,button_text = "Submit")
click_button button_text
assert_equal redirect_path, current_path, "redirect path #{redirect_path} was expected but it was #{current_path}"
end
def assert_delete_row(index_path, link_text, delete_text = "Delete", dropdown = false)
visit index_path
within(:xpath, "//tr[contains(.,'#{link_text}')]") do
find("i.caret").click if dropdown
click_link(delete_text)
end
popup = page.driver.browser.switch_to.alert
popup.accept
assert page.has_no_link?(link_text), "link '#{link_text}' was expected NOT to be on the page, but it was found."
assert page.has_content?('Successfully destroyed'), "flash message 'Successfully destroyed' was expected but it was not found on the page"
end
def assert_cannot_delete_row(index_path, link_text, delete_text = "Delete", dropdown = false, flash_message = true)
visit index_path
within(:xpath, "//tr[contains(.,'#{link_text}')]") do
find("i.caret").click if dropdown
click_link(delete_text)
end
popup = page.driver.browser.switch_to.alert
popup.accept
assert page.has_link?(link_text), "link '#{link_text}' was expected but it was not found on the page."
assert page.has_content?("is used by"), "flash message 'is used by' was expected but it was not found on the page."
end
def fix_mismatches
Location.all_import_missing_ids
Organization.all_import_missing_ids
end
end
class ActionView::TestCase
helper Rails.application.routes.url_helpers
end
end
Spork.each_run do
# This code will be run each time you run your specs.
class ActionController::TestCase
setup :setup_set_script_name, :set_api_user, :reset_setting_cache
def reset_setting_cache
Setting.cache.clear
end
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
class ActionDispatch::IntegrationTest
setup :login_admin
teardown do
DatabaseCleaner.clean # Truncate the database
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
end
private
def login_admin
visit "/"
fill_in "login_login", :with => "admin"
fill_in "login_password", :with => "secret"
click_button "Login"
end
end
end