forked from kentaro/webistrano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.rb
251 lines (198 loc) · 5.36 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require 'mocha'
class Test::Unit::TestCase
include AuthenticatedTestHelper
# Transactional fixtures
self.use_transactional_fixtures = true
# Instantiated fixtures
self.use_instantiated_fixtures = false
# Add more helper methods to be used by all tests here...
# returns a random string for testing
def random_string(size=10)
rand_array = []
"a".upto("z"){|e| rand_array << e}
result = ""
size.times do
result += (rand_array[rand(rand_array.size) - 1])
end
return result
end
def create_new_host(options = {})
options = {
:name => "#{(rand(999) % 255)}.#{(rand(999) % 255)}.#{(rand(999) % 255)}.#{(rand(999) % 255)}"
}.update(options)
h = Host.new
h.name = options[:name]
h.save!
return h
end
def create_new_project(options = {})
options = {
:name => random_string,
:template => 'rails'
}.update(options)
p = Project.new
p.name = options[:name]
p.template = options[:template]
p.save!
return p
end
def create_new_stage(options = {})
options = {
:project => create_new_project,
:name => random_string
}.update(options)
s = Stage.new
s.name = options[:name]
s.project = options[:project]
s.save!
return s
end
def create_new_role(options = {})
options = {
:stage => create_new_stage,
:host => create_new_host,
:name => random_string,
:primary => 0
}.update(options)
r = Role.new
r.name = options[:name]
r.stage = options[:stage]
r.host = options[:host]
r.primary = options[:primary]
r.save!
return r
end
def create_new_project_configuration(options = {})
options = {
:project => create_new_project,
:name => random_string,
:value => random_string,
:prompt_on_deploy => 0
}.update(options)
pc = ProjectConfiguration.new
pc.name = options[:name]
pc.value = options[:value]
pc.prompt_on_deploy = options[:prompt_on_deploy]
pc.project = options[:project]
pc.save!
return pc
end
def create_new_stage_configuration(options = {})
options = {
:stage => create_new_stage,
:name => random_string,
:value => random_string,
:prompt_on_deploy => 0
}.update(options)
sc = StageConfiguration.new
sc.name = options[:name]
sc.value = options[:value]
sc.prompt_on_deploy = options[:prompt_on_deploy]
sc.stage = options[:stage]
sc.save!
return sc
end
def create_new_role(options = {})
options = {
:stage => create_new_stage,
:name => random_string,
:host => create_new_host,
:primary => 0,
:no_release => 0,
:no_symlink => 0
}.update(options)
r = Role.new
r.name = options[:name]
r.host = options[:host]
r.stage = options[:stage]
r.primary = options[:primary]
r.no_release = options[:no_release]
r.save!
return r
end
def create_new_user(options = {})
options = {
:login => random_string,
:email => "#{random_string}@#{random_string}.com",
:password => random_string
}.update(options)
u = User.new
u.login = options[:login]
u.email = options[:email]
u.password = options[:password]
u.password_confirmation = options[:login]
u.save!
return u
end
def create_new_deployment(options = {})
options = {
:stage => create_new_stage,
:task => random_string,
:completed_at => nil,
:success => 0,
:prompt_config => {},
:roles => [],
:description => random_string,
:user => create_new_user
}.update(options)
d = Deployment.new
d.task = options[:task]
d.stage = options[:stage]
d.completed_at = options[:completed_at]
d.success = options[:success]
d.prompt_config = options[:prompt_config]
d.description = options[:description]
d.user = options[:user]
d.roles << options[:roles] unless options[:roles].empty?
d.save!
return d
end
def create_new_recipe(options = {})
options = {
:name => random_string,
:description => random_string,
:body => random_string
}.update(options)
r = Recipe.new
r.name = options[:name]
r.description = options[:description]
r.body = options[:body]
r.save!
return r
end
def create_new_user(options = {})
options = {
:login => random_string,
:email => "#{random_string}@#{random_string}.com",
:admin => 0,
:password => random_string
}.update(options)
u = User.new
u.login = options[:login]
u.email = options[:email]
u.admin = options[:admin]
u.password = options[:password]
u.password_confirmation = options[:password]
u.save!
return u
end
def prepare_email
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
return ActionMailer::Base.deliveries
end
def login(user=nil)
user = user || create_new_user
@request.session[:user] = user.id
return user
end
def admin_login
admin = login
admin.make_admin!
return admin
end
end