Merge pull request #3506 from appwrite/fix-realtime-manual-authentication
fix: realtime authentication on console
This commit is contained in:
commit
a02800695a
2 changed files with 111 additions and 2 deletions
|
@ -491,8 +491,12 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
|
|||
$database = new Database(new MariaDB($db), $cache);
|
||||
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
|
||||
$database->setNamespace("_console");
|
||||
$project = Authorization::skip(fn() => $database->getDocument('projects', $realtime->connections[$connection]['projectId']));
|
||||
$database->setNamespace("_{$project->getInternalId()}");
|
||||
$projectId = $realtime->connections[$connection]['projectId'];
|
||||
|
||||
if ($projectId !== 'console') {
|
||||
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectId));
|
||||
$database->setNamespace("_{$project->getInternalId()}");
|
||||
}
|
||||
|
||||
/*
|
||||
* Abuse Check
|
||||
|
|
|
@ -13,6 +13,111 @@ class RealtimeConsoleClientTest extends Scope
|
|||
use ProjectCustom;
|
||||
use SideConsole;
|
||||
|
||||
public function testManualAuthentication()
|
||||
{
|
||||
$user = $this->getUser();
|
||||
$userId = $user['$id'] ?? '';
|
||||
$session = $user['session'] ?? '';
|
||||
|
||||
/**
|
||||
* Test for SUCCESS
|
||||
*/
|
||||
$client = $this->getWebsocket(['account'], [
|
||||
'origin' => 'http://localhost'
|
||||
]);
|
||||
$response = json_decode($client->receive(), true);
|
||||
|
||||
$this->assertArrayHasKey('type', $response);
|
||||
$this->assertArrayHasKey('data', $response);
|
||||
$this->assertEquals('connected', $response['type']);
|
||||
$this->assertNotEmpty($response['data']);
|
||||
$this->assertCount(1, $response['data']['channels']);
|
||||
$this->assertContains('account', $response['data']['channels']);
|
||||
|
||||
$client->send(\json_encode([
|
||||
'type' => 'authentication',
|
||||
'data' => [
|
||||
'session' => $session
|
||||
]
|
||||
]));
|
||||
|
||||
$response = json_decode($client->receive(), true);
|
||||
|
||||
$this->assertArrayHasKey('type', $response);
|
||||
$this->assertArrayHasKey('data', $response);
|
||||
$this->assertEquals('response', $response['type']);
|
||||
$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();
|
||||
}
|
||||
|
||||
public function testAttributes()
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
|
Loading…
Reference in a new issue