2
2
3
3
namespace Biigle \Tests \Modules \UserDisks \Http \Controllers \Api ;
4
4
5
+ use Mockery ;
5
6
use ApiTestCase ;
6
7
use Biigle \Modules \UserDisks \UserDisk ;
8
+ use Biigle \Modules \UserDisks \Http \Controllers \Api \UserDiskController ;
7
9
8
10
class UserDiskControllerTest extends ApiTestCase
9
11
{
12
+ private $ mockS3 = null ;
13
+ public function setUp (): void
14
+ {
15
+ parent ::setUp ();
16
+ $ this ->mockS3 = Mockery::mock (UserDiskController::class)->shouldAllowmockingProtectedMethods ()->makePartial ();
17
+ $ this ->app ->instance (UserDiskController::class, $ this ->mockS3 );
18
+ }
19
+
10
20
public function testStore ()
11
21
{
12
22
$ this ->doTestApiRoute ('POST ' , "/api/v1/user-disks " );
@@ -37,12 +47,15 @@ public function testStore()
37
47
public function testStoreS3 ()
38
48
{
39
49
$ this ->beUser ();
50
+
51
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
40
52
$ this ->postJson ("/api/v1/user-disks " , [
41
53
'name ' => 'my disk ' ,
42
54
'type ' => 's3 ' ,
43
55
])
44
56
->assertStatus (422 );
45
57
58
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
46
59
$ this ->postJson ("/api/v1/user-disks " , [
47
60
'name ' => 'my disk ' ,
48
61
'type ' => 's3 ' ,
@@ -73,6 +86,8 @@ public function testStoreS3()
73
86
74
87
public function testDuplicateNames (){
75
88
$ this ->beUser ();
89
+
90
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
76
91
$ this ->postJson ("/api/v1/user-disks " , [
77
92
'name ' => 'my disk ' ,
78
93
'type ' => 's3 ' ,
@@ -97,6 +112,7 @@ public function testDuplicateNames(){
97
112
->assertStatus (422 );
98
113
99
114
$ this ->beEditor ();
115
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
100
116
$ this ->postJson ("/api/v1/user-disks " , [
101
117
'name ' => 'my disk ' ,
102
118
'type ' => 's3 ' ,
@@ -112,6 +128,8 @@ public function testDuplicateNames(){
112
128
public function testStoreS3RegionEmpty ()
113
129
{
114
130
$ this ->beUser ();
131
+
132
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
115
133
$ this ->postJson ("/api/v1/user-disks " , [
116
134
'name ' => 'my disk ' ,
117
135
'type ' => 's3 ' ,
@@ -130,12 +148,15 @@ public function testStoreS3RegionEmpty()
130
148
public function testStoreS3PathStyle ()
131
149
{
132
150
$ this ->beUser ();
151
+
152
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
133
153
$ this ->postJson ("/api/v1/user-disks " , [
134
154
'name ' => 'my disk ' ,
135
155
'type ' => 's3 ' ,
136
156
])
137
157
->assertStatus (422 );
138
158
159
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
139
160
$ this ->postJson ("/api/v1/user-disks " , [
140
161
'name ' => 'my disk ' ,
141
162
'type ' => 's3 ' ,
@@ -190,6 +211,7 @@ public function testUpdateS3()
190
211
]);
191
212
192
213
$ this ->be ($ disk ->user );
214
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
193
215
$ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
194
216
'type ' => 'unknown ' ,
195
217
'name ' => 'cba ' ,
@@ -230,6 +252,7 @@ public function testUpdateS3PathStyle()
230
252
]);
231
253
232
254
$ this ->be ($ disk ->user );
255
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
233
256
$ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
234
257
'endpoint ' => 'https://example.com/jkl ' ,
235
258
])
@@ -245,6 +268,7 @@ public function testUpdateS3PathStyle()
245
268
];
246
269
$ this ->assertEquals ($ expect , $ disk ->options );
247
270
271
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
248
272
$ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
249
273
'endpoint ' => 'https://jkl.example.com/ ' ,
250
274
])
@@ -334,4 +358,168 @@ public function testDestroy()
334
358
$ this ->deleteJson ("/api/v1/user-disks/ {$ disk ->id }" )->assertStatus (200 );
335
359
$ this ->assertNull ($ disk ->fresh ());
336
360
}
361
+ public function testStoreInvalidS3Config ()
362
+ {
363
+ $ this ->beUser ();
364
+
365
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
366
+ $ this ->postJson ("/api/v1/user-disks " , [
367
+ 'name ' => 'my disk ' ,
368
+ 'type ' => 's3 ' ,
369
+ 'key ' => 'abc ' ,
370
+ 'secret ' => 'abc ' ,
371
+ 'bucket ' => 'ucket ' ,
372
+ 'region ' => '' ,
373
+ 'endpoint ' => 'http://bucket.example.com ' ,
374
+ ])
375
+ ->assertUnprocessable ();
376
+
377
+ $ disk = UserDisk::where ('user_id ' , $ this ->user ()->id )->first ();
378
+ $ this ->assertEmpty ($ disk );
379
+ }
380
+
381
+ public function testUpdateInvalidS3Config ()
382
+ {
383
+ $ disk = UserDisk::factory ()->create ([
384
+ 'type ' => 's3 ' ,
385
+ 'name ' => 'abc ' ,
386
+ 'options ' => [
387
+ 'key ' => 'def ' ,
388
+ 'secret ' => 'ghi ' ,
389
+ 'bucket ' => 'jkl ' ,
390
+ 'region ' => 'us-east-1 ' ,
391
+ 'endpoint ' => 'https://jkl.example.com ' ,
392
+ 'use_path_style_endpoint ' => false ,
393
+ ],
394
+ ]);
395
+
396
+ $ this ->be ($ disk ->user );
397
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
398
+ $ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
399
+ 'endpoint ' => 'https://bucket.example.com ' ,
400
+ ])
401
+ ->assertUnprocessable ();
402
+
403
+ $ disk = $ disk ->fresh ();
404
+ $ this ->assertEquals ('https://jkl.example.com ' , $ disk ->options ['endpoint ' ]);
405
+ }
406
+
407
+ public function testStoreIncorrectBucketName ()
408
+ {
409
+ $ this ->beUser ();
410
+
411
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
412
+ $ this ->postJson ("/api/v1/user-disks " , [
413
+ 'name ' => 'my disk ' ,
414
+ 'type ' => 's3 ' ,
415
+ 'key ' => 'abc ' ,
416
+ 'secret ' => 'abc ' ,
417
+ 'bucket ' => 'ucket ' ,
418
+ 'region ' => '' ,
419
+ 'endpoint ' => 'http://bucket.example.com ' ,
420
+ ])
421
+ ->assertUnprocessable ();
422
+
423
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
424
+ $ this ->postJson ("/api/v1/user-disks " , [
425
+ 'name ' => 'my disk ' ,
426
+ 'type ' => 's3 ' ,
427
+ 'key ' => 'abc ' ,
428
+ 'secret ' => 'abc ' ,
429
+ 'bucket ' => 'bucket ' ,
430
+ 'region ' => '' ,
431
+ 'endpoint ' => 'http://ucket.example.com ' ,
432
+ ])
433
+ ->assertUnprocessable ();
434
+
435
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
436
+ $ this ->postJson ("/api/v1/user-disks " , [
437
+ 'name ' => 'my disk ' ,
438
+ 'type ' => 's3 ' ,
439
+ 'key ' => 'abc ' ,
440
+ 'secret ' => 'abc ' ,
441
+ 'bucket ' => 'ucket ' ,
442
+ 'region ' => '' ,
443
+ 'endpoint ' => 'http://example.com/bucket ' ,
444
+ ])
445
+ ->assertUnprocessable ();
446
+
447
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->never ();
448
+ $ this ->postJson ("/api/v1/user-disks " , [
449
+ 'name ' => 'my disk ' ,
450
+ 'type ' => 's3 ' ,
451
+ 'key ' => 'abc ' ,
452
+ 'secret ' => 'abc ' ,
453
+ 'bucket ' => 'bucket ' ,
454
+ 'region ' => '' ,
455
+ 'endpoint ' => 'http://example.com/ucket ' ,
456
+ ])
457
+ ->assertUnprocessable ();
458
+ }
459
+
460
+ public function testUpdateIncorrectBucketName ()
461
+ {
462
+ $ disk = UserDisk::factory ()->create ([
463
+ 'type ' => 's3 ' ,
464
+ 'name ' => 'abc ' ,
465
+ 'options ' => [
466
+ 'key ' => 'def ' ,
467
+ 'secret ' => 'ghi ' ,
468
+ 'bucket ' => 'jkl ' ,
469
+ 'region ' => 'us-east-1 ' ,
470
+ 'endpoint ' => 'https://jkl.example.com ' ,
471
+ 'use_path_style_endpoint ' => false ,
472
+ ],
473
+ ]);
474
+
475
+ $ this ->be ($ disk ->user );
476
+
477
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
478
+ $ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
479
+ 'type ' => 'unknown ' ,
480
+ 'name ' => 'cba ' ,
481
+ 'key ' => 'fed ' ,
482
+ 'secret ' => 'ihg ' ,
483
+ 'bucket ' => 'm ' ,
484
+ 'region ' => 'us-east-2 ' ,
485
+ 'endpoint ' => 'https://onm.example.com ' ,
486
+ ])
487
+ ->assertUnprocessable ();
488
+
489
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
490
+ $ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
491
+ 'type ' => 'unknown ' ,
492
+ 'name ' => 'cba ' ,
493
+ 'key ' => 'fed ' ,
494
+ 'secret ' => 'ihg ' ,
495
+ 'bucket ' => 'onm ' ,
496
+ 'region ' => 'us-east-2 ' ,
497
+ 'endpoint ' => 'https://m.example.com ' ,
498
+ ])
499
+ ->assertUnprocessable ();
500
+
501
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
502
+ $ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
503
+ 'type ' => 'unknown ' ,
504
+ 'name ' => 'cba ' ,
505
+ 'key ' => 'fed ' ,
506
+ 'secret ' => 'ihg ' ,
507
+ 'bucket ' => 'm ' ,
508
+ 'region ' => 'us-east-2 ' ,
509
+ 'endpoint ' => 'https://example.com/onm ' ,
510
+ ])
511
+ ->assertUnprocessable ();
512
+
513
+ $ this ->mockS3 ->shouldReceive ('canAccessDisk ' )->once ()->andReturn ([]);
514
+ $ this ->putJson ("/api/v1/user-disks/ {$ disk ->id }" , [
515
+ 'type ' => 'unknown ' ,
516
+ 'name ' => 'cba ' ,
517
+ 'key ' => 'fed ' ,
518
+ 'secret ' => 'ihg ' ,
519
+ 'bucket ' => 'onm ' ,
520
+ 'region ' => 'us-east-2 ' ,
521
+ 'endpoint ' => 'https://example.com/m ' ,
522
+ ])
523
+ ->assertUnprocessable ();
524
+ }
337
525
}
0 commit comments