Skip to content

Commit 917003e

Browse files
authored
Add test class for Authenticate middleware (#1115)
Add missing test for Authenticate middleware to ensure nothing is broken when we update packages. Bug: T424471
1 parent 18fe2d2 commit 917003e

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Tests\Middleware;
4+
5+
use App\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Artisan;
9+
use Illuminate\Support\Facades\Config;
10+
use Illuminate\Support\Facades\Route;
11+
use Tests\TestCase;
12+
13+
class AuthenticateTest extends TestCase {
14+
use RefreshDatabase;
15+
16+
private const ENDPOINT = '/api/test/authenticate-middleware';
17+
18+
protected function setUp(): void {
19+
parent::setUp();
20+
21+
Artisan::call('passport:install', ['--no-interaction' => true]);
22+
23+
// Register new test route with Authenticate middleware. This also tests the config in Kernel.php and auth.php.
24+
Route::middleware('auth:api')->get(self::ENDPOINT, function (Request $request) {
25+
return response()->json([
26+
'email' => $request->user()->email,
27+
]);
28+
});
29+
}
30+
31+
public function testReturnsCustomJsonWhenUnauthenticated(): void {
32+
$this->json('GET', self::ENDPOINT)
33+
->assertStatus(401)
34+
->assertJson(['error' => 'Unauthenticated.']);
35+
}
36+
37+
public function testAuthenticatesUsingPassportTokenFromCookie(): void {
38+
$user = User::factory()->create();
39+
40+
$this->withCredentials()
41+
->withUnencryptedCookie(Config::get('auth.cookies.key'), $this->issueTokenFor($user))
42+
->json('GET', self::ENDPOINT)
43+
->assertStatus(200)
44+
->assertJson(['email' => $user->email]);
45+
}
46+
47+
public function testFailsUsingInvalidPassportTokenFromCookie(): void {
48+
$this->withCredentials()
49+
->withUnencryptedCookie(Config::get('auth.cookies.key'), 'this is an invalid token')
50+
->json('GET', self::ENDPOINT)
51+
->assertStatus(401)
52+
->assertJson(['error' => 'Unauthenticated.']);
53+
}
54+
55+
public function testAuthenticatesUsingPassportTokenFromAuthorizationHeader(): void {
56+
$user = User::factory()->create();
57+
58+
$this->withCredentials()
59+
->withHeader('Authorization', 'Bearer ' . $this->issueTokenFor($user))
60+
->json('GET', self::ENDPOINT)
61+
->assertStatus(200)
62+
->assertJson(['email' => $user->email]);
63+
}
64+
65+
public function testFailsUsingInvalidPassportTokenFromAuthorizationHeader(): void {
66+
$this->withCredentials()
67+
->withHeader('Authorization', 'Bearer ' . 'this is an invalid token')
68+
->json('GET', self::ENDPOINT)
69+
->assertStatus(401)
70+
->assertJson(['error' => 'Unauthenticated.']);
71+
}
72+
73+
private function issueTokenFor(User $user): string {
74+
return $user->createToken('authenticate-middleware-test')->accessToken;
75+
}
76+
}

0 commit comments

Comments
 (0)