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); + } +}