From a6671a5caada60b4dff2ba2abfff44973a85d60c Mon Sep 17 00:00:00 2001 From: jaivix Date: Sun, 9 Jul 2023 16:50:09 +0530 Subject: [PATCH 1/3] fixed team creation response if teamId already exists --- app/config/errors.php | 5 +++++ app/controllers/api/teams.php | 29 +++++++++++++++----------- src/Appwrite/Extend/Exception.php | 1 + tests/e2e/Services/Teams/TeamsBase.php | 11 ++++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 2983d22cd8..1a93859457 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -227,6 +227,11 @@ return [ 'description' => 'The invite does not belong to the current user.', 'code' => 401, ], + Exception::TEAM_ID_CONFLICT => [ + 'name' => Exception::TEAM_ID_CONFLICT, + 'description' => 'Team ID already existed.', + 'code' => 409, + ], /** Membership */ Exception::MEMBERSHIP_NOT_FOUND => [ diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 338af70406..96b11c33dd 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -67,18 +67,23 @@ App::post('/v1/teams') $isAppUser = Auth::isAppUser(Authorization::getRoles()); $teamId = $teamId == 'unique()' ? ID::unique() : $teamId; - $team = Authorization::skip(fn() => $dbForProject->createDocument('teams', new Document([ - '$id' => $teamId, - '$permissions' => [ - Permission::read(Role::team($teamId)), - Permission::update(Role::team($teamId, 'owner')), - Permission::delete(Role::team($teamId, 'owner')), - ], - 'name' => $name, - 'total' => ($isPrivilegedUser || $isAppUser) ? 0 : 1, - 'prefs' => new \stdClass(), - 'search' => implode(' ', [$teamId, $name]), - ]))); + + try { + $team = Authorization::skip(fn() => $dbForProject->createDocument('teams', new Document([ + '$id' => $teamId, + '$permissions' => [ + Permission::read(Role::team($teamId)), + Permission::update(Role::team($teamId, 'owner')), + Permission::delete(Role::team($teamId, 'owner')), + ], + 'name' => $name, + 'total' => ($isPrivilegedUser || $isAppUser) ? 0 : 1, + 'prefs' => new \stdClass(), + 'search' => implode(' ', [$teamId, $name]), + ]))); + } catch (Duplicate $th) { + throw new Exception(Exception::TEAM_ID_CONFLICT); + } if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode if (!\in_array('owner', $roles)) { diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 7ed022b452..9e23c1cc52 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -83,6 +83,7 @@ class Exception extends \Exception public const TEAM_INVALID_SECRET = 'team_invalid_secret'; public const TEAM_MEMBERSHIP_MISMATCH = 'team_membership_mismatch'; public const TEAM_INVITE_MISMATCH = 'team_invite_mismatch'; + public const TEAM_ID_CONFLICT = 'team_id_conflict'; /** Membership */ public const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; diff --git a/tests/e2e/Services/Teams/TeamsBase.php b/tests/e2e/Services/Teams/TeamsBase.php index 04b5f93691..3abb312a23 100644 --- a/tests/e2e/Services/Teams/TeamsBase.php +++ b/tests/e2e/Services/Teams/TeamsBase.php @@ -79,6 +79,17 @@ trait TeamsBase $this->assertEquals(400, $response['headers']['status-code']); + $response = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'teamId' => $teamId, + 'name' => 'John' + ]); + + $this->assertEquals(409, $response['headers']['status-code']); + $this->assertEquals('team_id_conflict', $response['body']['type']); + return ['teamUid' => $teamUid, 'teamName' => $teamName]; } From 4db919b42dc4f7199ce19b2675d2b51f25e1b12d Mon Sep 17 00:00:00 2001 From: jaivix Date: Thu, 13 Jul 2023 00:33:38 +0530 Subject: [PATCH 2/3] linting and wording updates based on stnguyen90 feedback --- app/config/errors.php | 6 +++--- app/controllers/api/teams.php | 4 ++-- src/Appwrite/Extend/Exception.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 1a93859457..e71fb19547 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -227,9 +227,9 @@ return [ 'description' => 'The invite does not belong to the current user.', 'code' => 401, ], - Exception::TEAM_ID_CONFLICT => [ - 'name' => Exception::TEAM_ID_CONFLICT, - 'description' => 'Team ID already existed.', + Exception::TEAM_ALREADY_EXISTS => [ + 'name' => Exception::TEAM_ALREADY_EXISTS, + 'description' => 'Team with requested ID already exists.', 'code' => 409, ], diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 96b11c33dd..cdac9ba2fb 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -67,7 +67,7 @@ App::post('/v1/teams') $isAppUser = Auth::isAppUser(Authorization::getRoles()); $teamId = $teamId == 'unique()' ? ID::unique() : $teamId; - + try { $team = Authorization::skip(fn() => $dbForProject->createDocument('teams', new Document([ '$id' => $teamId, @@ -82,7 +82,7 @@ App::post('/v1/teams') 'search' => implode(' ', [$teamId, $name]), ]))); } catch (Duplicate $th) { - throw new Exception(Exception::TEAM_ID_CONFLICT); + throw new Exception(Exception::TEAM_ALREADY_EXISTS); } if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 9e23c1cc52..42f8876d69 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -83,7 +83,7 @@ class Exception extends \Exception public const TEAM_INVALID_SECRET = 'team_invalid_secret'; public const TEAM_MEMBERSHIP_MISMATCH = 'team_membership_mismatch'; public const TEAM_INVITE_MISMATCH = 'team_invite_mismatch'; - public const TEAM_ID_CONFLICT = 'team_id_conflict'; + public const TEAM_ALREADY_EXISTS = 'team_already_exists'; /** Membership */ public const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; From b78401923f42bef08f1381454f13dbcc91071d39 Mon Sep 17 00:00:00 2001 From: jaivix Date: Thu, 13 Jul 2023 12:10:52 +0530 Subject: [PATCH 3/3] test case minor fix --- tests/e2e/Services/Teams/TeamsBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Teams/TeamsBase.php b/tests/e2e/Services/Teams/TeamsBase.php index 3abb312a23..8e19bede17 100644 --- a/tests/e2e/Services/Teams/TeamsBase.php +++ b/tests/e2e/Services/Teams/TeamsBase.php @@ -88,7 +88,7 @@ trait TeamsBase ]); $this->assertEquals(409, $response['headers']['status-code']); - $this->assertEquals('team_id_conflict', $response['body']['type']); + $this->assertEquals('team_already_exists', $response['body']['type']); return ['teamUid' => $teamUid, 'teamName' => $teamName]; }