2020-01-12 02:58:02 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Scopes;
|
|
|
|
|
2022-08-26 21:21:08 +12:00
|
|
|
use Appwrite\Tests\Retryable;
|
2020-01-12 02:58:02 +13:00
|
|
|
use Tests\E2E\Client;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-12-15 04:42:25 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
2020-01-12 02:58:02 +13:00
|
|
|
|
|
|
|
abstract class Scope extends TestCase
|
|
|
|
{
|
2022-08-26 21:21:08 +12:00
|
|
|
use Retryable;
|
|
|
|
|
2022-12-20 00:25:01 +13:00
|
|
|
protected ?Client $client = null;
|
2022-12-15 21:51:27 +13:00
|
|
|
protected string $endpoint = 'http://localhost/v1';
|
|
|
|
|
2020-01-12 02:58:02 +13:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->client = new Client();
|
2023-05-30 18:26:17 +12:00
|
|
|
$this->client->setEndpoint($this->endpoint);
|
2020-01-12 02:58:02 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
$this->client = null;
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
protected function getLastEmail(): array
|
2020-01-12 02:58:02 +13:00
|
|
|
{
|
2022-01-19 12:23:19 +13:00
|
|
|
sleep(3);
|
2021-10-23 05:19:02 +13:00
|
|
|
|
2021-01-04 10:22:41 +13:00
|
|
|
$emails = json_decode(file_get_contents('http://maildev:1080/email'), true);
|
2020-01-12 02:58:02 +13:00
|
|
|
|
2020-10-28 08:48:38 +13:00
|
|
|
if ($emails && is_array($emails)) {
|
2020-01-12 02:58:02 +13:00
|
|
|
return end($emails);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
protected function getLastRequest(): array
|
2020-10-31 08:53:27 +13:00
|
|
|
{
|
2022-01-19 12:23:19 +13:00
|
|
|
sleep(2);
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2023-05-30 18:26:17 +12:00
|
|
|
$request = json_decode(file_get_contents('http://request-catcher:5000/__last_request__'), true);
|
|
|
|
$request['data'] = json_decode($request['data'], true);
|
2022-05-24 02:54:50 +12:00
|
|
|
|
2023-05-30 18:26:17 +12:00
|
|
|
return $request;
|
2020-10-31 08:53:27 +13:00
|
|
|
}
|
|
|
|
|
2020-01-12 02:58:02 +13:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
abstract public function getHeaders(): array;
|
2020-01-12 02:58:02 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-24 02:54:50 +12:00
|
|
|
abstract public function getProject(): array;
|
2020-01-13 10:28:26 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $root = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRoot(): array
|
|
|
|
{
|
2020-10-28 08:48:38 +13:00
|
|
|
if ((self::$root)) {
|
2020-01-13 10:28:26 +13:00
|
|
|
return self::$root;
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$email = uniqid() . 'user@localhost.test';
|
2020-01-13 10:28:26 +13:00
|
|
|
$password = 'password';
|
|
|
|
$name = 'User Name';
|
|
|
|
|
|
|
|
$root = $this->client->call(Client::METHOD_POST, '/account', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
2022-08-14 22:33:36 +12:00
|
|
|
'userId' => ID::unique(),
|
2020-01-13 10:28:26 +13:00
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
'name' => $name,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $root['headers']['status-code']);
|
|
|
|
|
2022-06-14 20:17:50 +12:00
|
|
|
$session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
|
2020-01-13 10:28:26 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => 'console',
|
|
|
|
], [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
|
|
|
|
2023-12-09 09:02:07 +13:00
|
|
|
var_dump($session['headers']['set-cookie']);
|
2020-07-03 09:48:02 +12:00
|
|
|
$session = $this->client->parseCookie((string)$session['headers']['set-cookie'])['a_session_console'];
|
2020-01-13 10:28:26 +13:00
|
|
|
|
|
|
|
self::$root = [
|
2022-08-14 22:33:36 +12:00
|
|
|
'$id' => ID::custom($root['body']['$id']),
|
2020-01-13 10:28:26 +13:00
|
|
|
'name' => $root['body']['name'],
|
2020-01-17 03:06:28 +13:00
|
|
|
'email' => $root['body']['email'],
|
2020-01-13 10:28:26 +13:00
|
|
|
'session' => $session,
|
|
|
|
];
|
|
|
|
|
|
|
|
return self::$root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $user = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getUser(): array
|
|
|
|
{
|
2020-10-28 08:48:38 +13:00
|
|
|
if (isset(self::$user[$this->getProject()['$id']])) {
|
2020-02-17 20:16:11 +13:00
|
|
|
return self::$user[$this->getProject()['$id']];
|
2020-01-13 10:28:26 +13:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
$email = uniqid() . 'user@localhost.test';
|
2020-01-13 10:28:26 +13:00
|
|
|
$password = 'password';
|
|
|
|
$name = 'User Name';
|
|
|
|
|
|
|
|
$user = $this->client->call(Client::METHOD_POST, '/account', [
|
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2020-02-17 20:16:11 +13:00
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2020-01-13 10:28:26 +13:00
|
|
|
], [
|
2022-08-14 22:33:36 +12:00
|
|
|
'userId' => ID::unique(),
|
2020-01-13 10:28:26 +13:00
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
'name' => $name,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $user['headers']['status-code']);
|
|
|
|
|
2022-06-14 20:17:50 +12:00
|
|
|
$session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [
|
2020-01-13 10:28:26 +13:00
|
|
|
'origin' => 'http://localhost',
|
|
|
|
'content-type' => 'application/json',
|
2020-02-17 20:16:11 +13:00
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2020-01-13 10:28:26 +13:00
|
|
|
], [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
]);
|
|
|
|
|
2022-06-30 21:23:38 +12:00
|
|
|
$token = $this->client->parseCookie((string)$session['headers']['set-cookie'])['a_session_' . $this->getProject()['$id']];
|
2020-01-13 10:28:26 +13:00
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
self::$user[$this->getProject()['$id']] = [
|
2022-08-14 22:33:36 +12:00
|
|
|
'$id' => ID::custom($user['body']['$id']),
|
2020-01-13 10:28:26 +13:00
|
|
|
'name' => $user['body']['name'],
|
2020-01-17 03:06:28 +13:00
|
|
|
'email' => $user['body']['email'],
|
2022-06-30 21:23:38 +12:00
|
|
|
'session' => $token,
|
|
|
|
'sessionId' => $session['body']['$id'],
|
2020-01-13 10:28:26 +13:00
|
|
|
];
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
return self::$user[$this->getProject()['$id']];
|
2020-01-13 10:28:26 +13:00
|
|
|
}
|
2022-01-19 12:23:19 +13:00
|
|
|
}
|