1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

Updated user update response

This commit is contained in:
eldadfux 2019-10-21 09:01:07 +03:00
parent 1a28d5dead
commit c51c73b53a
4 changed files with 205 additions and 74 deletions

View file

@ -12,7 +12,6 @@ use Utopia\Validator\Text;
use Utopia\Validator\Range;
use Utopia\Locale\Locale;
use Database\Database;
use Database\Validator\Authorization;
use Database\Validator\UID;
use DeviceDetector\DeviceDetector;
use GeoIp2\Database\Reader;
@ -56,6 +55,7 @@ $utopia->get('/v1/users')
return $value->getArrayCopy(array_merge(
[
'$uid',
'status',
'email',
'registration',
'confirm',
@ -98,13 +98,14 @@ $utopia->get('/v1/users/:userId')
$response->json(array_merge($user->getArrayCopy(array_merge(
[
'$uid',
'status',
'email',
'registration',
'confirm',
'name',
],
$oauthKeys
)), ['roles' => Authorization::getRoles()]));
)), ['roles' => []]));
}
);
@ -322,7 +323,7 @@ $utopia->post('/v1/users')
'registration',
'confirm',
'name',
], $oauthKeys)), ['roles' => Authorization::getRoles()]));
], $oauthKeys)), ['roles' => []]));
}
);
@ -333,9 +334,9 @@ $utopia->patch('/v1/users/:userId/status')
->label('sdk.method', 'updateUserStatus')
->label('sdk.description', '/docs/references/users/update-user-status.md')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('status', '', function () { return new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED]); }, 'User Status code. To activate the user pass '.Auth::USER_STATUS_ACTIVATED.', to blocking the user pass '.Auth::USER_STATUS_BLOCKED.' and for disabling the user pass '.Auth::USER_STATUS_UNACTIVATED)
->param('status', '', function () { return new WhiteList([Auth::USER_STATUS_ACTIVATED, Auth::USER_STATUS_BLOCKED, Auth::USER_STATUS_UNACTIVATED]); }, 'User Status code. To activate the user pass '.Auth::USER_STATUS_ACTIVATED.', to block the user pass '.Auth::USER_STATUS_BLOCKED.' and for disabling the user pass '.Auth::USER_STATUS_UNACTIVATED)
->action(
function ($userId, $status) use ($response, $projectDB) {
function ($userId, $status) use ($response, $projectDB, $providers) {
$user = $projectDB->getDocument($userId);
if (empty($user->getUid()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
@ -349,9 +350,27 @@ $utopia->patch('/v1/users/:userId/status')
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
$oauthKeys = [];
foreach ($providers as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
$oauthKeys[] = 'oauth'.ucfirst($key);
$oauthKeys[] = 'oauth'.ucfirst($key).'AccessToken';
}
$response
->json(array('result' => 'success'));
->json(array_merge($user->getArrayCopy(array_merge([
'$uid',
'status',
'email',
'registration',
'confirm',
'name',
], $oauthKeys)), ['roles' => []]));
}
);
@ -364,7 +383,7 @@ $utopia->patch('/v1/users/:userId/prefs')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('prefs', '', function () { return new \Utopia\Validator\Mock(); }, 'Prefs key-value JSON object string.')
->action(
function ($userId, $prefs) use ($response, $projectDB) {
function ($userId, $prefs) use ($response, $projectDB, $providers) {
$user = $projectDB->getDocument($userId);
if (empty($user->getUid()) || Database::SYSTEM_COLLECTION_USERS != $user->getCollection()) {
@ -374,11 +393,31 @@ $utopia->patch('/v1/users/:userId/prefs')
$user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [
'prefs' => json_encode(array_merge(json_decode($user->getAttribute('prefs', '{}'), true), $prefs)),
]));
if (false === $user) {
throw new Exception('Failed saving user to DB', 500);
}
$response->json(array('result' => 'success'));
$oauthKeys = [];
foreach ($providers as $key => $provider) {
if (!$provider['enabled']) {
continue;
}
$oauthKeys[] = 'oauth'.ucfirst($key);
$oauthKeys[] = 'oauth'.ucfirst($key).'AccessToken';
}
$response
->json(array_merge($user->getArrayCopy(array_merge([
'$uid',
'status',
'email',
'registration',
'confirm',
'name',
], $oauthKeys)), ['roles' => []]));
}
);

View file

@ -48,4 +48,73 @@ class BaseConsole extends TestCase
return $response;
}
public function initProject(array $scopes) {
$response = $this->register();
$this->assertEquals('http://localhost/success', $response['headers']['location']);
$this->assertEquals("", $response['body']);
$session = $this->client->parseCookie($response['headers']['set-cookie'])['a-session-console'];
$team = $this->client->call(Client::METHOD_POST, '/teams', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project Team',
]);
$this->assertEquals(201, $team['headers']['status-code']);
$this->assertEquals('Demo Project Team', $team['body']['name']);
$this->assertNotEmpty($team['body']['$uid']);
$project = $this->client->call(Client::METHOD_POST, '/projects', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project',
'teamId' => $team['body']['$uid'],
'description' => 'Demo Project Description',
'logo' => '',
'url' => 'https://appwrite.io',
'legalName' => '',
'legalCountry' => '',
'legalState' => '',
'legalCity' => '',
'legalAddress' => '',
'legalTaxId' => '',
]);
$this->assertEquals(201, $project['headers']['status-code']);
$this->assertNotEmpty($project['body']);
$key = $this->client->call(Client::METHOD_POST, '/projects/' . $project['body']['$uid'] . '/keys', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project Key',
'scopes' => $scopes,
]);
$this->assertEquals(201, $project['headers']['status-code']);
$this->assertNotEmpty($key['body']);
$this->assertNotEmpty($key['body']['secret']);
$user = $this->projectRegister($project['body']['$uid']);
$this->assertEquals('http://localhost/success', $user['headers']['location']);
$this->assertEquals("", $user['body']);
return [
'email' => $this->demoEmail,
'password' => $this->demoPassword,
'session' => $session,
'projectUid' => $project['body']['$uid'],
'projectAPIKeySecret' => $key['body']['secret'],
'projectSession' => $this->client->parseCookie($user['headers']['set-cookie'])['a-session-' . $project['body']['$uid']],
];
}
}

