cloud_door 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +7 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +204 -0
  8. data/Rakefile +11 -0
  9. data/bin/dropbox +133 -0
  10. data/bin/onedrive +133 -0
  11. data/cloud_door.gemspec +38 -0
  12. data/cloud_door_config.yml +12 -0
  13. data/data/.gitkeep +0 -0
  14. data/data/testlist +0 -0
  15. data/lib/cloud_door.rb +114 -0
  16. data/lib/cloud_door/account.rb +27 -0
  17. data/lib/cloud_door/cloud_storage.rb +294 -0
  18. data/lib/cloud_door/cloud_yaml.rb +45 -0
  19. data/lib/cloud_door/config.rb +61 -0
  20. data/lib/cloud_door/console.rb +334 -0
  21. data/lib/cloud_door/dropbox.rb +166 -0
  22. data/lib/cloud_door/exceptions.rb +153 -0
  23. data/lib/cloud_door/file_list.rb +164 -0
  24. data/lib/cloud_door/onedrive.rb +180 -0
  25. data/lib/cloud_door/onedrive_api.rb +169 -0
  26. data/lib/cloud_door/token.rb +67 -0
  27. data/lib/cloud_door/version.rb +3 -0
  28. data/log/.gitkeep +0 -0
  29. data/spec/cloud_door/account_spec.rb +96 -0
  30. data/spec/cloud_door/cloud_storage_spec.rb +10 -0
  31. data/spec/cloud_door/cloud_yaml_spec.rb +32 -0
  32. data/spec/cloud_door/config_spec.rb +95 -0
  33. data/spec/cloud_door/console_spec.rb +633 -0
  34. data/spec/cloud_door/dropbox_spec.rb +625 -0
  35. data/spec/cloud_door/file_list_spec.rb +451 -0
  36. data/spec/cloud_door/onedrive_api_spec.rb +256 -0
  37. data/spec/cloud_door/onedrive_spec.rb +652 -0
  38. data/spec/cloud_door/token_spec.rb +81 -0
  39. data/spec/fabricators/account_fabricator.rb +5 -0
  40. data/spec/fabricators/cloud_yaml_fabricator.rb +5 -0
  41. data/spec/fabricators/config_fabrication.rb +5 -0
  42. data/spec/fabricators/token_fabricator.rb +10 -0
  43. data/spec/spec_helper.rb +55 -0
  44. metadata +380 -0
