appwrite/tests/e2e/Services/GraphQL/TeamsClientTest.php

217 lines
6.9 KiB
PHP
Raw Normal View History

2022-06-30 10:48:15 +00:00
<?php
namespace Tests\E2E\Services\GraphQL;
use Tests\E2E\Client;
2022-06-30 12:00:00 +00:00
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
2022-06-30 10:48:15 +00:00
use Tests\E2E\Scopes\SideClient;
2023-01-16 09:25:40 +00:00
use Utopia\Database\Helpers\ID;
2022-06-30 10:48:15 +00:00
2022-09-22 08:29:42 +00:00
class TeamsClientTest extends Scope
2022-06-30 10:48:15 +00:00
{
2022-06-30 12:00:00 +00:00
use ProjectCustom;
2022-09-22 08:29:42 +00:00
use Base;
2022-06-30 10:48:15 +00:00
use SideClient;
private static array $cachedTeam = [];
private static array $cachedMembership = [];
protected function setupTeam(): array
2022-06-30 12:00:00 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedTeam[$key])) {
return self::$cachedTeam[$key];
}
2022-06-30 12:00:00 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TEAM);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-09-22 01:53:41 +00:00
'teamId' => ID::unique(),
2022-06-30 12:00:00 +00:00
'name' => 'Team Name',
'roles' => ['admin', 'developer', 'guest'],
],
];
$team = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($team['body']['data']);
$this->assertArrayNotHasKey('errors', $team['body']);
$team = $team['body']['data']['teamsCreate'];
$this->assertEquals('Team Name', $team['name']);
2026-03-31 16:34:37 +00:00
self::$cachedTeam[$key] = $team;
2022-06-30 12:00:00 +00:00
return $team;
}
protected function setupMembership(): array
2022-06-30 12:00:00 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedMembership[$key])) {
return self::$cachedMembership[$key];
}
$team = $this->setupTeam();
2022-06-30 12:00:00 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TEAM_MEMBERSHIP);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
2022-06-30 12:00:00 +00:00
'email' => 'user@appwrite.io',
'roles' => ['developer'],
'url' => 'http://localhost/membership',
],
];
$membership = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
], $graphQLPayload);
$this->assertIsArray($membership['body']['data']);
$this->assertArrayNotHasKey('errors', $membership['body']);
$membership = $membership['body']['data']['teamsCreateMembership'];
2022-12-08 03:08:57 +00:00
$this->assertEquals($team['_id'], $membership['teamId']);
2022-06-30 12:00:00 +00:00
$this->assertEquals(['developer'], $membership['roles']);
2026-03-31 16:34:37 +00:00
self::$cachedMembership[$key] = $membership;
2022-06-30 12:00:00 +00:00
return $membership;
}
public function testCreateTeam(): void
{
$team = $this->setupTeam();
$this->assertEquals('Team Name', $team['name']);
}
public function testCreateTeamMembership(): void
{
$membership = $this->setupMembership();
$this->assertEquals(['developer'], $membership['roles']);
}
2022-06-30 12:00:00 +00:00
public function testGetTeams()
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_TEAMS);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
];
$teams = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($teams['body']['data']);
$this->assertArrayNotHasKey('errors', $teams['body']);
}
public function testGetTeam()
2022-06-30 12:00:00 +00:00
{
$team = $this->setupTeam();
2022-06-30 12:00:00 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_TEAM);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
2022-06-30 12:00:00 +00:00
],
];
$team = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($team['body']['data']);
$this->assertArrayNotHasKey('errors', $team['body']);
$team = $team['body']['data']['teamsGet'];
$this->assertIsArray($team);
}
public function testGetTeamMemberships()
2022-06-30 12:00:00 +00:00
{
$team = $this->setupTeam();
2022-06-30 12:00:00 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_TEAM_MEMBERSHIPS);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
2022-06-30 12:00:00 +00:00
],
];
$memberships = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($memberships['body']['data']);
$this->assertArrayNotHasKey('errors', $memberships['body']);
2022-09-21 07:03:28 +00:00
$this->assertIsArray($memberships['body']['data']['teamsListMemberships']);
2022-06-30 12:00:00 +00:00
}
public function testGetTeamMembership()
2022-06-30 10:48:15 +00:00
{
$team = $this->setupTeam();
$membership = $this->setupMembership();
2022-06-30 10:48:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_TEAM_MEMBERSHIP);
2022-06-30 10:48:15 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
'membershipId' => $membership['_id'],
2022-06-30 10:48:15 +00:00
],
];
$membership = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
2022-09-22 01:55:31 +00:00
$this->assertIsArray($membership['body']['data']['teamsGetMembership']);
2022-06-30 10:48:15 +00:00
$this->assertArrayNotHasKey('errors', $membership['body']);
2022-06-30 12:00:00 +00:00
}
public function testDeleteTeamMembership()
2022-06-30 12:00:00 +00:00
{
$team = $this->setupTeam();
$membership = $this->setupMembership();
2022-06-30 12:00:00 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_TEAM_MEMBERSHIP);
2022-06-30 12:00:00 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
'membershipId' => $membership['_id'],
2022-06-30 12:00:00 +00:00
],
];
$team = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
2022-07-18 09:38:13 +00:00
$this->assertIsNotArray($team['body']);
$this->assertEquals(204, $team['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedMembership[$key] = [];
2022-06-30 10:48:15 +00:00
}
2022-07-06 03:51:37 +00:00
}