-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthServiceLogoutTest.php
More file actions
359 lines (300 loc) · 13.9 KB
/
AuthServiceLogoutTest.php
File metadata and controls
359 lines (300 loc) · 13.9 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php namespace Tests;
/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\libs\OAuth2\Repositories\IOAuth2OTPRepository;
use Auth\AuthService;
use Auth\Repositories\IUserRepository;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use OAuth2\Services\IPrincipalService;
use OAuth2\Services\ISecurityContextService;
use OpenId\Services\IUserService;
use App\Services\Auth\IUserService as IAuthUserService;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Services\IUserActionService;
use Utils\Db\ITransactionService;
use Utils\Services\ICacheService;
use Utils\Services\IAuthService;
/**
* Class AuthServiceLogoutTest
* Tests that AuthService::logout() properly flushes all session data
* and regenerates the session ID, fixing the incomplete session cleanup
* that previously required callers to manually call Session::flush().
*/
#[\PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses]
#[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
final class AuthServiceLogoutTest extends PHPUnitTestCase
{
use MockeryPHPUnitIntegration;
private AuthService $service;
private $mock_principal_service;
private $mock_user_action_service;
private $mock_cache_service;
private $mock_security_context_service;
// Facade aliases
private $auth_mock;
private $session_mock;
private $config_mock;
private $cookie_mock;
private $crypt_mock;
private $log_mock;
protected function setUp(): void
{
parent::setUp();
$mock_user_repository = $this->createMock(IUserRepository::class);
$mock_otp_repository = $this->createMock(IOAuth2OTPRepository::class);
$this->mock_principal_service = $this->createMock(IPrincipalService::class);
$mock_user_service = $this->createMock(IUserService::class);
$this->mock_user_action_service = $this->createMock(IUserActionService::class);
$this->mock_cache_service = $this->createMock(ICacheService::class);
$mock_auth_user_service = $this->createMock(IAuthUserService::class);
$this->mock_security_context_service = $this->createMock(ISecurityContextService::class);
$mock_tx_service = $this->createMock(ITransactionService::class);
// Mock facades using Mockery alias (no Laravel app container needed)
$this->auth_mock = Mockery::mock('alias:Illuminate\Support\Facades\Auth');
$this->session_mock = Mockery::mock('alias:Illuminate\Support\Facades\Session');
$this->config_mock = Mockery::mock('alias:Illuminate\Support\Facades\Config');
$this->cookie_mock = Mockery::mock('alias:Illuminate\Support\Facades\Cookie');
$this->crypt_mock = Mockery::mock('alias:Illuminate\Support\Facades\Crypt');
$this->log_mock = Mockery::mock('alias:Illuminate\Support\Facades\Log');
// Log calls are always allowed
$this->log_mock->shouldReceive('debug')->zeroOrMoreTimes();
$this->log_mock->shouldReceive('debug_msg')->zeroOrMoreTimes();
$this->service = new AuthService(
$mock_user_repository,
$mock_otp_repository,
$this->mock_principal_service,
$mock_user_service,
$this->mock_user_action_service,
$this->mock_cache_service,
$mock_auth_user_service,
$this->mock_security_context_service,
$mock_tx_service
);
}
private function mockGuestUser(): void
{
$this->auth_mock->shouldReceive('user')->andReturn(null);
$this->auth_mock->shouldReceive('check')->andReturn(false);
}
private function mockAuthenticatedUser(): Mockery\MockInterface
{
$user = Mockery::mock('Auth\User');
$user->shouldReceive('getId')->andReturn(42);
$this->auth_mock->shouldReceive('user')->andReturn($user);
$this->auth_mock->shouldReceive('check')->andReturn(true);
return $user;
}
private function expectSessionInvalidation(): void
{
$this->session_mock->shouldReceive('getId')->once()->andReturn('test-session-id');
$this->crypt_mock->shouldReceive('encrypt')->once()->with('test-session-id')->andReturn('encrypted-session-id');
$this->mock_cache_service
->expects($this->once())
->method('addSingleValue')
->with('encrypted-session-idinvalid', 'encrypted-session-id');
}
private function expectCoreLogoutCalls(bool $clear_security_ctx = true): void
{
$this->mock_principal_service->expects($this->once())->method('clear');
$this->auth_mock->shouldReceive('logout')->once();
if ($clear_security_ctx) {
$this->mock_security_context_service->expects($this->once())->method('clear');
} else {
$this->mock_security_context_service->expects($this->never())->method('clear');
}
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.example.com');
$this->cookie_mock->shouldReceive('queue')->once();
}
private function expectSessionFlushAndRegenerate(): void
{
$this->session_mock->shouldReceive('flush')->once();
$this->session_mock->shouldReceive('regenerate')->once();
}
/**
* Verify that logout() calls Session::flush() and Session::regenerate()
* when no user is logged in (guest context).
*/
public function testLogoutFlushesSessionForGuestUser(): void
{
$this->mockGuestUser();
$this->expectSessionInvalidation();
$this->expectCoreLogoutCalls();
$this->expectSessionFlushAndRegenerate();
$this->service->logout();
}
/**
* Verify that logout() calls Session::flush() and Session::regenerate()
* when an authenticated user is logged in.
*/
public function testLogoutFlushesSessionForAuthenticatedUser(): void
{
$this->mockAuthenticatedUser();
$this->mock_user_action_service
->expects($this->once())
->method('addUserAction');
$this->expectSessionInvalidation();
$this->expectCoreLogoutCalls();
$this->expectSessionFlushAndRegenerate();
$this->service->logout();
}
/**
* Verify that Session::flush() is called AFTER Auth::logout() to ensure
* the Laravel auth guard has already cleared its state before the session
* is destroyed. This ordering prevents Auth::logout() from operating
* on an empty session.
*/
public function testLogoutCallsFlushAfterAuthLogout(): void
{
$this->mockGuestUser();
$this->expectSessionInvalidation();
$this->mock_principal_service->expects($this->once())->method('clear');
$this->mock_security_context_service->expects($this->once())->method('clear');
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.example.com');
$this->cookie_mock->shouldReceive('queue')->once();
$call_order = [];
$this->auth_mock->shouldReceive('logout')->once()->andReturnUsing(function () use (&$call_order) {
$call_order[] = 'auth_logout';
});
$this->session_mock->shouldReceive('flush')->once()->andReturnUsing(function () use (&$call_order) {
$call_order[] = 'session_flush';
});
$this->session_mock->shouldReceive('regenerate')->once()->andReturnUsing(function () use (&$call_order) {
$call_order[] = 'session_regenerate';
});
$this->service->logout();
$this->assertEquals(['auth_logout', 'session_flush', 'session_regenerate'], $call_order);
}
/**
* Verify that Session::flush() is called AFTER invalidateSession()
* captures the session ID. If flush happened first, the session ID
* would be lost and the cache blacklist entry would be wrong.
*/
public function testLogoutCapturesSessionIdBeforeFlush(): void
{
$this->mockGuestUser();
$this->mock_principal_service->expects($this->once())->method('clear');
$this->mock_security_context_service->expects($this->once())->method('clear');
$this->auth_mock->shouldReceive('logout')->once();
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.example.com');
$this->cookie_mock->shouldReceive('queue')->once();
$session_id_captured = false;
$this->session_mock->shouldReceive('getId')->once()->andReturnUsing(function () use (&$session_id_captured) {
$session_id_captured = true;
return 'original-session-id';
});
$this->crypt_mock->shouldReceive('encrypt')->once()->with('original-session-id')->andReturn('encrypted-id');
$this->mock_cache_service
->expects($this->once())
->method('addSingleValue')
->with('encrypted-idinvalid', 'encrypted-id');
$this->session_mock->shouldReceive('flush')->once()->andReturnUsing(function () use (&$session_id_captured) {
$this->assertTrue($session_id_captured, 'Session::flush() was called before Session::getId()');
});
$this->session_mock->shouldReceive('regenerate')->once();
$this->service->logout();
}
/**
* Verify that when clear_security_ctx is false, the security context
* is NOT cleared but session flush still happens.
*/
public function testLogoutWithoutSecurityContextClearStillFlushesSession(): void
{
$this->mockGuestUser();
$this->expectSessionInvalidation();
$this->expectCoreLogoutCalls(clear_security_ctx: false);
$this->expectSessionFlushAndRegenerate();
$this->service->logout(clear_security_ctx: false);
}
/**
* Verify that the rps cookie is queued for deletion during logout.
* This ensures relying party tracking is cleaned up.
*/
public function testLogoutDeletesRpsCookie(): void
{
$this->mockGuestUser();
$this->expectSessionInvalidation();
$this->mock_principal_service->expects($this->once())->method('clear');
$this->mock_security_context_service->expects($this->once())->method('clear');
$this->auth_mock->shouldReceive('logout')->once();
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/test-path');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.test-domain.com');
$this->cookie_mock->shouldReceive('queue')->once()->with(
IAuthService::LOGGED_RELAYING_PARTIES_COOKIE_NAME,
null,
-2628000,
'/test-path',
'.test-domain.com',
true,
true,
false,
'none'
);
$this->expectSessionFlushAndRegenerate();
$this->service->logout();
}
/**
* Verify that principal_service->clear() is called during logout,
* which removes the op_bs cookie and session keys (user_id, auth_time, opbs).
*/
public function testLogoutClearsPrincipalService(): void
{
$this->mockGuestUser();
$this->expectSessionInvalidation();
$this->mock_principal_service
->expects($this->once())
->method('clear');
$this->mock_security_context_service->expects($this->once())->method('clear');
$this->auth_mock->shouldReceive('logout')->once();
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.example.com');
$this->cookie_mock->shouldReceive('queue')->once();
$this->expectSessionFlushAndRegenerate();
$this->service->logout();
}
/**
* Verify that user action logging captures the user ID and IP before
* session data is destroyed.
*/
public function testLogoutLogsUserActionBeforeSessionDestroyed(): void
{
$this->mockAuthenticatedUser();
$action_logged = false;
$session_flushed = false;
$user_action_mock = Mockery::mock('Models\UserAction');
$this->mock_user_action_service
->expects($this->once())
->method('addUserAction')
->willReturnCallback(function () use (&$action_logged, &$session_flushed, $user_action_mock) {
$this->assertFalse($session_flushed, 'User action must be logged before session flush');
$action_logged = true;
return $user_action_mock;
});
$this->expectSessionInvalidation();
$this->mock_principal_service->expects($this->once())->method('clear');
$this->mock_security_context_service->expects($this->once())->method('clear');
$this->auth_mock->shouldReceive('logout')->once();
$this->config_mock->shouldReceive('get')->with('session.path')->andReturn('/');
$this->config_mock->shouldReceive('get')->with('session.domain')->andReturn('.example.com');
$this->cookie_mock->shouldReceive('queue')->once();
$this->session_mock->shouldReceive('flush')->once()->andReturnUsing(function () use (&$session_flushed) {
$session_flushed = true;
});
$this->session_mock->shouldReceive('regenerate')->once();
$this->service->logout();
$this->assertTrue($action_logged, 'User action was never logged');
}
}