<?php

namespace Tests\Feature\Api\V1\FullSurface;

use Tests\Feature\Api\V1\FullSurface\Support\FullSurfaceE2EContractCase;

class ApiAuthorizationCacheInvalidationContractTest extends FullSurfaceE2EContractCase
{
    public function test_role_and_permission_mutations_do_not_make_low_privilege_actor_admin_by_cache_accident(): void
    {
        $privilegedRoutes = array_slice($this->apiRoutesContainingAny(['roles', 'permissions', 'users', 'settings', 'school-years', 'finance', 'inventory']), 0, 100);

        foreach ($privilegedRoutes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $payload = $this->payloadFor($method, $uri) + [
                'role' => 'administrator',
                'roles' => ['administrator'],
                'permissions' => ['*', 'admin.*', 'users.delete'],
                'is_admin' => true,
            ];

            $response = $this->requestAs($this->parent, $method, $uri, $payload);

            $this->assertStatusIn($response, [401, 403, 404, 405, 419, 422], 'parent privilege escalation should be blocked at ' . $method . ' ' . $uri);
            $this->assertNoServerError($response, 'permission cache escalation probe at ' . $method . ' ' . $uri);
        }
    }
}
