Skip to content

Commit b83e185

Browse files
committed
Improve API authentication tests to properly verify fix
- Override default stateful domains (exclude localhost) in key tests - Verify session middleware is actually applied via hasSession() checks - Use production-like domains (production.example.com) instead of localhost - Tests now properly fail when fix is disabled (verified 3 failures) - All 294 tests pass with fix enabled
1 parent a7a1337 commit b83e185

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

tests/Feature/Authorization/ApiAuthenticationTest.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,21 @@
2525
});
2626

2727
test('authentication works when APP_URL is empty using same-domain fallback', function () {
28-
config(['app.url' => '']);
28+
config([
29+
'app.url' => '',
30+
'log-viewer.api_stateful_domains' => [], // Override to exclude localhost
31+
]);
2932

30-
LogViewer::auth(fn ($request) => true);
33+
// Auth callback that requires session to be started (proving session middleware was applied)
34+
LogViewer::auth(function ($request) {
35+
if (! $request->hasSession() || ! $request->session()->isStarted()) {
36+
return false;
37+
}
38+
return true;
39+
});
3140

32-
$response = getJson(route('log-viewer.folders'), [
33-
'referer' => 'http://localhost/',
41+
$response = getJson('http://production.example.com/log-viewer/api/folders', [
42+
'referer' => 'http://production.example.com/',
3443
]);
3544

3645
$response->assertOk();
@@ -72,9 +81,18 @@
7281
});
7382

7483
test('same-domain requests work without APP_URL configured', function () {
75-
config(['app.url' => null]);
84+
config([
85+
'app.url' => null,
86+
'log-viewer.api_stateful_domains' => [], // Override to exclude localhost
87+
]);
7688

77-
LogViewer::auth(fn ($request) => true);
89+
// Auth callback that requires session to be started (proving session middleware was applied)
90+
LogViewer::auth(function ($request) {
91+
if (! $request->hasSession() || ! $request->session()->isStarted()) {
92+
return false;
93+
}
94+
return true;
95+
});
7896

7997
// Simulate request from same domain
8098
$response = getJson('http://production.example.com/log-viewer/api/folders', [
@@ -85,13 +103,22 @@
85103
});
86104

87105
test('same-domain requests with custom port work without APP_URL', function () {
88-
config(['app.url' => null]);
106+
config([
107+
'app.url' => null,
108+
'log-viewer.api_stateful_domains' => [], // Override to exclude localhost
109+
]);
89110

90-
LogViewer::auth(fn ($request) => true);
111+
// Auth callback that requires session to be started (proving session middleware was applied)
112+
LogViewer::auth(function ($request) {
113+
if (! $request->hasSession() || ! $request->session()->isStarted()) {
114+
return false;
115+
}
116+
return true;
117+
});
91118

92119
// Simulate request from same domain with custom port
93-
$response = getJson('http://localhost:8080/log-viewer/api/folders', [
94-
'referer' => 'http://localhost:8080/log-viewer',
120+
$response = getJson('http://production.example.com:8080/log-viewer/api/folders', [
121+
'referer' => 'http://production.example.com:8080/log-viewer',
95122
]);
96123

97124
$response->assertOk();

0 commit comments

Comments
 (0)