/
dropbox_spec.rb
581 lines (481 loc) · 18.7 KB
/
dropbox_spec.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# encoding: utf-8
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Storage::Dropbox do
let(:model) { Backup::Model.new(:test_trigger, 'test label') }
let(:storage) do
Backup::Storage::Dropbox.new(model) do |db|
db.api_key = 'my_api_key'
db.api_secret = 'my_api_secret'
db.keep = 5
end
end
it 'should be a subclass of Storage::Base' do
Backup::Storage::Dropbox.
superclass.should == Backup::Storage::Base
end
describe '#initialize' do
after { Backup::Storage::Dropbox.clear_defaults! }
it 'should load pre-configured defaults through Base' do
Backup::Storage::Dropbox.any_instance.expects(:load_defaults!)
storage
end
it 'should pass the model reference to Base' do
storage.instance_variable_get(:@model).should == model
end
it 'should pass the storage_id to Base' do
storage = Backup::Storage::Dropbox.new(model, 'my_storage_id')
storage.storage_id.should == 'my_storage_id'
end
context 'when no pre-configured defaults have been set' do
it 'should use the values given' do
storage.api_key.should == 'my_api_key'
storage.api_secret.should == 'my_api_secret'
storage.access_type.should == :app_folder
storage.path.should == 'backups'
storage.chunk_size.should == 4
storage.chunk_retries.should == 10
storage.retry_waitsec.should == 30
storage.storage_id.should be_nil
storage.keep.should == 5
end
it 'should use default values if none are given' do
storage = Backup::Storage::Dropbox.new(model)
storage.api_key.should be_nil
storage.api_secret.should be_nil
storage.access_type.should == :app_folder
storage.path.should == 'backups'
storage.chunk_size.should == 4
storage.chunk_retries.should == 10
storage.retry_waitsec.should == 30
storage.storage_id.should be_nil
storage.keep.should be_nil
end
end # context 'when no pre-configured defaults have been set'
context 'when pre-configured defaults have been set' do
before do
Backup::Storage::Dropbox.defaults do |s|
s.api_key = 'some_api_key'
s.api_secret = 'some_api_secret'
s.access_type = 'some_access_type'
s.path = 'some_path'
s.keep = 15
s.chunk_size = 50
s.chunk_retries = 15
s.retry_waitsec = 40
end
end
it 'should use pre-configured defaults' do
storage = Backup::Storage::Dropbox.new(model)
storage.api_key.should == 'some_api_key'
storage.api_secret.should == 'some_api_secret'
storage.access_type.should == 'some_access_type'
storage.path.should == 'some_path'
storage.storage_id.should be_nil
storage.keep.should == 15
storage.chunk_size.should == 50
storage.chunk_retries.should == 15
storage.retry_waitsec.should == 40
end
it 'should override pre-configured defaults' do
storage = Backup::Storage::Dropbox.new(model) do |s|
s.api_key = 'new_api_key'
s.api_secret = 'new_api_secret'
s.access_type = 'new_access_type'
s.path = 'new_path'
s.keep = 10
s.chunk_size = 40
s.chunk_retries = 20
s.retry_waitsec = 50
end
storage.api_key.should == 'new_api_key'
storage.api_secret.should == 'new_api_secret'
storage.access_type.should == 'new_access_type'
storage.path.should == 'new_path'
storage.storage_id.should be_nil
storage.keep.should == 10
storage.chunk_size.should == 40
storage.chunk_retries.should == 20
storage.retry_waitsec.should == 50
end
end # context 'when pre-configured defaults have been set'
end # describe '#initialize'
describe '#connection' do
let(:session) { mock }
let(:client) { mock }
let(:s) { sequence '' }
context 'when a cached session exists' do
before do
storage.expects(:cached_session).in_sequence(s).returns(session)
storage.expects(:create_write_and_return_new_session!).never
DropboxClient.expects(:new).in_sequence(s).
with(session, :app_folder).returns(client)
end
it 'should use the cached session to create the client' do
storage.send(:connection).should be(client)
end
it 'should return an already existing client' do
storage.send(:connection).should be(client)
storage.send(:connection).should be(client)
end
end
context 'when a cached session does not exist' do
before do
storage.expects(:cached_session).in_sequence(s).returns(false)
Backup::Logger.expects(:info).in_sequence(s).with(
'Creating a new session!'
)
storage.expects(:create_write_and_return_new_session!).in_sequence(s).
returns(session)
DropboxClient.expects(:new).in_sequence(s).
with(session, :app_folder).returns(client)
end
it 'should create a new session and return the client' do
storage.send(:connection).should be(client)
end
it 'should return an already existing client' do
storage.send(:connection).should be(client)
storage.send(:connection).should be(client)
end
end
context 'when an error is raised creating a client for the session' do
it 'should wrap and raise the error' do
storage.stubs(:cached_session).returns(true)
DropboxClient.expects(:new).raises('error')
expect do
storage.send(:connection)
end.to raise_error {|err|
err.should be_an_instance_of(
Backup::Errors::Storage::Dropbox::ConnectionError
)
err.message.should ==
'Storage::Dropbox::ConnectionError: RuntimeError: error'
}
end
end
end # describe '#connection'
describe '#cached_session' do
let(:session) { mock }
context 'when a cached session file exists' do
before do
storage.expects(:cache_exists?).returns(true)
storage.expects(:cached_file).returns('cached_file')
File.expects(:read).with('cached_file').returns('yaml_data')
end
context 'when the cached session is successfully loaded' do
it 'should return the sesssion' do
DropboxSession.expects(:deserialize).with('yaml_data').
returns(session)
Backup::Logger.expects(:info).with(
'Session data loaded from cache!'
)
storage.send(:cached_session).should be(session)
end
end
context 'when errors occur loading the session' do
it 'should log a warning and return false' do
DropboxSession.expects(:deserialize).with('yaml_data').
raises('error message')
Backup::Logger.expects(:warn).with do |err|
err.should be_an_instance_of(
Backup::Errors::Storage::Dropbox::CacheError
)
err.message.should == 'Storage::Dropbox::CacheError: ' +
"Could not read session data from cache.\n" +
" Cache data might be corrupt.\n" +
" Reason: RuntimeError\n" +
" error message"
end
expect do
storage.send(:cached_session).should be_false
end.not_to raise_error
end
end
end
context 'when a cached session file does not exist' do
before { storage.stubs(:cache_exists?).returns(false) }
it 'should return false' do
storage.send(:cached_session).should be_false
end
end
end
describe '#transfer!' do
let(:connection) { mock }
let(:package) { mock }
let(:file) { mock }
let(:uploader) { mock }
let(:s) { sequence '' }
before do
storage.instance_variable_set(:@package, package)
storage.stubs(:local_path).returns('/local/path')
storage.stubs(:connection).returns(connection)
storage.stubs(:remote_path_for).with(package).returns('remote/path')
file.stubs(:stat).returns(stub(:size => 6_291_456))
uploader.stubs(:total_size).returns(6_291_456)
uploader.stubs(:offset).returns(
0, 2_097_152, 4_194_304, 6_291_456,
0, 2_097_152, 4_194_304, 6_291_456
)
storage.chunk_size = 2
storage.chunk_retries = 1
end
it 'transfers the package files' do
storage.expects(:files_to_transfer_for).in_sequence(s).with(package).
multiple_yields(
['2011.12.31.11.00.02.backup.tar.enc-aa', 'backup.tar.enc-aa'],
['2011.12.31.11.00.02.backup.tar.enc-ab', 'backup.tar.enc-ab']
)
# first yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::Dropbox started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc-aa'."
)
File.expects(:open).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc-aa'), 'r'
).yields(file)
connection.expects(:get_chunked_uploader).in_sequence(s).
with(file, 6_291_456).returns(uploader)
uploader.expects(:upload).in_sequence(s).times(3).with(2_097_152)
uploader.expects(:finish).in_sequence(s).
with(File.join('remote/path', 'backup.tar.enc-aa'))
# second yield
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::Dropbox started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc-ab'."
)
File.expects(:open).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc-ab'), 'r'
).yields(file)
connection.expects(:get_chunked_uploader).in_sequence(s).
with(file, 6_291_456).returns(uploader)
uploader.expects(:upload).in_sequence(s).times(3).with(2_097_152)
uploader.expects(:finish).in_sequence(s).
with(File.join('remote/path', 'backup.tar.enc-ab'))
storage.send(:transfer!)
end
it 'retries on errors' do
storage.expects(:files_to_transfer_for).in_sequence(s).with(package).
yields('2011.12.31.11.00.02.backup.tar.enc', 'backup.tar.enc')
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::Dropbox started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc'."
)
File.expects(:open).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc'), 'r'
).yields(file)
connection.expects(:get_chunked_uploader).in_sequence(s).
with(file, 6_291_456).returns(uploader)
uploader.expects(:upload).in_sequence(s).raises('an error')
Backup::Logger.expects(:info).in_sequence(s).with('Chunk retry 1 of 1.')
storage.expects(:sleep).with(30)
uploader.expects(:upload).in_sequence(s).times(3).with(2_097_152)
uploader.expects(:finish).in_sequence(s).
with(File.join('remote/path', 'backup.tar.enc'))
storage.send(:transfer!)
end
it 'fails when retries are exceeded' do
storage.expects(:files_to_transfer_for).in_sequence(s).with(package).
yields('2011.12.31.11.00.02.backup.tar.enc', 'backup.tar.enc')
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::Dropbox started transferring " +
"'2011.12.31.11.00.02.backup.tar.enc'."
)
File.expects(:open).in_sequence(s).with(
File.join('/local/path', '2011.12.31.11.00.02.backup.tar.enc'), 'r'
).yields(file)
connection.expects(:get_chunked_uploader).in_sequence(s).
with(file, 6_291_456).returns(uploader)
uploader.expects(:upload).in_sequence(s).raises('an error')
Backup::Logger.expects(:info).in_sequence(s).with('Chunk retry 1 of 1.')
storage.expects(:sleep).with(30)
uploader.expects(:upload).in_sequence(s).raises('another error')
uploader.expects(:finish).never
expect do
storage.send(:transfer!)
end.to raise_error(Backup::Errors::Storage::Dropbox::TransferError)
end
end # describe '#transfer!'
describe '#remove!' do
let(:package) { mock }
let(:connection) { mock }
let(:s) { sequence '' }
before do
storage.stubs(:storage_name).returns('Storage::Dropbox')
storage.stubs(:connection).returns(connection)
end
it 'should remove the package files' do
storage.expects(:remote_path_for).in_sequence(s).with(package).
returns('remote/path')
storage.expects(:transferred_files_for).in_sequence(s).with(package).
multiple_yields(
['2011.12.31.11.00.02.backup.tar.enc-aa', 'backup.tar.enc-aa'],
['2011.12.31.11.00.02.backup.tar.enc-ab', 'backup.tar.enc-ab']
)
# after both yields
Backup::Logger.expects(:info).in_sequence(s).with(
"Storage::Dropbox started removing " +
"'2011.12.31.11.00.02.backup.tar.enc-aa' from Dropbox.\n" +
"Storage::Dropbox started removing " +
"'2011.12.31.11.00.02.backup.tar.enc-ab' from Dropbox."
)
connection.expects(:file_delete).in_sequence(s).with('remote/path')
storage.send(:remove!, package)
end
end # describe '#remove!'
describe '#cached_file' do
it 'should return the path to the cache file' do
storage.send(:cached_file).should ==
File.join(Backup::Config.cache_path, 'my_api_keymy_api_secret')
end
end
describe '#cache_exists?' do
it 'should check if #cached_file exists' do
storage.expects(:cached_file).returns('/path/to/cache_file')
File.expects(:exist?).with('/path/to/cache_file')
storage.send(:cache_exists?)
end
end
describe '#write_cache!' do
let(:session) { mock }
let(:cache_file) { mock }
it 'should write a serialized session to file' do
storage.expects(:cached_file).returns('/path/to/cache_file')
session.expects(:serialize).returns('serialized_data')
FileUtils.expects(:mkdir_p).with(Backup::Config.cache_path)
File.expects(:open).with('/path/to/cache_file', 'w').yields(cache_file)
cache_file.expects(:write).with('serialized_data')
storage.send(:write_cache!, session)
end
end
describe '#create_write_and_return_new_session!' do
let(:session) { mock }
let(:template) { mock }
let(:s) { sequence '' }
before do
storage.stubs(:cached_file).returns('/path/to/cache_file')
DropboxSession.expects(:new).in_sequence(s).
with('my_api_key', 'my_api_secret').returns(session)
session.expects(:get_request_token).in_sequence(s)
Backup::Template.expects(:new).in_sequence(s).with(
{:session => session, :cached_file => '/path/to/cache_file'}
).returns(template)
template.expects(:render).in_sequence(s).with(
'storage/dropbox/authorization_url.erb'
)
Timeout.expects(:timeout).in_sequence(s).with(180).yields
STDIN.expects(:gets).in_sequence(s)
end
context 'when session is authenticated' do
before do
session.expects(:get_access_token).in_sequence(s)
end
it 'should cache and return the new session' do
template.expects(:render).in_sequence(s).with(
'storage/dropbox/authorized.erb'
)
storage.expects(:write_cache!).in_sequence(s).with(session)
template.expects(:render).in_sequence(s).with(
'storage/dropbox/cache_file_written.erb'
)
storage.send(:create_write_and_return_new_session!).should be(session)
end
end
context 'when session is not authenticated' do
before do
session.expects(:get_access_token).in_sequence(s).raises('error message')
end
it 'should wrap and re-raise the error' do
template.expects(:render).with('storage/dropbox/authorized.erb').never
storage.expects(:write_cache!).never
template.expects(:render).with('storage/dropbox/cache_file_written.erb').never
expect do
storage.send(:create_write_and_return_new_session!)
end.to raise_error {|err|
err.should be_an_instance_of(
Backup::Errors::Storage::Dropbox::AuthenticationError
)
err.message.should == 'Storage::Dropbox::AuthenticationError: ' +
"Could not create or authenticate a new session\n" +
" Reason: RuntimeError\n" +
" error message"
}
end
end
end
describe 'deprecations' do
after do
Backup::Storage::Dropbox.clear_defaults!
end
describe '#email' do
before do
Backup::Logger.expects(:warn).with do |err|
err.message.should match(
"Dropbox#email has been deprecated as of backup v.3.0.17"
)
end
end
context 'when set directly' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.new(model) do |storage|
storage.email = 'foo'
end
end
end
context 'when set as a default' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.defaults do |storage|
storage.email = 'foo'
end
Backup::Storage::Dropbox.new(model)
end
end
end
describe '#password' do
before do
Backup::Logger.expects(:warn).with do |err|
err.message.should match(
"Dropbox#password has been deprecated as of backup v.3.0.17"
)
end
end
context 'when set directly' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.new(model) do |storage|
storage.password = 'foo'
end
end
end
context 'when set as a default' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.defaults do |storage|
storage.password = 'foo'
end
Backup::Storage::Dropbox.new(model)
end
end
end
describe '#timeout' do
before do
Backup::Logger.expects(:warn).with do |err|
err.message.should match(
"Dropbox#timeout has been deprecated as of backup v.3.0.21"
)
end
end
context 'when set directly' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.new(model) do |storage|
storage.timeout = 'foo'
end
end
end
context 'when set as a default' do
it 'should issue a deprecation warning' do
Backup::Storage::Dropbox.defaults do |storage|
storage.timeout = 'foo'
end
Backup::Storage::Dropbox.new(model)
end
end
end
end
end