This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
/
passenger_application_test.rb
300 lines (248 loc) · 10.9 KB
/
passenger_application_test.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require File.expand_path('../test_helper', __FILE__)
require File.expand_path('../../PassengerApplication', __FILE__)
require File.expand_path('../../PassengerPref.rb', __FILE__)
# if ENV['TRY_TO_RUN_ALL_TESTS_TOGETHER'] == 'true'
# require 'PassengerPref'
# else
# class PrefPanePassenger
# class << self
# attr_accessor :sharedInstance
# end
#
# def applicationMarkedDirty(app)
# end
# end
# end
PrefPanePassenger.sharedInstance = PrefPanePassenger.new
class Hash
def except(*keys)
copy = dup
keys.each do |key|
copy.delete(key)
end
copy
end
end
describe "PassengerApplication, with a new application" do
tests PassengerApplication
def after_setup
passenger_app.stubs(:execute)
end
it "should initialize with empty path & host" do
passenger_app.path.should == ''
passenger_app.host.should == ''
passenger_app.vhostname.should == '*:80'
passenger_app.should.be.new_app
assigns(:dirty).should.be false
assigns(:valid).should.be false
end
it "should not start the application if only one of host or path is entered" do
passenger_app.expects(:start).times(0)
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
passenger_app.setValue_forKey('', 'host')
passenger_app.setValue_forKey('/Users/het-manfred/rails code/blog', 'path')
end
it "should set the default host if a path is entered (probably via browse) and replace underscores with hyphens" do
passenger_app.setValue_forKey('/Users/het-manfred/rails code/my_supercool_blog', 'path')
assigns(:host).should == 'my-supercool-blog.local'
end
it "should set a default host if initialized with initWithPath" do
app = PassengerApplication.alloc.initWithPath("/some/path/to/RailsApp")
app.host.should == 'railsapp.local'
app.should.be.valid
end
it "should start the application for the first time" do
assigns(:valid, true)
passenger_app.expects(:start).times(1)
passenger_app.apply
end
it "should start the application by gracefully restarting apache" do
passenger_app.expects(:save_config!).times(1)
passenger_app.start
end
it "should be valid if a path is set as it will also set the host" do
passenger_app.setValue_forKey('/Users/het-manfred/rails code/blog', 'path')
assigns(:valid).should.be true
end
it "should mark the app as dirty if it's initialized with a path" do
PassengerApplication.alloc.initWithPath('/Users/het-manfred/rails code/blog').should.be.dirty
end
it "should return a hash with a default user_defined_data variable that contains the permissions directive and also set it as the value for @user_defined_data" do
passenger_app.setValue_forKey('/some/path/to/rails/app', 'path')
string = %{
<directory "/some/path/to/rails/app/public">
Order allow,deny
Allow from all
</directory>}.sub(/^\n/, '')
passenger_app.to_hash['user_defined_data'].should == string
assigns(:user_defined_data).should == string
end
end
describe "PassengerApplication, in general" do
tests PassengerApplication
def after_setup
@vhost = File.expand_path('../fixtures/blog.vhost.conf', __FILE__)
@instance_to_be_tested = PassengerApplication.alloc.initWithFile(@vhost)
Kernel.stubs(:system)
end
it "should set valid to false after opening a file, because the apply button should still be disabled" do
assigns(:valid).should.be false
end
it "should parse the correct host & path from a vhost file" do
passenger_app.host.should == "het-manfreds-blog.local"
passenger_app.path.should == "/Users/het-manfred/rails code/blog"
passenger_app.environment.should == PassengerApplication::DEVELOPMENT
passenger_app.allow_mod_rewrite.should.be false
passenger_app.vhostname.should == '*:80'
passenger_app = PassengerApplication.alloc.initWithFile(File.expand_path('../fixtures/wiki.vhost.conf', __FILE__))
passenger_app.host.should == "het-manfreds-wiki.local"
passenger_app.path.should == "/Users/het-manfred/rails code/wiki"
passenger_app.environment.should == PassengerApplication::PRODUCTION
passenger_app.allow_mod_rewrite.should.be true
passenger_app.vhostname.should == 'het-manfreds-wiki.local:443'
passenger_app.user_defined_data.should == %{
<Location "/">
AuthType Basic
AuthName "Development Preview"
AuthUserFile /home2/cogat/htpasswd
Require valid-user
</Location>}.sub(/^\n/, '')
end
it "should set @new_app to false" do
assigns(:new_app).should.be false
end
it "should return the path to the config file" do
passenger_app.config_path.should == File.join(SharedPassengerBehaviour::PASSENGER_APPS_DIR, "het-manfreds-blog.local.vhost.conf")
end
it "should be able to save the config file" do
passenger_app.expects(:execute).with('/usr/bin/ruby', PassengerApplication::CONFIG_INSTALLER, [passenger_app.to_hash].to_yaml)
passenger_app.save_config!
end
it "should mark the application as dirty if a value has changed" do
assigns(:dirty).should.be false
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
assigns(:dirty).should.be true
end
it "should let the PrefPanePassenger instance know that an app has been marked dirty" do
PrefPanePassenger.sharedInstance.expects(:applicationMarkedDirty).with(passenger_app)
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
end
it "should be valid if both a path and a host are entered" do
passenger_app.setValue_forKey('', 'host')
assigns(:valid).should.be false
passenger_app.setValue_forKey('foo.local', 'host')
assigns(:valid).should.be true
passenger_app.setValue_forKey(nil, 'host')
assigns(:valid).should.be false
passenger_app.setValue_forKey('foo.local', 'host')
assigns(:valid).should.be true
passenger_app.setValue_forKey('', 'path')
assigns(:valid).should.be false
passenger_app.setValue_forKey('/some/path', 'path')
assigns(:valid).should.be true
passenger_app.setValue_forKey(nil, 'path')
assigns(:valid).should.be false
passenger_app.setValue_forKey('/some/path', 'path')
assigns(:valid).should.be true
end
it "should not apply if the applications configuration is not valid" do
passenger_app.setValue_forKey('', 'host')
passenger_app.expects(:restart).times(0)
passenger_app.apply
assigns(:valid).should.be false
assigns(:dirty).should.be true
end
it "should restart the application for an existing application" do
passenger_app.expects(:restart).times(1)
passenger_app.setValue_forKey('/some/path', 'path')
passenger_app.apply
assigns(:dirty).should.be false
assigns(:valid).should.be false
end
it "should save the config before restarting if it was marked dirty" do
passenger_app.expects(:save_config!).times(1)
assigns(:valid, true)
assigns(:dirty, true)
passenger_app.apply
end
it "should not save the config before restarting if it wasn't marked dirty" do
passenger_app.expects(:save_config!).times(0)
assigns(:dirty, false)
passenger_app.restart
end
it "should restart the application" do
Kernel.expects(:system).with("/usr/bin/touch '/Users/het-manfred/rails code/blog/tmp/restart.txt'")
passenger_app.restart
end
it "should remove application(s)" do
PassengerApplication.expects(:execute).with('/usr/bin/ruby', PassengerApplication::CONFIG_UNINSTALLER, [passenger_app.to_hash].to_yaml)
PassengerApplication.removeApplications([passenger_app].to_ns)
end
it "should return it's attributes as a hash without NS classes" do
assigns(:host, 'app.local'.to_ns)
assigns(:user_defined_data, "<directory \"/some/path\">\n foo bar\n</directory>")
assigns(:allow_mod_rewrite, false.to_ns)
assigns(:vhostname, 'het-manfreds-wiki.local:443')
passenger_app.to_hash.should == {
'config_path' => passenger_app.config_path,
'host' => 'app.local',
'path' => passenger_app.path,
'environment' => 'development',
'allow_mod_rewrite' => false,
'vhostname' => 'het-manfreds-wiki.local:443',
'user_defined_data' => "<directory \"/some/path\">\n foo bar\n</directory>"
}
passenger_app.to_hash.to_yaml.should.not.include 'NSCF'
end
it "should load existing applications" do
dir = SharedPassengerBehaviour::PASSENGER_APPS_DIR
blog, paste = ["#{dir}/blog.vhost.conf", "#{dir}/paste.vhost.conf"]
blog_app, paste_app = stub("PassengerApplication: blog"), stub("PassengerApplication: paste")
Dir.stubs(:glob).with("#{dir}/*.vhost.conf").returns([blog, paste])
PassengerApplication.any_instance.stubs(:initWithFile).with(blog).returns(blog_app)
PassengerApplication.any_instance.stubs(:initWithFile).with(paste).returns(paste_app)
PassengerApplication.existingApplications.should == [blog_app, paste_app]
end
it "should start multiple applications at once" do
app1 = PassengerApplication.alloc.initWithPath('/rails/app1'.to_ns)
app2 = PassengerApplication.alloc.initWithPath('/rails/app2'.to_ns)
[app1, app2].each { |app| app.instance_variable_set(:@valid, true) }
PassengerApplication.expects(:execute).times(1).with('/usr/bin/ruby', PassengerApplication::CONFIG_INSTALLER, [app1.to_hash, app2.to_hash].to_yaml)
PassengerApplication.startApplications [app1, app2].to_ns
app1.should.not.be.new_app
app1.should.not.be.valid
app1.should.not.be.dirty
app2.should.not.be.new_app
app2.should.not.be.valid
app2.should.not.be.dirty
end
it "should remember all the original values for the case that the user wants to revert" do
passenger_app.setValue_forKey('foo.local', 'host')
passenger_app.setValue_forKey('/some/path', 'path')
passenger_app.setValue_forKey('production', 'environment')
passenger_app.setValue_forKey(true, 'allow_mod_rewrite')
passenger_app.should.be.dirty
passenger_app.should.be.valid
passenger_app.to_hash.except('config_path', 'user_defined_data', 'new_app', 'vhostname').should == {
'host' => 'foo.local',
'path' => '/some/path',
'environment' => 'production',
'allow_mod_rewrite' => true,
}
passenger_app.revert
passenger_app.should.not.be.dirty
passenger_app.should.not.be.valid
passenger_app.to_hash.except('config_path', 'user_defined_data', 'new_app', 'vhostname').should == {
'host' => 'het-manfreds-blog.local',
'path' => '/Users/het-manfred/rails code/blog',
'environment' => 'development',
'allow_mod_rewrite' => false
}
end
it "should first remove a config and then add it again if the host has changed so we don't leave stale files/hosts" do
passenger_app.setValue_forKey('foo.local', 'host')
passenger_app.expects(:execute).with('/usr/bin/ruby', PassengerApplication::CONFIG_UNINSTALLER, [assigns(:original_values)].to_yaml)
passenger_app.expects(:save_config!)
passenger_app.apply
end
end