1
0
Fork 0
mirror of synced 2024-07-15 19:36:08 +12:00

Test fixes

This commit is contained in:
Jake Barnby 2022-06-27 18:46:01 +12:00
parent d019937e4f
commit b09e5aac8a
4 changed files with 145 additions and 153 deletions

View file

@ -323,10 +323,12 @@ class Builder
if (str_starts_with($route->getPath(), '/v1/mock/')) { if (str_starts_with($route->getPath(), '/v1/mock/')) {
continue; continue;
} }
$namespace = $route->getLabel('sdk.namespace', ''); $namespace = $route->getLabel('sdk.namespace', '');
$methodName = $namespace . \ucfirst($route->getLabel('sdk.method', '')); $methodName = $namespace . \ucfirst($route->getLabel('sdk.method', ''));
$responseModelNames = $route->getLabel('sdk.response.model', "none"); $responseModelNames = $route->getLabel('sdk.response.model', "none");
// TODO: Handle "none" responses
if ($responseModelNames === "none") { if ($responseModelNames === "none") {
continue; continue;
} }
@ -363,10 +365,18 @@ class Builder
'resolve' => $resolve 'resolve' => $resolve
]; ];
if ($method == 'GET') { switch ($method) {
$queryFields[$methodName] = $field; case 'GET':
} elseif ($method == 'POST' || $method == 'PUT' || $method == 'PATCH' || $method == 'DELETE') { $queryFields[$methodName] = $field;
$mutationFields[$methodName] = $field; break;
case 'POST':
case 'PUT':
case 'PATCH':
case 'DELETE':
$mutationFields[$methodName] = $field;
break;
default:
throw new \Exception("Unsupported method: $method");
} }
} }
} }
@ -508,7 +518,7 @@ class Builder
$wg->wait(); $wg->wait();
$time_elapsed_secs = (microtime(true) - $start) * 1000; $time_elapsed_secs = (microtime(true) - $start) * 1000;
Console::info('[INFO] Built GraphQL Project Collection Schema (approx. ' . $count . ' attributes) in ' . $time_elapsed_secs . 'ms'); Console::info('[INFO] Built GraphQL Project Collection Schema (' . $count . ' attributes) in ' . $time_elapsed_secs . 'ms');
return [ return [
'query' => $queryFields, 'query' => $queryFields,

View file

@ -16,7 +16,7 @@ trait GraphQLBase
static string $CREATE_STRING_ATTRIBUTE = 'create_string_attribute'; static string $CREATE_STRING_ATTRIBUTE = 'create_string_attribute';
static string $CREATE_INTEGER_ATTRIBUTE = 'create_integer_attribute'; static string $CREATE_INTEGER_ATTRIBUTE = 'create_integer_attribute';
static string $CREATE_FLOAT_ATTRIBUTE = 'create_float_attribute'; static string $CREATE_FLOAT_ATTRIBUTE = 'create_float_attribute';
static string $CREATE_BOOLEAN_ATTRIBUTE = 'create_float_attribute'; static string $CREATE_BOOLEAN_ATTRIBUTE = 'create_boolean_attribute';
static string $CREATE_URL_ATTRIBUTE = 'create_string_attribute'; static string $CREATE_URL_ATTRIBUTE = 'create_string_attribute';
static string $CREATE_EMAIL_ATTRIBUTE = 'create_string_attribute'; static string $CREATE_EMAIL_ATTRIBUTE = 'create_string_attribute';
static string $CREATE_IP_ATTRIBUTE = 'create_string_attribute'; static string $CREATE_IP_ATTRIBUTE = 'create_string_attribute';
@ -122,10 +122,10 @@ trait GraphQLBase
return 'mutation updateCollection($collectionId: String!, $name: String!, $permission: String!, $read: [String!]!, $write: [String!]!, $enabled: Boolean){ return 'mutation updateCollection($collectionId: String!, $name: String!, $permission: String!, $read: [String!]!, $write: [String!]!, $enabled: Boolean){
databaseUpdateCollection (collectionId: $collectionId, name: $name, permission: $permission, read: $read, write: $write, enabled: $enabled) { databaseUpdateCollection (collectionId: $collectionId, name: $name, permission: $permission, read: $read, write: $write, enabled: $enabled) {
_id _id
_read
_write
name name
permission permission
read
write
} }
}'; }';
case self::$DELETE_COLLECTION: case self::$DELETE_COLLECTION:
@ -135,7 +135,6 @@ trait GraphQLBase
case self::$CREATE_STRING_ATTRIBUTE: case self::$CREATE_STRING_ATTRIBUTE:
return 'mutation createStringAttribute($collectionId: String!, $key: String!, $size: Int!, $required: Boolean!, $default: String, $array: Boolean){ return 'mutation createStringAttribute($collectionId: String!, $key: String!, $size: Int!, $required: Boolean!, $default: String, $array: Boolean){
databaseCreateStringAttribute (collectionId: $collectionId, key: $key, size: $size, required: $required, default: $default, array: $array) { databaseCreateStringAttribute (collectionId: $collectionId, key: $key, size: $size, required: $required, default: $default, array: $array) {
_id
key key
required required
default default
@ -145,7 +144,6 @@ trait GraphQLBase
case self::$CREATE_INTEGER_ATTRIBUTE: case self::$CREATE_INTEGER_ATTRIBUTE:
return 'mutation createIntegerAttribute($collectionId: String!, $key: String!, $required: Boolean!, $min: Int, $max: Int, $default: Int, $array: Boolean){ return 'mutation createIntegerAttribute($collectionId: String!, $key: String!, $required: Boolean!, $min: Int, $max: Int, $default: Int, $array: Boolean){
databaseCreateIntegerAttribute (collectionId: $collectionId, key: $key, min: $min, max: $max, required: $required, default: $default, array: $array) { databaseCreateIntegerAttribute (collectionId: $collectionId, key: $key, min: $min, max: $max, required: $required, default: $default, array: $array) {
_id
key key
required required
min min
@ -157,7 +155,6 @@ trait GraphQLBase
case self::$CREATE_FLOAT_ATTRIBUTE: case self::$CREATE_FLOAT_ATTRIBUTE:
return 'mutation createFloatAttribute($collectionId: String!, $key: String!, $required: Boolean!, $min: Float, $max: Float, $default: Float, $array: Boolean){ return 'mutation createFloatAttribute($collectionId: String!, $key: String!, $required: Boolean!, $min: Float, $max: Float, $default: Float, $array: Boolean){
databaseCreateFloatAttribute (collectionId: $collectionId, key: $key, min: $min, max: $max, required: $required, default: $default, array: $array) { databaseCreateFloatAttribute (collectionId: $collectionId, key: $key, min: $min, max: $max, required: $required, default: $default, array: $array) {
_id
key key
required required
min min
@ -169,7 +166,6 @@ trait GraphQLBase
case self::$CREATE_BOOLEAN_ATTRIBUTE: case self::$CREATE_BOOLEAN_ATTRIBUTE:
return 'mutation createBooleanAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: Boolean, $array: Boolean){ return 'mutation createBooleanAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: Boolean, $array: Boolean){
databaseCreateBooleanAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) { databaseCreateBooleanAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) {
_id
key key
required required
default default
@ -179,7 +175,6 @@ trait GraphQLBase
case self::$CREATE_URL_ATTRIBUTE: case self::$CREATE_URL_ATTRIBUTE:
return 'mutation createUrlAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){ return 'mutation createUrlAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){
databaseCreateUrlAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) { databaseCreateUrlAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) {
_id
key key
required required
default default
@ -189,7 +184,6 @@ trait GraphQLBase
case self::$CREATE_EMAIL_ATTRIBUTE: case self::$CREATE_EMAIL_ATTRIBUTE:
return 'mutation createEmailAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){ return 'mutation createEmailAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){
databaseCreateEmailAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) { databaseCreateEmailAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) {
_id
key key
required required
default default
@ -199,7 +193,6 @@ trait GraphQLBase
case self::$CREATE_IP_ATTRIBUTE: case self::$CREATE_IP_ATTRIBUTE:
return 'mutation createIpAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){ return 'mutation createIpAttribute($collectionId: String!, $key: String!, $required: Boolean!, $default: String, $array: Boolean){
databaseCreateIpAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) { databaseCreateIpAttribute (collectionId: $collectionId, key: $key, required: $required, default: $default, array: $array) {
_id
key key
required required
default default
@ -209,7 +202,6 @@ trait GraphQLBase
case self::$CREATE_ENUM_ATTRIBUTE: case self::$CREATE_ENUM_ATTRIBUTE:
return 'mutation createEnumAttribute($collectionId: String!, $key: String!, $elements: [String!]!, $required: Boolean!, $default: String, $array: Boolean){ return 'mutation createEnumAttribute($collectionId: String!, $key: String!, $elements: [String!]!, $required: Boolean!, $default: String, $array: Boolean){
databaseCreateEnumAttribute (collectionId: $collectionId, key: $key, elements: $elements, required: $required, default: $default, array: $array) { databaseCreateEnumAttribute (collectionId: $collectionId, key: $key, elements: $elements, required: $required, default: $default, array: $array) {
_id
key key
elements elements
required required
@ -221,13 +213,9 @@ trait GraphQLBase
return 'query getDocument($collectionId: String!, $documentId: String!){ return 'query getDocument($collectionId: String!, $documentId: String!){
databaseGetDocument (collectionId: $collectionId, documentId: $documentId) { databaseGetDocument (collectionId: $collectionId, documentId: $documentId) {
_id _id
collectionId _collection
data { _read
name _write
age
alive
salary
}
} }
}'; }';
case self::$LIST_DOCUMENTS : case self::$LIST_DOCUMENTS :
@ -244,15 +232,9 @@ trait GraphQLBase
return 'mutation createDocument($collectionId: String!, $documentId: String!, $data: Json!, $read: [String!]!, $write: [String!]!){ return 'mutation createDocument($collectionId: String!, $documentId: String!, $data: Json!, $read: [String!]!, $write: [String!]!){
databaseCreateDocument (collectionId: $collectionId, documentId: $documentId, data: $data, read: $read, write: $write) { databaseCreateDocument (collectionId: $collectionId, documentId: $documentId, data: $data, read: $read, write: $write) {
_id _id
documentId _collection
data { _read
name _write
age
alive
salary
}
read
write
} }
}'; }';
case self::$CREATE_DOCUMENT_GQL_HOOKS: case self::$CREATE_DOCUMENT_GQL_HOOKS:
@ -266,15 +248,9 @@ trait GraphQLBase
}'; }';
case self::$UPDATE_DOCUMENT: case self::$UPDATE_DOCUMENT:
return 'mutation updateDocument($collectionId: String!, $documentId: String!, $data: Json!, $read: [String!]!, $write: [String!]!){ return 'mutation updateDocument($collectionId: String!, $documentId: String!, $data: Json!, $read: [String!]!, $write: [String!]!){
databaseUpdateDocument (collectionId: $collectionId, documentId: $documentId,data: $data, read: $read, write: $write) { databaseUpdateDocument (collectionId: $collectionId, documentId: $documentId, data: $data, read: $read, write: $write) {
_id _id
collectionId _collection
data {
name
age
alive
salary
}
} }
}'; }';
case self::$DELETE_DOCUMENT: case self::$DELETE_DOCUMENT:
@ -517,7 +493,7 @@ trait GraphQLBase
} }
}'; }';
case self::$CREATE_TEAM: case self::$CREATE_TEAM:
return 'mutation createTeam($teamId: String!, $name: String!, $roles: [Json]){ return 'mutation createTeam($teamId: String!, $name: String!, $roles: [String]){
teamsCreate(teamId: $teamId, name : $name, roles: $roles) { teamsCreate(teamId: $teamId, name : $name, roles: $roles) {
_id _id
name name
@ -721,7 +697,6 @@ trait GraphQLBase
public function testCreateCollection(): array public function testCreateCollection(): array
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = '';
$query = $this->getQuery(self::$CREATE_COLLECTION); $query = $this->getQuery(self::$CREATE_COLLECTION);
$collectionAttrs = [ $collectionAttrs = [
@ -729,7 +704,7 @@ trait GraphQLBase
'name' => 'Actors', 'name' => 'Actors',
'permission' => 'collection', 'permission' => 'collection',
'read' => ['role:all'], 'read' => ['role:all'],
'write' => ['role:member', 'role:admin'], 'write' => ['role:member'],
]; ];
$gqlPayload = [ $gqlPayload = [
@ -741,7 +716,6 @@ trait GraphQLBase
'origin' => 'http://localhost', 'origin' => 'http://localhost',
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $projectId, 'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$errorMessage = 'User (role: guest) missing scope (collections.write)'; $errorMessage = 'User (role: guest) missing scope (collections.write)';
@ -750,30 +724,26 @@ trait GraphQLBase
$this->assertIsArray($actors['body']['data']); $this->assertIsArray($actors['body']['data']);
$this->assertNull($actors['body']['data']['databaseCreateCollection']); $this->assertNull($actors['body']['data']['databaseCreateCollection']);
$key = $this->createKey('test', ['collections.write']);
$actors = $this->client->call(Client::METHOD_POST, '/graphql', [ $actors = $this->client->call(Client::METHOD_POST, '/graphql', [
'origin' => 'http://localhost', 'origin' => 'http://localhost',
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $projectId, 'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key 'x-appwrite-key' => $this->getProject()['apiKey']
], $gqlPayload); ], $gqlPayload);
$this->assertEquals(201, $actors['headers']['status-code']); $this->assertEquals(201, $actors['headers']['status-code']);
$this->assertNull($actors['body']['errors']); $this->assertArrayNotHasKey('errors', $actors['body']);
$this->assertIsArray($actors['body']['data']); $this->assertIsArray($actors['body']['data']);
$data = $actors['body']['data']['databaseCreateCollection']; $data = $actors['body']['data']['databaseCreateCollection'];
$this->assertEquals('Actors', $data['name']); $this->assertEquals('Actors', $data['name']);
$this->assertArrayHasKey('id', $data); $this->assertArrayHasKey('_id', $data);
$this->assertArrayHasKey('permissions', $data); $this->assertArrayHasKey('permission', $data);
$this->assertContains('role:all', $data['read']); $this->assertContains('role:all', $data['_read']);
$this->assertContains('role:member', $data['write']); $this->assertContains('role:member', $data['_write']);
$this->assertContains('role:admin', $data['write']);
return [ return [
'collectionId' => $data['id'], 'collectionId' => $data['_id'],
'key' => $key
]; ];
} }
@ -784,10 +754,10 @@ trait GraphQLBase
public function testCreateStringAttribute(array $data): void public function testCreateStringAttribute(array $data): void
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = $data['key']; $key = $this->getProject()['apiKey'];
$query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE); $query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE);
$attributeAttrs = [ $stringAttrs = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'key' => 'name', 'key' => 'name',
'size' => 256, 'size' => 256,
@ -796,7 +766,7 @@ trait GraphQLBase
$gqlPayload = [ $gqlPayload = [
'query' => $query, 'query' => $query,
'variables' => $attributeAttrs 'variables' => $stringAttrs
]; ];
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
@ -806,9 +776,12 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($attribute['body']['errors']); $this->assertArrayNotHasKey('errors', $attribute['body']);
$this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']);
$this->assertIsArray($attribute['body']['data']['databaseCreateStringAttribute']); $this->assertIsArray($attribute['body']['data']['databaseCreateStringAttribute']);
// Wait for attribute to be ready
sleep(2);
} }
/** /**
@ -818,20 +791,20 @@ trait GraphQLBase
public function testCreateIntegerAttribute(array $data): void public function testCreateIntegerAttribute(array $data): void
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = $data['key']; $key = $this->getProject()['apiKey'];
$query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE); $query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE);
$attributeAttrs = [ $intAttrs = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'key' => 'age', 'key' => 'age',
'min' => 18, 'min' => 18,
'max' => 99, 'max' => 150,
'required' => true, 'required' => true,
]; ];
$gqlPayload = [ $gqlPayload = [
'query' => $query, 'query' => $query,
'variables' => $attributeAttrs 'variables' => $intAttrs
]; ];
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
@ -841,9 +814,12 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($attribute['body']['errors']); $this->assertArrayNotHasKey('errors', $attribute['body']);
$this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']);
$this->assertIsArray($attribute['body']['data']['databaseCreateIntegerAttribute']); $this->assertIsArray($attribute['body']['data']['databaseCreateIntegerAttribute']);
// Wait for attribute to be ready
sleep(2);
} }
/** /**
@ -853,10 +829,10 @@ trait GraphQLBase
public function testCreateBooleanAttribute(array $data): void public function testCreateBooleanAttribute(array $data): void
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = $data['key']; $key = $this->getProject()['apiKey'];
$query = $this->getQuery(self::$CREATE_BOOLEAN_ATTRIBUTE); $query = $this->getQuery(self::$CREATE_BOOLEAN_ATTRIBUTE);
$attributeAttrs = [ $booleanAttrs = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'key' => 'alive', 'key' => 'alive',
'required' => true, 'required' => true,
@ -864,7 +840,7 @@ trait GraphQLBase
$gqlPayload = [ $gqlPayload = [
'query' => $query, 'query' => $query,
'variables' => $attributeAttrs 'variables' => $booleanAttrs
]; ];
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
@ -874,9 +850,12 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($attribute['body']['errors']); $this->assertArrayNotHasKey('errors', $attribute['body']);
$this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']);
$this->assertIsArray($attribute['body']['data']['databaseCreateBooleanAttribute']); $this->assertIsArray($attribute['body']['data']['databaseCreateBooleanAttribute']);
// Wait for attribute to be ready
sleep(2);
} }
/** /**
@ -886,10 +865,10 @@ trait GraphQLBase
public function testCreateFloatAttribute(array $data): void public function testCreateFloatAttribute(array $data): void
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = $data['key']; $key = $this->getProject()['apiKey'];
$query = $this->getQuery(self::$CREATE_FLOAT_ATTRIBUTE); $query = $this->getQuery(self::$CREATE_FLOAT_ATTRIBUTE);
$attributeAttrs = [ $floatAttrs = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'key' => 'salary', 'key' => 'salary',
'min' => 1000.0, 'min' => 1000.0,
@ -900,7 +879,7 @@ trait GraphQLBase
$gqlPayload = [ $gqlPayload = [
'query' => $query, 'query' => $query,
'variables' => $attributeAttrs 'variables' => $floatAttrs
]; ];
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [ $attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
@ -910,9 +889,12 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($attribute['body']['errors']); $this->assertArrayNotHasKey('errors', $attribute['body']);
$this->assertIsArray($attribute['body']['data']); $this->assertIsArray($attribute['body']['data']);
$this->assertIsArray($attribute['body']['data']['databaseCreateFloatAttribute']); $this->assertIsArray($attribute['body']['data']['databaseCreateFloatAttribute']);
// Wait for attribute to be ready
sleep(2);
} }
/** /**
@ -926,7 +908,7 @@ trait GraphQLBase
public function testCreateDocumentREST(array $data): void public function testCreateDocumentREST(array $data): void
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = $data['key']; $key = $this->getProject()['apiKey'];
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST); $query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
$documentAttrs = [ $documentAttrs = [
@ -951,7 +933,7 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseCreateDocument']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']);
} }
@ -989,7 +971,7 @@ trait GraphQLBase
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $gqlPayload); ], $gqlPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['actorCreate']); $this->assertIsArray($document['body']['data']['actorCreate']);
} }

View file

@ -47,6 +47,7 @@ class GraphQLClientTest extends Scope
'password' => 'password', 'password' => 'password',
] ]
]; ];
$session1 = $this->client->call(Client::METHOD_POST, '/graphql', [ $session1 = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $projectId, 'x-appwrite-project' => $projectId,
@ -115,9 +116,12 @@ class GraphQLClientTest extends Scope
/** /**
* @depends testCreateCollection * @depends testCreateCollection
* @depends testCreateStringAttribute
* @depends testCreateIntegerAttribute
* @depends testCreateBooleanAttribute
* @depends testCreateAccounts * @depends testCreateAccounts
*/ */
public function testWildCardPermissions(array $data, array $accounts) public function testWildCardPermissions(array $data, $str, $int, $bool, array $accounts)
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
@ -137,28 +141,27 @@ class GraphQLClientTest extends Scope
'read' => ['role:all'], 'read' => ['role:all'],
'write' => ['role:all'], 'write' => ['role:all'],
]; ];
$graphQLPayload = [ $graphQLPayload = [
'query' => $query, 'query' => $query,
'variables' => $docVariables 'variables' => $docVariables
]; ];
$document = $this->client->call(Client::METHOD_POST, '/graphql', [ $document = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $projectId, 'x-appwrite-project' => $projectId,
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseCreateDocument']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']);
$doc = $document['body']['data']['databaseCreateDocument']; $doc = $document['body']['data']['databaseCreateDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals('Robert', $doc['name']); $this->assertEquals($docVariables['read'], $doc['_read']);
$this->assertEquals(100, $doc['age']); $this->assertEquals($docVariables['write'], $doc['_write']);
$this->assertEquals($docVariables['read'], $doc['read']);
$this->assertEquals($docVariables['write'], $doc['write']);
/* /*
* Account 1 tries to access it * Account 1 tries to access it
@ -166,7 +169,7 @@ class GraphQLClientTest extends Scope
$query = $this->getQuery(self::$GET_DOCUMENT); $query = $this->getQuery(self::$GET_DOCUMENT);
$getDocumentVariables = [ $getDocumentVariables = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'documentId' => $doc['$id'] 'documentId' => $doc['_id']
]; ];
$graphQLPayload = [ $graphQLPayload = [
'query' => $query, 'query' => $query,
@ -178,16 +181,16 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseGetDocument']); $this->assertIsArray($document['body']['data']['databaseGetDocument']);
$doc = $document['body']['data']['databaseGetDocument']; $doc = $document['body']['data']['databaseGetDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals('Robert', $doc['name']); $this->assertEquals('Robert', $doc['name']);
$this->assertEquals(100, $doc['age']); $this->assertEquals(100, $doc['age']);
$this->assertEquals($docVariables['read'], $doc['read']); $this->assertEquals($docVariables['read'], $doc['_read']);
$this->assertEquals($docVariables['write'], $doc['write']); $this->assertEquals($docVariables['write'], $doc['write']);
/* /*
@ -199,25 +202,28 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseGetDocument']); $this->assertIsArray($document['body']['data']['databaseGetDocument']);
$doc = $document['body']['data']['databaseGetDocument']; $doc = $document['body']['data']['databaseGetDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals('Robert', $doc['name']); $this->assertEquals('Robert', $doc['name']);
$this->assertEquals(100, $doc['age']); $this->assertEquals(100, $doc['age']);
$this->assertEquals($docVariables['read'], $doc['read']); $this->assertEquals($docVariables['read'], $doc['_read']);
$this->assertEquals($docVariables['write'], $doc['write']); $this->assertEquals($docVariables['write'], $doc['write']);
} }
/** /**
* @depends testCreateCollection * @depends testCreateCollection
* @depends testCreateStringAttribute
* @depends testCreateIntegerAttribute
* @depends testCreateBooleanAttribute
* @depends testCreateAccounts * @depends testCreateAccounts
* @throws \Exception * @throws \Exception
*/ */
public function testUserRole(array $data, array $accounts) public function testUserRole(array $data, $str, $int, $bool, array $accounts)
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
@ -227,6 +233,7 @@ class GraphQLClientTest extends Scope
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST); $query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
$createDocumentVariables = [ $createDocumentVariables = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'documentId' => 'unique()',
'data' => [ 'data' => [
'name' => 'Robert', 'name' => 'Robert',
'age' => '100', 'age' => '100',
@ -247,17 +254,15 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseCreateDocument']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']);
$doc = $document['body']['data']['databaseCreateDocument']; $doc = $document['body']['data']['databaseCreateDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($createDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']); $this->assertEquals($createDocumentVariables['write'], $doc['_write']);
$this->assertEquals($createDocumentVariables['read'], $doc['read']);
$this->assertEquals($createDocumentVariables['write'], $doc['write']);
/* /*
* Account 1 tries to access it * Account 1 tries to access it
@ -265,7 +270,7 @@ class GraphQLClientTest extends Scope
$query = $this->getQuery(self::$GET_DOCUMENT); $query = $this->getQuery(self::$GET_DOCUMENT);
$getDocumentVariables = [ $getDocumentVariables = [
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'documentId' => $doc['$id'] 'documentId' => $doc['_id']
]; ];
$graphQLPayload = [ $graphQLPayload = [
'query' => $query, 'query' => $query,
@ -277,16 +282,16 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseGetDocument']); $this->assertIsArray($document['body']['data']['databaseGetDocument']);
$doc = $document['body']['data']['databaseGetDocument']; $doc = $document['body']['data']['databaseGetDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($createDocumentVariables['data']['name'], $doc['name']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']); $this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
$this->assertEquals($createDocumentVariables['read'], $doc['read']); $this->assertEquals($createDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['write'], $doc['write']); $this->assertEquals($createDocumentVariables['write'], $doc['write']);
/* /*
@ -321,16 +326,14 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseUpdateDocument']); $this->assertIsArray($document['body']['data']['databaseUpdateDocument']);
$doc = $document['body']['data']['database_updateDocument']; $doc = $document['body']['data']['database_updateDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($updateDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
$this->assertEquals($updateDocumentVariables['read'], $doc['read']);
$this->assertEquals($updateDocumentVariables['write'], $doc['write']); $this->assertEquals($updateDocumentVariables['write'], $doc['write']);
/* /*
@ -351,25 +354,26 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseGetDocument']); $this->assertIsArray($document['body']['data']['databaseGetDocument']);
$doc = $document['body']['data']['databaseGetDocument']; $doc = $document['body']['data']['databaseGetDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($updateDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
$this->assertEquals($updateDocumentVariables['read'], $doc['read']);
$this->assertEquals($updateDocumentVariables['write'], $doc['write']); $this->assertEquals($updateDocumentVariables['write'], $doc['write']);
} }
/** /**
* @depends testCreateCollection * @depends testCreateCollection
* @depends testCreateStringAttribute
* @depends testCreateIntegerAttribute
* @depends testCreateBooleanAttribute
* @depends testCreateAccounts * @depends testCreateAccounts
* @throws \Exception * @throws \Exception
*/ */
public function testTeamRole(array $data, array $accounts) public function testTeamRole(array $data, $str, $int, $bool, array $accounts)
{ {
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
/** /**
@ -377,7 +381,8 @@ class GraphQLClientTest extends Scope
*/ */
$query = $this->getQuery(self::$CREATE_TEAM); $query = $this->getQuery(self::$CREATE_TEAM);
$createTeamVariables = [ $createTeamVariables = [
'name' => 'Test Team' 'teamId' => 'unique()',
'name' => 'Test Team',
]; ];
$graphQLPayload = [ $graphQLPayload = [
'query' => $query, 'query' => $query,
@ -389,12 +394,12 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['teams_create']); $this->assertIsArray($document['body']['data']['teamsCreate']);
$team = $document['body']['data']['teams_create']; $team = $document['body']['data']['teamsCreate'];
$this->assertArrayHasKey('id', $team); $this->assertArrayHasKey('_id', $team);
$this->assertEquals($createTeamVariables['name'], $team['name']); $this->assertEquals($createTeamVariables['name'], $team['name']);
/* /*
@ -407,8 +412,8 @@ class GraphQLClientTest extends Scope
'name' => 'Robert', 'name' => 'Robert',
'age' => 100 'age' => 100
], ],
'read' => ["team:{$team['id']}"], 'read' => ["team:{$team['_id']}"],
'write' => ["team:{$team['id']}"], 'write' => ["team:{$team['_id']}"],
]; ];
$graphQLPayload = [ $graphQLPayload = [
'query' => $query, 'query' => $query,
@ -420,16 +425,14 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseCreateDocument']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']);
$doc = $document['body']['data']['databaseCreateDocument']; $doc = $document['body']['data']['databaseCreateDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($createDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
$this->assertEquals($createDocumentVariables['read'], $doc['read']);
$this->assertEquals($createDocumentVariables['write'], $doc['write']); $this->assertEquals($createDocumentVariables['write'], $doc['write']);
/* /*
@ -450,16 +453,14 @@ class GraphQLClientTest extends Scope
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'], 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
], $graphQLPayload); ], $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseGetDocument']); $this->assertIsArray($document['body']['data']['databaseGetDocument']);
$doc = $document['body']['data']['databaseGetDocument']; $doc = $document['body']['data']['databaseGetDocument'];
$this->assertArrayHasKey('$id', $doc); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']); $this->assertEquals($createDocumentVariables['read'], $doc['_read']);
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
$this->assertEquals($createDocumentVariables['read'], $doc['read']);
$this->assertEquals($createDocumentVariables['write'], $doc['write']); $this->assertEquals($createDocumentVariables['write'], $doc['write']);
/* /*

View file

@ -16,6 +16,9 @@ class GraphQLServerTest extends Scope
/** /**
* @depends testCreateCollection * @depends testCreateCollection
* @depends testCreateStringAttribute
* @depends testCreateIntegerAttribute
* @depends testCreateBooleanAttribute
*/ */
public function testDocumentCreate(array $data) public function testDocumentCreate(array $data)
{ {
@ -24,10 +27,11 @@ class GraphQLServerTest extends Scope
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST); $query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
$variables = [ $variables = [
'documentId' => 'unique()',
'collectionId' => $data['collectionId'], 'collectionId' => $data['collectionId'],
'data' => [ 'data' => [
'name' => 'Robert', 'name' => 'Robert',
'ago' => 100, 'age' => 100,
'alive' => true, 'alive' => true,
], ],
'read' => ['role:all'], 'read' => ['role:all'],
@ -58,17 +62,16 @@ class GraphQLServerTest extends Scope
'x-appwrite-key' => $key 'x-appwrite-key' => $key
]), $graphQLPayload); ]), $graphQLPayload);
$this->assertNull($document['body']['errors']); $this->assertArrayNotHasKey('errors', $document['body']);
$this->assertIsArray($document['body']['data']); $this->assertIsArray($document['body']['data']);
$this->assertIsArray($document['body']['data']['databaseCreateDocument']); $this->assertIsArray($document['body']['data']['databaseCreateDocument']);
$doc = $document['body']['data']['databaseCreateDocument']; $doc = $document['body']['data']['databaseCreateDocument'];
$this->assertArrayHasKey('$id', $doc);
$this->assertEquals($data['collectionId'], $doc['$collection']); $this->assertArrayHasKey('_id', $doc);
$this->assertEquals($variables['data']['name'], $doc['name']); $this->assertEquals($data['collectionId'], $doc['_collection']);
$this->assertEquals($variables['data']['age'], $doc['age']); $this->assertEquals($variables['read'], $doc['_read']);
$this->assertEquals($variables['read'], $doc['read']); $this->assertEquals($variables['write'], $doc['_write']);
$this->assertEquals($variables['write'], $doc['write']);
} }
/** /**
@ -80,7 +83,6 @@ class GraphQLServerTest extends Scope
* Try to create a user without the required scope * Try to create a user without the required scope
*/ */
$projectId = $this->getProject()['$id']; $projectId = $this->getProject()['$id'];
$key = '';
$query = $this->getQuery(self::$CREATE_USER); $query = $this->getQuery(self::$CREATE_USER);
$variables = [ $variables = [
@ -98,7 +100,6 @@ class GraphQLServerTest extends Scope
$user = $this->client->call(Client::METHOD_POST, '/graphql', [ $user = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $projectId, 'x-appwrite-project' => $projectId,
'x-appwrite-key' => $key
], $graphQLPayload); ], $graphQLPayload);
$errorMessage = 'User (role: guest) missing scope (users.write)'; $errorMessage = 'User (role: guest) missing scope (users.write)';
@ -239,13 +240,11 @@ class GraphQLServerTest extends Scope
'x-appwrite-key' => $key 'x-appwrite-key' => $key
], $graphQLPayload); ], $graphQLPayload);
//\var_dump($countries); $errorMessage = 'app.' . $projectId . '@service.localhost (role: application) missing scope (locale.read)';
$this->assertEquals(401, $countries['headers']['status-code']);
$errorMessage = 'app.${projectId}@service.localhost (role: application) missing scope (locale.read)';
$this->assertEquals( 401, $countries['headers']['status-code']);
$this->assertEquals($countries['body']['errors'][0]['message'], $errorMessage); $this->assertEquals($countries['body']['errors'][0]['message'], $errorMessage);
$this->assertIsArray($countries['body']['data']); $this->assertIsArray($countries['body']['data']);
$this->assertNull($countries['body']['data']['locale_getCountries']); $this->assertNull($countries['body']['data']['localeGetCountries']);
} }
} }