Files
XC_VM/tests/Unit/AuthRepositoryTest.php
T
Divarion_D 52d801acb2 test: dual-backend TestDb (SQLite + MariaDB) + skip-on-panel guards
TestDb can now run the DB-touching unit tests against a real MariaDB as
well as the default in-memory SQLite, so the suite can be exercised on
the panel host (which ships pdo_mysql, not pdo_sqlite). When
XCVM_TEST_DB_DSN is set it connects there and translates the SQLite test
DDL on the fly: AUTOINCREMENT -> AUTO_INCREMENT, a bare INTEGER PRIMARY
KEY gains AUTO_INCREMENT, and each CREATE TABLE is preceded by DROP TABLE
IF EXISTS (a MariaDB schema persists across the per-test connections that
:memory: starts fresh). DDL is routed through exec() on both backends so
the ModuleMigrator path (which runs DDL via query()) works too, and the
MySQL session uses a permissive sql_mode to match SQLite's leniency.
AuthRepositoryTest back-quotes the reserved column `key`.

Three env-fragile guards are tagged #[Group('skip-on-panel')] so the
deployed-panel run can exclude them (--exclude-group skip-on-panel):
ArchitectureTest and StreamTokenCallSitesTest scan the repo src/ tree
(absent / polluted in a flat deploy; they also self-skip when it is
missing), and LoginSessionFixationTest runs in isolated child processes
that the panel's ionCube/OPcache PHP cannot reconstitute.

Verified green on all three backends: SQLite (local, 721), MariaDB 11.4
(container, 721), and the panel's bundled PHP 8.1 + server MariaDB
(713, with the group excluded).
2026-09-13 21:17:06 +03:00

79 lines
2.6 KiB
PHP

<?php
use XcVm\Core\Auth\AuthRepository;
use PHPUnit\Framework\TestCase;
/**
* AuthRepository — access-code and HMAC-key reads/deletes. Covers the
* SQLite-compatible query methods against an in-memory database injected via
* the $db global. (getGroupPermissions uses MySQL JSON_CONTAINS and deleteCode
* regenerates nginx configs, so both are left to integration tests.)
*/
final class AuthRepositoryTest extends TestCase {
private TestDb $db;
private array $serverBackup;
protected function setUp(): void {
$this->serverBackup = $_SERVER;
$this->db = new TestDb();
$this->db->exec(
'CREATE TABLE access_codes (id INTEGER PRIMARY KEY, type INTEGER, code TEXT);
CREATE TABLE hmac_keys (id INTEGER PRIMARY KEY, `key` TEXT);
INSERT INTO access_codes (id, type, code) VALUES (1, 0, "alpha"), (2, 1, "beta"), (3, 0, "gamma");
INSERT INTO hmac_keys (id, `key`) VALUES (7, "k7"), (8, "k8");'
);
$GLOBALS['db'] = $this->db;
}
protected function tearDown(): void {
$_SERVER = $this->serverBackup;
unset($GLOBALS['db']);
}
public function testGetAllCodesKeyedById(): void {
$codes = AuthRepository::getAllCodes();
$this->assertSame([1, 2, 3], array_keys($codes));
$this->assertSame('beta', $codes[2]['code']);
}
public function testGetAllCodesFilteredByType(): void {
$codes = AuthRepository::getAllCodes(0);
$this->assertSame([1, 3], array_keys($codes), 'only type 0');
}
public function testGetCodeByIdReturnsRowOrNull(): void {
$this->assertSame('alpha', AuthRepository::getCodeById(1)['code']);
$this->assertNull(AuthRepository::getCodeById(999));
}
public function testGetCurrentCodeReadsXcCodeServerParam(): void {
$_SERVER['XC_CODE'] = 'gamma';
$this->assertSame('gamma', AuthRepository::getCurrentCode());
$row = AuthRepository::getCurrentCode(true);
$this->assertSame(3, (int) $row['id'], 'full row when $rInfo = true');
$_SERVER['XC_CODE'] = 'no_such_code';
$this->assertNull(AuthRepository::getCurrentCode(true));
}
public function testGetAllHmacKeyedById(): void {
$keys = AuthRepository::getAllHMAC();
$this->assertSame([7, 8], array_keys($keys));
$this->assertSame('k7', $keys[7]['key']);
}
public function testGetHmacByIdReturnsRowOrNull(): void {
$this->assertSame('k8', AuthRepository::getHMACById(8)['key']);
$this->assertNull(AuthRepository::getHMACById(999));
}
public function testDeleteHmacRemovesExistingAndReportsMissing(): void {
$this->assertTrue(AuthRepository::deleteHMAC(7));
$this->assertNull(AuthRepository::getHMACById(7), 'gone after delete');
$this->assertFalse(AuthRepository::deleteHMAC(7), 'already deleted');
$this->assertFalse(AuthRepository::deleteHMAC(999));
}
}