508fa39835
- Wrap startup syncs in safeStartup() to prevent crash on external service failure - Add migrate-entrypoint.sh for auto-generating migrations from schema diff - Update Dockerfile migration stage to use entrypoint script - Add test job to build-and-publish workflow (runs before build) - Add tests.yaml workflow to run tests on every push - Fix test setup to use real RSA key pair instead of plain strings - Add test script to package.json
238 lines
6.8 KiB
TypeScript
238 lines
6.8 KiB
TypeScript
/**
|
|
* Global test setup — mock heavy external dependencies so unit tests
|
|
* never touch real databases, APIs, or file-system keys.
|
|
*/
|
|
|
|
import { mock } from "bun:test";
|
|
import crypto from "crypto";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Generate a real RSA key pair for modules that call crypto.createPrivateKey()
|
|
// at import time (e.g. readSecureValue.ts).
|
|
// ---------------------------------------------------------------------------
|
|
const { privateKey: _testPrivateKey, publicKey: _testPublicKey } =
|
|
crypto.generateKeyPairSync("rsa", {
|
|
modulusLength: 2048,
|
|
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Mock the constants module — almost every source file imports from here.
|
|
// We provide safe defaults so modules can be imported without side-effects.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
mock.module("../src/constants", () => ({
|
|
prisma: createMockPrisma(),
|
|
PORT: "3333",
|
|
API_BASE_URL: "http://localhost:3333",
|
|
sessionDuration: 30 * 24 * 60 * 60_000,
|
|
accessTokenDuration: "10min",
|
|
refreshTokenDuration: "30d",
|
|
accessTokenPrivateKey: _testPrivateKey,
|
|
refreshTokenPrivateKey: _testPrivateKey,
|
|
permissionsPrivateKey: _testPrivateKey,
|
|
secureValuesPrivateKey: _testPrivateKey,
|
|
secureValuesPublicKey: _testPublicKey,
|
|
msalClient: { acquireTokenByCode: mock(() => Promise.resolve({})) },
|
|
connectWiseApi: {
|
|
get: mock(() => Promise.resolve({ data: {} })),
|
|
post: mock(() => Promise.resolve({ data: {} })),
|
|
},
|
|
unifi: createMockUnifi(),
|
|
unifiControllerBaseUrl: "https://unifi.test.local",
|
|
unifiSite: "default",
|
|
unifiUsername: "admin",
|
|
unifiPassword: "test-pass",
|
|
io: { of: mock(() => ({ on: mock() })) },
|
|
engine: {},
|
|
}));
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function createMockPrisma() {
|
|
const createModelProxy = () =>
|
|
new Proxy(
|
|
{},
|
|
{
|
|
get(_target, prop) {
|
|
return mock(() => Promise.resolve(null));
|
|
},
|
|
},
|
|
);
|
|
|
|
return new Proxy(
|
|
{},
|
|
{
|
|
get(_target, prop) {
|
|
if (prop === "$connect" || prop === "$disconnect")
|
|
return mock(() => Promise.resolve());
|
|
return createModelProxy();
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
export function createMockUnifi() {
|
|
return {
|
|
login: mock(() => Promise.resolve()),
|
|
getAllSites: mock(() => Promise.resolve([])),
|
|
getSiteOverview: mock(() => Promise.resolve({})),
|
|
getDevices: mock(() => Promise.resolve([])),
|
|
getWlanConf: mock(() => Promise.resolve([])),
|
|
updateWlanConf: mock(() => Promise.resolve({})),
|
|
getNetworks: mock(() => Promise.resolve([])),
|
|
createSite: mock(() =>
|
|
Promise.resolve({ name: "default", description: "Default" }),
|
|
),
|
|
getWlanGroups: mock(() => Promise.resolve([])),
|
|
createWlanGroup: mock(() => Promise.resolve({})),
|
|
getUserGroups: mock(() => Promise.resolve([])),
|
|
createUserGroup: mock(() => Promise.resolve({})),
|
|
getApGroups: mock(() => Promise.resolve([])),
|
|
createApGroup: mock(() => Promise.resolve({})),
|
|
updateApGroup: mock(() => Promise.resolve({})),
|
|
getAccessPoints: mock(() => Promise.resolve([])),
|
|
getWifiLimits: mock(() => Promise.resolve({})),
|
|
getPrivatePSKs: mock(() => Promise.resolve([])),
|
|
createPrivatePSK: mock(() => Promise.resolve({})),
|
|
};
|
|
}
|
|
|
|
/** Build a minimal Prisma-shaped User row for controller tests. */
|
|
export function buildMockUser(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "user-1",
|
|
userId: "ms-uid-1",
|
|
name: "Test User",
|
|
login: "test@example.com",
|
|
email: "test@example.com",
|
|
emailVerified: null,
|
|
image: null,
|
|
token: "ms-token",
|
|
permissions: null,
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
roles: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal Prisma-shaped Role row. */
|
|
export function buildMockRole(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "role-1",
|
|
title: "Test Role",
|
|
moniker: "test-role",
|
|
permissions: "mock-permissions-token",
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
users: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal Prisma-shaped Company row. */
|
|
export function buildMockCompany(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "company-1",
|
|
name: "Test Company",
|
|
cw_Identifier: "TestCo",
|
|
cw_CompanyId: 123,
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal Session row. */
|
|
export function buildMockSession(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "session-1",
|
|
sessionKey: "sk-abc123",
|
|
userId: "user-1",
|
|
expires: new Date(Date.now() + 30 * 24 * 60 * 60_000),
|
|
refreshedAt: null,
|
|
invalidatedAt: null,
|
|
refreshTokenGenerated: false,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal CredentialType row. */
|
|
export function buildMockCredentialType(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "ctype-1",
|
|
name: "Login Credential",
|
|
permissionScope: "credential.login",
|
|
icon: null,
|
|
fields: [
|
|
{
|
|
id: "username",
|
|
name: "Username",
|
|
required: true,
|
|
secure: false,
|
|
valueType: "plain_text",
|
|
},
|
|
{
|
|
id: "password",
|
|
name: "Password",
|
|
required: true,
|
|
secure: true,
|
|
valueType: "password",
|
|
},
|
|
],
|
|
credentials: [],
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal Credential row. */
|
|
export function buildMockCredential(overrides: Record<string, any> = {}) {
|
|
const ctype = buildMockCredentialType();
|
|
const company = buildMockCompany();
|
|
return {
|
|
id: "cred-1",
|
|
name: "Test Credential",
|
|
notes: null,
|
|
typeId: ctype.id,
|
|
companyId: company.id,
|
|
subCredentialOfId: null,
|
|
fields: { username: "admin" },
|
|
type: ctype,
|
|
company,
|
|
securevalues: [
|
|
{
|
|
id: "sv-1",
|
|
name: "password",
|
|
content: "encrypted-data",
|
|
hash: "BLAKE2s$abc$salt",
|
|
credentialId: "cred-1",
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
},
|
|
],
|
|
subCredentials: [],
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
/** Build a minimal UnifiSite row. */
|
|
export function buildMockUnifiSite(overrides: Record<string, any> = {}) {
|
|
return {
|
|
id: "usite-1",
|
|
name: "Main Office",
|
|
siteId: "default",
|
|
companyId: null,
|
|
createdAt: new Date("2025-01-01"),
|
|
updatedAt: new Date("2025-01-01"),
|
|
...overrides,
|
|
};
|
|
}
|