Skip to content

Commit a1e01e6

Browse files
committed
Add and update tests
1 parent 4e1bc98 commit a1e01e6

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

tests/Http/Controllers/Api/UserDiskControllerTest.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22

33
namespace Biigle\Tests\Modules\UserDisks\Http\Controllers\Api;
44

5+
use Mockery;
56
use ApiTestCase;
67
use Biigle\Modules\UserDisks\UserDisk;
8+
use Biigle\Modules\UserDisks\Http\Controllers\Api\UserDiskController;
79

810
class UserDiskControllerTest extends ApiTestCase
911
{
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+
1020
public function testStore()
1121
{
1222
$this->doTestApiRoute('POST', "/api/v1/user-disks");
@@ -37,12 +47,15 @@ public function testStore()
3747
public function testStoreS3()
3848
{
3949
$this->beUser();
50+
51+
$this->mockS3->shouldReceive('canAccessDisk')->never();
4052
$this->postJson("/api/v1/user-disks", [
4153
'name' => 'my disk',
4254
'type' => 's3',
4355
])
4456
->assertStatus(422);
4557

58+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
4659
$this->postJson("/api/v1/user-disks", [
4760
'name' => 'my disk',
4861
'type' => 's3',
@@ -73,6 +86,8 @@ public function testStoreS3()
7386

7487
public function testDuplicateNames(){
7588
$this->beUser();
89+
90+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
7691
$this->postJson("/api/v1/user-disks", [
7792
'name' => 'my disk',
7893
'type' => 's3',
@@ -97,6 +112,7 @@ public function testDuplicateNames(){
97112
->assertStatus(422);
98113

99114
$this->beEditor();
115+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
100116
$this->postJson("/api/v1/user-disks", [
101117
'name' => 'my disk',
102118
'type' => 's3',
@@ -112,6 +128,8 @@ public function testDuplicateNames(){
112128
public function testStoreS3RegionEmpty()
113129
{
114130
$this->beUser();
131+
132+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
115133
$this->postJson("/api/v1/user-disks", [
116134
'name' => 'my disk',
117135
'type' => 's3',
@@ -130,12 +148,15 @@ public function testStoreS3RegionEmpty()
130148
public function testStoreS3PathStyle()
131149
{
132150
$this->beUser();
151+
152+
$this->mockS3->shouldReceive('canAccessDisk')->never();
133153
$this->postJson("/api/v1/user-disks", [
134154
'name' => 'my disk',
135155
'type' => 's3',
136156
])
137157
->assertStatus(422);
138158

159+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
139160
$this->postJson("/api/v1/user-disks", [
140161
'name' => 'my disk',
141162
'type' => 's3',
@@ -190,6 +211,7 @@ public function testUpdateS3()
190211
]);
191212

192213
$this->be($disk->user);
214+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
193215
$this->putJson("/api/v1/user-disks/{$disk->id}", [
194216
'type' => 'unknown',
195217
'name' => 'cba',
@@ -230,6 +252,7 @@ public function testUpdateS3PathStyle()
230252
]);
231253

232254
$this->be($disk->user);
255+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
233256
$this->putJson("/api/v1/user-disks/{$disk->id}", [
234257
'endpoint' => 'https://example.com/jkl',
235258
])
@@ -245,6 +268,7 @@ public function testUpdateS3PathStyle()
245268
];
246269
$this->assertEquals($expect, $disk->options);
247270

271+
$this->mockS3->shouldReceive('canAccessDisk')->once()->andReturn([]);
248272
$this->putJson("/api/v1/user-disks/{$disk->id}", [
249273
'endpoint' => 'https://jkl.example.com/',
250274
])
@@ -334,4 +358,168 @@ public function testDestroy()
334358
$this->deleteJson("/api/v1/user-disks/{$disk->id}")->assertStatus(200);
335359
$this->assertNull($disk->fresh());
336360
}
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+
}
337525
}

0 commit comments

Comments
 (0)