From 0d1fec47c64263e94fe2064e02ac6f8c1daa98fd Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 18 Nov 2024 08:00:14 +0000 Subject: [PATCH] tokens test --- .github/workflows/tests.yml | 1 + tests/e2e/Services/Storage/StorageBase.php | 43 ---------- tests/e2e/Services/Tokens/TokensBase.php | 96 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 43 deletions(-) create mode 100644 tests/e2e/Services/Tokens/TokensBase.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7c53b03b52..3e895b051f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -125,6 +125,7 @@ jobs: Webhooks, VCS, Messaging, + Tokens, ] steps: diff --git a/tests/e2e/Services/Storage/StorageBase.php b/tests/e2e/Services/Storage/StorageBase.php index 836861c4fb..79e8083552 100644 --- a/tests/e2e/Services/Storage/StorageBase.php +++ b/tests/e2e/Services/Storage/StorageBase.php @@ -5,7 +5,6 @@ namespace Tests\E2E\Services\Storage; use Appwrite\Extend\Exception; use CURLFile; use Tests\E2E\Client; -use Utopia\Database\DateTime; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -753,48 +752,6 @@ trait StorageBase return $data; } - /** - * @group fileTokens - * @depends testCreateBucketFile - */ - public function testCreateFileToken(array $data): array - { - $bucketId = $data['bucketId']; - $fileId = $data['fileId']; - - $res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/tokens', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(201, $res['headers']['status-code']); - $this->assertEquals('file', $res['body']['resourceType']); - - $data['tokenId'] = $res['body']['$id']; - return $data; - } - - /** - * @group fileTokens - * @depends testCreateFileToken - */ - public function testUpdateFileToken(array $data): array - { - $bucketId = $data['bucketId']; - $fileId = $data['fileId']; - $tokenId = $data['tokenId']; - - $expiry = DateTime::now(); - $res = $this->client->call(Client::METHOD_PUT, '/storage/buckets/'. $bucketId . '/files/'. $fileId . '/tokens/' . $tokenId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'expire' => $expiry, - ]); - - $this->assertEquals($expiry, $res['body']['expire']); - return $data; - } /** * @depends testCreateBucketFileZstdCompression diff --git a/tests/e2e/Services/Tokens/TokensBase.php b/tests/e2e/Services/Tokens/TokensBase.php new file mode 100644 index 0000000000..5c57677783 --- /dev/null +++ b/tests/e2e/Services/Tokens/TokensBase.php @@ -0,0 +1,96 @@ +client->call(Client::METHOD_POST, '/storage/buckets', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'bucketId' => ID::unique(), + 'name' => 'Test Bucket', + 'fileSecurity' => true, + 'maximumFileSize' => 2000000, //2MB + 'allowedFileExtensions' => ['jpg', 'png', 'jfif'], + 'permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + $this->assertEquals(201, $bucket['headers']['status-code']); + $this->assertNotEmpty($bucket['body']['$id']); + + $bucketId = $bucket['body']['$id']; + + $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + 'permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + $this->assertEquals(201, $file['headers']['status-code']); + $this->assertNotEmpty($file['body']['$id']); + + $fileId = $file['body']['$id']; + + $res = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/tokens', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(201, $res['headers']['status-code']); + $this->assertEquals('files', $res['body']['resourceType']); + + $data = []; + $data['fileId'] = $fileId; + $data['bucketId'] = $bucketId; + $data['tokenId'] = $res['body']['$id']; + return $data; + } + + /** + * @group fileTokens + * @depends testCreateFileToken + */ + public function testUpdateFileToken(array $data): array + { + $bucketId = $data['bucketId']; + $fileId = $data['fileId']; + $tokenId = $data['tokenId']; + + $expiry = DateTime::now(); + $res = $this->client->call(Client::METHOD_PUT, '/storage/buckets/'. $bucketId . '/files/'. $fileId . '/tokens/' . $tokenId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'expire' => $expiry, + ]); + + $this->assertEquals($expiry, $res['body']['expire']); + return $data; + } + +}