@@ -0,0 +1,652 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'OneDrive' do
4
+ describe 'reset_token' do
5
+ subject { storage.reset_token(token_value) }
6
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
7
+ let(:token_value) { {'access_token' => 'token2'} }
8
+ context 'success' do
9
+ it do
10
+ subject
11
+ expect(storage.token.access_token).to eq 'token2'
12
+ end
13
+ end
14
+ context 'fail' do
15
+ context 'not Token class' do
16
+ before(:each) do
17
+ storage.token = 'token'
18
+ end
19
+ it { expect { subject }.to raise_error(CloudDoor::TokenClassException) }
20
+ end
21
+ end
22
+ end
23
+
24
+ describe 'show_user' do
25
+ subject { storage.show_user }
26
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
27
+ context 'success' do
28
+ let(:posit) { {'name' => 'onedrive'} }
29
+ it do
30
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_user)
31
+ .and_return(posit)
32
+ is_expected.to eq posit
33
+ end
34
+ end
35
+ end
36
+
37
+ describe 'show_files' do
38
+ subject { storage.show_files(file_name) }
39
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
40
+ let(:list_file) { storage.file_list.list_file }
41
+ context 'success' do
42
+ let(:file_name) { nil }
43
+ context 'data exists' do
44
+ let(:posit) do
45
+ {'data' => [
46
+ {'id' => 'file.1234', 'name' => 'file1', 'type' => 'file'},
47
+ {'id' => 'file.5678', 'name' => 'file2', 'type' => 'file'}
48
+ ]}
49
+ end
50
+ let(:result) do
51
+ {
52
+ 'file1' => {'id' => 'file.1234', 'type' => 'file'},
53
+ 'file2' => {'id' => 'file.5678', 'type' => 'file'}
54
+ }
55
+ end
56
+ it do
57
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
58
+ .with(CloudDoor::OneDrive::ROOT_ID)
59
+ .and_return(posit)
60
+ is_expected.to eq result
61
+ end
62
+ end
63
+ context 'data not exists' do
64
+ let(:posit) { {'data' => []} }
65
+ it do
66
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
67
+ .with(CloudDoor::OneDrive::ROOT_ID)
68
+ .and_return(posit)
69
+ is_expected.to eq({})
70
+ end
71
+ end
72
+ end
73
+ context 'fail' do
74
+ let(:file_name) { 'file9' }
75
+ context 'file id not exits' do
76
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
77
+ end
78
+ context 'not directory' do
79
+ before(:each) do
80
+ list = [{'items' => {'file9' => {'id' => 'file.1234', 'type' => 'file'}}}]
81
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
82
+ end
83
+ it { expect { subject }.to raise_error(CloudDoor::NotDirectoryException) }
84
+ end
85
+ end
86
+ after(:each) do
87
+ File.delete(list_file) if File.exist?(list_file)
88
+ end
89
+ end
90
+
91
+ describe 'change_directory' do
92
+ subject { storage.change_directory(file_name) }
93
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
94
+ let(:list_file) { storage.file_list.list_file }
95
+ context 'success' do
96
+ let(:file_name) { 'folder1' }
97
+ before(:each) do
98
+ list = [{'items' => {'folder1' => {'id' => 'folder.1234', 'type' => 'folder'}}}]
99
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
100
+ end
101
+ context 'data exists' do
102
+ let(:posit) do
103
+ {'data' => [
104
+ {'id' => 'file.1234', 'name' => 'file1', 'type' => 'file'},
105
+ {'id' => 'file.5678', 'name' => 'file2', 'type' => 'file'}
106
+ ]}
107
+ end
108
+ let(:result) do
109
+ {
110
+ 'file1' => {'id' => 'file.1234', 'type' => 'file'},
111
+ 'file2' => {'id' => 'file.5678', 'type' => 'file'}
112
+ }
113
+ end
114
+ it do
115
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
116
+ .with('folder.1234')
117
+ .and_return(posit)
118
+ is_expected.to eq result
119
+ end
120
+ end
121
+ context 'data not exists' do
122
+ let(:posit) { {'data' => []} }
123
+ it do
124
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
125
+ .with('folder.1234')
126
+ .and_return(posit)
127
+ is_expected.to eq({})
128
+ end
129
+ end
130
+ end
131
+ context 'fail' do
132
+ let(:file_name) { 'file9' }
133
+ context 'file name not input' do
134
+ let(:file_name) { '' }
135
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
136
+ end
137
+ context 'file id not exits' do
138
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
139
+ end
140
+ context 'not directory' do
141
+ before(:each) do
142
+ list = [{'items' => {'file9' => {'id' => 'file.1234', 'type' => 'file'}}}]
143
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
144
+ end
145
+ it { expect { subject }.to raise_error(CloudDoor::NotDirectoryException) }
146
+ end
147
+ end
148
+ after(:each) do
149
+ File.delete(list_file) if File.exist?(list_file)
150
+ end
151
+ end
152
+
153
+ describe 'show_current_directory' do
154
+ subject { storage.show_current_directory }
155
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
156
+ it { is_expected.to eq('/top') }
157
+ end
158
+
159
+ describe 'show_property' do
160
+ subject { storage.show_property(file_name) }
161
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
162
+ let(:list_file) { storage.file_list.list_file }
163
+ before(:each) do
164
+ list = [{'items' => {'file1' => {'id' => 'file.1234', 'type' => 'file'}}}]
165
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
166
+ end
167
+ context 'success' do
168
+ context 'file exists' do
169
+ let(:file_name) { 'file1' }
170
+ let(:posit) do
171
+ {
172
+ 'name' => 'file',
173
+ 'id' => 'file.1234',
174
+ 'type' => 'file',
175
+ 'size' => 1024,
176
+ 'created_time' => '2014-06-01 12:20:30',
177
+ 'updated_time' => '2014-06-05 13:30:40'
178
+ }
179
+ end
180
+ before(:each) do
181
+ storage.stub(:file_exist?)
182
+ .with(file_name)
183
+ .and_return(true)
184
+ end
185
+ it do
186
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_file)
187
+ .with('file.1234')
188
+ .and_return(posit)
189
+ is_expected.to eq posit
190
+ end
191
+ end
192
+ end
193
+ context 'fail' do
194
+ context 'file name not input' do
195
+ let(:file_name) { '' }
196
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
197
+ end
198
+ context 'file id not exits' do
199
+ let(:file_name) { 'test' }
200
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
201
+ end
202
+ context 'file not exits on cloud' do
203
+ let(:file_name) { 'file1' }
204
+ before(:each) do
205
+ storage.stub(:file_exist?)
206
+ .with(file_name)
207
+ .and_return(false)
208
+ end
209
+ it { expect { subject }.to raise_error(CloudDoor::FileNotExistsException) }
210
+ end
211
+ context 'no data' do
212
+ let(:file_name) { 'file1' }
213
+ let(:posit) { nil }
214
+ let(:posit_dir) do
215
+ JSON('data' => [
216
+ {'id' => 'file.1234', 'name' => 'file1', 'type' => 'file'},
217
+ {'id' => 'file.5678', 'name' => 'file2', 'type' => 'file'}
218
+ ])
219
+ end
220
+ before(:each) do
221
+ storage.stub(:file_exist?)
222
+ .with(file_name)
223
+ .and_return(true)
224
+ CloudDoor::OneDriveApi.any_instance.stub(:request_file)
225
+ .and_return(posit)
226
+ end
227
+ it { expect { subject }.to raise_error(CloudDoor::NoDataException) }
228
+ end
229
+ end
230
+ after(:each) do
231
+ File.delete(list_file) if File.exist?(list_file)
232
+ end
233
+ end
234
+
235
+ =begin
236
+ describe 'pick_cloud_info' do
237
+ subject { storage.pick_cloud_info(method, key) }
238
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
239
+ context 'user' do
240
+ let(:method) { 'request_user' }
241
+ let(:key) { 'name' }
242
+ let(:posit) { {'name' => 'onedrive'} }
243
+ it do
244
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_user)
245
+ .and_return(posit)
246
+ is_expected.to eq 'onedrive'
247
+ end
248
+ end
249
+ context 'dir' do
250
+ let(:method) { 'request_dir' }
251
+ let(:key) { 'data' }
252
+ let(:posit) { {'data' => ['file1']} }
253
+ it do
254
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
255
+ .with(CloudDoor::OneDrive::ROOT_ID)
256
+ .and_return(posit)
257
+ is_expected.to eq ['file1']
258
+ end
259
+ end
260
+ context 'file' do
261
+ let(:storage) { create_storage(CloudDoor::OneDrive, 'file.1234') }
262
+ let(:method) { 'request_file' }
263
+ let(:key) { 'name' }
264
+ let(:posit) { {'name' => 'file1', 'id' => 'file.1234'} }
265
+ it do
266
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_file)
267
+ .with(storage.file_id)
268
+ .and_return(posit)
269
+ is_expected.to eq 'file1'
270
+ end
271
+ end
272
+ context 'fail' do
273
+ let(:method) { 'request_user' }
274
+ let(:key) { 'name' }
275
+ let(:posit) { {'name' => 'onedrive'} }
276
+ before(:each) do
277
+ CloudDoor::OneDriveApi.any_instance.stub(:request_user)
278
+ .and_return(posit)
279
+ end
280
+ context 'method not exists' do
281
+ let(:method) { 'request_member' }
282
+ it { expect { subject }.to raise_error(CloudDoor::RequestMethodNotFoundException) }
283
+ end
284
+ context 'data not exists' do
285
+ let(:posit) { nil }
286
+ it { expect { subject }.to raise_error(CloudDoor::NoDataException) }
287
+ end
288
+ context 'key not exists' do
289
+ let(:key) { 'firstname' }
290
+ it { expect { subject }.to raise_error(CloudDoor::RequestPropertyNotFoundException) }
291
+ end
292
+ end
293
+ end
294
+ =end
295
+
296
+ describe 'download_file' do
297
+ subject { storage.download_file(file_name) }
298
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
299
+ let(:list_file) { storage.file_list.list_file }
300
+ context 'success' do
301
+ let(:file_name) { 'file1' }
302
+ let(:posit) { 'test' }
303
+ before(:each) do
304
+ list = [{'items' => {'file1' => {'id' => 'file.1234', 'type' => 'file'}}}]
305
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
306
+ end
307
+ it do
308
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_download)
309
+ .with('file.1234')
310
+ .and_return(posit)
311
+ is_expected.to be_truthy
312
+ end
313
+ after(:each) do
314
+ File.delete('file1') if File.exist?('file1')
315
+ end
316
+ end
317
+ context 'fail' do
318
+ context 'file name not input' do
319
+ let(:file_name) { '' }
320
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
321
+ end
322
+ context 'file id not exits' do
323
+ let(:file_name) { 'test' }
324
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
325
+ end
326
+ context 'not file' do
327
+ let(:file_name) { 'folder1' }
328
+ before(:each) do
329
+ list = [{'items' => {'folder1' => {'id' => 'folder.1234', 'type' => 'folder'}}}]
330
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
331
+ end
332
+ it { expect { subject }.to raise_error(CloudDoor::NotFileException) }
333
+ end
334
+ end
335
+ after(:each) do
336
+ File.delete(list_file) if File.exist?(list_file)
337
+ end
338
+ end
339
+
340
+ describe 'upload_file' do
341
+ subject { storage.upload_file(file_name) }
342
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
343
+ let(:list_file) { storage.file_list.list_file }
344
+ let(:up_file) { 'upload' }
345
+ context 'success' do
346
+ let(:file_name) { up_file }
347
+ let(:posit) { {'id' => 'file.1234', 'name' => 'file1'} }
348
+ let(:posit_dir) { {'data' => [{'id' => 'file.1234', 'name' => 'file1'}]} }
349
+ before(:each) do
350
+ open(up_file, 'wb') { |file| file << 'upload' }
351
+ end
352
+ it do
353
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_upload)
354
+ .with('upload', CloudDoor::OneDrive::ROOT_ID)
355
+ .and_return(posit)
356
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
357
+ .with(CloudDoor::OneDrive::ROOT_ID)
358
+ .and_return(posit_dir)
359
+ is_expected.to be_truthy
360
+ end
361
+ end
362
+ context 'fail' do
363
+ context 'upload file name not input' do
364
+ let(:file_name) { '' }
365
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
366
+ end
367
+ context 'file not exits' do
368
+ let(:file_name) { up_file }
369
+ it { expect { subject }.to raise_error(CloudDoor::FileNotExistsException) }
370
+ end
371
+ end
372
+ after(:each) do
373
+ File.delete(up_file) if File.exist?(up_file)
374
+ File.delete(list_file) if File.exist?(list_file)
375
+ end
376
+ end
377
+
378
+ describe 'delete_file' do
379
+ subject { storage.delete_file(file_name) }
380
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
381
+ let(:list_file) { storage.file_list.list_file }
382
+ context 'success' do
383
+ let(:file_name) { 'file1' }
384
+ let(:posit) { {'id' => 'file.1234', 'name' => 'file1'} }
385
+ let(:posit_dir) { {'data' => [{'id' => 'file.5678', 'name' => 'file2'}]} }
386
+ before(:each) do
387
+ list = [{'items' => {'file1' => {'id' => 'file.1234', 'type' => 'file'}}}]
388
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
389
+ end
390
+ it do
391
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_delete)
392
+ .with('file.1234')
393
+ .and_return(posit)
394
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
395
+ .with(CloudDoor::OneDrive::ROOT_ID)
396
+ .and_return(posit_dir)
397
+ is_expected.to be_truthy
398
+ end
399
+ end
400
+ context 'fail' do
401
+ context 'file name not input' do
402
+ let(:file_name) { '' }
403
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
404
+ end
405
+ context 'file id not exits' do
406
+ let(:file_name) { 'test' }
407
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
408
+ end
409
+ end
410
+ after(:each) do
411
+ File.delete(list_file) if File.exist?(list_file)
412
+ end
413
+ end
414
+
415
+ describe 'make_directory' do
416
+ subject { storage.make_directory(mkdir_name) }
417
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
418
+ let(:list_file) { storage.file_list.list_file }
419
+ context 'success' do
420
+ let(:mkdir_name) { 'folder1' }
421
+ let(:posit) { {'id' => 'folder.1234', 'name' => 'folder1'} }
422
+ let(:posit_dir) { {'data' => [{'id' => 'folder.1234', 'name' => 'folder1'}]} }
423
+ it do
424
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_mkdir)
425
+ .with(mkdir_name, CloudDoor::OneDrive::ROOT_ID)
426
+ .and_return(posit)
427
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
428
+ .with(CloudDoor::OneDrive::ROOT_ID)
429
+ .and_return(posit_dir)
430
+ is_expected.to be_truthy
431
+ end
432
+ end
433
+ context 'fail' do
434
+ context 'file name not input' do
435
+ let(:mkdir_name) { '' }
436
+ it { expect { subject }.to raise_error(CloudDoor::DirectoryNameEmptyException) }
437
+ end
438
+ end
439
+ after(:each) do
440
+ File.delete(list_file) if File.exist?(list_file)
441
+ end
442
+ end
443
+
444
+ describe 'assign_upload_file_name' do
445
+ subject { storage.assign_upload_file_name(file_name) }
446
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
447
+ context 'file' do
448
+ let(:file_name) { 'testfile' }
449
+ it { is_expected.to eq file_name }
450
+ end
451
+ context 'directory' do
452
+ let(:file_name) { 'testdir' }
453
+ before(:each) do
454
+ Dir.mkdir(file_name)
455
+ end
456
+ it { is_expected.to eq "#{file_name}.zip" }
457
+ after(:each) do
458
+ Dir.rmdir(file_name) if File.exist?(file_name)
459
+ end
460
+ end
461
+ end
462
+
463
+ describe 'file_exist?' do
464
+ subject { storage.file_exist?(file_name) }
465
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
466
+ let(:list_file) { storage.file_list.list_file }
467
+ before(:each) do
468
+ list = [{'items' => {'file1' => {'id' => 'file.1234', 'type' => 'file'}}}]
469
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
470
+ end
471
+ context 'return true' do
472
+ let(:posit) { {'data' => [{'id' => 'file.1234', 'name' => 'file1'}]} }
473
+ context 'file exists' do
474
+ let(:file_name) { 'file1' }
475
+ it do
476
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_dir)
477
+ .with(CloudDoor::OneDrive::ROOT_ID)
478
+ .and_return(posit)
479
+ is_expected.to be_truthy
480
+ end
481
+ end
482
+ end
483
+ context 'return false' do
484
+ let(:posit) { {'data' => [{'id' => 'file.5678', 'name' => 'file2'}]} }
485
+ context 'file not found' do
486
+ let(:file_name) { 'file1' }
487
+ before(:each) do
488
+ CloudDoor::OneDriveApi.any_instance.stub(:request_dir)
489
+ .and_return(posit)
490
+ end
491
+ it { is_expected.to be_falsey }
492
+ end
493
+ end
494
+ after(:each) do
495
+ File.delete(list_file) if File.exist?(list_file)
496
+ end
497
+ end
498
+
499
+ describe 'has_file?' do
500
+ subject { storage.has_file?(file_name) }
501
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
502
+ let(:list_file) { storage.file_list.list_file }
503
+ before(:each) do
504
+ file1 = {'id' => 'file.1234', 'type' => 'file'}
505
+ folder1 = {'id' => 'folder.5678', 'type' => 'folder'}
506
+ list = [
507
+ {'items' => {'file1' => file1, 'folder1' => folder1}}
508
+ ]
509
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
510
+ end
511
+ context 'return true' do
512
+ let(:file_name) { 'folder1' }
513
+ context 'count > 0' do
514
+ let(:posit) { {'count' => 5} }
515
+ before(:each) do
516
+ storage.stub(:file_exist?)
517
+ .with(file_name)
518
+ .and_return(true)
519
+ end
520
+ it do
521
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_file)
522
+ .with('folder.5678')
523
+ .and_return(posit)
524
+ is_expected.to be_truthy
525
+ end
526
+ end
527
+ end
528
+ context 'return false' do
529
+ context 'target is file' do
530
+ let(:file_name) { 'file1' }
531
+ it { is_expected.to be_falsey }
532
+ end
533
+ context 'count == 0' do
534
+ let(:file_name) { 'folder1' }
535
+ let(:posit) { {'count' => 0} }
536
+ before(:each) do
537
+ storage.stub(:file_exist?)
538
+ .with(file_name)
539
+ .and_return(true)
540
+ CloudDoor::OneDriveApi.any_instance.stub(:request_file)
541
+ .and_return(posit)
542
+ end
543
+ it { is_expected.to be_falsey }
544
+ end
545
+ end
546
+ context 'fail' do
547
+ context 'file name not input' do
548
+ let(:file_name) { '' }
549
+ it { expect { subject }.to raise_error(CloudDoor::FileNameEmptyException) }
550
+ end
551
+ context 'file id not exits' do
552
+ let(:file_name) { 'test' }
553
+ it { expect { subject }.to raise_error(CloudDoor::SetIDException) }
554
+ end
555
+ context 'data not found' do
556
+ let(:file_name) { 'folder1' }
557
+ let(:posit) { nil }
558
+ before(:each) do
559
+ storage.stub(:file_exist?)
560
+ .with(file_name)
561
+ .and_return(true)
562
+ CloudDoor::OneDriveApi.any_instance.stub(:request_file)
563
+ .and_return(posit)
564
+ end
565
+ it { expect { subject }.to raise_error(CloudDoor::NoDataException) }
566
+ end
567
+ end
568
+ after(:each) do
569
+ File.delete(list_file) if File.exist?(list_file)
570
+ end
571
+ end
572
+
573
+ describe 'file?' do
574
+ subject { storage.file?(file_name) }
575
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
576
+ let(:list_file) { storage.file_list.list_file }
577
+ before(:each) do
578
+ file1 = {'id' => 'file.1234', 'name' => 'file1', 'type' => 'file'}
579
+ folder1 = {'id' => 'folder.5678', 'name' => 'folder1', 'type' => 'folder'}
580
+ list = [
581
+ {'items' => {'file1' => file1, 'folder1' => folder1}}
582
+ ]
583
+ open(list_file, 'wb') { |file| file << Marshal.dump(list) }
584
+ end
585
+ context 'return true' do
586
+ let(:file_name) { 'file1' }
587
+ context 'file' do
588
+ it { is_expected.to be_truthy }
589
+ end
590
+ end
591
+ context 'return false' do
592
+ context 'file name not input' do
593
+ let(:file_name) { '' }
594
+ it { is_expected.to be_falsey }
595
+ end
596
+ context 'parent' do
597
+ let(:file_name) { '../' }
598
+ it { is_expected.to be_falsey }
599
+ end
600
+ context 'folder' do
601
+ let(:file_name) { 'folder1' }
602
+ it { is_expected.to be_falsey }
603
+ end
604
+ end
605
+ end
606
+
607
+ describe 'load_token' do
608
+ let(:token) { Fabricate.build(:token) }
609
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
610
+ let(:token_file) { storage.token.token_file }
611
+ before(:each) do
612
+ open(token_file, 'wb') { |file| file << Marshal.dump(token) }
613
+ end
614
+ it do
615
+ result = storage.load_token
616
+ expect(result.is_a?(CloudDoor::Token)).to be_truthy
617
+ end
618
+ after(:each) do
619
+ File.delete(token_file) if File.exist?(token_file)
620
+ end
621
+ end
622
+
623
+ describe 'refresh_token' do
624
+ subject { storage.refresh_token }
625
+ let(:storage) { create_storage(CloudDoor::OneDrive) }
626
+ context 'success' do
627
+ let(:posit) { {'access_token' => 'token2'} }
628
+ it do
629
+ expect_any_instance_of(CloudDoor::OneDriveApi).to receive(:request_refresh_token)
630
+ .and_return(posit)
631
+ subject
632
+ expect(storage.token.access_token).to eq 'token2'
633
+ end
634
+ end
635
+ context 'fail' do
636
+ context 'not Token class' do
637
+ before(:each) do
638
+ storage.token = 'token'
639
+ end
640
+ it { expect { subject }.to raise_error(CloudDoor::TokenClassException) }
641
+ end
642
+ context 'not Token class' do
643
+ let(:posit) { nil }
644
+ before(:each) do
645
+ CloudDoor::OneDriveApi.any_instance.stub(:request_refresh_token)
646
+ .and_return(posit)
647
+ end
648
+ it { expect { subject }.to raise_error(CloudDoor::NoDataException) }
649
+ end
650
+ end
651
+ end
652
+ end