spob-flix_cloud-gem 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,532 @@
1
+ require 'test_helper'
2
+
3
+ class FlixCloud::JobTest < Test::Unit::TestCase
4
+
5
+ context "When validating a job object with no attributes set" do
6
+ setup do
7
+ @job = FlixCloud::Job.new
8
+ @job.valid?
9
+ end
10
+
11
+ should "require an api key" do
12
+ assert_match /api_key is required/, @job.errors.to_s
13
+ end
14
+
15
+ should "require file_locations" do
16
+ assert_match /file_locations is required/, @job.errors.to_s
17
+ end
18
+
19
+ should "require recipe_id or recipe_name" do
20
+ assert_match /recipe_id or recipe_name is required/, @job.errors.to_s
21
+ end
22
+ end
23
+
24
+
25
+ context "When validating a job object with both recipe_id and recipe_name are set" do
26
+ setup do
27
+ @job = FlixCloud::Job.new(:recipe_name => 'recipe-name', :recipe_id => 1)
28
+ @job.valid?
29
+ end
30
+
31
+ should "require that both recipe_id and recipe_name cannot be used" do
32
+ assert_match /recipe_id and recipe_name cannot both be used/, @job.errors.to_s
33
+ end
34
+ end
35
+
36
+
37
+ context "When validating a job object with a file_locations object that is invalid" do
38
+ setup do
39
+ @job = FlixCloud::Job.new(:file_locations => {})
40
+ @job.valid?
41
+ end
42
+
43
+ should "inherit the file_locations object's errors" do
44
+ assert @job.errors.any?{|error|
45
+ error.is_a?(Hash) && error[:file_locations] && !error[:file_locations].empty?
46
+ }, "Did not inherit file_locations object's errors"
47
+ end
48
+ end
49
+
50
+
51
+ context "When validating a job object with errors on the deepest nested object possible (parameters)" do
52
+ setup do
53
+ @job = FlixCloud::Job.new(:file_locations => {:input => {:parameters => {}}})
54
+ @job.valid?
55
+ end
56
+
57
+ should "inherit the errors of the deepest nested object possible (parameters)" do
58
+ first_level_errors = @job.errors.find{|error| error.is_a?(Hash) && error[:file_locations] && !error[:file_locations].empty? }[:file_locations]
59
+ second_level_errors = first_level_errors.find{|error| error.is_a?(Hash) && error[:input] && !error[:input].empty? }[:input]
60
+
61
+ assert second_level_errors.any?{|error|
62
+ error.is_a?(Hash) && error[:parameters] && !error[:parameters].empty?
63
+ }, "Did not inherit the errors of the deepest nested object possible (parameters)"
64
+ end
65
+ end
66
+
67
+
68
+ context "A job with no attributes set" do
69
+ setup do
70
+ @job = FlixCloud::Job.new
71
+ end
72
+
73
+ should "serialize to xml, excluding everything but api-key and recipe-id" do
74
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-id></recipe-id></api-request>}, @job.to_xml
75
+ end
76
+ end
77
+
78
+
79
+ context "A job with file_locations set" do
80
+ setup do
81
+ @job = FlixCloud::Job.new(:file_locations => {})
82
+ end
83
+
84
+ should "serialize to xml, excluding everything but api-key, recipe-id, and file-locations" do
85
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-id></recipe-id><file-locations></file-locations></api-request>}, @job.to_xml
86
+ end
87
+ end
88
+
89
+
90
+ context "A job with file_locations and input set" do
91
+ setup do
92
+ @job = FlixCloud::Job.new(:file_locations => {:input => {}})
93
+ end
94
+
95
+ should "serialize to xml, excluding everything but api-key, recipe-id, file-locations, and input" do
96
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-id></recipe-id><file-locations><input><url></url></input></file-locations></api-request>}, @job.to_xml
97
+ end
98
+ end
99
+
100
+
101
+ context "A job with file-locations, input, and input-parameters set" do
102
+ setup do
103
+ @job = FlixCloud::Job.new(:file_locations => {:input => {:parameters => {}}})
104
+ end
105
+
106
+ should "serialize to xml, excluding everything but api-key, recipe-id, file-locations, input, and input-parameters" do
107
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-id></recipe-id><file-locations><input><url></url><parameters><user></user><password></password></parameters></input></file-locations></api-request>}, @job.to_xml
108
+ end
109
+ end
110
+
111
+
112
+ context "A job with recipe_id set" do
113
+ setup do
114
+ @job = FlixCloud::Job.new(:recipe_id => 1)
115
+ end
116
+
117
+ should "serialize to xml and include the recipe_id, not the recipe_name" do
118
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-id>1</recipe-id></api-request>}, @job.to_xml
119
+ end
120
+ end
121
+
122
+
123
+ context "A job with a recipe_name set" do
124
+ setup do
125
+ @job = FlixCloud::Job.new(:recipe_name => 'recipe-name')
126
+ end
127
+
128
+ should "serialize to xml and include the recipe_name, not the recipe_id" do
129
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key></api-key><recipe-name>recipe-name</recipe-name></api-request>}, @job.to_xml
130
+ end
131
+ end
132
+
133
+
134
+ context "A job with all attributes set" do
135
+ setup do
136
+ @job = FlixCloud::Job.new(:recipe_id => 1,
137
+ :api_key => 'this_is_an_api_key',
138
+ :file_locations => { :input => { :url => 'http://flixcloud.com/somefile.mp4',
139
+ :parameters => { :user => 'user',
140
+ :password => 'password'}},
141
+ :output => { :url => 'ftp://flixcloud.com/somefile.mp4',
142
+ :parameters => { :user => 'user',
143
+ :password => 'password'}},
144
+ :watermark => { :url => 'http://flixcloud.com/somefile.mp4',
145
+ :parameters => { :user => 'user',
146
+ :password => 'password'}}})
147
+ end
148
+
149
+ should "serialize everything to xml" do
150
+ assert_equal %{<?xml version="1.0" encoding="UTF-8"?><api-request><api-key>this_is_an_api_key</api-key><recipe-id>1</recipe-id><file-locations><input><url>http://flixcloud.com/somefile.mp4</url><parameters><user>user</user><password>password</password></parameters></input><output><url>ftp://flixcloud.com/somefile.mp4</url><parameters><user>user</user><password>password</password></parameters></output><watermark><url>http://flixcloud.com/somefile.mp4</url><parameters><user>user</user><password>password</password></parameters></watermark></file-locations></api-request>}, @job.to_xml
151
+ end
152
+ end
153
+
154
+
155
+ context "An invalid job when attempting to save" do
156
+ setup do
157
+ @job = FlixCloud::Job.new
158
+ @result = @job.save
159
+ end
160
+
161
+ should "return false" do
162
+ assert_equal false, @result
163
+ end
164
+
165
+ should "set errors on the job" do
166
+ assert @job.errors.is_a?(Array)
167
+ assert_not_equal [], @job.errors
168
+ end
169
+ end
170
+
171
+ context "A valid job when attempting to save" do
172
+ setup do
173
+ FakeWeb.allow_net_connect = false
174
+ @job = FlixCloud::Job.new(:recipe_id => 1,
175
+ :api_key => 'this_is_an_api_key',
176
+ :file_locations => { :input => { :url => 'http://flixcloud.com/somefile.mp4',
177
+ :parameters => { :user => 'user',
178
+ :password => 'password'}},
179
+ :output => { :url => 'ftp://flixcloud.com/somefile.mp4',
180
+ :parameters => { :user => 'user',
181
+ :password => 'password'}},
182
+ :watermark => { :url => 'http://flixcloud.com/somefile.mp4',
183
+ :parameters => { :user => 'user',
184
+ :password => 'password'}}})
185
+ end
186
+
187
+ teardown do
188
+ FakeWeb.clean_registry
189
+ FakeWeb.allow_net_connect = true
190
+ end
191
+
192
+ context "when saving with malformed xml (should really never happen, but what if?)" do
193
+ setup do
194
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><errors><error>Malformed XML</error></errors>},
195
+ :status => ['400', 'Bad Request'])
196
+ end
197
+
198
+ should "not be successful" do
199
+ assert !@job.save
200
+ end
201
+
202
+ should "have a 400 response code" do
203
+ @job.save
204
+ assert_equal 400, @job.response.code
205
+ end
206
+
207
+ should "store the errors on the job" do
208
+ @job.save
209
+ assert_equal ['Malformed XML'], @job.errors
210
+ end
211
+ end
212
+
213
+
214
+ context "when saving with an invalid schema (should really never happen, but what if?)" do
215
+ setup do
216
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><errors><error>Schema is invalid</error></errors>},
217
+ :status => ['400', 'Bad Request'])
218
+ end
219
+
220
+ should "not be successful" do
221
+ assert !@job.save
222
+ end
223
+
224
+ should "have a 400 response code" do
225
+ @job.save
226
+ assert_equal 400, @job.response.code
227
+ end
228
+
229
+ should "store the errors on the job" do
230
+ @job.save
231
+ assert_equal ['Schema is invalid'], @job.errors
232
+ end
233
+ end
234
+
235
+
236
+ context "when saving and the api-key is not valid" do
237
+ setup do
238
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :status => ['401', 'Unauthorized'])
239
+ end
240
+
241
+ should "not be successful" do
242
+ assert !@job.save
243
+ end
244
+
245
+ should "have a 401 response code" do
246
+ @job.save
247
+ assert_equal 401, @job.response.code
248
+ end
249
+ end
250
+
251
+
252
+ context "when saving and there are errors on the job so it can't be saved" do
253
+ setup do
254
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><errors><error>You are missing this thing and that thing</error></errors>},
255
+ :status => ['200', 'OK'])
256
+ end
257
+
258
+ should "return false" do
259
+ assert_equal false, @job.save
260
+ end
261
+
262
+ should "set the jobs errors to the response body's errors" do
263
+ @job.save
264
+ assert_equal ["You are missing this thing and that thing"], @job.errors
265
+ end
266
+ end
267
+
268
+ context "when saving was successful" do
269
+ setup do
270
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><job><id type="integer">1</id><initialized-job-at type="datetime">2009-04-07T23:15:33+02:00</initialized-job-at></job>},
271
+ :status => ['201', 'Created'])
272
+ end
273
+
274
+ should "return true" do
275
+ assert_equal true, @job.save
276
+ end
277
+
278
+ should "save the id from the response on the job" do
279
+ @job.save
280
+ assert_equal 1, @job.id
281
+ end
282
+
283
+ should "save the initialization time from the response on the job" do
284
+ @job.save
285
+ assert_equal "Tue Apr 07 21:15:33 UTC 2009", @job.initialized_at.to_s
286
+ end
287
+ end
288
+ end
289
+
290
+ context "When using shortcut attributes for job initialization" do
291
+ setup do
292
+ @job = FlixCloud::Job.new(:api_key => 'your-api-key',
293
+ :recipe_id => 2,
294
+ :input_url => 'your-input-url',
295
+ :input_user => 'your-input-user',
296
+ :input_password => 'your-input-password',
297
+ :output_url => 'your-output-url',
298
+ :output_user => 'your-output-user',
299
+ :output_password => 'your-output-password',
300
+ :watermark_url => 'your-watermark-url',
301
+ :watermark_user => 'your-watermark-user',
302
+ :watermark_password => 'your-watermark-password')
303
+ end
304
+
305
+ should "set the urls" do
306
+ assert_equal 'your-input-url', @job.file_locations.input.url
307
+ assert_equal 'your-output-url', @job.file_locations.output.url
308
+ assert_equal 'your-watermark-url', @job.file_locations.watermark.url
309
+ end
310
+
311
+ should "set the users" do
312
+ assert_equal 'your-input-user', @job.file_locations.input.parameters.user
313
+ assert_equal 'your-output-user', @job.file_locations.output.parameters.user
314
+ assert_equal 'your-watermark-user', @job.file_locations.watermark.parameters.user
315
+ end
316
+
317
+ should "set the passwords" do
318
+ assert_equal 'your-input-password', @job.file_locations.input.parameters.password
319
+ assert_equal 'your-output-password', @job.file_locations.output.parameters.password
320
+ assert_equal 'your-watermark-password', @job.file_locations.watermark.parameters.password
321
+ end
322
+ end
323
+
324
+
325
+ context "When calling save! on an invalid job" do
326
+ setup do
327
+ @job = FlixCloud::Job.new
328
+ end
329
+
330
+ should "raise a FlixCloud::SaveError" do
331
+ assert_raises FlixCloud::SaveError do
332
+ @job.save!
333
+ end
334
+ end
335
+ end
336
+
337
+
338
+ context "When calling save! on a valid job" do
339
+ setup do
340
+ FakeWeb.allow_net_connect = false
341
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><job><id type="integer">1</id><initialized-job-at type="datetime">2009-04-07T23:15:33+02:00</initialized-job-at></job>},
342
+ :status => ['201', 'Created'])
343
+ @job = FlixCloud::Job.new(:api_key => 'your-api-key',
344
+ :recipe_id => 2,
345
+ :input_url => 'your-input-url',
346
+ :input_user => 'your-input-user',
347
+ :input_password => 'your-input-password',
348
+ :output_url => 'your-output-url',
349
+ :output_user => 'your-output-user',
350
+ :output_password => 'your-output-password',
351
+ :watermark_url => 'your-watermark-url',
352
+ :watermark_user => 'your-watermark-user',
353
+ :watermark_password => 'your-watermark-password')
354
+ end
355
+
356
+ teardown do
357
+ FakeWeb.clean_registry
358
+ FakeWeb.allow_net_connect = true
359
+ end
360
+
361
+ should "return true" do
362
+ assert_equal true, @job.save!
363
+ end
364
+ end
365
+
366
+
367
+ context "When creating an invalid job" do
368
+ setup do
369
+ @job = FlixCloud::Job.create
370
+ end
371
+
372
+ should "return a FlixCloud::Job" do
373
+ assert @job.is_a?(FlixCloud::Job)
374
+ end
375
+
376
+ should "have errors on the job" do
377
+ assert !@job.errors.empty?
378
+ end
379
+ end
380
+
381
+
382
+ context "When creating a valid job" do
383
+ setup do
384
+ FakeWeb.allow_net_connect = false
385
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><job><id type="integer">1</id><initialized-job-at type="datetime">2009-04-07T23:15:33+02:00</initialized-job-at></job>},
386
+ :status => ['201', 'Created'])
387
+ @job = FlixCloud::Job.create(:api_key => 'your-api-key',
388
+ :recipe_id => 2,
389
+ :input_url => 'your-input-url',
390
+ :input_user => 'your-input-user',
391
+ :input_password => 'your-input-password',
392
+ :output_url => 'your-output-url',
393
+ :output_user => 'your-output-user',
394
+ :output_password => 'your-output-password',
395
+ :watermark_url => 'your-watermark-url',
396
+ :watermark_user => 'your-watermark-user',
397
+ :watermark_password => 'your-watermark-password')
398
+ end
399
+
400
+ teardown do
401
+ FakeWeb.clean_registry
402
+ FakeWeb.allow_net_connect = true
403
+ end
404
+
405
+ should "return a FlixCloud::Job" do
406
+ assert @job.is_a?(FlixCloud::Job)
407
+ end
408
+
409
+ should "not have errors on the job" do
410
+ assert @job.errors.empty?
411
+ end
412
+
413
+ should "have an id set" do
414
+ assert_not_nil @job.id
415
+ end
416
+ end
417
+
418
+
419
+ context "When using create! to create an invalid job" do
420
+ should "raise a FlixCloud::CreationError exception" do
421
+ assert_raises FlixCloud::CreateError do
422
+ @job = FlixCloud::Job.create!
423
+ end
424
+ end
425
+ end
426
+
427
+
428
+ context "When using create! to create a valid job" do
429
+ setup do
430
+ FakeWeb.allow_net_connect = false
431
+ FakeWeb.register_uri(:post, 'https://flixcloud.com/jobs', :string => %{<?xml version="1.0" encoding="UTF-8"?><job><id type="integer">1</id><initialized-job-at type="datetime">2009-04-07T23:15:33+02:00</initialized-job-at></job>},
432
+ :status => ['201', 'Created'])
433
+ @job = FlixCloud::Job.create!(:api_key => 'your-api-key',
434
+ :recipe_id => 2,
435
+ :input_url => 'your-input-url',
436
+ :input_user => 'your-input-user',
437
+ :input_password => 'your-input-password',
438
+ :output_url => 'your-output-url',
439
+ :output_user => 'your-output-user',
440
+ :output_password => 'your-output-password',
441
+ :watermark_url => 'your-watermark-url',
442
+ :watermark_user => 'your-watermark-user',
443
+ :watermark_password => 'your-watermark-password')
444
+ end
445
+
446
+ teardown do
447
+ FakeWeb.clean_registry
448
+ FakeWeb.allow_net_connect = true
449
+ end
450
+
451
+ should "return a job" do
452
+ assert @job.is_a?(FlixCloud::Job)
453
+ end
454
+
455
+ should "return a job with an id" do
456
+ assert_not_nil @job.id
457
+ end
458
+ end
459
+
460
+
461
+ context "When calling save on a valid job and the server connection gets interrupted" do
462
+ setup do
463
+ HttpClient::Resource.expects(:new).raises(HttpClient::ServerBrokeConnection)
464
+ @job = FlixCloud::Job.new(:api_key => 'your-api-key',
465
+ :recipe_id => 2,
466
+ :input_url => 'your-input-url',
467
+ :input_user => 'your-input-user',
468
+ :input_password => 'your-input-password',
469
+ :output_url => 'your-output-url',
470
+ :output_user => 'your-output-user',
471
+ :output_password => 'your-output-password',
472
+ :watermark_url => 'your-watermark-url',
473
+ :watermark_user => 'your-watermark-user',
474
+ :watermark_password => 'your-watermark-password')
475
+ end
476
+
477
+ should "railse a FlixCloud::ServerBrokeConnection exception" do
478
+ assert_raises FlixCloud::ServerBrokeConnection do
479
+ @job.save
480
+ end
481
+ end
482
+ end
483
+
484
+
485
+ context "When calling save on a valid job and the connection times out" do
486
+ setup do
487
+ HttpClient::Resource.expects(:new).raises(HttpClient::RequestTimeout)
488
+ @job = FlixCloud::Job.new(:api_key => 'your-api-key',
489
+ :recipe_id => 2,
490
+ :input_url => 'your-input-url',
491
+ :input_user => 'your-input-user',
492
+ :input_password => 'your-input-password',
493
+ :output_url => 'your-output-url',
494
+ :output_user => 'your-output-user',
495
+ :output_password => 'your-output-password',
496
+ :watermark_url => 'your-watermark-url',
497
+ :watermark_user => 'your-watermark-user',
498
+ :watermark_password => 'your-watermark-password')
499
+ end
500
+
501
+ should "railse a FlixCloud::RequestTimeout exception" do
502
+ assert_raises FlixCloud::RequestTimeout do
503
+ @job.save
504
+ end
505
+ end
506
+ end
507
+
508
+
509
+ context "When calling save on a valid job and the connection is refused" do
510
+ setup do
511
+ HttpClient::Resource.expects(:new).raises(HttpClient::ConnectionRefused)
512
+ @job = FlixCloud::Job.new(:api_key => 'your-api-key',
513
+ :recipe_id => 2,
514
+ :input_url => 'your-input-url',
515
+ :input_user => 'your-input-user',
516
+ :input_password => 'your-input-password',
517
+ :output_url => 'your-output-url',
518
+ :output_user => 'your-output-user',
519
+ :output_password => 'your-output-password',
520
+ :watermark_url => 'your-watermark-url',
521
+ :watermark_user => 'your-watermark-user',
522
+ :watermark_password => 'your-watermark-password')
523
+ end
524
+
525
+ should "railse a FlixCloud::ConnectionRefused exception" do
526
+ assert_raises FlixCloud::ConnectionRefused do
527
+ @job.save
528
+ end
529
+ end
530
+ end
531
+
532
+ end