*/ public static function mutatingRouteProvider(): array { return [ 'create about me' => ['POST', '/api/website/about-me'], 'update about me' => ['PUT', '/api/website/about-me/1'], 'delete about me' => ['DELETE', '/api/website/about-me/1'], 'create project' => ['POST', '/api/website/projects'], 'update project' => ['PUT', '/api/website/projects/1'], 'delete project' => ['DELETE', '/api/website/projects/1'], 'create post' => ['POST', '/api/blog/posts'], 'update post' => ['PUT', '/api/blog/posts/1'], 'delete post' => ['DELETE', '/api/blog/posts/1'], 'create category' => ['POST', '/api/blog/categories'], 'update category' => ['PUT', '/api/blog/categories/1'], 'delete category' => ['DELETE', '/api/blog/categories/1'], 'create tag' => ['POST', '/api/blog/tags'], 'update tag' => ['PUT', '/api/blog/tags/1'], 'delete tag' => ['DELETE', '/api/blog/tags/1'], ]; } #[DataProvider('mutatingRouteProvider')] public function test_anonymous_requests_cannot_reach_write_endpoints(string $method, string $uri): void { $this->call($method, $uri)->assertUnauthorized(); } #[DataProvider('mutatingRouteProvider')] public function test_authenticated_non_admins_cannot_reach_write_endpoints(string $method, string $uri): void { Sanctum::actingAs(User::factory()->create()); $this->call($method, $uri)->assertForbidden(); } public function test_public_reads_remain_available_anonymously(): void { $this->getJson('/api/website/about-me')->assertOk(); $this->getJson('/api/website/services/fa')->assertOk(); $this->getJson('/api/blog/posts')->assertOk(); $this->getJson('/api/blog/categories')->assertOk(); $this->getJson('/api/blog/tags')->assertOk(); } public function test_user_email_endpoint_is_no_longer_public(): void { $user = User::factory()->create(); $this->getJson("/user/{$user->id}")->assertNotFound(); } public function test_admin_can_log_in_and_receives_a_session_cookie(): void { $admin = User::factory()->admin()->create([ 'email' => 'admin@parsaaghayi.ir', 'password' => 'correct-horse-battery', ]); $response = $this->postJson('/api/auth/login', [ 'email' => 'admin@parsaaghayi.ir', 'password' => 'correct-horse-battery', ]); $response->assertOk() ->assertJsonPath('user.email', 'admin@parsaaghayi.ir') ->assertCookie(AuthController::SESSION_COOKIE); // The token must never be readable by client-side JavaScript. $cookie = $response->headers->getCookies()[0] ?? null; $this->assertNotNull($cookie); $this->assertTrue($cookie->isHttpOnly()); $this->assertArrayNotHasKey('token', $response->json()); $this->assertSame(1, $admin->tokens()->count()); } public function test_login_rejects_a_valid_non_admin_account(): void { User::factory()->create([ 'email' => 'user@parsaaghayi.ir', 'password' => 'correct-horse-battery', ]); $this->postJson('/api/auth/login', [ 'email' => 'user@parsaaghayi.ir', 'password' => 'correct-horse-battery', ])->assertUnprocessable(); $this->assertDatabaseCount('personal_access_tokens', 0); } public function test_login_rejects_a_bad_password_without_revealing_which_field_failed(): void { User::factory()->admin()->create([ 'email' => 'admin@parsaaghayi.ir', 'password' => 'correct-horse-battery', ]); $wrongPassword = $this->postJson('/api/auth/login', [ 'email' => 'admin@parsaaghayi.ir', 'password' => 'wrong-password', ]); $unknownAccount = $this->postJson('/api/auth/login', [ 'email' => 'nobody@parsaaghayi.ir', 'password' => 'wrong-password', ]); $wrongPassword->assertUnprocessable(); $unknownAccount->assertUnprocessable(); // Identical error shape, so the endpoint cannot be used to enumerate // which email addresses have accounts. $this->assertSame( array_keys($wrongPassword->json('errors')), array_keys($unknownAccount->json('errors')) ); } public function test_auth_me_requires_authentication(): void { $this->getJson('/api/auth/me')->assertUnauthorized(); } public function test_session_cookie_authenticates_requests(): void { User::factory()->admin()->create([ 'email' => 'admin@parsaaghayi.ir', 'password' => 'password', ]); $login = $this->postJson('/api/auth/login', [ 'email' => 'admin@parsaaghayi.ir', 'password' => 'password', ]); $token = $login->headers->getCookies()[0]->getValue(); // No Authorization header: the cookie alone must authenticate, which is // how the Next.js proxy will call the API. $this->withCookie(AuthController::SESSION_COOKIE, $token) ->getJson('/api/auth/me') ->assertOk() ->assertJsonPath('user.is_admin', true); } public function test_logout_revokes_the_admin_panel_token(): void { $this->actingAsAdmin(); $this->postJson('/api/auth/logout')->assertOk(); $this->assertDatabaseCount('personal_access_tokens', 0); } }