Files

78 lines
3.1 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Fixtures for tests/api/test_api.py: the "system" user that API writes run
* as, and four Bearer keys, written to a JSON file as {name: key}:
*
* thor, gandalf read_write, public tickets only (automated reporters)
* reader read, public tickets only
* allseeing read_write, sees every visibility level
*
* php tests/api/fixtures.php /tmp/api-keys.json
*
* CLI only, and only against a database whose name starts with tinker_ui (the
* .env tests/ui/stack.sh writes). Replaces every API key in that database.
*/
if (php_sapi_name() !== 'cli') {
http_response_code(403);
exit('CLI access only');
}
$root = dirname(__DIR__, 2);
require_once $root . '/config/config.php';
require_once $root . '/helpers/Database.php';
require_once $root . '/models/ApiKeyModel.php';
$out = $argv[1] ?? '';
if ($out === '') {
fwrite(STDERR, "usage: php tests/api/fixtures.php <keys.json>\n");
exit(2);
}
if (!str_starts_with((string)($GLOBALS['config']['DB_NAME'] ?? ''), 'tinker_ui')) {
fwrite(STDERR, "refusing: the configured database must start with tinker_ui\n");
exit(2);
}
$conn = Database::getConnection();
// Users: system (API writes), an admin, and two ordinary users for the MCP
// permission checks (tests/api/mcp_test.php).
foreach ([['system', 'System', 1], ['ci.tester', 'CI Tester', 1], ['alice', 'Alice', 0], ['bob', 'Bob', 0]] as [$name, $display, $admin]) {
$stmt = $conn->prepare(
"INSERT INTO users (username, display_name, email, is_admin) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE is_admin = VALUES(is_admin)"
);
$email = $name . '@example.invalid';
$stmt->bind_param('sssi', $name, $display, $email, $admin);
$stmt->execute();
}
$uid = fn(string $name): int => (int)$conn->query(
"SELECT user_id FROM users WHERE username = '" . $conn->real_escape_string($name) . "'"
)->fetch_row()[0];
$creator = $uid('system');
// A confidential ticket only ci.tester (its creator, an admin) can see, and a
// system comment (no user_id) like the ones automated reporters leave.
$admin = $uid('ci.tester');
$conn->query("DELETE FROM ticket_comments WHERE ticket_id = '100000009'");
$conn->query("DELETE FROM tickets WHERE ticket_id = '100000009'");
$conn->query(
"INSERT INTO tickets (ticket_id, title, description, status, priority, category, type, created_by, visibility)
VALUES ('100000009', '[test-node-09][manual][security] Confidential fixture', 'fixture', 'Open', 3,
'Security', 'Task', $admin, 'confidential')"
);
$conn->query(
"INSERT INTO ticket_comments (ticket_id, user_id, user_name, comment_text, markdown_enabled)
VALUES ('100000001', NULL, 'hwmonDaemon', 'System comment fixture', 0)"
);
$conn->query("DELETE FROM api_keys");
$model = new ApiKeyModel($conn);
$keys = [];
foreach ([['thor', 'read_write', false], ['gandalf', 'read_write', false], ['reader', 'read', false], ['allseeing', 'read_write', true]] as [$name, $scope, $seeAll]) {
$keys[$name] = $model->createKey($name, $creator, null, $scope, $seeAll)['api_key'];
}
file_put_contents($out, json_encode($keys));
chmod($out, 0600);
echo 'API keys: ' . implode(', ', array_keys($keys)) . " -> $out\n";