2021-03-13 07:00:41 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Services\GraphQL;
|
|
|
|
|
|
|
|
use Tests\E2E\Client;
|
2022-04-09 01:52:20 +12:00
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
2021-03-13 07:00:41 +13:00
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\SideServer;
|
|
|
|
|
2021-03-17 02:34:11 +13:00
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
class GraphQLServerTest extends Scope
|
2021-03-13 07:00:41 +13:00
|
|
|
{
|
2022-04-09 01:52:20 +12:00
|
|
|
use ProjectCustom;
|
2021-03-13 07:00:41 +13:00
|
|
|
use SideServer;
|
2021-03-17 03:34:43 +13:00
|
|
|
use GraphQLBase;
|
2021-03-13 07:00:41 +13:00
|
|
|
|
2021-03-13 09:17:29 +13:00
|
|
|
/**
|
2022-04-09 01:52:20 +12:00
|
|
|
* @depends testCreateCollection
|
2022-06-27 18:46:01 +12:00
|
|
|
* @depends testCreateStringAttribute
|
|
|
|
* @depends testCreateIntegerAttribute
|
|
|
|
* @depends testCreateBooleanAttribute
|
2022-04-09 01:52:20 +12:00
|
|
|
*/
|
|
|
|
public function testDocumentCreate(array $data)
|
|
|
|
{
|
2021-03-13 09:17:29 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
2021-03-17 07:16:33 +13:00
|
|
|
$key = '';
|
2022-04-09 01:52:20 +12:00
|
|
|
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
|
2021-03-13 09:17:29 +13:00
|
|
|
|
|
|
|
$variables = [
|
2022-06-27 18:46:01 +12:00
|
|
|
'documentId' => 'unique()',
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-13 09:17:29 +13:00
|
|
|
'data' => [
|
2022-04-09 01:52:20 +12:00
|
|
|
'name' => 'Robert',
|
2022-06-27 18:46:01 +12:00
|
|
|
'age' => 100,
|
2022-04-09 01:52:20 +12:00
|
|
|
'alive' => true,
|
2021-03-13 09:17:29 +13:00
|
|
|
],
|
2022-04-09 01:52:20 +12:00
|
|
|
'read' => ['role:all'],
|
|
|
|
'write' => ['role:all'],
|
2021-03-13 09:17:29 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
2021-03-13 09:17:29 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$errorMessage = 'User (role: guest) missing scope (documents.write)';
|
|
|
|
$this->assertEquals($errorMessage, $document['body']['errors'][0]['message']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertNull($document['body']['data']['databaseCreateDocument']);
|
2021-03-13 07:00:41 +13:00
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$key = $this->getNewKey(['documents.write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
]), $graphQLPayload);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-13 09:17:29 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseCreateDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseCreateDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
|
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
|
|
|
$this->assertEquals($variables['read'], $doc['_read']);
|
|
|
|
$this->assertEquals($variables['write'], $doc['_write']);
|
2021-03-13 09:17:29 +13:00
|
|
|
}
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testUserCreate()
|
|
|
|
{
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
|
|
|
* Try to create a user without the required scope
|
|
|
|
*/
|
2021-03-13 09:17:29 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
2021-03-17 07:16:33 +13:00
|
|
|
$query = $this->getQuery(self::$CREATE_USER);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-13 09:17:29 +13:00
|
|
|
$variables = [
|
2022-05-02 21:34:10 +12:00
|
|
|
'userId' => 'unique()',
|
2021-03-13 09:17:29 +13:00
|
|
|
'email' => 'users.service@example.com',
|
|
|
|
'password' => 'password',
|
|
|
|
'name' => 'Project User',
|
|
|
|
];
|
|
|
|
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
2021-03-13 09:17:29 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$errorMessage = 'User (role: guest) missing scope (users.write)';
|
|
|
|
$this->assertEquals($errorMessage, $user['body']['errors'][0]['message']);
|
2022-05-04 21:19:28 +12:00
|
|
|
$this->assertArrayNotHasKey('data', $user['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
2022-05-04 21:19:28 +12:00
|
|
|
* Create the user with the required scopes
|
2021-03-19 09:17:25 +13:00
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$key = $this->getNewKey(['users.write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
]), $graphQLPayload);
|
|
|
|
|
2021-03-13 09:17:29 +13:00
|
|
|
$this->assertIsArray($user['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($user['body']['data']['usersCreate']);
|
|
|
|
|
|
|
|
$data = $user['body']['data']['usersCreate'];
|
2022-05-04 21:19:28 +12:00
|
|
|
$this->assertArrayHasKey('_id', $data);
|
2021-03-13 09:17:29 +13:00
|
|
|
$this->assertArrayHasKey('registration', $data);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertEquals($variables['name'], $data['name']);
|
|
|
|
$this->assertEquals($variables['email'], $data['email']);
|
2022-05-04 21:19:28 +12:00
|
|
|
$this->assertEquals(true, $data['status']);
|
2021-03-13 09:17:29 +13:00
|
|
|
$this->assertEquals(false, $data['emailVerification']);
|
2021-03-19 09:14:18 +13:00
|
|
|
|
2022-05-04 21:19:28 +12:00
|
|
|
return ['userId' => $user['body']['data']['usersCreate']['_id']];
|
2021-03-19 09:14:18 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-09 01:52:20 +12:00
|
|
|
* @depends testUserCreate
|
|
|
|
*/
|
|
|
|
public function testUserDelete(array $data)
|
|
|
|
{
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
|
|
|
* Try to delete a user without the required scope
|
|
|
|
*/
|
2021-03-19 09:14:18 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$key = '';
|
|
|
|
$query = $this->getQuery(self::$DELETE_USER);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-19 09:14:18 +13:00
|
|
|
$variables = [
|
|
|
|
'userId' => $data['userId'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
2021-03-19 09:14:18 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$errorMessage = 'User (role: guest) missing scope (users.write)';
|
|
|
|
$this->assertEquals($errorMessage, $user['body']['errors'][0]['message']);
|
2022-05-04 21:45:36 +12:00
|
|
|
$this->assertArrayNotHasKey('data', $user['body']);
|
2021-03-19 09:14:18 +13:00
|
|
|
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
2022-05-04 21:45:36 +12:00
|
|
|
* Delete the user with the required scopes
|
2021-03-19 09:17:25 +13:00
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$key = $this->getNewKey(['users.write']);
|
2021-03-19 09:14:18 +13:00
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
]), $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertNull($user['body']['errors']);
|
|
|
|
$this->assertIsArray($user['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($user['body']['data']['usersDeleteUser']);
|
|
|
|
$this->assertEquals([], $user['body']['data']['usersDeleteUser']);
|
2021-03-19 09:14:18 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to fetch the user and check that its empty
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$GET_USER);
|
2022-04-09 01:52:20 +12:00
|
|
|
$key = $this->getNewKey(['users.read']);
|
2021-03-19 09:14:18 +13:00
|
|
|
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
2021-03-19 09:14:18 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$errorMessage = 'User not found';
|
|
|
|
$this->assertEquals($errorMessage, $user['body']['errors'][0]['message']);
|
2021-03-19 09:14:18 +13:00
|
|
|
$this->assertIsArray($user['body']['data']);
|
|
|
|
$this->assertNull($user['body']['data']['users_get']);
|
2021-03-17 02:34:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
public function testScopeBasedAuth()
|
|
|
|
{
|
|
|
|
$key = $this->getNewKey(['locale.read']);
|
2021-03-17 02:34:11 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
|
|
|
* Check that countries can be fetched
|
|
|
|
*/
|
2021-03-17 07:16:33 +13:00
|
|
|
$query = $this->getQuery(self::$LIST_COUNTRIES);
|
2021-03-17 02:34:11 +13:00
|
|
|
$variables = [];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
2021-03-17 02:34:11 +13:00
|
|
|
];
|
|
|
|
$countries = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($countries['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($countries['body']['data']['localeGetCountries']);
|
|
|
|
|
|
|
|
$data = $countries['body']['data']['localeGetCountries'];
|
2021-03-17 02:34:11 +13:00
|
|
|
$this->assertEquals(194, count($data['countries']));
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals(194, $data['total']);
|
2021-03-17 02:34:11 +13:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
|
2021-03-19 09:17:25 +13:00
|
|
|
/**
|
2022-05-02 21:34:10 +12:00
|
|
|
* Create a key without any scopes
|
2021-03-19 09:17:25 +13:00
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$key = $this->getNewKey([]);
|
2021-03-17 02:34:11 +13:00
|
|
|
$countries = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $key
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$errorMessage = 'app.' . $projectId . '@service.localhost (role: application) missing scope (locale.read)';
|
|
|
|
$this->assertEquals(401, $countries['headers']['status-code']);
|
2021-03-17 02:34:11 +13:00
|
|
|
$this->assertEquals($countries['body']['errors'][0]['message'], $errorMessage);
|
|
|
|
$this->assertIsArray($countries['body']['data']);
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertNull($countries['body']['data']['localeGetCountries']);
|
2021-03-13 09:17:29 +13:00
|
|
|
}
|
2021-03-13 07:00:41 +13:00
|
|
|
|
|
|
|
}
|