From 821aa45d7d6b48275d0523e84df78097aed93a9a Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 27 Aug 2021 11:31:26 +0200 Subject: [PATCH] add tests against failure --- app/realtime.php | 6 +- tests/e2e/Services/Realtime/RealtimeBase.php | 83 +++++++++++++++++++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index c574fda0f1..78c4f8d1d9 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -498,7 +498,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re switch ($message['type']) { case 'authentication': if (!array_key_exists('session', $message['data'])) { - throw new Exception('Payload not valid.', 1003); + throw new Exception('Payload is not valid.', 1003); } $session = Auth::decodeSession($message['data']['session']); @@ -513,7 +513,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re || !Auth::sessionVerify($user->getAttribute('sessions', []), Auth::$secret) // Validate user has valid login token ) { // cookie not valid - throw new Exception('Session not valid.', 1003); + throw new Exception('Session is not valid.', 1003); } $roles = Auth::getRoles($user); @@ -533,7 +533,7 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re break; default: - throw new Exception('Message type not valid.', 1003); + throw new Exception('Message type is not valid.', 1003); break; } } catch (\Throwable $th) { diff --git a/tests/e2e/Services/Realtime/RealtimeBase.php b/tests/e2e/Services/Realtime/RealtimeBase.php index 50c0041008..d1afacb7a7 100644 --- a/tests/e2e/Services/Realtime/RealtimeBase.php +++ b/tests/e2e/Services/Realtime/RealtimeBase.php @@ -185,13 +185,16 @@ trait RealtimeBase $client->close(); } - public function testAuthenticateMessage() + public function testManualAuthentication() { $user = $this->getUser(); $userId = $user['$id'] ?? ''; $session = $user['session'] ?? ''; $projectId = $this->getProject()['$id']; + /** + * Test for SUCCESS + */ $client = $this->getWebsocket(['account'], [ 'origin' => 'http://localhost' ]); @@ -219,6 +222,71 @@ trait RealtimeBase $this->assertNotEmpty($response['data']); $this->assertEquals('authentication', $response['data']['to']); $this->assertTrue($response['data']['success']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + + /** + * Test for FAILURE + */ + $client->send(\json_encode([ + 'type' => 'authentication', + 'data' => [ + 'session' => 'invalid_session' + ] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Session is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'type' => 'authentication', + 'data' => [] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Payload is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'type' => 'unknown', + 'data' => [ + 'session' => 'invalid_session' + ] + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message type is not valid.', $response['data']['message']); + + $client->send(\json_encode([ + 'test' => '123', + ])); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message format is not valid.', $response['data']['message']); + $client->close(); } @@ -243,6 +311,9 @@ trait RealtimeBase $this->assertCount(2, $response['data']['channels']); $this->assertContains('account', $response['data']['channels']); $this->assertContains('account.' . $userId, $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); + /** * Test Account Name Event */ @@ -535,6 +606,8 @@ trait RealtimeBase $this->assertCount(2, $response['data']['channels']); $this->assertContains('documents', $response['data']['channels']); $this->assertContains('collections', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); /** * Test Collection Create @@ -703,6 +776,8 @@ trait RealtimeBase $this->assertNotEmpty($response['data']); $this->assertCount(1, $response['data']['channels']); $this->assertContains('files', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); /** * Test File Create @@ -799,6 +874,8 @@ trait RealtimeBase $this->assertNotEmpty($response['data']); $this->assertCount(1, $response['data']['channels']); $this->assertContains('executions', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); /** * Test File Create @@ -901,6 +978,8 @@ trait RealtimeBase $this->assertNotEmpty($response['data']); $this->assertCount(1, $response['data']['channels']); $this->assertContains('teams', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); /** * Test Team Create @@ -985,6 +1064,8 @@ trait RealtimeBase $this->assertNotEmpty($response['data']); $this->assertCount(1, $response['data']['channels']); $this->assertContains('memberships', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($user['$id'], $response['data']['user']['$id']); $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamId.'/memberships', array_merge([ 'content-type' => 'application/json',