postJson('/api/auth/register', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => 'password123', 'password_confirmation' => 'password123', ]); $response->assertStatus(201) ->assertJsonStructure([ 'success', 'message', 'data' => [ 'user' => ['id', 'name', 'email'], 'token', ] ]) ->assertJson(['success' => true]); $this->assertDatabaseHas('users', [ 'email' => 'john@example.com', 'name' => 'John Doe', ]); } /** * Test registration fails with invalid email. */ public function test_registration_fails_with_invalid_email() { $response = $this->postJson('/api/auth/register', [ 'name' => 'John Doe', 'email' => 'invalid-email', 'password' => 'password123', 'password_confirmation' => 'password123', ]); $response->assertStatus(422) ->assertJsonValidationErrors('email'); } /** * Test registration fails with duplicate email. */ public function test_registration_fails_with_duplicate_email() { User::factory()->create(['email' => 'john@example.com']); $response = $this->postJson('/api/auth/register', [ 'name' => 'Jane Doe', 'email' => 'john@example.com', 'password' => 'password123', 'password_confirmation' => 'password123', ]); $response->assertStatus(422) ->assertJsonValidationErrors('email'); } /** * Test registration fails with mismatched passwords. */ public function test_registration_fails_with_mismatched_passwords() { $response = $this->postJson('/api/auth/register', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => 'password123', 'password_confirmation' => 'different123', ]); $response->assertStatus(422) ->assertJsonValidationErrors('password'); } /** * Test successful user login. */ public function test_user_can_login() { $user = User::factory()->create([ 'email' => 'john@example.com', 'password' => bcrypt('password123'), ]); $response = $this->postJson('/api/auth/login', [ 'email' => 'john@example.com', 'password' => 'password123', ]); $response->assertStatus(200) ->assertJsonStructure([ 'success', 'message', 'data' => [ 'user' => ['id', 'name', 'email'], 'token', ] ]) ->assertJson(['success' => true]); } /** * Test login fails with invalid credentials. */ public function test_login_fails_with_invalid_credentials() { User::factory()->create([ 'email' => 'john@example.com', 'password' => bcrypt('password123'), ]); $response = $this->postJson('/api/auth/login', [ 'email' => 'john@example.com', 'password' => 'wrongpassword', ]); $response->assertStatus(401) ->assertJson(['success' => false]); } /** * Test login fails with nonexistent user. */ public function test_login_fails_with_nonexistent_user() { $response = $this->postJson('/api/auth/login', [ 'email' => 'nonexistent@example.com', 'password' => 'password123', ]); $response->assertStatus(401) ->assertJson(['success' => false]); } /** * Test get current user returns authenticated user. */ public function test_get_user_returns_authenticated_user() { $user = User::factory()->create(); $token = $user->createToken('AppToken')->accessToken; $response = $this->getJson('/api/user', [ 'Authorization' => "Bearer $token", ]); $response->assertStatus(200) ->assertJsonStructure([ 'success', 'message', 'data' => ['id', 'name', 'email'], ]) ->assertJson([ 'success' => true, 'data' => [ 'id' => $user->id, 'email' => $user->email, ] ]); } /** * Test get user fails without authentication. */ public function test_get_user_fails_without_authentication() { $response = $this->getJson('/api/user'); $response->assertStatus(401); } /** * Test successful logout. */ public function test_user_can_logout() { $user = User::factory()->create(); $token = $user->createToken('AppToken')->accessToken; $response = $this->postJson('/api/logout', [], [ 'Authorization' => "Bearer $token", ]); $response->assertStatus(200) ->assertJson(['success' => true]); } /** * Test logout fails without authentication. */ public function test_logout_fails_without_authentication() { $response = $this->postJson('/api/logout'); $response->assertStatus(401); } }