From 0332d35d9e296b009c42b9197cc98bbebf3bd6cd Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 11 Oct 2023 14:21:20 +0100 Subject: [PATCH] test: e2e universal token --- .../Account/AccountCustomClientTest.php | 60 +++++++++++++++++++ tests/e2e/Services/Users/UsersBase.php | 28 +++++++++ 2 files changed, 88 insertions(+) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 89851686cb..b24d5c3815 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -982,6 +982,66 @@ class AccountCustomClientTest extends Scope return $data; } + /** + * @depends testGetAccountSessions + * @depends testGetAccountLogs + */ + public function testExchangeUniversalToken(array $data): array + { + $response = $this->client->call(Client::METHOD_POST, '/users/' . $data['id'] . '/tokens', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + + $userId = $response['body']['userId']; + $secret = $response['body']['secret']; + + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_PUT, '/account/sessions/token', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'userId' => $userId, + 'secret' => $secret, + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertNotEmpty($response['body']['userId']); + $this->assertNotEmpty($response['body']['expire']); + $this->assertNotEmpty($response['body']['secret']); + + /** + * Test for FAILURE + */ + // Invalid userId + $response = $this->client->call(Client::METHOD_PUT, '/account/sessions/token', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'userId' => ID::custom('ewewe'), + 'secret' => $secret, + ]); + + $this->assertEquals(401, $response['headers']['status-code']); + + // Invalid secret + $response = $this->client->call(Client::METHOD_PUT, '/account/sessions/token', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'userId' => $userId, + 'secret' => '123456', + ]); + + $this->assertEquals(401, $response['headers']['status-code']); + + return $data; + } + /** * @depends testUpdatePhone */ diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index baf601789a..765fb989ec 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -229,6 +229,34 @@ trait UsersBase } } + /** + * @depends testCreateAccount + */ + public function testCreateUniversalToken(array $data): void + { + /** + * Test for SUCCESS + */ + $token = $this->client->call(Client::METHOD_POST, '/users/' . $data['userId'] . '/tokens', array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($token['headers']['status-code'], 201); + $this->assertEquals($token['body']['userId'], $data['userId']); + $this->assertNotEmpty($token['body']['secret']); + $this->assertNotEmpty($token['body']['expire']); + + /** + * Test for FAILURE + */ + $token = $this->client->call(Client::METHOD_POST, '/users/invalid/tokens', array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($token['headers']['status-code'], 404); + $this->assertEmpty($token['body']['secret']); + } + /** * Tests all optional parameters of createUser (email, phone, anonymous..) *