Add S3 and Wasabi as storage providers (#281)

This commit is contained in:
Taki Elias
2024-09-07 03:29:43 +06:00
committed by GitHub
parent 1391eb32d8
commit e39e8c17a2
24 changed files with 1055 additions and 130 deletions

View File

@ -6,6 +6,11 @@
use App\Facades\FTP;
use App\Models\Backup;
use App\Models\Database;
use App\Models\StorageProvider as StorageProviderModel;
use App\StorageProviders\S3;
use App\StorageProviders\Wasabi;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
@ -52,6 +57,24 @@ public function test_see_providers_list(): void
'provider' => StorageProvider::DROPBOX,
]);
$this->get(route('settings.storage-providers'))
->assertSuccessful()
->assertSee($provider->profile);
$provider = \App\Models\StorageProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => StorageProvider::S3,
]);
$this->get(route('settings.storage-providers'))
->assertSuccessful()
->assertSee($provider->profile);
$provider = \App\Models\StorageProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => StorageProvider::WASABI,
]);
$this->get(route('settings.storage-providers'))
->assertSuccessful()
->assertSee($provider->profile);
@ -101,6 +124,178 @@ public function test_cannot_delete_provider(): void
]);
}
public function test_s3_connect_successful()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::S3,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method
$s3ClientMock->expects($this->once())
->method('execute')
->willReturn(['Buckets' => []]);
// Mock the S3 class to return the mocked S3Client
$s3 = $this->getMockBuilder(S3::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$s3->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertTrue($s3->connect());
}
public function test_s3_connect_failure()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::S3,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method to throw an S3Exception
$s3ClientMock->expects($this->once())
->method('execute')
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
// Mock the S3 class to return the mocked S3Client
$s3 = $this->getMockBuilder(S3::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$s3->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertFalse($s3->connect());
}
public function test_wasabi_connect_successful()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method
$s3ClientMock->expects($this->once())
->method('execute')
->willReturn(['Buckets' => []]);
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertTrue($wasabi->connect());
}
public function test_wasabi_connect_failure()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method to throw an S3Exception
$s3ClientMock->expects($this->once())
->method('execute')
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertFalse($wasabi->connect());
}
/**
* @TODO: complete FTP tests
*/