| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Integration\Admin;
- use App\Domain\Auth\Role;
- use App\Domain\Auth\TokenKind;
- use App\Tests\Integration\Support\AppTestCase;
- final class ConsumersControllerTest extends AppTestCase
- {
- public function testCreateAndListConsumer(): void
- {
- $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
- $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :name', ['name' => 'moderate']);
- $created = $this->request(
- 'POST',
- '/api/v1/admin/consumers',
- ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
- json_encode(['name' => 'fw-edge-01', 'policy_id' => $policyId]) ?: null,
- );
- self::assertSame(201, $created->getStatusCode());
- $body = $this->decode($created);
- self::assertSame('fw-edge-01', $body['name']);
- self::assertSame($policyId, $body['policy_id']);
- $list = $this->request('GET', '/api/v1/admin/consumers', [
- 'Authorization' => 'Bearer ' . $token,
- ]);
- self::assertSame(200, $list->getStatusCode());
- $listBody = $this->decode($list);
- self::assertGreaterThan(0, $listBody['total']);
- }
- public function testCreateRejectsUnknownPolicy(): void
- {
- $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
- $resp = $this->request(
- 'POST',
- '/api/v1/admin/consumers',
- ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
- json_encode(['name' => 'bogus', 'policy_id' => 99999]) ?: null,
- );
- self::assertSame(400, $resp->getStatusCode());
- self::assertArrayHasKey('policy_id', $this->decode($resp)['details']);
- }
- public function testPatchTogglesAuditEnabled(): void
- {
- $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
- $policyId = (int) $this->db->fetchOne('SELECT id FROM policies WHERE name = :name', ['name' => 'moderate']);
- $created = $this->request(
- 'POST',
- '/api/v1/admin/consumers',
- ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
- json_encode(['name' => 'fw-audit-toggle', 'policy_id' => $policyId]) ?: null,
- );
- self::assertSame(201, $created->getStatusCode());
- $body = $this->decode($created);
- self::assertTrue($body['audit_enabled']);
- $consumerId = (int) $body['id'];
- $patch = $this->request(
- 'PATCH',
- "/api/v1/admin/consumers/{$consumerId}",
- ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json'],
- json_encode(['audit_enabled' => false]) ?: null,
- );
- self::assertSame(200, $patch->getStatusCode());
- self::assertFalse($this->decode($patch)['audit_enabled']);
- }
- }
|