feat: add test for max sessions
This commit is contained in:
parent
cbcc17ded6
commit
cda8e53386
2 changed files with 120 additions and 9 deletions
|
@ -332,14 +332,20 @@ App::shutdown()
|
||||||
$sessionLimit = $project->getAttribute('auth', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS;
|
$sessionLimit = $project->getAttribute('auth', [])['maxSessions'] ?? APP_LIMIT_USER_SESSIONS;
|
||||||
$session = $response->getPayload();
|
$session = $response->getPayload();
|
||||||
$userId = $session['userId'] ?? '';
|
$userId = $session['userId'] ?? '';
|
||||||
if(empty($userId)) return;
|
if (empty($userId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$user = $dbForProject->getDocument('users', $userId);
|
$user = $dbForProject->getDocument('users', $userId);
|
||||||
if($user->isEmpty()) return;
|
if ($user->isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$sessions = $user->getAttribute('sessions', []);
|
$sessions = $user->getAttribute('sessions', []);
|
||||||
$count = \count($sessions);
|
$count = \count($sessions);
|
||||||
if($count <= $sessionLimit) return;
|
if ($count <= $sessionLimit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for ($i = 0; $i < ($count - $sessionLimit); $i++) {
|
for ($i = 0; $i < ($count - $sessionLimit); $i++) {
|
||||||
$session = array_pop($sessions);
|
$session = array_pop($sessions);
|
||||||
|
|
|
@ -874,6 +874,111 @@ class ProjectsConsoleClientTest extends Scope
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testUpdateProjectAuthLimit
|
||||||
|
*/
|
||||||
|
public function testUpdateProjectAuthSessionLimit($data): array
|
||||||
|
{
|
||||||
|
$id = $data['projectId'] ?? '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for failure
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/max-sessions', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'limit' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(400, $response['headers']['status-code']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for SUCCESS
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $id . '/auth/max-sessions', array_merge([
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
], $this->getHeaders()), [
|
||||||
|
'limit' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
$this->assertNotEmpty($response['body']['$id']);
|
||||||
|
|
||||||
|
$email = uniqid() . 'user@localhost.test';
|
||||||
|
$password = 'password';
|
||||||
|
$name = 'User Name';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new user
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
]), [
|
||||||
|
'userId' => ID::unique(),
|
||||||
|
'email' => $email,
|
||||||
|
'password' => $password,
|
||||||
|
'name' => $name,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals($response['headers']['status-code'], 501);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create new session
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
]), [
|
||||||
|
'email' => $email,
|
||||||
|
'password' => $password,
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
$sessionId1 = $response['body']['$id'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create new session
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_POST, '/account/sessions/email', array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
]), [
|
||||||
|
'email' => $email,
|
||||||
|
'password' => $password,
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
$sessionCookie = $response['headers']['set-cookie'];
|
||||||
|
$sessionId2 = $response['body']['$id'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List sessions
|
||||||
|
*/
|
||||||
|
$response = $this->client->call(Client::METHOD_GET, '/account/sessions', [
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $id,
|
||||||
|
'Cookie' => $sessionCookie,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
|
$sessions = $response['body']['sessions'];
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($sessions));
|
||||||
|
$this->assertEquals($sessionId2, $sessions[0]['$id']);
|
||||||
|
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
public function testUpdateProjectServiceStatusAdmin(): array
|
public function testUpdateProjectServiceStatusAdmin(): array
|
||||||
{
|
{
|
||||||
$team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
|
$team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([
|
||||||
|
|
Loading…
Reference in a new issue