From 96bef81ebc2ac062fbe3bd2bde8be5775fe2cf1e Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 26 May 2025 11:33:58 +0530 Subject: [PATCH] fix: tests! --- tests/e2e/Services/Teams/TeamsBaseClient.php | 83 +++--------- .../Services/Teams/TeamsConsoleClientTest.php | 125 +++++++++++++++--- 2 files changed, 119 insertions(+), 89 deletions(-) diff --git a/tests/e2e/Services/Teams/TeamsBaseClient.php b/tests/e2e/Services/Teams/TeamsBaseClient.php index 2aacf4abc1..9b19572d7b 100644 --- a/tests/e2e/Services/Teams/TeamsBaseClient.php +++ b/tests/e2e/Services/Teams/TeamsBaseClient.php @@ -10,17 +10,6 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator; trait TeamsBaseClient { - /** - * Checks if the current project is the Console project. - * - * This is necessary because the last admin of an org - * cannot be removed, which affects certain test conditions. - */ - private function isConsoleProject(): bool - { - return $this->getProject()['$id'] === 'console'; - } - /** * @depends testCreateTeam */ @@ -71,11 +60,7 @@ trait TeamsBaseClient ]); $this->assertEquals(200, $response['headers']['status-code']); - if ($this->isConsoleProject()) { - $this->assertCount(1, $response['body']['memberships']); - } else { - $this->assertCount(0, $response['body']['memberships']); - } + $this->assertCount(0, $response['body']['memberships']); $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([ 'content-type' => 'application/json', @@ -99,11 +84,7 @@ trait TeamsBaseClient ]); $this->assertEquals(200, $response['headers']['status-code']); - if ($this->isConsoleProject()) { - $this->assertCount(1, $response['body']['memberships']); - } else { - $this->assertCount(0, $response['body']['memberships']); - } + $this->assertCount(0, $response['body']['memberships']); $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([ 'content-type' => 'application/json', @@ -392,11 +373,7 @@ trait TeamsBaseClient $this->assertEquals(200, $memberships['headers']['status-code']); $this->assertIsInt($memberships['body']['total']); $this->assertNotEmpty($memberships['body']['memberships']); - if ($this->isConsoleProject()) { - $this->assertCount(4, $memberships['body']['memberships']); - } else { - $this->assertCount(3, $memberships['body']['memberships']); - } + $this->assertCount(3, $memberships['body']['memberships']); $response = $this->client->call(Client::METHOD_GET, '/teams/' . $data['teamUid'] . '/memberships', array_merge([ 'content-type' => 'application/json', @@ -410,11 +387,7 @@ trait TeamsBaseClient $this->assertEquals(200, $response['headers']['status-code']); $this->assertIsInt($response['body']['total']); $this->assertNotEmpty($response['body']['memberships']); - if ($this->isConsoleProject()) { - $this->assertCount(3, $response['body']['memberships']); - } else { - $this->assertCount(2, $response['body']['memberships']); - } + $this->assertCount(2, $response['body']['memberships']); $this->assertEquals($memberships['body']['memberships'][1]['$id'], $response['body']['memberships'][0]['$id']); } @@ -737,8 +710,8 @@ trait TeamsBaseClient } /** - * @depends testUpdateTeamMembershipRoles - */ + * @depends testUpdateTeamMembershipRoles + */ public function testDeleteTeamMembership($data): array { $teamUid = $data['teamUid'] ?? ''; @@ -751,11 +724,7 @@ trait TeamsBaseClient ], $this->getHeaders())); $this->assertEquals(200, $response['headers']['status-code']); - if ($this->isConsoleProject()) { - $this->assertCount(5, $response['body']['memberships']); - } else { - $this->assertCount(4, $response['body']['memberships']); - } + $this->assertEquals(4, $response['body']['total']); $ownerMembershipUid = $response['body']['memberships'][0]['$id']; @@ -785,18 +754,14 @@ trait TeamsBaseClient 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, ]); - if ($this->isConsoleProject()) { - $this->assertEquals(400, $response['headers']['status-code']); - } else { - $this->assertEquals(401, $response['headers']['status-code']); - } + $this->assertEquals(401, $response['headers']['status-code']); /** * Test for SUCCESS */ /** - * Test for when a user other than the owner tries to delete an owner's membership. + * Test for when a user other than the owner tries to delete their membership */ $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $membershipUid, [ 'origin' => 'http://localhost', @@ -805,13 +770,8 @@ trait TeamsBaseClient 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, ]); - if ($this->isConsoleProject()) { - $this->assertEquals(400, $response['headers']['status-code']); - $this->assertNotEmpty($response['body']); - } else { - $this->assertEquals(204, $response['headers']['status-code']); - $this->assertEmpty($response['body']); - } + $this->assertEquals(204, $response['headers']['status-code']); + $this->assertEmpty($response['body']); $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([ 'content-type' => 'application/json', @@ -819,11 +779,7 @@ trait TeamsBaseClient ], $this->getHeaders())); $this->assertEquals(200, $response['headers']['status-code']); - if ($this->isConsoleProject()) { - $this->assertEquals(5, $response['body']['total']); - } else { - $this->assertEquals(3, $response['body']['total']); - } + $this->assertEquals(3, $response['body']['total']); /** * Test for when the owner tries to delete their membership @@ -834,13 +790,8 @@ trait TeamsBaseClient 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); - if ($this->isConsoleProject()) { - $this->assertEquals(400, $response['headers']['status-code']); - $this->assertNotEmpty($response['body']); - } else { - $this->assertEquals(204, $response['headers']['status-code']); - $this->assertEmpty($response['body']); - } + $this->assertEquals(204, $response['headers']['status-code']); + $this->assertEmpty($response['body']); $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships/' . $ownerMembershipUid, array_merge([ 'origin' => 'http://localhost', @@ -848,11 +799,7 @@ trait TeamsBaseClient 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders())); - if ($this->isConsoleProject()) { - $this->assertEquals(200, $response['headers']['status-code']); - } else { - $this->assertEquals(404, $response['headers']['status-code']); - } + $this->assertEquals(404, $response['headers']['status-code']); return []; } diff --git a/tests/e2e/Services/Teams/TeamsConsoleClientTest.php b/tests/e2e/Services/Teams/TeamsConsoleClientTest.php index 3798019884..f414123788 100644 --- a/tests/e2e/Services/Teams/TeamsConsoleClientTest.php +++ b/tests/e2e/Services/Teams/TeamsConsoleClientTest.php @@ -41,7 +41,7 @@ class TeamsConsoleClientTest extends Scope /** * Test for SUCCESS */ - $response = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([ + $developer = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ @@ -51,7 +51,8 @@ class TeamsConsoleClientTest extends Scope 'url' => 'http://localhost:5000/join-us#title' ]); - $this->assertEquals(201, $response['headers']['status-code']); + $developerUserId = $developer['body']['$id']; + $this->assertEquals(201, $developer['headers']['status-code']); $response = $this->client->call(Client::METHOD_GET, '/users', array_merge([ 'content-type' => 'application/json', @@ -66,7 +67,7 @@ class TeamsConsoleClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); - $ownerMembershipUid = $response['body']['memberships'][1]['$id']; + $ownerMembershipUid = $response['body']['memberships'][0]['$id']; $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $ownerMembershipUid, array_merge([ 'origin' => 'http://localhost', @@ -77,6 +78,16 @@ class TeamsConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertEquals('There must be at least one owner in the organization.', $response['body']['message']); + // Remove the excess developer member to reduce the membership count in `TeamsBaseClient` tests. + // This is necessary because the only owner cannot be removed in the console project / top level team / organization. + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $developerUserId, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(204, $response['headers']['status-code']); + return $data; } @@ -87,21 +98,6 @@ class TeamsConsoleClientTest extends Scope $membershipUid = $data['membershipUid'] ?? ''; $session = $data['session'] ?? ''; - /** - * Test for FAILURE - */ - $roles = ['developer']; - $response = $this->client->call(Client::METHOD_PATCH, '/teams/' . $teamUid . '/memberships/' . $membershipUid, array_merge([ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'roles' => $roles - ]); - - $this->assertEquals(400, $response['headers']['status-code']); - $this->assertEquals('There must be at least one owner in the organization.', $response['body']['message']); - /** * Test for unknown team */ @@ -110,7 +106,7 @@ class TeamsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'roles' => $roles + 'roles' => ['developer'] ]); $this->assertEquals(404, $response['headers']['status-code']); @@ -123,7 +119,7 @@ class TeamsConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'roles' => $roles + 'roles' => ['developer'] ]); $this->assertEquals(404, $response['headers']['status-code']); @@ -138,7 +134,7 @@ class TeamsConsoleClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, ], [ - 'roles' => $roles + 'roles' => ['developer'] ]); $this->assertEquals(401, $response['headers']['status-code']); @@ -146,4 +142,91 @@ class TeamsConsoleClientTest extends Scope return $data; } + + /** + * @depends testUpdateTeamMembershipRoles + */ + public function testDeleteTeamMembership($data): array + { + $teamUid = $data['teamUid'] ?? ''; + $membershipUid = $data['membershipUid'] ?? ''; + $session = $data['session'] ?? ''; + + $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(3, $response['body']['total']); + + $ownerMembershipUid = $response['body']['memberships'][0]['$id']; + + /** + * Test deleting a membership that does not exists + */ + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/dne', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, + ]); + + $this->assertEquals(404, $response['headers']['status-code']); + + /** + * Test deleting another user's membership + */ + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $ownerMembershipUid, [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, + ]); + + $this->assertEquals(401, $response['headers']['status-code']); + $this->assertEquals('The current user is not authorized to perform the requested action.', $response['body']['message']); + + /** + * Test for when a user other than the owner tries to delete their membership + */ + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $membershipUid, [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(3, $response['body']['total']); + + /** + * Test for when the owner tries to delete their membership + */ + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid . '/memberships/' . $ownerMembershipUid, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertEquals('There must be at least one owner in the organization.', $response['body']['message']); + + $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid . '/memberships/' . $ownerMembershipUid, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + + return []; + } }