-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathresque-web_test.rb
More file actions
395 lines (329 loc) · 10.4 KB
/
resque-web_test.rb
File metadata and controls
395 lines (329 loc) · 10.4 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# vim:fileencoding=utf-8
require_relative 'test_helper'
require 'resque/server/test_helper'
context 'on GET to /schedule' do
setup { get '/schedule' }
test('is 200') { assert last_response.ok? }
end
context 'on GET to /schedule with scheduled jobs' do
setup do
Resque::Scheduler.env = 'production'
Resque.schedule = {
'some_ivar_job' => {
'cron' => '* * * * *',
'class' => 'SomeIvarJob',
'args' => '/tmp',
'rails_env' => 'production'
},
'some_other_job' => {
'every' => ['1m', ['1h']],
'queue' => 'high',
'custom_job_class' => 'SomeOtherJob',
'args' => {
'b' => 'blah'
}
},
'some_fancy_job' => {
'every' => ['1m'],
'queue' => 'fancy',
'class' => 'SomeFancyJob',
'args' => 'sparkles',
'rails_env' => 'fancy'
},
'shared_env_job' => {
'cron' => '* * * * *',
'class' => 'SomeSharedEnvJob',
'args' => '/tmp',
'rails_env' => 'fancy, production'
}
}
Resque::Scheduler.load_schedule!
get '/schedule'
end
test('is 200') { assert last_response.ok? }
test 'see the scheduled job' do
assert last_response.body.include?('SomeIvarJob')
end
test 'include(highlight) jobs for other envs' do
assert last_response.body.include?('SomeFancyJob')
end
test 'includes job used in multiple environments' do
assert last_response.body.include?('SomeSharedEnvJob')
end
test 'allows delete when dynamic' do
Resque::Scheduler.stubs(:dynamic).returns(true)
get '/schedule'
assert last_response.body.include?('Delete')
end
test "doesn't allow delete when static" do
Resque::Scheduler.stubs(:dynamic).returns(false)
get '/schedule'
assert !last_response.body.include?('Delete')
end
end
context 'on GET to /delayed' do
[
{
'class' => SomeIvarJob,
'args' => %w(foo bar),
't' => 3600
},
{
'class' => SomeFancyJob,
'args' => [],
't' => 30
},
{
'class' => FakePHPClass,
'args' => %w(1 10 100),
't' => 36_000
}
].each do |job|
test "is 200 with class #{job['class']}" do
Resque.enqueue_at(Time.now + job['t'], job['class'], *job['args'])
get '/delayed'
assert last_response.ok?
end
test "contains link to all schedules for class #{job['class']}" do
Resque.enqueue_at(Time.now + job['t'], job['class'], *job['args'])
get '/delayed'
assert !(last_response.body =~ %r{/delayed/jobs/#{URI::Parser.new.escape(job['class'].to_s)}}).nil?
end
end
end
context 'on GET to /delayed/jobs/:klass' do
setup do
@t = Time.now + 3600
Resque.enqueue_at(@t, SomeIvarJob, 'foo', 'bar')
get(
URI('/delayed/jobs/SomeIvarJob?args=' <<
URI.encode_www_form_component(%w(foo bar).to_json)).to_s
)
end
test('is 200') { assert last_response.ok? }
test 'see the scheduled job' do
assert last_response.body.include?(@t.to_s)
end
context 'with a namespaced class' do
setup do
@t = Time.now + 3600
module Foo
class Bar
def self.queue
'bar'
end
end
end
Resque.enqueue_at(@t, Foo::Bar, 'foo', 'bar')
get(
URI('/delayed/jobs/Foo::Bar?args=' <<
URI.encode_www_form_component(%w(foo bar).to_json)).to_s
)
end
test('is 200') { assert last_response.ok? }
test 'see the scheduled job' do
assert last_response.body.include?(@t.to_s)
end
end
end
module Test
RESQUE_SCHEDULE = {
'job_without_params' => {
'cron' => '* * * * *',
'class' => 'JobWithoutParams',
'args' => {
'host' => 'localhost'
},
'rails_env' => 'production'
},
'job_with_params' => {
'every' => '1m',
'class' => 'JobWithParams',
'args' => {
'host' => 'localhost'
},
'parameters' => {
'log_level' => {
'description' => 'The level of logging',
'default' => 'warn'
}
}
}
}.freeze
end
context 'POST /schedule/requeue' do
setup do
Resque.schedule = Test::RESQUE_SCHEDULE
Resque::Scheduler.load_schedule!
end
test 'job without params' do
# Regular jobs without params should redirect to /overview
job_name = 'job_without_params'
Resque::Scheduler.stubs(:enqueue_from_config)
.once.with(Resque.schedule[job_name])
post '/schedule/requeue', 'job_name' => job_name
follow_redirect!
assert_equal 'http://example.org/overview', last_request.url
assert last_response.ok?
end
test 'job with params' do
# If a job has params defined,
# it should render the template with a form for the job params
job_name = 'job_with_params'
post '/schedule/requeue', 'job_name' => job_name
assert last_response.ok?, last_response.errors
assert last_response.body.include?('This job requires parameters')
assert last_response.body.include?(
%(<input type="hidden" name="job_name" value="#{job_name}">)
)
Resque.schedule[job_name]['parameters'].each do |_param_name, param_config|
assert last_response.body.include?(
'<span style="border-bottom:1px dotted;" ' <<
%[title="#{param_config['description']}">(?)</span>]
)
assert last_response.body.include?(
'<input type="text" name="log_level" ' <<
%(value="#{param_config['default']}">)
)
end
end
end
context 'POST /schedule/requeue_with_params' do
setup do
Resque.schedule = Test::RESQUE_SCHEDULE
Resque::Scheduler.load_schedule!
end
test 'job with params' do
job_name = 'job_with_params'
log_level = 'error'
job_config = Resque.schedule[job_name]
args = job_config['args'].merge('log_level' => log_level)
job_config = job_config.merge('args' => args)
Resque::Scheduler.stubs(:enqueue_from_config).once.with(job_config)
post '/schedule/requeue_with_params',
'job_name' => job_name,
'log_level' => log_level
follow_redirect!
assert_equal 'http://example.org/overview', last_request.url
assert last_response.ok?, last_response.errors
end
end
context 'on POST to /delayed/search' do
setup do
t = Time.now + 60
Resque.enqueue_at(t, SomeIvarJob, 'string arg')
Resque.enqueue(SomeQuickJob)
end
test 'should find matching scheduled job' do
post '/delayed/search', 'search' => 'ivar'
assert last_response.status == 200
assert last_response.body.include?('SomeIvarJob')
end
test 'the form should encode string params' do
post '/delayed/search', 'search' => 'ivar'
assert_match('value="["string arg"]', last_response.body)
end
test 'should find matching queued job' do
post '/delayed/search', 'search' => 'quick'
assert last_response.status == 200
assert last_response.body.include?('SomeQuickJob')
end
test 'should escape XSS attempt' do
post '/delayed/search', 'search' => '"><script>alert(document.cookie);</script>"'
assert !last_response.body.include?('<script>alert(document.cookie);</script>')
end
end
context 'on POST to /delayed/cancel_now' do
setup do
Resque.reset_delayed_queue
Resque.enqueue_at(Time.now + 10, SomeIvarJob, 'arg')
Resque.enqueue_at(Time.now + 100, SomeQuickJob)
end
test 'removes the specified job' do
job_timestamp, *remaining = Resque.delayed_queue_peek(0, 10)
assert_equal 1, remaining.size
post '/delayed/cancel_now',
'timestamp' => job_timestamp,
'klass' => SomeIvarJob.name,
'args' => Resque.encode(['arg'])
assert_equal 302, last_response.status
assert_equal remaining, Resque.delayed_queue_peek(0, 10)
end
test 'does not remove the job if the params do not match' do
timestamps = Resque.delayed_queue_peek(0, 10)
post '/delayed/cancel_now',
'timestamp' => timestamps.first,
'klass' => SomeIvarJob.name
assert_equal 302, last_response.status
assert_equal timestamps, Resque.delayed_queue_peek(0, 10)
end
test 'redirects to overview' do
post '/delayed/cancel_now'
assert last_response.status == 302
assert last_response.header['Location'].include? '/delayed'
end
end
context 'on POST to /delayed/clear' do
setup { post '/delayed/clear' }
test 'redirects to delayed' do
assert last_response.status == 302
assert last_response.header['Location'].include? '/delayed'
end
end
context 'on POST to /delayed/queue_now' do
setup { post '/delayed/queue_now', timestamp: 0 }
test 'returns ok status' do
assert last_response.status == 200
end
end
context 'on GET to /delayed/:timestamp' do
setup { get '/delayed/1234567890' }
test 'shows delayed_timestamp view' do
assert last_response.status == 200
end
end
context 'DELETE /schedule when dynamic' do
setup do
Resque.schedule = Test::RESQUE_SCHEDULE
Resque::Scheduler.load_schedule!
Resque::Scheduler.stubs(:dynamic).returns(true)
end
test 'redirects to schedule page' do
delete '/schedule'
status = last_response.status
redirect_location = last_response.original_headers['Location']
response_status_msg = "Expected response to be a 302, but was a #{status}."
redirect_msg = "Redirect to #{redirect_location} instead of /schedule."
assert status == 302, response_status_msg
assert_match %r{/schedule/?$}, redirect_location, redirect_msg
end
test 'does not show the deleted job' do
delete '/schedule', job_name: 'job_with_params'
follow_redirect!
msg = 'The job should not have been shown on the /schedule page.'
assert !last_response.body.include?('job_with_params'), msg
end
test 'removes job from redis' do
delete '/schedule', job_name: 'job_with_params'
msg = 'The job was not deleted from redis.'
assert_nil Resque.fetch_schedule('job_with_params'), msg
end
end
context 'DELETE /schedule when static' do
setup do
Resque.schedule = Test::RESQUE_SCHEDULE
Resque::Scheduler.load_schedule!
Resque::Scheduler.stubs(:dynamic).returns(false)
end
test 'does not remove the job from the UI' do
delete '/schedule', job_name: 'job_with_params'
follow_redirect!
msg = 'The job should not have been removed from the /schedule page.'
assert last_response.body.include?('job_with_params'), msg
end
test 'does not remove job from redis' do
delete '/schedule', job_name: 'job_with_params'
msg = 'The job should not have been deleted from redis.'
assert Resque.fetch_schedule('job_with_params'), msg
end
end