PoliciesControllerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Admin;
  4. use App\Domain\Auth\Role;
  5. use App\Domain\Auth\TokenKind;
  6. use App\Tests\Integration\Support\AppTestCase;
  7. /**
  8. * Covers SPEC §M07.2: policy CRUD + preview, RBAC split (Viewer reads,
  9. * Admin writes), threshold replacement on PATCH, 409 on delete with
  10. * referencing consumers.
  11. */
  12. final class PoliciesControllerTest extends AppTestCase
  13. {
  14. public function testViewerCanListSeededPolicies(): void
  15. {
  16. $token = $this->createToken(TokenKind::Admin, role: Role::Viewer);
  17. $response = $this->request('GET', '/api/v1/admin/policies', [
  18. 'Authorization' => 'Bearer ' . $token,
  19. ]);
  20. self::assertSame(200, $response->getStatusCode());
  21. $body = $this->decode($response);
  22. self::assertSame(3, $body['total']); // strict, moderate, paranoid
  23. $names = array_map(static fn (array $p): string => $p['name'], $body['items']);
  24. self::assertContains('strict', $names);
  25. self::assertContains('moderate', $names);
  26. self::assertContains('paranoid', $names);
  27. }
  28. public function testShowIncludesThresholdsKeyedBySlug(): void
  29. {
  30. $token = $this->createToken(TokenKind::Admin, role: Role::Viewer);
  31. $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :n', ['n' => 'moderate']);
  32. $response = $this->request('GET', "/api/v1/admin/policies/{$policyId}", [
  33. 'Authorization' => 'Bearer ' . $token,
  34. ]);
  35. self::assertSame(200, $response->getStatusCode());
  36. $body = $this->decode($response);
  37. self::assertSame('moderate', $body['name']);
  38. self::assertNotEmpty($body['thresholds']);
  39. $slugs = array_map(static fn (array $t): string => $t['category_slug'], $body['thresholds']);
  40. self::assertContains('brute_force', $slugs);
  41. }
  42. public function testViewerCannotCreate(): void
  43. {
  44. $token = $this->createToken(TokenKind::Admin, role: Role::Viewer);
  45. $response = $this->request(
  46. 'POST',
  47. '/api/v1/admin/policies',
  48. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  49. json_encode([
  50. 'name' => 'aggressive',
  51. 'thresholds' => ['brute_force' => 0.5],
  52. ]) ?: null,
  53. );
  54. self::assertSame(403, $response->getStatusCode());
  55. }
  56. public function testAdminCanCreatePolicyWithThresholds(): void
  57. {
  58. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  59. $response = $this->request(
  60. 'POST',
  61. '/api/v1/admin/policies',
  62. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  63. json_encode([
  64. 'name' => 'aggressive',
  65. 'description' => 'block everything',
  66. 'include_manual_blocks' => true,
  67. 'thresholds' => [
  68. 'brute_force' => 0.1,
  69. 'spam' => 0.2,
  70. ],
  71. ]) ?: null,
  72. );
  73. self::assertSame(201, $response->getStatusCode());
  74. $body = $this->decode($response);
  75. self::assertSame('aggressive', $body['name']);
  76. self::assertCount(2, $body['thresholds']);
  77. }
  78. public function testCreateRejectsDuplicateName(): void
  79. {
  80. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  81. $response = $this->request(
  82. 'POST',
  83. '/api/v1/admin/policies',
  84. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  85. json_encode(['name' => 'moderate', 'thresholds' => ['brute_force' => 1.0]]) ?: null,
  86. );
  87. self::assertSame(400, $response->getStatusCode());
  88. $details = $this->decode($response)['details'];
  89. self::assertArrayHasKey('name', $details);
  90. }
  91. public function testCreateRejectsUnknownCategorySlug(): void
  92. {
  93. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  94. $response = $this->request(
  95. 'POST',
  96. '/api/v1/admin/policies',
  97. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  98. json_encode(['name' => 'bogus', 'thresholds' => ['unknown_slug' => 1.0]]) ?: null,
  99. );
  100. self::assertSame(400, $response->getStatusCode());
  101. $details = $this->decode($response)['details'];
  102. self::assertStringContainsString('unknown_slug', (string) $details['thresholds']);
  103. }
  104. public function testPatchReplacesThresholdsWholesale(): void
  105. {
  106. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  107. $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :n', ['n' => 'moderate']);
  108. $response = $this->request(
  109. 'PATCH',
  110. "/api/v1/admin/policies/{$policyId}",
  111. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  112. json_encode(['thresholds' => ['brute_force' => 5.0]]) ?: null,
  113. );
  114. self::assertSame(200, $response->getStatusCode());
  115. $body = $this->decode($response);
  116. self::assertCount(1, $body['thresholds']);
  117. self::assertSame('brute_force', $body['thresholds'][0]['category_slug']);
  118. self::assertEqualsWithDelta(5.0, $body['thresholds'][0]['threshold'], 0.0001);
  119. }
  120. public function testDeleteWithReferencingConsumerReturns409(): void
  121. {
  122. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  123. $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :n', ['n' => 'moderate']);
  124. $this->db->insert('consumers', [
  125. 'name' => 'fw-edge',
  126. 'policy_id' => $policyId,
  127. 'is_active' => 1,
  128. ]);
  129. $response = $this->request('DELETE', "/api/v1/admin/policies/{$policyId}", [
  130. 'Authorization' => 'Bearer ' . $token,
  131. ]);
  132. self::assertSame(409, $response->getStatusCode());
  133. $body = $this->decode($response);
  134. self::assertSame('policy_in_use', $body['error']);
  135. self::assertNotEmpty($body['consumers']);
  136. }
  137. public function testDeleteSucceedsWithoutReferences(): void
  138. {
  139. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  140. $createResp = $this->request(
  141. 'POST',
  142. '/api/v1/admin/policies',
  143. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  144. json_encode(['name' => 'tmp', 'thresholds' => ['brute_force' => 1.0]]) ?: null,
  145. );
  146. $newId = (int) $this->decode($createResp)['id'];
  147. $deleteResp = $this->request('DELETE', "/api/v1/admin/policies/{$newId}", [
  148. 'Authorization' => 'Bearer ' . $token,
  149. ]);
  150. self::assertSame(204, $deleteResp->getStatusCode());
  151. }
  152. public function testPreviewReturnsCountSampleAndPolicyName(): void
  153. {
  154. $token = $this->createToken(TokenKind::Admin, role: Role::Viewer);
  155. $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :n', ['n' => 'paranoid']);
  156. $response = $this->request('GET', "/api/v1/admin/policies/{$policyId}/preview", [
  157. 'Authorization' => 'Bearer ' . $token,
  158. ]);
  159. self::assertSame(200, $response->getStatusCode());
  160. $body = $this->decode($response);
  161. self::assertArrayHasKey('count', $body);
  162. self::assertArrayHasKey('sample', $body);
  163. self::assertSame('paranoid', $body['policy']);
  164. self::assertIsArray($body['sample']);
  165. }
  166. }