From 9b951e86e9672f8963031672c8bb2b14802eab06 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Wed, 26 Jun 2024 13:06:10 -0400 Subject: [PATCH] test: Testing team delete removes membership --- .../Services/Teams/TeamsCustomServerTest.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/tests/e2e/Services/Teams/TeamsCustomServerTest.php b/tests/e2e/Services/Teams/TeamsCustomServerTest.php index 325b5892fc..5168f09810 100644 --- a/tests/e2e/Services/Teams/TeamsCustomServerTest.php +++ b/tests/e2e/Services/Teams/TeamsCustomServerTest.php @@ -2,9 +2,12 @@ namespace Tests\E2E\Services\Teams; +use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; +use Utopia\Database\Helpers\ID; +use Utopia\Database\Validator\Datetime as DatetimeValidator; class TeamsCustomServerTest extends Scope { @@ -12,4 +15,97 @@ class TeamsCustomServerTest extends Scope use TeamsBaseServer; use ProjectCustom; use SideServer; + + public function testMembershipDeletedWhenTeamDeleted(): array + { + /* 1. Create Team */ + $response = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'teamId' => ID::unique(), + 'name' => 'Demo' + ]); + + $teamUid = $response['body']['$id']; + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals('Demo', $response['body']['name']); + $this->assertGreaterThan(-1, $response['body']['total']); + $this->assertIsInt($response['body']['total']); + $this->assertArrayHasKey('prefs', $response['body']); + $dateValidator = new DatetimeValidator(); + $this->assertEquals(true, $dateValidator->isValid($response['body']['$createdAt'])); + + /* 2. Create user. */ + $email = uniqid() . 'friend@localhost.test'; + $name = 'Friend User'; + $password = 'password'; + + // Create a user account before we create a invite so we can check if the user has permissions when it shouldn't + $user = $this->client->call(Client::METHOD_POST, '/account', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console' + ], [ + 'userId' => 'unique()', + 'email' => $email, + 'password' => $password, + 'name' => $name, + ], false); + + $this->assertEquals(201, $user['headers']['status-code']); + $userId = json_decode($user['body'],true )['$id']; + + /* 3. Add membership to user. */ + $response = $this->client->call(Client::METHOD_POST, '/teams/' . $teamUid . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'email' => $email, + 'name' => $name, + 'roles' => ['admin', 'editor'], + 'url' => 'http://localhost:5000/join-us#title' + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + /* 4. Ensure user is a member. */ + $response = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], $this->getHeaders())); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($teamUid, $response['body']['memberships'][0]['teamId']); + + /* 5. Delete Team */ + $response = $this->client->call(Client::METHOD_DELETE, '/teams/' . $teamUid, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(204, $response['headers']['status-code']); + $this->assertEmpty($response['body']); + + /* 6. Ensure Team got deleted */ + $response = $this->client->call(Client::METHOD_GET, '/teams/' . $teamUid, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(404, $response['headers']['status-code']); + + /* 7. Ensure memberships got removed from the user. */ + $response = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEmpty($response['body']['memberships']); + + return []; + } }