2021-03-17 07:16:33 +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-17 07:16:33 +13:00
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\SideClient;
|
|
|
|
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
class GraphQLClientTest extends Scope
|
2021-03-17 07:16:33 +13:00
|
|
|
{
|
2022-04-09 01:52:20 +12:00
|
|
|
use ProjectCustom;
|
2021-03-17 07:16:33 +13:00
|
|
|
use SideClient;
|
|
|
|
use GraphQLBase;
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
public function testCreateAccounts(): array
|
|
|
|
{
|
2021-03-17 07:16:33 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_ACCOUNT);
|
2022-05-04 21:19:36 +12:00
|
|
|
$email = 'test' . \rand() . '@test.com';
|
2021-03-17 07:16:33 +13:00
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
2022-05-04 21:19:36 +12:00
|
|
|
'variables' => [
|
2022-04-09 01:52:20 +12:00
|
|
|
'userId' => 'unique()',
|
|
|
|
'name' => 'Tester',
|
2022-05-04 21:19:36 +12:00
|
|
|
'email' => $email,
|
|
|
|
'password' => 'password',
|
|
|
|
],
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$account1 = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($account1['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$account1 = $account1['body']['data']['accountCreate'];
|
|
|
|
$this->assertEquals('Tester', $account1['name']);
|
2022-05-04 21:19:36 +12:00
|
|
|
$this->assertEquals($email, $account1['email']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
2022-05-04 21:19:36 +12:00
|
|
|
// Create First Account Session
|
2021-03-17 07:16:33 +13:00
|
|
|
$query = $this->getQuery(self::$CREATE_ACCOUNT_SESSION);
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
2022-05-04 21:19:36 +12:00
|
|
|
'variables' => [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => 'password',
|
|
|
|
]
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
2022-06-27 18:46:01 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$session1 = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($session1['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($session1['body']['data']['accountCreateSession']);
|
2022-05-04 21:19:36 +12:00
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$session1Cookie = $this->client->parseCookie((string)$session1['headers']['set-cookie'])['a_session_' . $this->getProject()['$id']];
|
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
/*
|
|
|
|
* Create Second Account
|
2022-04-09 01:52:20 +12:00
|
|
|
*/
|
2021-03-17 07:16:33 +13:00
|
|
|
$query = $this->getQuery(self::$CREATE_ACCOUNT);
|
2022-05-04 21:19:36 +12:00
|
|
|
$email = 'test' . \rand() . '@test.com';
|
2021-03-17 07:16:33 +13:00
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
2022-05-04 21:19:36 +12:00
|
|
|
'variables' => [
|
|
|
|
'userId' => 'unique()',
|
|
|
|
'email' => $email,
|
|
|
|
'password' => 'password',
|
|
|
|
'name' => 'Tester2',
|
|
|
|
],
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$account2 = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($account2['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($account2['body']['data']['accountCreate']);
|
2022-05-04 21:19:36 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $account2['body']);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
|
|
|
$account2 = $account2['body']['data']['accountCreate'];
|
2022-05-04 21:19:36 +12:00
|
|
|
$this->assertEquals('Tester2', $account2['name']);
|
|
|
|
$this->assertEquals($email, $account2['email']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create Second Account Session
|
2022-04-09 01:52:20 +12:00
|
|
|
*/
|
2021-03-17 07:16:33 +13:00
|
|
|
$query = $this->getQuery(self::$CREATE_ACCOUNT_SESSION);
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
2022-05-04 21:19:36 +12:00
|
|
|
'variables' => [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => 'password',
|
|
|
|
],
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$session2 = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($session2['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($session2['body']['data']['accountCreateSession']);
|
2022-05-04 21:19:36 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $session2['body']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$session2Cookie = $this->client->parseCookie((string)$session2['headers']['set-cookie'])['a_session_' . $this->getProject()['$id']];
|
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
return [
|
2022-04-09 01:52:20 +12:00
|
|
|
'session1Cookie' => $session1Cookie,
|
|
|
|
'session2Cookie' => $session2Cookie,
|
|
|
|
'user1Id' => $session1['body']['data']['accountCreateSession']['userId'],
|
|
|
|
'user2Id' => $session2['body']['data']['accountCreateSession']['userId'],
|
2021-03-17 07:16:33 +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
|
|
|
* @depends testCreateAccounts
|
|
|
|
*/
|
2022-06-27 18:46:01 +12:00
|
|
|
public function testWildCardPermissions(array $data, $str, $int, $bool, array $accounts)
|
2022-04-09 01:52:20 +12:00
|
|
|
{
|
2021-03-17 07:16:33 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
/*
|
|
|
|
* Account 1 Creates a document with wildcard permissions
|
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
|
|
|
|
|
|
|
|
$docVariables = [
|
|
|
|
'documentId' => 'unique()',
|
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-17 07:16:33 +13:00
|
|
|
'data' => [
|
2022-04-09 01:52:20 +12:00
|
|
|
'name' => 'Robert',
|
|
|
|
'age' => 100,
|
|
|
|
'alive' => true,
|
2021-03-17 07:16:33 +13:00
|
|
|
],
|
2022-04-09 01:52:20 +12:00
|
|
|
'read' => ['role:all'],
|
|
|
|
'write' => ['role:all'],
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
2022-06-27 18:46:01 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $docVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
2022-06-27 18:46:01 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +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($docVariables['read'], $doc['_read']);
|
|
|
|
$this->assertEquals($docVariables['write'], $doc['_write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 1 tries to access it
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENT);
|
|
|
|
$getDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2022-06-27 18:46:01 +12:00
|
|
|
'documentId' => $doc['_id']
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $getDocumentVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseGetDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseGetDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals('Robert', $doc['name']);
|
|
|
|
$this->assertEquals(100, $doc['age']);
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertEquals($docVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($docVariables['write'], $doc['write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 2 tries to access it
|
|
|
|
*/
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseGetDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseGetDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals('Robert', $doc['name']);
|
|
|
|
$this->assertEquals(100, $doc['age']);
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertEquals($docVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($docVariables['write'], $doc['write']);
|
2021-03-17 07:16:33 +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
|
|
|
* @depends testCreateAccounts
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-27 18:46:01 +12:00
|
|
|
public function testUserRole(array $data, $str, $int, $bool, array $accounts)
|
2022-04-09 01:52:20 +12:00
|
|
|
{
|
2021-03-17 07:16:33 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
/*
|
|
|
|
* Account 1 Creates a document with user permissions
|
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
|
2021-03-17 07:16:33 +13:00
|
|
|
$createDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2022-06-27 18:46:01 +12:00
|
|
|
'documentId' => 'unique()',
|
2021-03-17 07:16:33 +13:00
|
|
|
'data' => [
|
2022-04-09 01:52:20 +12:00
|
|
|
'name' => 'Robert',
|
|
|
|
'age' => '100',
|
|
|
|
'alive' => true,
|
2021-03-17 07:16:33 +13:00
|
|
|
],
|
|
|
|
'read' => ["user:{$accounts['user1Id']}"],
|
|
|
|
'write' => ["user:{$accounts['user1Id']}"],
|
|
|
|
];
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $createDocumentVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +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($createDocumentVariables['read'], $doc['_read']);
|
|
|
|
$this->assertEquals($createDocumentVariables['write'], $doc['_write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 1 tries to access it
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENT);
|
|
|
|
$getDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2022-06-27 18:46:01 +12:00
|
|
|
'documentId' => $doc['_id']
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $getDocumentVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseGetDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseGetDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($createDocumentVariables['data']['name'], $doc['name']);
|
|
|
|
$this->assertEquals($createDocumentVariables['data']['age'], $doc['age']);
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertEquals($createDocumentVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($createDocumentVariables['write'], $doc['write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 2 tries to access it
|
|
|
|
*/
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals('No document found', $document['body']['errors'][0]['message']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 1 Updates the document permissions
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$UPDATE_DOCUMENT);
|
|
|
|
$updateDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-17 07:16:33 +13:00
|
|
|
'documentId' => $doc['$id'],
|
|
|
|
'data' => [],
|
2022-04-09 01:52:20 +12:00
|
|
|
'read' => ['role:all'],
|
|
|
|
'write' => ['role:all']
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $updateDocumentVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseUpdateDocument']);
|
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
$doc = $document['body']['data']['database_updateDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
|
|
|
$this->assertEquals($updateDocumentVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($updateDocumentVariables['write'], $doc['write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 2 tries to access it
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENT);
|
|
|
|
$getDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-17 07:16:33 +13:00
|
|
|
'documentId' => $doc['$id']
|
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $getDocumentVariables
|
2021-03-17 07:16:33 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session2Cookie'],
|
2021-03-17 07:16:33 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-17 07:16:33 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseGetDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseGetDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
|
|
|
$this->assertEquals($updateDocumentVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($updateDocumentVariables['write'], $doc['write']);
|
2021-03-17 07:16:33 +13:00
|
|
|
}
|
|
|
|
|
2021-03-23 01:21:59 +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
|
|
|
* @depends testCreateAccounts
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-06-27 18:46:01 +12:00
|
|
|
public function testTeamRole(array $data, $str, $int, $bool, array $accounts)
|
2022-04-09 01:52:20 +12:00
|
|
|
{
|
2021-03-23 01:21:59 +13:00
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
/**
|
|
|
|
* Account 1 creates a team
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$CREATE_TEAM);
|
|
|
|
$createTeamVariables = [
|
2022-06-27 18:46:01 +12:00
|
|
|
'teamId' => 'unique()',
|
|
|
|
'name' => 'Test Team',
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $createTeamVariables
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-23 01:21:59 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-23 01:21:59 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['teamsCreate']);
|
2022-04-09 01:52:20 +12:00
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$team = $document['body']['data']['teamsCreate'];
|
|
|
|
$this->assertArrayHasKey('_id', $team);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($createTeamVariables['name'], $team['name']);
|
2021-03-23 01:21:59 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 1 Creates a document with team permissions
|
|
|
|
*/
|
2022-04-09 01:52:20 +12:00
|
|
|
$query = $this->getQuery(self::$CREATE_DOCUMENT_REST);
|
2021-03-23 01:21:59 +13:00
|
|
|
$createDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-23 01:21:59 +13:00
|
|
|
'data' => [
|
2022-04-09 01:52:20 +12:00
|
|
|
'name' => 'Robert',
|
|
|
|
'age' => 100
|
2021-03-23 01:21:59 +13:00
|
|
|
],
|
2022-06-27 18:46:01 +12:00
|
|
|
'read' => ["team:{$team['_id']}"],
|
|
|
|
'write' => ["team:{$team['_id']}"],
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $createDocumentVariables
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-23 01:21:59 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-23 01:21:59 +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($createDocumentVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($createDocumentVariables['write'], $doc['write']);
|
2021-03-23 01:21:59 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Account 1 tries to access it
|
|
|
|
*/
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENT);
|
|
|
|
$getDocumentVariables = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'collectionId' => $data['collectionId'],
|
2021-03-23 01:21:59 +13:00
|
|
|
'documentId' => $doc['$id']
|
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $getDocumentVariables
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-23 01:21:59 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
2021-03-23 01:21:59 +13:00
|
|
|
$this->assertIsArray($document['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($document['body']['data']['databaseGetDocument']);
|
|
|
|
|
|
|
|
$doc = $document['body']['data']['databaseGetDocument'];
|
2022-06-27 18:46:01 +12:00
|
|
|
$this->assertArrayHasKey('_id', $doc);
|
|
|
|
$this->assertEquals($data['collectionId'], $doc['_collection']);
|
|
|
|
$this->assertEquals($createDocumentVariables['read'], $doc['_read']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($createDocumentVariables['write'], $doc['write']);
|
2021-03-23 01:21:59 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a membership
|
|
|
|
*/
|
2022-05-04 21:19:36 +12:00
|
|
|
$email = \rand() . 'friend@localhost.test';
|
2021-03-23 01:21:59 +13:00
|
|
|
$query = $this->getQuery(self::$CREATE_TEAM_MEMBERSHIP);
|
|
|
|
$createMembershipVariable = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'teamId' => $team['id'],
|
2021-03-23 01:21:59 +13:00
|
|
|
'name' => 'Friend User',
|
|
|
|
'email' => $email,
|
|
|
|
'roles' => ['owner'],
|
|
|
|
'url' => 'http://localhost:5000/join-us#title'
|
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $createMembershipVariable
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$membership = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
2022-04-09 01:52:20 +12:00
|
|
|
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $accounts['session1Cookie'],
|
2021-03-23 01:21:59 +13:00
|
|
|
], $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertNull($membership['body']['errors']);
|
|
|
|
$this->assertIsArray($membership['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($membership['body']['data']['teamsCreateMembership']);
|
|
|
|
|
|
|
|
$membership = $membership['body']['data']['teamsCreateMembership'];
|
2021-03-23 01:21:59 +13:00
|
|
|
$this->assertNotEmpty($membership['id']);
|
|
|
|
$this->assertNotEmpty($membership['userId']);
|
|
|
|
$this->assertNotEmpty($membership['teamId']);
|
|
|
|
$this->assertCount(1, $membership['roles']);
|
|
|
|
$this->assertIsInt($membership['joined']);
|
|
|
|
$this->assertEquals(false, $membership['confirm']);
|
|
|
|
|
|
|
|
$lastEmail = $this->getLastEmail();
|
|
|
|
|
|
|
|
$this->assertEquals($email, $lastEmail['to'][0]['address']);
|
|
|
|
$this->assertEquals('Friend User', $lastEmail['to'][0]['name']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals('Invitation to ' . $createTeamVariables['name'] . ' Team at ' . $this->getProject()['name'], $lastEmail['subject']);
|
2021-03-23 01:21:59 +13:00
|
|
|
|
|
|
|
$secret = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256);
|
|
|
|
$inviteUid = substr($lastEmail['text'], strpos($lastEmail['text'], '?inviteId=', 0) + 10, 13);
|
|
|
|
$userUid = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 13);
|
|
|
|
|
|
|
|
/** Update membership status */
|
|
|
|
$query = $this->getQuery(self::$UPDATE_MEMBERSHIP_STATUS);
|
|
|
|
$updateMembershipStatus = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'teamId' => $team['id'],
|
2021-03-23 01:21:59 +13:00
|
|
|
'inviteId' => $inviteUid,
|
|
|
|
'userId' => $userUid,
|
|
|
|
'secret' => $secret,
|
|
|
|
];
|
|
|
|
$graphQLPayload = [
|
2022-04-09 01:52:20 +12:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $updateMembershipStatus
|
2021-03-23 01:21:59 +13:00
|
|
|
];
|
|
|
|
$updatedMembership = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertNull($updatedMembership['body']['errors']);
|
|
|
|
$this->assertIsArray($updatedMembership['body']['data']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertIsArray($updatedMembership['body']['data']['teamsUpdateMembershipStatus']);
|
|
|
|
|
|
|
|
$updatedMembership = $updatedMembership['body']['data']['teamsUpdateMembershipStatus'];
|
2021-03-23 01:21:59 +13:00
|
|
|
$this->assertNotEmpty($membership['id'], $updatedMembership['id']);
|
2022-04-09 01:52:20 +12:00
|
|
|
$this->assertEquals($membership['userId'], $updatedMembership['userId']);
|
2021-03-23 01:21:59 +13:00
|
|
|
$this->assertEquals($membership['teamId'], $updatedMembership['teamId']);
|
|
|
|
$this->assertCount(1, $updatedMembership['roles']);
|
|
|
|
$this->assertIsInt($updatedMembership['joined']);
|
|
|
|
$this->assertEquals(true, $updatedMembership['confirm']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-17 07:16:33 +13:00
|
|
|
}
|