Unifi-Voucher-Tool/tests/UniFiControllerTest.php
Friederich Loheide a36dcbba27
All checks were successful
CI / PHP Lint (push) Successful in 37s
CI / PHP Lint-1 (push) Successful in 53s
CI / Unit Tests & Static Analysis (push) Successful in 40s
CI / PHP Lint (pull_request) Successful in 39s
CI / PHP Lint-1 (pull_request) Successful in 44s
CI / Unit Tests & Static Analysis (pull_request) Successful in 44s
Tests für Anmeldung und Controller, PHP 8.1, Sprach-Fallback
Bisher deckten die Tests nur Hilfsklassen ab. Neu sind die beiden
Stellen, an denen ein Fehler richtig weh tut:

- AuthTest (12 Fälle): richtige und falsche Anmeldung, Rate-Limit greift
  vor der Passwortprüfung, 2FA-Nutzer gilt vor dem Code ausdrücklich
  nicht als angemeldet, CSRF-Prüfung, und die Client-IP hinter einem
  Proxy – inklusive der Fälle, in denen X-Forwarded-For ignoriert werden
  muss
- UniFiControllerTest (8 Fälle): die Auswahl des eigenen Vouchers. Der
  Controller liefert Voucher unsortiert; wer den ersten nimmt, gibt bei
  parallelen Anfragen den Code eines anderen Gastes aus. Dazu QoS-Payload
  und die Fehlerfälle.

Dafür nötig: Auth nimmt die Datenbank optional über den Konstruktor
entgegen (ohne Argument unverändert), und UniFiController::apiRequest()
ist protected statt private, damit Tests aufgezeichnete Antworten
einspielen können. Beide Tests brauchen weder Datenbank noch Netz.

PHP-Anforderung von 7.4 auf 8.1 gehoben: Composer, CI-Matrix (8.1/8.3),
Docker-Image (8.3) und README. Die 7.4-Angabe war ohnehin ungeprüft.

I18n: fehlt ein Schlüssel in der gewählten Sprache, erscheint jetzt der
deutsche Text statt des technischen Schlüssels.

PHPStan deckt zusätzlich VoucherService und I18n ab.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 11:13:01 +00:00

152 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests;
use Exception;
use PHPUnit\Framework\TestCase;
use UniFiController;
require_once __DIR__ . '/../includes/UniFiController.php';
/**
* Controller mit eingespielten Antworten statt echter HTTP-Aufrufe.
* Die aufgezeichneten Antworten entsprechen dem Format der UniFi-API.
*/
class RecordedController extends UniFiController
{
/** @var array<int, array> */
public array $calls = [];
/** @var array<int, array|null> */
private array $responses;
public function __construct(array $responses)
{
parent::__construct('https://unifi.example.com:8443', 'api', 'geheim', 'default');
$this->responses = $responses;
}
protected function apiRequest($endpoint, $data = null, $method = 'POST')
{
$this->calls[] = ['endpoint' => $endpoint, 'data' => $data, 'method' => $method];
return array_shift($this->responses);
}
}
/**
* Die Auswahl des richtigen Vouchers ist der heikle Teil: der Controller
* liefert alle Voucher unsortiert zurück. Wer blind den ersten nimmt, gibt
* bei parallelen Anfragen den Code eines anderen Gastes aus.
*/
class UniFiControllerTest extends TestCase
{
private function created(int $time = 1000): array
{
return ['data' => [['create_time' => $time]]];
}
public function testPicksOwnVoucherNotAForeignOne(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [
['code' => '1111122222', 'note' => 'Fremder Gast', 'create_time' => 5000, '_id' => 'a'],
['code' => '3333344444', 'note' => '2026-09-24_Empfang', 'create_time' => 4000, '_id' => 'b'],
]],
]);
$result = $controller->createVoucher('2026-09-24_Empfang', 2, 480);
$this->assertSame('3333344444', $result['code'], 'Der Voucher mit passender Notiz gehört zu dieser Anfrage');
$this->assertSame('b', $result['unifi_id']);
}
public function testPicksNewestWhenNoteAppearsTwice(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [
['code' => '1111122222', 'note' => 'Empfang', 'create_time' => 4000, '_id' => 'alt'],
['code' => '3333344444', 'note' => 'Empfang', 'create_time' => 9000, '_id' => 'neu'],
]],
]);
$this->assertSame('neu', $controller->createVoucher('Empfang', 1, 60)['unifi_id']);
}
public function testFallsBackToNewestWhenNoteDoesNotMatch(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [
['code' => '1111122222', 'note' => 'Etwas anderes', 'create_time' => 4000, '_id' => 'alt'],
['code' => '3333344444', 'note' => 'Auch anders', 'create_time' => 9000, '_id' => 'neu'],
]],
]);
$this->assertSame('neu', $controller->createVoucher('Sonderzeichen …', 1, 60)['unifi_id']);
}
public function testFormatsCodeInFiveDigitBlocks(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
]);
$result = $controller->createVoucher('Gast', 1, 60);
$this->assertSame('12345-67890', $result['formatted_code']);
$this->assertSame('1234567890', $result['code'], 'Der rohe Code bleibt unverändert');
}
public function testFailsWhenControllerDoesNotConfirmCreation(): void
{
$controller = new RecordedController([['data' => []]]);
$this->expectException(Exception::class);
$controller->createVoucher('Gast', 1, 60);
}
public function testFailsWhenNoVouchersComeBack(): void
{
$controller = new RecordedController([$this->created(), ['data' => []]]);
$this->expectException(Exception::class);
$controller->createVoucher('Gast', 1, 60);
}
public function testSendsQosLimitsOnlyWhenSet(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
]);
$controller->createVoucher('Gast', 3, 240, ['down' => 20000, 'up' => 5000, 'quota_mb' => 1024]);
$payload = $controller->calls[0]['data'];
$this->assertSame('create-voucher', $payload['cmd']);
$this->assertSame(240, $payload['expire']);
$this->assertSame(3, $payload['quota']);
$this->assertTrue($payload['qos_overwrite']);
$this->assertSame(20000, $payload['down']);
$this->assertSame(5000, $payload['up']);
$this->assertSame(1024, $payload['bytes']);
}
public function testOmitsQosWhenNotRequested(): void
{
$controller = new RecordedController([
$this->created(),
['data' => [['code' => '1234567890', 'note' => 'Gast', 'create_time' => 1, '_id' => 'x']]],
]);
$controller->createVoucher('Gast', 1, 60);
$this->assertArrayNotHasKey('qos_overwrite', $controller->calls[0]['data']);
$this->assertArrayNotHasKey('down', $controller->calls[0]['data']);
}
}