From f10f03f1d27e1330ad875c193f27bc81c33eba34 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sun, 19 Jan 2020 22:38:00 +0200 Subject: [PATCH] Fixed prefs input for users service --- app/controllers/api/users.php | 18 ++++++++---------- tests/e2e/Services/Users/UsersBase.php | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 04f660cdb..2bd916f70 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -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); } diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index fc65b0b51..0b305ac02 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -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; }