View file

@ -8,72 +8,7 @@ class ProjectDatabaseTest extends BaseProjects
{
public function testRegisterSuccess()
{
$response = $this->register();
$this->assertEquals('http://localhost/success', $response['headers']['location']);
$this->assertEquals("", $response['body']);
$session = $this->client->parseCookie($response['headers']['set-cookie'])['a-session-console'];
$team = $this->client->call(Client::METHOD_POST, '/teams', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project Team',
]);
$this->assertEquals(201, $team['headers']['status-code']);
$this->assertEquals('Demo Project Team', $team['body']['name']);
$this->assertNotEmpty($team['body']['$uid']);
$project = $this->client->call(Client::METHOD_POST, '/projects', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project',
'teamId' => $team['body']['$uid'],
'description' => 'Demo Project Description',
'logo' => '',
'url' => 'https://appwrite.io',
'legalName' => '',
'legalCountry' => '',
'legalState' => '',
'legalCity' => '',
'legalAddress' => '',
'legalTaxId' => '',
]);
$this->assertEquals(201, $project['headers']['status-code']);
$this->assertNotEmpty($project['body']);
$key = $this->client->call(Client::METHOD_POST, '/projects/' . $project['body']['$uid'] . '/keys', [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'cookie' => 'a-session-console=' . $session,
], [
'name' => 'Demo Project Key',
'scopes' => ['collections.read', 'collections.write', 'documents.read', 'documents.write',],
]);
$this->assertEquals(201, $project['headers']['status-code']);
$this->assertNotEmpty($key['body']);
$this->assertNotEmpty($key['body']['secret']);
$user = $this->projectRegister($project['body']['$uid']);
$this->assertEquals('http://localhost/success', $user['headers']['location']);
$this->assertEquals("", $user['body']);
return [
'email' => $this->demoEmail,
'password' => $this->demoPassword,
'session' => $session,
'projectUid' => $project['body']['$uid'],
'projectAPIKeySecret' => $key['body']['secret'],
'projectSession' => $this->client->parseCookie($user['headers']['set-cookie'])['a-session-' . $project['body']['$uid']],
];
return $this->initProject(['collections.read', 'collections.write', 'documents.read', 'documents.write',]);
}
/**

View file

@ -0,0 +1,88 @@
<?php
namespace Tests\E2E;
use Tests\E2E\Client;
class ProjectUsersTest extends BaseProjects
{
public function testRegisterSuccess()
{
return $this->initProject(['users.read', 'users.write']);
}
/**
* @depends testRegisterSuccess
*/
public function testUserCreateSuccess($data)
{
$user = $this->client->call(Client::METHOD_POST, '/users', [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectUid'],
'x-appwrite-key' => $data['projectAPIKeySecret'],
], [
'email' => 'users.service@example.com',
'password' => 'password',
'name' => 'Project User',
]);
$this->assertEquals($user['headers']['status-code'], 201);
$this->assertEquals($user['body']['name'], 'Project User');
$this->assertEquals($user['body']['email'], 'users.service@example.com');
$this->assertEquals($user['body']['status'], 0);
$this->assertGreaterThan(0, $user['body']['registration']);
$this->assertIsArray($user['body']['roles']);
return array_merge($data, ['userId' => $user['body']['$uid']]);
}
/**
* @depends testUserCreateSuccess
*/
public function testUserReadSuccess($data)
{
$user = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'], [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectUid'],
'x-appwrite-key' => $data['projectAPIKeySecret'],
]);
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['name'], 'Project User');
$this->assertEquals($user['body']['email'], 'users.service@example.com');
$this->assertEquals($user['body']['status'], 0);
$this->assertGreaterThan(0, $user['body']['registration']);
$this->assertIsArray($user['body']['roles']);
return $data;
}
/**
* @depends testUserReadSuccess
*/
public function testUserUpdateStatusSuccess($data)
{
$user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/status', [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectUid'],
'x-appwrite-key' => $data['projectAPIKeySecret'],
], [
'status' => 2,
]);
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['status'], 2);
$user = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'], [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectUid'],
'x-appwrite-key' => $data['projectAPIKeySecret'],
]);
var_dump($user);
$this->assertEquals($user['headers']['status-code'], 200);
$this->assertEquals($user['body']['status'], 2);
return $data;
}
}