tokens test and scopes

This commit is contained in:
Damodar Lohani
2024-11-18 08:52:16 +00:00
parent 0d1fec47c6
commit d32dc0a4d9
9 changed files with 170 additions and 83 deletions
+2
View File
@@ -76,6 +76,8 @@ $admins = [
'topics.read',
'subscribers.write',
'subscribers.read',
'tokens.read',
'tokens.write',
];
return [
+6
View File
@@ -130,4 +130,10 @@ return [ // List of publicly visible scopes
'assistant.read' => [
'description' => 'Access to read the Assistant service',
],
'tokens.read' => [
'description' => 'Access to read your project\'s tokens',
],
'tokens.write' => [
'description' => 'Access to create, update, and delete your project\'s tokens',
],
];
+5
View File
@@ -10,6 +10,7 @@ use Appwrite\Event\Func;
use Appwrite\Event\Usage;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Network\Validator\Origin;
use Appwrite\Platform\Appwrite;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Request\Filters\V16 as RequestV16;
use Appwrite\Utopia\Request\Filters\V17 as RequestV17;
@@ -38,6 +39,7 @@ use Utopia\Logger\Adapter\Sentry;
use Utopia\Logger\Log;
use Utopia\Logger\Log\User;
use Utopia\Logger\Logger;
use Utopia\Platform\Service;
use Utopia\System\System;
use Utopia\Validator\Hostname;
use Utopia\Validator\Text;
@@ -1100,3 +1102,6 @@ App::wildcard()
foreach (Config::getParam('services', []) as $service) {
include_once $service['controller'];
}
$platform = new Appwrite();
$platform->init(Service::TYPE_HTTP);
@@ -29,7 +29,7 @@ class CreateToken extends Action
public function __construct()
{
$this->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
$this->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
->setHttpPath('/v1/tokens')
->desc('Create token')
->groups(['api', 'token'])
@@ -2,7 +2,12 @@
namespace Appwrite\Platform\Modules\Tokens\Services;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\CreateToken;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\DeleteToken;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\GetToken;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\GetTokenJWT;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\ListTokens;
use Appwrite\Platform\Modules\Tokens\Http\Tokens\UpdateToken;
use Utopia\Platform\Service;
class Http extends Service
@@ -10,6 +15,14 @@ class Http extends Service
public function __construct()
{
$this->type = Service::TYPE_HTTP;
$this->addAction(ListTokens::getName(), new ListTokens());
$this
->addAction(CreateToken::getName(), new CreateToken())
->addAction(DeleteToken::getName(), new DeleteToken())
->addAction(GetToken::getName(), new GetToken())
->addAction(GetTokenJWT::getName(), new GetTokenJWT())
->addAction(ListTokens::getName(), new ListTokens())
->addAction(UpdateToken::getName(), new UpdateToken())
;
}
}
+1 -81
View File
@@ -11,86 +11,6 @@ use Utopia\Database\Helpers\Role;
trait TokensBase
{
/**
* @group fileTokens
*/
public function testCreateFileToken(): array
{
$bucket = $this->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;
}
}
@@ -0,0 +1,14 @@
<?php
namespace Tests\E2E\Services\Tokens;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideConsole;
class TokensConsoleClientTest extends Scope
{
use SideConsole;
use TokensBase;
use ProjectCustom;
}
@@ -0,0 +1,15 @@
<?php
namespace Tests\E2E\Services\Tokens;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
class TokensCustomClientTest extends Scope
{
use TokensBase;
use ProjectCustom;
use SideClient;
}
@@ -0,0 +1,112 @@
<?php
namespace Tests\E2E\Services\Tokens;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
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;
class TokensCustomServerTest extends Scope
{
use TokensBase;
use ProjectCustom;
use SideServer;
public function testCreateToken(): array
{
$bucket = $this->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, '/tokens', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $this->getHeaders()), [
'resourceType' => 'files',
'resourceId' => $bucketId . ':' . $fileId
]);
$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;
}
/**
* @depends testCreateToken
*/
public function testUpdateToken(array $data): array
{
$bucketId = $data['bucketId'];
$fileId = $data['fileId'];
$tokenId = $data['tokenId'];
$expiry = DateTime::now();
$res = $this->client->call(Client::METHOD_PUT, '/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 testUpdateToken
*/
public function testDeleteToken(array $data): array
{
return $data;
}
}