1
0

ConsumersControllerTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. final class ConsumersControllerTest extends AppTestCase
  8. {
  9. public function testCreateAndListConsumer(): void
  10. {
  11. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  12. $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :name', ['name' => 'moderate']);
  13. $created = $this->request(
  14. 'POST',
  15. '/api/v1/admin/consumers',
  16. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  17. json_encode(['name' => 'fw-edge-01', 'policy_id' => $policyId]) ?: null,
  18. );
  19. self::assertSame(201, $created->getStatusCode());
  20. $body = $this->decode($created);
  21. self::assertSame('fw-edge-01', $body['name']);
  22. self::assertSame($policyId, $body['policy_id']);
  23. $list = $this->request('GET', '/api/v1/admin/consumers', [
  24. 'Authorization' => 'Bearer ' . $token,
  25. ]);
  26. self::assertSame(200, $list->getStatusCode());
  27. $listBody = $this->decode($list);
  28. self::assertGreaterThan(0, $listBody['total']);
  29. }
  30. public function testCreateRejectsUnknownPolicy(): void
  31. {
  32. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  33. $resp = $this->request(
  34. 'POST',
  35. '/api/v1/admin/consumers',
  36. ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
  37. json_encode(['name' => 'bogus', 'policy_id' => 99999]) ?: null,
  38. );
  39. self::assertSame(400, $resp->getStatusCode());
  40. self::assertArrayHasKey('policy_id', $this->decode($resp)['details']);
  41. }
  42. }