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

62 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace Tests\E2E\Services\GraphQL;
use Tests\E2E\Client;
2022-04-08 13:52:20 +00:00
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2021-03-16 13:34:11 +00:00
2022-04-08 13:52:20 +00:00
class GraphQLServerTest extends Scope
{
2022-04-08 13:52:20 +00:00
use ProjectCustom;
use SideServer;
2021-03-16 14:34:43 +00:00
use GraphQLBase;
2022-04-08 13:52:20 +00:00
public function testScopeBasedAuth()
{
$key = $this->getNewKey(['locale.read']);
2021-03-16 13:34:11 +00:00
$projectId = $this->getProject()['$id'];
2022-04-08 13:52:20 +00:00
2021-03-18 20:17:25 +00:00
/**
* Check that countries can be fetched
*/
2021-03-16 18:16:33 +00:00
$query = $this->getQuery(self::$LIST_COUNTRIES);
2021-03-16 13:34:11 +00:00
$variables = [];
$graphQLPayload = [
2022-04-08 13:52:20 +00:00
'query' => $query,
'variables' => $variables
2021-03-16 13:34:11 +00: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-08 13:52:20 +00:00
$this->assertIsArray($countries['body']['data']['localeGetCountries']);
$data = $countries['body']['data']['localeGetCountries'];
2021-03-16 13:34:11 +00:00
$this->assertEquals(194, count($data['countries']));
2022-04-08 13:52:20 +00:00
$this->assertEquals(194, $data['total']);
2021-03-16 13:34:11 +00:00
2021-03-16 18:16:33 +00:00
2021-03-18 20:17:25 +00:00
/**
2022-05-02 09:34:10 +00:00
* Create a key without any scopes
2021-03-18 20:17:25 +00:00
*/
2022-04-08 13:52:20 +00:00
$key = $this->getNewKey([]);
2021-03-16 13:34:11 +00: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 06:46:01 +00:00
$errorMessage = 'app.' . $projectId . '@service.localhost (role: application) missing scope (locale.read)';
$this->assertEquals(401, $countries['headers']['status-code']);
2021-03-16 13:34:11 +00:00
$this->assertEquals($countries['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($countries['body']['data']);
2022-06-27 06:46:01 +00:00
$this->assertNull($countries['body']['data']['localeGetCountries']);
2021-03-12 20:17:29 +00:00
}
}