1
0
Fork 0
mirror of synced 2024-10-02 18:26:49 +13:00

adds test for target controllers

This commit is contained in:
prateek banga 2023-08-24 01:54:25 +05:30
parent ac33d7828f
commit ec54000263
7 changed files with 278 additions and 68 deletions

View file

@ -243,7 +243,7 @@ return [
Exception::USER_TARGET_ALREADY_EXISTS => [
'name' => Exception::USER_TARGET_ALREADY_EXISTS,
'description' => 'A target with the same ID already exists.',
'code' => 404,
'code' => 409,
],
/** Teams */

View file

@ -1834,9 +1834,9 @@ App::get('/v1/account/targets')
->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('response')
->inject('user')
->action(function (Response $response, Document $user) {
->inject('response')
->action(function (Document $user, Response $response) {
$targets = $user->getAttribute('targets', []);
@ -1900,13 +1900,13 @@ App::get('/v1/account/targets/:targetId')
->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_SESSION)
->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('response')
->inject('user')
->action(function (string $targetId, Response $response, Document $user) {
->inject('response')
->action(function (string $targetId, Document $user, Response $response) {
$target = $user->find('$id', $targetId, 'targets');
@ -2999,9 +2999,12 @@ App::put('/v1/account/verification/phone')
});
App::post('/v1/account/targets')
->desc('Create User Target')
->desc('Create Account\'s Target')
->groups(['api', 'account'])
->label('event', 'users.[userId].targets.[targetId].create')
->label('audits.event', 'target.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')
@ -3010,26 +3013,20 @@ App::post('/v1/account/targets')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('providerId', '', new UID(), 'ID of the provider.', false)
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', false)
->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 $userId, string $providerId, string $identifier, Response $response, Database $dbForProject, Event $events) {
$provider = $dbForProject->getDocument('providers', $providerId);
->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);
}
$user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) {
throw new Exception(Exception::USER_NOT_FOUND);
}
$target = $dbForProject->getDocument('targets', $targetId);
if (!$target->isEmpty()) {
@ -3038,29 +3035,34 @@ App::post('/v1/account/targets')
$target = $dbForProject->createDocument('targets', new Document([
'$id' => $targetId,
// TO DO: what permissions should be given when created a target.
'$permissions' => [
Permission::read(Role::any())
Permission::read(Role::user($user->getId())),
Permission::update(Role::user($user->getId())),
Permission::delete(Role::user($user->getId())),
],
'providerId' => $providerId,
'providerInternalId' => $provider->getInternalId(),
'providerType' => null,
'userId' => $userId,
'userId' => $user->getId(),
'userInternalId' => $user->getInternalId(),
'identifier' => $identifier,
]));
$dbForProject->deleteCachedDocument('users', $user->getId());
$events
->setParam('userId', $userId);
->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 user target\'s identifier')
->desc('Update account\'s target identifier')
->groups(['api', 'account'])
->label('event', 'users.[userId].targets.[targetId].update')
->label('audits.event', 'target.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')
@ -3069,19 +3071,13 @@ App::patch('/v1/account/targets/:targetId/identifier')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', true)
->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 $userId, string $identifier, Response $response, Database $dbForProject, Event $events) {
$user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) {
throw new Exception(Exception::USER_NOT_FOUND);
}
->action(function (string $targetId, string $identifier, Document $user, Response $response, Database $dbForProject, Event $events) {
$target = $dbForProject->getDocument('targets', $targetId);
@ -3096,37 +3092,34 @@ App::patch('/v1/account/targets/:targetId/identifier')
$dbForProject->deleteCachedDocument('users', $user->getId());
$events
->setParam('userId', $userId);
->setParam('userId', $user->getId())
->setParam('targetId', $targetId);
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($target, Response::MODEL_TARGET);
});
App::delete('/v1/account/targets/:targetId')
->desc('Delete user target')
->desc('Delete account\'s target')
->groups(['api', 'account'])
->label('event', 'users.[userId].targets.[targetId].delete')
->label('audits.event', 'target.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_OK)
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_NONE)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('targetId', '', new UID(), 'Target ID.')
->inject('user')
->inject('response')
->inject('dbForProject')
->inject('events')
->action(function (string $targetId, string $userId, Response $response, Database $dbForProject, Event $events) {
$user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) {
throw new Exception(Exception::USER_NOT_FOUND);
}
->action(function (string $targetId, Document $user, Response $response, Database $dbForProject, Event $events) {
$target = $dbForProject->getDocument('targets', $targetId);
@ -3136,13 +3129,14 @@ App::delete('/v1/account/targets/:targetId')
$target = $dbForProject->deleteDocument('targets', $target->getId());
$dbForProject->deleteCachedDocument('users', $user->getId());
$user = $dbForProject->getDocument('users', $userId);
$user = $dbForProject->getDocument('users', $user->getId());
// clone user object to send to workers
$clone = clone $user;
$events
->setParam('userId', $userId)
->setParam('userId', $user->getId())
->setParam('targetId', $targetId)
->setPayload($response->output($clone, Response::MODEL_USER));
$response->noContent();

View file

@ -376,6 +376,8 @@ App::post('/v1/users/:userId/targets')
->desc('Create User Target')
->groups(['api', 'users'])
->label('event', 'users.[userId].targets.[targetId].create')
->label('audits.event', 'target.create')
->label('audits.resource', 'user/{response.$id}')
->label('scope', 'targets.write')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users')
@ -384,14 +386,14 @@ App::post('/v1/users/:userId/targets')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('providerId', '', new UID(), 'ID of the provider.', false)
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', false)
->param('userId', '', new UID(), 'User ID.')
->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('response')
->inject('dbForProject')
->inject('events')
->action(function (string $targetId, string $userId, string $providerId, string $identifier, Response $response, Database $dbForProject, Event $events) {
->action(function (string $userId, string $targetId, string $providerId, string $identifier, Response $response, Database $dbForProject, Event $events) {
$provider = $dbForProject->getDocument('providers', $providerId);
if ($provider->isEmpty()) {
@ -425,7 +427,8 @@ App::post('/v1/users/:userId/targets')
]));
$dbForProject->deleteCachedDocument('users', $user->getId());
$events
->setParam('userId', $userId);
->setParam('userId', $userId)
->setParam('targetId', $targetId);
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($target, Response::MODEL_TARGET);
@ -532,7 +535,7 @@ App::get('/v1/users/:userId/prefs')
App::get('/v1/users/:userId/targets/:targetId')
->desc('Get User Target')
->groups(['api', 'users'])
->label('scope', 'users.read')
->label('scope', 'targets.read')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users')
->label('sdk.method', 'getTarget')
@ -725,7 +728,7 @@ App::get('/v1/users/:userId/logs')
App::get('/v1/users/:userId/targets')
->desc('List User Targets')
->groups(['api', 'users'])
->label('scope', 'users.read')
->label('scope', 'targets.read')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users')
->label('sdk.method', 'listTargets')
@ -1215,6 +1218,8 @@ App::patch('/v1/users/:userId/targets/:targetId/identifier')
->desc('Update user target\'s identifier')
->groups(['api', 'users'])
->label('event', 'users.[userId].targets.[targetId].update')
->label('audits.event', 'target.update')
->label('audits.resource', 'user/{response.$id}')
->label('scope', 'targets.write')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users')
@ -1223,13 +1228,13 @@ App::patch('/v1/users/:userId/targets/:targetId/identifier')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', true)
->param('userId', '', new UID(), 'User ID.')
->param('targetId', '', new UID(), 'Target ID.')
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)')
->inject('response')
->inject('dbForProject')
->inject('events')
->action(function (string $targetId, string $userId, string $identifier, Response $response, Database $dbForProject, Event $events) {
->action(function (string $userId, string $targetId, string $identifier, Response $response, Database $dbForProject, Event $events) {
$user = $dbForProject->getDocument('users', $userId);
@ -1250,10 +1255,10 @@ App::patch('/v1/users/:userId/targets/:targetId/identifier')
$dbForProject->deleteCachedDocument('users', $user->getId());
$events
->setParam('userId', $userId);
->setParam('userId', $userId)
->setParam ('targetId', $targetId);
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($target, Response::MODEL_TARGET);
});
@ -1388,20 +1393,22 @@ App::delete('/v1/users/:userId/targets/:targetId')
->desc('Delete user target')
->groups(['api', 'users'])
->label('event', 'users.[userId].targets.[targetId].delete')
->label('audits.event', 'target.delete')
->label('audits.resource', 'user/{response.$id}')
->label('scope', 'targets.write')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users')
->label('sdk.method', 'deleteTarget')
->label('sdk.description', '/docs/references/users/delete-target.md')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_NONE)
->param('userId', '', new UID(), 'ID of the user.', false)
->param('targetId', '', new UID(), 'Target ID.', false)
->param('userId', '', new UID(), 'User ID.')
->param('targetId', '', new UID(), 'Target ID.')
->inject('response')
->inject('dbForProject')
->inject('events')
->action(function (string $targetId, string $userId, Response $response, Database $dbForProject, Event $events) {
->action(function (string $userId, string $targetId, Response $response, Database $dbForProject, Event $events) {
$user = $dbForProject->getDocument('users', $userId);
@ -1424,6 +1431,7 @@ App::delete('/v1/users/:userId/targets/:targetId')
$events
->setParam('userId', $userId)
->setParam('targetId', $targetId)
->setPayload($response->output($clone, Response::MODEL_USER));
$response->noContent();

View file

@ -81,6 +81,8 @@ trait ProjectCustom
'locale.read',
'avatars.read',
'health.read',
'targets.read',
'targets.write',
'providers.read',
'providers.write',
'messages.read',

View file

@ -363,7 +363,7 @@ trait AccountBase
$this->assertEquals($response['headers']['status-code'], 200);
$this->assertIsArray($response['body']['logs']);
$this->assertNotEmpty($response['body']['logs']);
$this->assertCount(3, $response['body']['logs']);
$this->assertCount(5, $response['body']['logs']);
$this->assertIsNumeric($response['body']['total']);
$this->assertContains($response['body']['logs'][1]['event'], ["session.create"]);
$this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP));

View file

@ -116,6 +116,118 @@ 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';

View file

@ -1211,6 +1211,100 @@ trait UsersBase
$this->assertEquals($response['headers']['status-code'], 400);
}
/**
* @depends testGetUser
*/
public function testCreateUserTarget(array $data): array
{
$provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/sendgrid', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'name' => 'Sengrid1',
'apiKey' => 'my-apikey'
]);
$this->assertEquals(201, $provider['headers']['status-code']);
$response = $this->client->call(Client::METHOD_POST, '/users/' . $data['userId'] . '/targets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'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 $response['body'];
}
/**
* @depends testCreateUserTarget
*/
public function testUpdateUserTarget(array $data): array
{
$response = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/targets/' . $data['$id'] .'/identifier', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'identifier' => 'my-updated-token',
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('my-updated-token', $response['body']['identifier']);
return $response['body'];
}
/**
* @depends testGetUser
*/
public function testListUserTarget(array $data)
{
$response = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/targets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(1, $response['body']['total']);
}
/**
* @depends testUpdateUserTarget
*/
public function testGetUserTarget(array $data)
{
$response = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/targets/' .$data['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($data['$id'], $response['body']['$id']);
}
/**
* @depends testUpdateUserTarget
*/
public function testDeleteUserTarget(array $data)
{
$response = $this->client->call(Client::METHOD_DELETE, '/users/' . $data['userId'] . '/targets/' .$data['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals(204, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/targets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(0, $response['body']['total']);
}
/**
* @depends testGetUser
*/