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

394 lines
13 KiB
PHP
Raw Normal View History

2022-06-30 10:48:15 +00:00
<?php
namespace Tests\E2E\Services\GraphQL;
2026-01-15 03:14:53 +00:00
use PHPUnit\Framework\Attributes\Group;
2022-06-30 10:48:15 +00:00
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\SideServer;
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 TeamsServerTest 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 SideServer;
private static array $cachedTeam = [];
private static array $cachedMembership = [];
private static array $cachedTeamWithPrefs = [];
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', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $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;
}
protected function setupTeamWithPrefs(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedTeamWithPrefs[$key])) {
return self::$cachedTeamWithPrefs[$key];
}
$team = $this->setupTeam();
// Get the team first
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::GET_TEAM);
$graphQLPayload = [
'query' => $query,
'variables' => [
'teamId' => $team['_id'],
],
];
$teamResult = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($teamResult['body']['data']);
$this->assertArrayNotHasKey('errors', $teamResult['body']);
$fetchedTeam = $teamResult['body']['data']['teamsGet'];
// Update preferences
$query = $this->getQuery(self::UPDATE_TEAM_PREFERENCES);
$graphQLPayload = [
'query' => $query,
'variables' => [
'teamId' => $fetchedTeam['_id'],
'prefs' => [
'key' => 'value'
]
],
];
$prefs = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($prefs['body']['data']);
$this->assertArrayNotHasKey('errors', $prefs['body']);
$this->assertIsArray($prefs['body']['data']['teamsUpdatePrefs']);
$this->assertEquals('{"key":"value"}', $prefs['body']['data']['teamsUpdatePrefs']['data']);
2026-03-31 16:34:37 +00:00
self::$cachedTeamWithPrefs[$key] = $fetchedTeam;
return $fetchedTeam;
}
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);
2023-03-23 12:54:12 +00:00
return $team;
}
public function testUpdateTeamPrefs()
2023-03-23 12:54:12 +00:00
{
$team = $this->setupTeamWithPrefs();
chore: bump PHPStan to level 4 and fix all new errors Raises `phpstan.neon` level from 3 to 4 and fixes the 549 new errors that level 4 surfaces across 157 files. Fixes are root-cause — no `@phpstan-ignore`, no `@var` casts, no baseline entries, no widened types. A handful of latent bugs were fixed along the way: - `app/controllers/general.php`: path-traversal guard was negating `\substr(...)` before the strict comparison (`!\substr(...) === $base` was always `false === $base`). Rewritten as `\substr(...) !== $base`. - `src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php` and `.../TablesDB/Logs/XList.php`: were importing the raw Matomo `DeviceDetector` (whose `getDevice()` returns `?int`) but treating the result as an array with `deviceName/deviceBrand/deviceModel` keys. Swapped to `Appwrite\Detector\Detector`, matching the wrapper already used a few lines below for `$os`/`$client`. - `src/Appwrite/Platform/Modules/Functions/Workers/Builds.php`: a match key was checking `$resourceKey === 'functions'` when `$resourceKey` is `'functionId'|'siteId'` — always false. Switched to the intended `$resource->getCollection() === 'functions'` check. - `src/Appwrite/OpenSSL/OpenSSL.php`: `encrypt()` return type tightened to `string|false` to match `openssl_encrypt`; this lets callers' `=== false` error handling remain meaningful. - `app/controllers/api/messaging.php`: removed a dead `array_key_exists('from', [])` branch in the Msg91 provider (empty array literal; branch was unreachable). Large cleanup categories across the 549 fixes: - Removed redundant `?? default` on array offsets and expressions that PHPStan now knows are non-nullable. - Removed unreachable statements (mostly `return;` after `throw` or `markTestSkipped()`). - Removed redundant `is_array`/`is_string`/`is_bool`/`instanceof` checks on already-narrowed types. - Added `default =>` arms (or throwing arms) to non-exhaustive matches on `string`/`mixed` input. - Removed dead `$document === false` branches where method return types were tightened to non-nullable `Document`. - Removed unused properties (`$version` on Etsy/Zoom OAuth2, `$paths` on Installer State, `$source` on MigrationsWorker, `$account2` on two GraphQL auth tests), unused traits (`ApiVectorsDB`, `DatabaseFixture`), and an unused `cleanupStaleExecutions` task method. - Replaced `assertTrue(true)` and redundant `assertIsArray`/`assertIsString`/ `assertNotNull` assertions with `addToAssertionCount(1)` or `assertNotEmpty` where the runtime type was already known.
2026-04-19 12:01:20 +00:00
$this->assertNotEmpty($team);
2023-03-23 12:54:12 +00:00
}
public function testGetTeamPreferences()
2023-03-23 12:54:12 +00:00
{
$team = $this->setupTeamWithPrefs();
2023-03-23 12:54:12 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_TEAM_PREFERENCES);
2023-03-23 12:54:12 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'teamId' => $team['_id'],
]
];
$prefs = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($prefs['body']['data']);
$this->assertArrayNotHasKey('errors', $prefs['body']);
$this->assertIsArray($prefs['body']['data']['teamsGetPrefs']);
2022-06-30 12:00:00 +00:00
}
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 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::GET_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
],
];
$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 12:00:00 +00:00
$this->assertArrayNotHasKey('errors', $membership['body']);
}
public function testUpdateTeam()
2022-06-30 10:48:15 +00:00
{
$team = $this->setupTeam();
2022-06-30 10:48:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_TEAM_NAME);
2022-06-30 10:48:15 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
2022-06-30 10:48:15 +00:00
'name' => 'New Name',
],
];
$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']);
2023-03-23 12:54:12 +00:00
$team = $team['body']['data']['teamsUpdateName'];
2022-06-30 10:48:15 +00:00
$this->assertEquals('New Name', $team['name']);
}
public function testUpdateTeamMembershipRoles()
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::UPDATE_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
'roles' => ['developer', 'admin'],
],
];
$membership = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($membership['body']['data']);
$this->assertArrayNotHasKey('errors', $membership['body']);
2023-08-17 23:08:30 +00:00
$membership = $membership['body']['data']['teamsUpdateMembership'];
2022-06-30 12:00:00 +00:00
$this->assertEquals(['developer', 'admin'], $membership['roles']);
}
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 12:00:00 +00:00
}
2026-01-15 03:14:53 +00:00
#[Group('cl-ignore')]
2022-06-30 10:48:15 +00:00
public function testDeleteTeam()
{
// Create a fresh team for deletion test
2022-06-30 10:48:15 +00:00
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::CREATE_TEAM);
$graphQLPayload = [
'query' => $query,
'variables' => [
'teamId' => ID::unique(),
'name' => 'Team To Delete',
'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);
$team = $team['body']['data']['teamsCreate'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_TEAM);
2022-06-30 10:48:15 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'teamId' => $team['_id'],
2022-06-30 10:48:15 +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']);
2022-06-30 10:48:15 +00:00
}
2022-07-11 21:52:00 +00:00
}