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']); } }