1
0
Fork 0
mirror of synced 2024-06-26 10:10:57 +12:00

Fixed prefs input for users service

This commit is contained in:
Eldad Fux 2020-01-19 22:38:00 +02:00
parent a1023a683d
commit f10f03f1d2
2 changed files with 27 additions and 10 deletions

View file

@ -6,6 +6,7 @@ use Auth\Auth;
use Auth\Validator\Password;
use Utopia\Exception;
use Utopia\Response;
use Utopia\Validator\Assoc;
use Utopia\Validator\WhiteList;
use Utopia\Validator\Email;
use Utopia\Validator\Text;
@ -130,12 +131,9 @@ $utopia->get('/v1/users/:userId/prefs')
$prefs = $user->getAttribute('prefs', '');
if (empty($prefs)) {
$prefs = '[]';
}
try {
$prefs = json_decode($prefs, true);
$prefs = ($prefs) ? $prefs : [];
} catch (\Exception $error) {
throw new Exception('Failed to parse prefs', 500);
}
@ -387,7 +385,7 @@ $utopia->patch('/v1/users/:userId/prefs')
->label('sdk.method', 'updateUserPrefs')
->label('sdk.description', '/docs/references/users/update-user-prefs.md')
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
->param('prefs', '', function () { return new \Utopia\Validator\Mock(); }, 'Prefs key-value JSON object string.')
->param('prefs', '', function () { return new Assoc();}, 'Prefs key-value JSON object.')
->action(
function ($userId, $prefs) use ($response, $projectDB, $providers) {
$user = $projectDB->getDocument($userId);
@ -396,8 +394,11 @@ $utopia->patch('/v1/users/:userId/prefs')
throw new Exception('User not found', 404);
}
$old = json_decode($user->getAttribute('prefs', '{}'), true);
$old = ($old) ? $old : [];
$user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [
'prefs' => json_encode(array_merge(json_decode($user->getAttribute('prefs', '{}'), true), $prefs)),
'prefs' => json_encode(array_merge($old, $prefs)),
]));
if (false === $user) {
@ -406,12 +407,9 @@ $utopia->patch('/v1/users/:userId/prefs')
$prefs = $user->getAttribute('prefs', '');
if (empty($prefs)) {
$prefs = '[]';
}
try {
$prefs = json_decode($prefs, true);
$prefs = ($prefs) ? $prefs : [];
} catch (\Exception $error) {
throw new Exception('Failed to parse prefs', 500);
}

View file

@ -146,6 +146,25 @@ trait UsersBase
$this->assertEquals($user['body']['key1'], 'value1');
$this->assertEquals($user['body']['key2'], 'value2');
/**
* Test for FAILURE
*/
$user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/prefs', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$uid'],
], $this->getHeaders()), [
'prefs' => 'bad-string',
]);
$this->assertEquals($user['headers']['status-code'], 400);
$user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/prefs', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$uid'],
], $this->getHeaders()));
$this->assertEquals($user['headers']['status-code'], 400);
return $data;
}