From d7c85622607e7066bb76c2ea67eb823fd028c95e Mon Sep 17 00:00:00 2001 From: prateek banga Date: Fri, 25 Aug 2023 16:02:46 +0530 Subject: [PATCH] removes target controllers from account service --- app/controllers/api/account.php | 194 ------------------ .../Account/AccountCustomClientTest.php | 112 ---------- 2 files changed, 306 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index efcef85b68..fecbf51d17 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1825,29 +1825,6 @@ App::get('/v1/account/logs') ]), Response::MODEL_LOG_LIST); }); -App::get('/v1/account/targets') - ->desc('List Account Targets') - ->groups(['api', 'account']) - ->label('scope', 'account') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'listTargets') - ->label('sdk.description', '/docs/references/account/list-targets.md') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_TARGET_LIST) - ->inject('user') - ->inject('response') - ->action(function (Document $user, Response $response) { - - $targets = $user->getAttribute('targets', []); - - $response->dynamic(new Document([ - 'targets' => $targets, - 'total' => \count($targets), - ]), Response::MODEL_TARGET_LIST); - }); - App::get('/v1/account/sessions/:sessionId') ->desc('Get Session') ->groups(['api', 'account']) @@ -1892,33 +1869,6 @@ App::get('/v1/account/sessions/:sessionId') throw new Exception(Exception::USER_SESSION_NOT_FOUND); }); -App::get('/v1/account/targets/:targetId') - ->desc('Get Target') - ->groups(['api', 'account']) - ->label('scope', 'account') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'getTarget') - ->label('sdk.description', '/docs/references/account/get-Target.md') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_TARGET) - ->label('sdk.offline.model', '/account/targets') - ->label('sdk.offline.key', '{targetId}') - ->param('targetId', '', new UID(), 'Target ID.') - ->inject('user') - ->inject('response') - ->action(function (string $targetId, Document $user, Response $response) { - - $target = $user->find('$id', $targetId, 'targets'); - - if (empty($target)) { - throw new Exception(Exception::USER_TARGET_NOT_FOUND); - } - - $response->dynamic($target, Response::MODEL_TARGET); - }); - App::patch('/v1/account/name') ->desc('Update Name') ->groups(['api', 'account']) @@ -2999,147 +2949,3 @@ App::put('/v1/account/verification/phone') $response->dynamic($verificationDocument, Response::MODEL_TOKEN); }); - -App::post('/v1/account/targets') - ->desc('Create Account\'s Target') - ->groups(['api', 'account']) - ->label('event', 'users.[userId].targets.[targetId].create') - ->label('audits.event', 'targets.create') - ->label('audits.resource', 'user/{response.userId}') - ->label('audits.userId', '{response.userId}') - ->label('scope', 'targets.write') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'createTarget') - ->label('sdk.description', '/docs/references/account/create-target.md') - ->label('sdk.response.code', Response::STATUS_CODE_CREATED) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_TARGET) - ->param('targetId', '', new UID(), 'Target ID.') - ->param('providerId', '', new UID(), 'Provider ID.') - ->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)') - ->inject('user') - ->inject('response') - ->inject('dbForProject') - ->inject('events') - ->action(function (string $targetId, string $providerId, string $identifier, Document $user, Response $response, Database $dbForProject, Event $events) { - $provider = Authorization::skip(fn () => $dbForProject->getDocument('providers', $providerId)); - - if ($provider->isEmpty()) { - throw new Exception(Exception::PROVIDER_NOT_FOUND); - } - - $target = $dbForProject->getDocument('targets', $targetId); - - if (!$target->isEmpty()) { - throw new Exception(Exception::USER_TARGET_ALREADY_EXISTS); - } - - $target = $dbForProject->createDocument('targets', new Document([ - '$id' => $targetId, - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::user($user->getId())), - Permission::delete(Role::user($user->getId())), - ], - 'providerId' => $providerId, - 'providerInternalId' => $provider->getInternalId(), - 'providerType' => null, - 'userId' => $user->getId(), - 'userInternalId' => $user->getInternalId(), - 'identifier' => $identifier, - ])); - $dbForProject->deleteCachedDocument('users', $user->getId()); - $events - ->setParam('userId', $user->getId()) - ->setParam('targetId', $targetId); - $response - ->setStatusCode(Response::STATUS_CODE_CREATED) - ->dynamic($target, Response::MODEL_TARGET); - }); - -App::patch('/v1/account/targets/:targetId/identifier') - ->desc('Update account\'s target identifier') - ->groups(['api', 'account']) - ->label('event', 'users.[userId].targets.[targetId].update') - ->label('audits.event', 'targets.update') - ->label('audits.resource', 'user/{response.userId}') - ->label('audits.userId', '{response.userId}') - ->label('scope', 'targets.write') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'updateTargetIdentifier') - ->label('sdk.description', '/docs/references/account/update-target-identifier.md') - ->label('sdk.response.code', Response::STATUS_CODE_OK) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_TARGET) - ->param('targetId', '', new UID(), 'Target ID.') - ->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)') - ->inject('user') - ->inject('response') - ->inject('dbForProject') - ->inject('events') - ->action(function (string $targetId, string $identifier, Document $user, Response $response, Database $dbForProject, Event $events) { - - $target = $dbForProject->getDocument('targets', $targetId); - - if ($target->isEmpty()) { - throw new Exception(Exception::USER_TARGET_NOT_FOUND); - } - - // Update the target identifier here - $target->setAttribute('identifier', $identifier); - - $target = $dbForProject->updateDocument('targets', $target->getId(), $target); - $dbForProject->deleteCachedDocument('users', $user->getId()); - - $events - ->setParam('userId', $user->getId()) - ->setParam('targetId', $targetId); - - $response - ->dynamic($target, Response::MODEL_TARGET); - }); - -App::delete('/v1/account/targets/:targetId') - ->desc('Delete account\'s target') - ->groups(['api', 'account']) - ->label('event', 'users.[userId].targets.[targetId].delete') - ->label('audits.event', 'targets.delete') - ->label('audits.resource', 'user/{response.userId}') - ->label('audits.userId', '{response.userId}') - ->label('scope', 'targets.write') - ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT]) - ->label('sdk.namespace', 'account') - ->label('sdk.method', 'deleteTarget') - ->label('sdk.description', '/docs/references/account/delete-target.md') - ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) - ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) - ->label('sdk.response.model', Response::MODEL_NONE) - ->param('targetId', '', new UID(), 'Target ID.') - ->inject('user') - ->inject('response') - ->inject('dbForProject') - ->inject('events') - ->action(function (string $targetId, Document $user, Response $response, Database $dbForProject, Event $events) { - - $target = $dbForProject->getDocument('targets', $targetId); - - if ($target->isEmpty()) { - throw new Exception(Exception::USER_TARGET_NOT_FOUND); - } - - $target = $dbForProject->deleteDocument('targets', $target->getId()); - $dbForProject->deleteCachedDocument('users', $user->getId()); - $user = $dbForProject->getDocument('users', $user->getId()); - - // clone user object to send to workers - $clone = clone $user; - - $events - ->setParam('userId', $user->getId()) - ->setParam('targetId', $targetId) - ->setPayload($response->output($clone, Response::MODEL_USER)); - - $response->noContent(); - }); diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 6dce5d8f59..d272605e58 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -116,118 +116,6 @@ class AccountCustomClientTest extends Scope return []; } - /** - * @depends testCreateAccountSession - */ - public function testCreateAccountTarget(array $data): array - { - $session = $data['session'] ?? ''; - $apiKey = $this->getProject()['apiKey']; - - $provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/sendgrid', [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $apiKey, - ], [ - 'name' => 'Sengrid1', - 'apiKey' => 'my-apikey' - ]); - $this->assertEquals(201, $provider['headers']['status-code']); - $response = $this->client->call(Client::METHOD_POST, '/account/targets', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ]), [ - 'targetId' => ID::unique(), - 'providerId' => $provider['body']['$id'], - 'identifier' => 'my-token', - ]); - $this->assertEquals(201, $response['headers']['status-code']); - $this->assertEquals($provider['body']['$id'], $response['body']['providerId']); - $this->assertEquals('my-token', $response['body']['identifier']); - return ['target' => $response['body'], 'session' => $session]; - } - - /** - * @depends testCreateAccountTarget - */ - public function testUpdateAccountTarget(array $data): array - { - $session = $data['session'] ?? ''; - $target = $data['target']; - $response = $this->client->call(Client::METHOD_PATCH, '/account/targets/' . $target['$id'] . '/identifier', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ]), [ - 'identifier' => 'my-updated-token', - ]); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals('my-updated-token', $response['body']['identifier']); - return $data; - } - - /** - * @depends testCreateAccountSession - */ - public function testListAccountTarget(array $data) - { - $session = $data['session'] ?? ''; - $response = $this->client->call(Client::METHOD_GET, '/account/targets', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ])); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(1, $response['body']['total']); - } - - /** - * @depends testCreateAccountTarget - */ - public function testGetAccountTarget(array $data) - { - $session = $data['session'] ?? ''; - $target = $data['target']; - - $response = $this->client->call(Client::METHOD_GET, '/account/targets/' . $target['$id'], array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ])); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals($data['target']['$id'], $response['body']['$id']); - } - - /** - * @depends testUpdateAccountTarget - */ - public function testDeleteAccountTarget(array $data) - { - $session = $data['session'] ?? ''; - $target = $data['target']; - - $response = $this->client->call(Client::METHOD_DELETE, '/account/targets/' . $target['$id'], array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ])); - $this->assertEquals(204, $response['headers']['status-code']); - $response = $this->client->call(Client::METHOD_GET, '/account/targets', array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, - ])); - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(0, $response['body']['total']); - } - public function testBlockedAccount(): array { $email = uniqid() . 'user@localhost.test';