Fixed prefs input for users service
This commit is contained in:
parent
a1023a683d
commit
f10f03f1d2
|
@ -6,6 +6,7 @@ use Auth\Auth;
|
||||||
use Auth\Validator\Password;
|
use Auth\Validator\Password;
|
||||||
use Utopia\Exception;
|
use Utopia\Exception;
|
||||||
use Utopia\Response;
|
use Utopia\Response;
|
||||||
|
use Utopia\Validator\Assoc;
|
||||||
use Utopia\Validator\WhiteList;
|
use Utopia\Validator\WhiteList;
|
||||||
use Utopia\Validator\Email;
|
use Utopia\Validator\Email;
|
||||||
use Utopia\Validator\Text;
|
use Utopia\Validator\Text;
|
||||||
|
@ -130,12 +131,9 @@ $utopia->get('/v1/users/:userId/prefs')
|
||||||
|
|
||||||
$prefs = $user->getAttribute('prefs', '');
|
$prefs = $user->getAttribute('prefs', '');
|
||||||
|
|
||||||
if (empty($prefs)) {
|
|
||||||
$prefs = '[]';
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$prefs = json_decode($prefs, true);
|
$prefs = json_decode($prefs, true);
|
||||||
|
$prefs = ($prefs) ? $prefs : [];
|
||||||
} catch (\Exception $error) {
|
} catch (\Exception $error) {
|
||||||
throw new Exception('Failed to parse prefs', 500);
|
throw new Exception('Failed to parse prefs', 500);
|
||||||
}
|
}
|
||||||
|
@ -387,7 +385,7 @@ $utopia->patch('/v1/users/:userId/prefs')
|
||||||
->label('sdk.method', 'updateUserPrefs')
|
->label('sdk.method', 'updateUserPrefs')
|
||||||
->label('sdk.description', '/docs/references/users/update-user-prefs.md')
|
->label('sdk.description', '/docs/references/users/update-user-prefs.md')
|
||||||
->param('userId', '', function () { return new UID(); }, 'User unique ID.')
|
->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(
|
->action(
|
||||||
function ($userId, $prefs) use ($response, $projectDB, $providers) {
|
function ($userId, $prefs) use ($response, $projectDB, $providers) {
|
||||||
$user = $projectDB->getDocument($userId);
|
$user = $projectDB->getDocument($userId);
|
||||||
|
@ -396,8 +394,11 @@ $utopia->patch('/v1/users/:userId/prefs')
|
||||||
throw new Exception('User not found', 404);
|
throw new Exception('User not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$old = json_decode($user->getAttribute('prefs', '{}'), true);
|
||||||
|
$old = ($old) ? $old : [];
|
||||||
|
|
||||||
$user = $projectDB->updateDocument(array_merge($user->getArrayCopy(), [
|
$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) {
|
if (false === $user) {
|
||||||
|
@ -406,12 +407,9 @@ $utopia->patch('/v1/users/:userId/prefs')
|
||||||
|
|
||||||
$prefs = $user->getAttribute('prefs', '');
|
$prefs = $user->getAttribute('prefs', '');
|
||||||
|
|
||||||
if (empty($prefs)) {
|
|
||||||
$prefs = '[]';
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$prefs = json_decode($prefs, true);
|
$prefs = json_decode($prefs, true);
|
||||||
|
$prefs = ($prefs) ? $prefs : [];
|
||||||
} catch (\Exception $error) {
|
} catch (\Exception $error) {
|
||||||
throw new Exception('Failed to parse prefs', 500);
|
throw new Exception('Failed to parse prefs', 500);
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,6 +146,25 @@ trait UsersBase
|
||||||
$this->assertEquals($user['body']['key1'], 'value1');
|
$this->assertEquals($user['body']['key1'], 'value1');
|
||||||
$this->assertEquals($user['body']['key2'], 'value2');
|
$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;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue