Files
XC_VM/tests/Unit/AuthRepositoryTest.php
T
Divarion_D 9e118da975 test: run the unit suite on MariaDB only, and on the panel's own stack
The suite ran on in-memory SQLite, an engine production never uses: it hid
column lengths, MySQL-only syntax and type coercion (the identity bug fixed in
the previous commit passed CI that way). SQLite is gone:

- TestDb connects only to MariaDB/MySQL (XCVM_TEST_DB_DSN/_USER/_PASS; root over
  the local socket, else 127.0.0.1:3306). Each instance gets a throwaway
  schema, dropped with it, in the installer's sql_mode, with an explicit
  connect timeout (a boot under test can zero default_socket_timeout).
- InstallSchema and the tests take the production DDL as is: the SQLite
  reducers, the keys they re-added, the faked information_schema and the
  sqliteCreateFunction() stand-ins are removed; test DDL is MySQL DDL.
- ReplicaBootTest, DbConnectRefusalTest and LazyDatabaseHandlerTest reach the
  test server instead of SQLite files; RecordingFinalizer no longer swallows
  an information_schema failure "for tests on SQLite".
- `make test-db` starts a throwaway mariadb:11.4 (tmpfs) for a machine without
  a server; the CI PHPUnit job gets the same as a service.

The suite also runs on a panel host with the bundled PHP and its own php.ini:
tests reach the code through MAIN_HOME, not `<repo>/src`; repository checks
and process-isolated tests (ionCube Loader + OPcache CLI segfault on a script
read from stdin) are in the skip-on-panel group; xcvm_test_child_php() starts
a child that fakes \XC_VM without the real extension; Redis-down tests use
RedisManager::useConnector() instead of an eval'd \XC_VM.

Stale opt-in tests fixed on the way: the real-agent provisioning test answers
the agent-started check, the extension integration test sends the typed starts
and reads the binary signature raw, a fake producer starts as PHP_BINARY, and
the certbot test waits for the line its background command writes.
2026-10-04 16:12:23 +03:00

78 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
* query methods against the TestDb 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 AUTO_INCREMENT, type INTEGER, code TEXT);
CREATE TABLE hmac_keys (id INTEGER PRIMARY KEY AUTO_INCREMENT, `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));
}
}