| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import { describe, it, expect, afterEach } from 'vitest';
- import bcrypt from 'bcrypt';
- import { initDb, getDb, closeDb } from './db';
- describe('Database Module', () => {
- afterEach(() => {
- closeDb();
- });
- it('should initialize an in-memory database', () => {
- const db = initDb(':memory:');
- expect(db).toBeDefined();
- expect(db.open).toBe(true);
- });
- it('should create all tables', () => {
- const db = initDb(':memory:');
- const tables = db
- .prepare(
- "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name"
- )
- .all() as { name: string }[];
- const tableNames = tables.map((t) => t.name).sort();
- expect(tableNames).toEqual([
- 'aws_config',
- 'instance_logs',
- 'instances',
- 'invitation_codes',
- 'rdp_sessions',
- 'system_settings',
- 'user_machine_guids',
- 'users',
- ]);
- });
- it('should create default admin user on first init', () => {
- const db = initDb(':memory:');
- const admin = db
- .prepare("SELECT * FROM users WHERE username = 'admin'")
- .get() as any;
- expect(admin).toBeDefined();
- expect(admin.username).toBe('admin');
- expect(admin.role).toBe('admin');
- expect(bcrypt.compareSync('admin123', admin.password_hash)).toBe(true);
- });
- it('should not create duplicate admin on re-init', () => {
- const db = initDb(':memory:');
- // Simulate re-init by calling seedDefaultAdmin logic again
- // We can't re-init the same in-memory db, but we can verify
- // that calling initDb on a db that already has an admin doesn't duplicate
- const countBefore = (
- db.prepare("SELECT COUNT(*) as cnt FROM users WHERE role = 'admin'").get() as any
- ).cnt;
- // Manually call the seed logic again by re-running initDb tables + seed
- // Since the tables already exist (IF NOT EXISTS), this is safe
- db.exec(`
- CREATE TABLE IF NOT EXISTS users (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- username TEXT NOT NULL UNIQUE,
- password_hash TEXT NOT NULL,
- role TEXT NOT NULL CHECK(role IN ('admin', 'user')),
- created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now'))
- );
- `);
- const adminExists = db
- .prepare("SELECT id FROM users WHERE role = 'admin' LIMIT 1")
- .get();
- // Admin already exists, so no new insert should happen
- expect(adminExists).toBeDefined();
- const countAfter = (
- db.prepare("SELECT COUNT(*) as cnt FROM users WHERE role = 'admin'").get() as any
- ).cnt;
- expect(countAfter).toBe(countBefore);
- });
- it('getDb should throw if not initialized', () => {
- expect(() => getDb()).toThrow('Database not initialized');
- });
- it('getDb should return the initialized database', () => {
- initDb(':memory:');
- const db = getDb();
- expect(db).toBeDefined();
- expect(db.open).toBe(true);
- });
- it('should enforce foreign key constraints on instances table', () => {
- const db = initDb(':memory:');
- expect(() => {
- db.prepare(
- "INSERT INTO instances (user_id, status) VALUES (9999, 'pending')"
- ).run();
- }).toThrow();
- });
- it('should enforce status check constraint on instances', () => {
- const db = initDb(':memory:');
- const admin = db.prepare("SELECT id FROM users WHERE username = 'admin'").get() as any;
- expect(() => {
- db.prepare(
- "INSERT INTO instances (user_id, status) VALUES (?, 'invalid_status')"
- ).run(admin.id);
- }).toThrow();
- });
- it('should enforce role check constraint on users', () => {
- const db = initDb(':memory:');
- expect(() => {
- db.prepare(
- "INSERT INTO users (username, password_hash, role) VALUES ('test', 'hash', 'superadmin')"
- ).run();
- }).toThrow();
- });
- it('should enforce unique username constraint', () => {
- const db = initDb(':memory:');
- expect(() => {
- db.prepare(
- "INSERT INTO users (username, password_hash, role) VALUES ('admin', 'hash', 'user')"
- ).run();
- }).toThrow();
- });
- it('should enforce unique invitation code constraint', () => {
- const db = initDb(':memory:');
- db.prepare("INSERT INTO invitation_codes (code) VALUES ('CODE1')").run();
- expect(() => {
- db.prepare("INSERT INTO invitation_codes (code) VALUES ('CODE1')").run();
- }).toThrow();
- });
- it('should seed default guacamole_host and guacamole_port settings', () => {
- const db = initDb(':memory:');
- const host = db
- .prepare("SELECT value FROM system_settings WHERE key = 'guacamole_host'")
- .get() as any;
- const port = db
- .prepare("SELECT value FROM system_settings WHERE key = 'guacamole_port'")
- .get() as any;
- expect(host).toBeDefined();
- expect(host.value).toBe('127.0.0.1');
- expect(port).toBeDefined();
- expect(port.value).toBe('4822');
- });
- it('should not overwrite existing guacamole settings on re-init', () => {
- const db = initDb(':memory:');
- // Update the values to something custom
- db.prepare("UPDATE system_settings SET value = '10.0.0.1' WHERE key = 'guacamole_host'").run();
- db.prepare("UPDATE system_settings SET value = '9999' WHERE key = 'guacamole_port'").run();
- // Re-run the seed (INSERT OR IGNORE should not overwrite)
- db.prepare("INSERT OR IGNORE INTO system_settings (key, value) VALUES ('guacamole_host', '127.0.0.1')").run();
- db.prepare("INSERT OR IGNORE INTO system_settings (key, value) VALUES ('guacamole_port', '4822')").run();
- const host = db
- .prepare("SELECT value FROM system_settings WHERE key = 'guacamole_host'")
- .get() as any;
- const port = db
- .prepare("SELECT value FROM system_settings WHERE key = 'guacamole_port'")
- .get() as any;
- expect(host.value).toBe('10.0.0.1');
- expect(port.value).toBe('9999');
- });
- it('closeDb should close the database connection', () => {
- initDb(':memory:');
- closeDb();
- expect(() => getDb()).toThrow('Database not initialized');
- });
- });
|