test: add initial unit tests (#56)

* chore: add initial db migration

* test: add unit tests for packages auth, common, widgets

* fix: deep source issues

* fix: format issues

* wip: add unit tests for api routers

* fix: deep source issues

* test:  add missing unit tests for integration router

* wip: board tests

* test: add unit tests for board router

* fix: remove unnecessary null assertions

* fix: deepsource issues

* fix: formatting

* fix: pnpm lock

* fix: lint and typecheck issues

* chore: address pull request feedback

* fix: non-null assertions

* fix: lockfile broken
This commit is contained in:
Meier Lukas
2024-02-10 19:00:08 +01:00
committed by GitHub
parent 13aae82790
commit f070a0cb0a
34 changed files with 3014 additions and 129 deletions

View File

@@ -0,0 +1,94 @@
import { describe, expect, it, vi } from "vitest";
import type { Session } from "@homarr/auth";
import { schema } from "@homarr/db";
import { createDb } from "@homarr/db/test";
import { userRouter } from "../user";
// Mock the auth module to return an empty session
vi.mock("@homarr/auth", async () => {
const mod = await import("@homarr/auth/security");
return { ...mod, auth: () => ({}) as Session };
});
describe("initUser should initialize the first user", () => {
it("should throw an error if a user already exists", async () => {
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
await db.insert(schema.users).values({
id: "test",
name: "test",
password: "test",
});
const act = async () =>
await caller.initUser({
username: "test",
password: "12345678",
confirmPassword: "12345678",
});
await expect(act()).rejects.toThrow("User already exists");
});
it("should create a user if none exists", async () => {
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
await caller.initUser({
username: "test",
password: "12345678",
confirmPassword: "12345678",
});
const user = await db.query.users.findFirst({
columns: {
id: true,
},
});
expect(user).toBeDefined();
});
it("should not create a user if the password and confirmPassword do not match", async () => {
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
const act = async () =>
await caller.initUser({
username: "test",
password: "12345678",
confirmPassword: "12345679",
});
await expect(act()).rejects.toThrow("Passwords do not match");
});
it("should not create a user if the password is too short", async () => {
const db = createDb();
const caller = userRouter.createCaller({
db,
session: null,
});
const act = async () =>
await caller.initUser({
username: "test",
password: "1234567",
confirmPassword: "1234567",
});
await expect(act()).rejects.toThrow("too_small");
});
});