Skip to content

Self-hosted, plugin-based server management.

2.4 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { RawSqliteUserEncryptionMigrationStore } from "../../utils/user-encryption-migration-store.js";

describe("RawSqliteUserEncryptionMigrationStore", () => {
  function createDb() {
    const all = vi.fn(() => [{ id: 1 }]);
    const get = vi.fn(() => ({ id: "user-1" }));
    const run = vi.fn();
    const prepare = vi.fn(() => ({ all, get, run }));

    return { db: { prepare }, all, get, run };
  }

  it("owns legacy user encryption migration reads", () => {
    const { db, all } = createDb();
    const store = new RawSqliteUserEncryptionMigrationStore(db);

    expect(store.listHostRecords("user-1")).toEqual([{ id: 1 }]);
    expect(store.listCredentialRecords("user-1")).toEqual([{ id: 1 }]);

    expect(db.prepare).toHaveBeenCalledWith(
      "SELECT * FROM ssh_data WHERE user_id = ?",
    );
    expect(db.prepare).toHaveBeenCalledWith(
      "SELECT * FROM ssh_credentials WHERE user_id = ?",
    );
    expect(all).toHaveBeenCalledWith("user-1");
  });

  it("owns legacy sensitive field update statements", () => {
    const { db, run } = createDb();
    const store = new RawSqliteUserEncryptionMigrationStore(db);

    store.updateHostSensitiveFields(12, {
      password: "p",
      key: "k",
      key_password: "kp",
      key_type: "ed25519",
      sudo_password: "sp",
    });
    store.updateCredentialSensitiveFields(13, {
      password: "p",
      key: "k",
      key_password: "kp",
      private_key: "priv",
      public_key: "pub",
      key_type: "rsa",
    });

    expect(db.prepare).toHaveBeenCalledWith(
      expect.stringContaining("UPDATE ssh_data"),
    );
    expect(db.prepare).toHaveBeenCalledWith(
      expect.stringContaining("UPDATE ssh_credentials"),
    );
    expect(run).toHaveBeenCalledWith("p", "k", "kp", "ed25519", "sp", 12);
    expect(run).toHaveBeenCalledWith("p", "k", "kp", "priv", "pub", "rsa", 13);
  });

  it("owns password-reset dynamic field updates", () => {
    const { db, run } = createDb();
    const store = new RawSqliteUserEncryptionMigrationStore(db);

    store.updatePasswordResetFields(
      "ssh_credentials",
      99,
      ["password", "key"],
      { password: "p", key: "k" },
    );

    expect(db.prepare).toHaveBeenCalledWith(
      "UPDATE ssh_credentials SET password = ?, key = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
    );
    expect(run).toHaveBeenCalledWith("p", "k", 99);
  });
});