From 66b043b980f1e613939185da9db6c1da1f3130c7 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Mon, 21 Oct 2019 09:43:12 +0300 Subject: [PATCH] Added more tests to users API --- app/controllers/users.php | 27 +++++++---------- tests/e2e/ProjectUsersTest.php | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/app/controllers/users.php b/app/controllers/users.php index 8b3684e34..dbb809016 100644 --- a/app/controllers/users.php +++ b/app/controllers/users.php @@ -398,26 +398,19 @@ $utopia->patch('/v1/users/:userId/prefs') throw new Exception('Failed saving user to DB', 500); } - $oauthKeys = []; + $prefs = $user->getAttribute('prefs', ''); - foreach ($providers as $key => $provider) { - if (!$provider['enabled']) { - continue; - } - - $oauthKeys[] = 'oauth'.ucfirst($key); - $oauthKeys[] = 'oauth'.ucfirst($key).'AccessToken'; + if (empty($prefs)) { + $prefs = '[]'; } - $response - ->json(array_merge($user->getArrayCopy(array_merge([ - '$uid', - 'status', - 'email', - 'registration', - 'confirm', - 'name', - ], $oauthKeys)), ['roles' => []])); + try { + $prefs = json_decode($prefs, true); + } catch (\Exception $error) { + throw new Exception('Failed to parse prefs', 500); + } + + $response->json($prefs); } ); diff --git a/tests/e2e/ProjectUsersTest.php b/tests/e2e/ProjectUsersTest.php index bc80be4b5..2025164e3 100644 --- a/tests/e2e/ProjectUsersTest.php +++ b/tests/e2e/ProjectUsersTest.php @@ -72,6 +72,34 @@ class ProjectUsersTest extends BaseProjects $this->assertEquals($logs['headers']['status-code'], 200); $this->assertIsArray($logs['body']); + $users = $this->client->call(Client::METHOD_GET, '/users', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $data['projectUid'], + 'x-appwrite-key' => $data['projectAPIKeySecret'], + ]); + + $this->assertEquals($users['headers']['status-code'], 200); + $this->assertIsArray($users['body']); + $this->assertIsArray($users['body']['users']); + $this->assertIsInt($users['body']['sum']); + $this->assertGreaterThan(0, $users['body']['sum']); + + $users = $this->client->call(Client::METHOD_GET, '/users', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $data['projectUid'], + 'x-appwrite-key' => $data['projectAPIKeySecret'], + ], [ + 'search' => 'demo' + ]); + + $this->assertEquals($users['headers']['status-code'], 200); + $this->assertIsArray($users['body']); + $this->assertIsArray($users['body']['users']); + $this->assertIsInt($users['body']['sum']); + $this->assertEquals(1, $users['body']['sum']); + $this->assertGreaterThan(0, $users['body']['sum']); + $this->assertCount(1, $users['body']['users']); + return $data; } @@ -102,4 +130,30 @@ class ProjectUsersTest extends BaseProjects return $data; } + + /** + * @depends testUserReadSuccess + */ + public function testUserUpdatePrefsSuccess($data) + { + $user = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/prefs', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $data['projectUid'], + 'x-appwrite-key' => $data['projectAPIKeySecret'], + ], [ + 'prefs' => [ + 'key1' => 'value1', + 'key2' => 'value2', + ], + ]); + + $this->assertEquals($user['headers']['status-code'], 200); + $this->assertEquals($user['body']['key1'], 'value1'); + $this->assertEquals($user['body']['key2'], 'value2'); + + return $data; + } + + // TODO add test for session delete + // TODO add test for all sessions delete }