1
0
Fork 0
mirror of synced 2024-09-20 03:17:30 +12:00
appwrite/tests/e2e/Services/GraphQL/GraphQLServerTest.php

252 lines
9.3 KiB
PHP
Raw Normal View History

<?php
namespace Tests\E2E\Services\GraphQL;
use Tests\E2E\Client;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2021-03-17 02:34:11 +13:00
class GraphQLServerTest extends Scope
{
use SideServer;
2021-03-17 03:34:43 +13:00
use GraphQLBase;
2021-03-13 09:17:29 +13:00
/**
* @depends testCreateCollection
*/
public function testDocumentCreate(array $data) {
$projectId = $this->getProject()['$id'];
2021-03-17 07:16:33 +13:00
$key = '';
$query = $this->getQuery(self::$CREATE_DOCUMENT);
2021-03-13 09:17:29 +13:00
$variables = [
'collectionId' => $data['actorsId'],
'data' => [
'firstName' => 'Robert',
'lastName' => "Downey"
],
'read' => ['*'],
'write' => ['*'],
];
$graphQLPayload = [
"query" => $query,
"variables" => $variables
];
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
2021-03-17 07:16:33 +13:00
$errorMessage = "User (role: guest) missing scope (documents.write)";
$this->assertEquals($document['headers']['status-code'], 401);
$this->assertEquals($document['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($document['body']['data']);
$this->assertNull($document['body']['data']['database_createDocument']);
2021-03-17 07:16:33 +13:00
$key = $this->createKey('test', ['documents.write']);
$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);
2021-03-13 09:17:29 +13:00
$this->assertEquals($document['headers']['status-code'], 201);
$this->assertNull($document['body']['errors']);
$this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['database_createDocument']);
$doc = $document['body']['data']['database_createDocument'];
$this->assertArrayHasKey('$id', $doc);
$this->assertEquals($data['actorsId'], $doc['$collection']);
2021-03-17 07:16:33 +13:00
$this->assertEquals($variables['data']['firstName'], $doc['firstName']);
$this->assertEquals($variables['data']['lastName'], $doc['lastName']);
2021-03-13 09:17:29 +13:00
$permissions = $doc['$permissions'];
2021-03-17 07:16:33 +13:00
$this->assertEquals($variables['read'], $permissions['read']);
$this->assertEquals($variables['write'], $permissions['write']);
2021-03-13 09:17:29 +13:00
}
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
$key = '';
$query = $this->getQuery(self::$CREATE_USER);
2021-03-13 09:17:29 +13:00
$variables = [
'email' => 'users.service@example.com',
'password' => 'password',
'name' => 'Project User',
];
$graphQLPayload = [
"query" => $query,
"variables" => $variables
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
2021-03-17 07:16:33 +13:00
$errorMessage = "User (role: guest) missing scope (users.write)";
$this->assertEquals($user['headers']['status-code'], 401);
$this->assertEquals($user['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($user['body']['data']);
$this->assertNull($user['body']['data']['users_create']);
2021-03-19 09:17:25 +13:00
/**
* Create the user with the reqiured scopes
*/
2021-03-17 07:16:33 +13:00
$key = $this->createKey('test', ['users.write']);
$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->assertEquals($user['headers']['status-code'], 201);
$this->assertNull($user['body']['errors']);
$this->assertIsArray($user['body']['data']);
$this->assertIsArray($user['body']['data']['users_create']);
$data = $user['body']['data']['users_create'];
$this->assertArrayHasKey('id', $data);
$this->assertArrayHasKey('registration', $data);
2021-03-17 07:16:33 +13:00
$this->assertEquals($variables['name'], $data['name']);
$this->assertEquals($variables['email'], $data['email']);
2021-03-13 09:17:29 +13:00
$this->assertEquals(0, $data['status']);
$this->assertEquals(false, $data['emailVerification']);
$this->assertEquals([], $data['prefs']);
2021-03-19 09:14:18 +13:00
return ['userId' => $user['body']['data']['users_create']['id']];
}
/**
* @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);
$variables = [
'userId' => $data['userId'],
];
$graphQLPayload = [
"query" => $query,
"variables" => $variables
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
$errorMessage = "User (role: guest) missing scope (users.write)";
$this->assertEquals($user['headers']['status-code'], 401);
$this->assertEquals($user['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($user['body']['data']);
$this->assertNull($user['body']['data']['users_deleteUser']);
2021-03-19 09:17:25 +13:00
/**
* Delete the user with the reqiured scopes
*/
2021-03-19 09:14:18 +13:00
$key = $this->createKey('test', ['users.write']);
$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->assertEquals($user['headers']['status-code'], 200);
$this->assertNull($user['body']['errors']);
$this->assertIsArray($user['body']['data']);
$this->assertIsArray($user['body']['data']['users_deleteUser']);
$this->assertEquals([], $user['body']['data']['users_deleteUser']);
/**
* Try to fetch the user and check that its empty
*/
$query = $this->getQuery(self::$GET_USER);
$key = $this->createKey('test', ['users.read']);
$variables = [
'userId' => $data['userId'],
];
$graphQLPayload = [
"query" => $query,
"variables" => $variables
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
$errorMessage = "User not found";
$this->assertEquals($user['headers']['status-code'], 404);
$this->assertEquals($user['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($user['body']['data']);
$this->assertNull($user['body']['data']['users_get']);
2021-03-17 02:34:11 +13:00
}
public function testScopeBasedAuth() {
$key = $this->createKey("test", ['locale.read']);
$projectId = $this->getProject()['$id'];
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 = [
"query" => $query,
"variables" => $variables
];
$countries = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
$this->assertEquals($countries['headers']['status-code'], 200);
$this->assertNull($countries['body']['errors']);
$this->assertIsArray($countries['body']['data']);
$this->assertIsArray($countries['body']['data']['locale_getCountries']);
$data = $countries['body']['data']['locale_getCountries'];
$this->assertEquals(194, count($data['countries']));
$this->assertEquals(194, $data['sum']);
2021-03-17 07:16:33 +13:00
2021-03-19 09:17:25 +13:00
/**
* Create a key withouut any scopes
*/
2021-03-17 02:34:11 +13:00
$key = $this->createKey("test", []);
$countries = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload);
$errorMessage = "app.${projectId}@service.localhost (role: application) missing scope (locale.read)";
$this->assertEquals($countries['headers']['status-code'], 401);
$this->assertEquals($countries['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($countries['body']['data']);
$this->assertNull($countries['body']['data']['locale_getCountries']);
2021-03-13 09:17:29 +13:00
}
}