From 7b58303cde4857cfd33cb71e998f1ff19bb89554 Mon Sep 17 00:00:00 2001 From: James Collins Date: Mon, 13 Mar 2023 12:31:20 +1000 Subject: [PATCH] added test --- tests/Feature/AuthEndpointTest.php | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/Feature/AuthEndpointTest.php diff --git a/tests/Feature/AuthEndpointTest.php b/tests/Feature/AuthEndpointTest.php new file mode 100644 index 0000000..e4dcc5f --- /dev/null +++ b/tests/Feature/AuthEndpointTest.php @@ -0,0 +1,52 @@ +create([ + 'password' => bcrypt('password'), + ]); + + // Test successful login + $response = $this->postJson('/api/login', [ + 'username' => $user->username, + 'password' => 'password', + ]); + $response->assertStatus(200); + $response->assertJsonStructure([ + 'token', + ]); + $token = $response->json('token'); + + // Test getting authenticated user + $response = $this->withHeaders([ + 'Authorization' => "Bearer $token", + ])->get('/api/me'); + $response->assertStatus(200); + $response->assertJson([ + 'user' => [ + 'id' => $user->id, + 'username' => $user->username, + ] + ]); + + // Test logout + $response = $this->withHeaders([ + 'Authorization' => "Bearer $token", + ])->postJson('/api/logout'); + $response->assertStatus(204); + + // Test failed login + $response = $this->postJson('/api/login', [ + 'username' => $user->username, + 'password' => 'wrongpassword', + ]); + $response->assertStatus(422); + } +}