1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<?php
use PHPUnit\Framework\TestCase;
final class LogoutHandlerTest extends ApiTestCase
{
// ── Successful logout ─────────────────────────────────────────────────────
public function test_logout_returns_ok(): void
{
// Step 1: Login (response includes csrf_token)
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$this->assertEquals('ok', $body['result'] ?? null);
$this->assertNotNull($body['csrf_token']);
// Step 2: Logout
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'logout',
]);
$this->assertEquals(200, $status);
$this->assertEquals('ok', $body['result'] ?? null);
}
// ── Session is destroyed after logout ─────────────────────────────────────
public function test_logout_destroys_session(): void
{
// Step 1: Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertEquals(200, $status);
$this->assertNotNull($body['csrf_token']);
// Step 2: Logout
$this->post($this->apiUrl, [
'op' => 'logout',
]);
// Step 3: Try an auth-required operation → should be 401
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'list',
]);
$this->assertEquals(401, $status);
}
// ── Logout without authentication ─────────────────────────────────────────
public function test_logout_without_auth(): void
{
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'logout',
]);
$this->assertEquals(401, $status);
$this->assertArrayHasKey('error', $body);
}
// ── Double logout ─────────────────────────────────────────────────────────
public function test_double_logout_second_fails(): void
{
// Login
[$status, $body] = $this->post($this->apiUrl, [
'op' => 'perform-login',
'user' => 'test',
'password' => 'test',
]);
$this->assertNotNull($body['csrf_token']);
// First logout
[$status1, $body1] = $this->post($this->apiUrl, [
'op' => 'logout',
]);
$this->assertEquals(200, $status1);
$this->assertEquals('ok', $body1['result'] ?? null);
// Second logout — session already destroyed → 401
[$status2, $body2] = $this->post($this->apiUrl, [
'op' => 'logout',
]);
$this->assertEquals(401, $status2);
}
}
|