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

@@ -1,8 +1,8 @@
import { TRPCError } from "@trpc/server";
import superjson from "superjson";
import type { Database } from "@homarr/db";
import { and, createId, db, eq, inArray } from "@homarr/db";
import type { Database, SQL } from "@homarr/db";
import { and, createId, eq, inArray } from "@homarr/db";
import {
boards,
integrationItems,
@@ -47,8 +47,8 @@ const filterUpdatedItems = <TInput extends { id: string }>(
);
export const boardRouter = createTRPCRouter({
getAll: publicProcedure.query(async () => {
return await db.query.boards.findMany({
getAll: publicProcedure.query(async ({ ctx }) => {
return await ctx.db.query.boards.findMany({
columns: {
id: true,
name: true,
@@ -85,23 +85,45 @@ export const boardRouter = createTRPCRouter({
await ctx.db.delete(boards).where(eq(boards.id, input.id));
}),
default: publicProcedure.query(async ({ ctx }) => {
return await getFullBoardByName(ctx.db, "default");
return await getFullBoardWithWhere(ctx.db, eq(boards.name, "default"));
}),
byName: publicProcedure
.input(validation.board.byName)
.query(async ({ input, ctx }) => {
return await getFullBoardByName(ctx.db, input.name);
return await getFullBoardWithWhere(ctx.db, eq(boards.name, input.name));
}),
saveGeneralSettings: publicProcedure
.input(validation.board.saveGeneralSettings)
.mutation(async ({ input }) => {
await db.update(boards).set(input).where(eq(boards.name, "default"));
.mutation(async ({ ctx, input }) => {
const board = await ctx.db.query.boards.findFirst({
where: eq(boards.id, input.boardId),
});
if (!board) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Board not found",
});
}
await ctx.db
.update(boards)
.set({
pageTitle: input.pageTitle,
metaTitle: input.metaTitle,
logoImageUrl: input.logoImageUrl,
faviconImageUrl: input.faviconImageUrl,
})
.where(eq(boards.id, input.boardId));
}),
save: publicProcedure
.input(validation.board.save)
.mutation(async ({ input, ctx }) => {
await ctx.db.transaction(async (tx) => {
const dbBoard = await getFullBoardByName(tx, input.name);
const dbBoard = await getFullBoardWithWhere(
tx,
eq(boards.id, input.boardId),
);
const addedSections = filterAddedItems(
input.sections,
@@ -199,12 +221,17 @@ export const boardRouter = createTRPCRouter({
);
for (const section of updatedSections) {
const prev = dbBoard.sections.find(
(dbSection) => dbSection.id === section.id,
);
await tx
.update(sections)
.set({
kind: section.kind,
position: section.position,
name: "name" in section ? section.name : null,
name:
prev?.kind === "category" && "name" in section
? section.name
: null,
})
.where(eq(sections.id, section.id));
}
@@ -249,9 +276,9 @@ export const boardRouter = createTRPCRouter({
}),
});
const getFullBoardByName = async (db: Database, name: string) => {
const getFullBoardWithWhere = async (db: Database, where: SQL<unknown>) => {
const board = await db.query.boards.findFirst({
where: eq(boards.name, name),
where,
with: {
sections: {
with: {

View File

@@ -1,6 +1,7 @@
import crypto from "crypto";
import { TRPCError } from "@trpc/server";
import type { Database } from "@homarr/db";
import { and, createId, eq } from "@homarr/db";
import { integrations, integrationSecrets } from "@homarr/db/schema/sqlite";
import type { IntegrationSecretKind } from "@homarr/definitions";
@@ -128,18 +129,20 @@ export const integrationRouter = createTRPCRouter({
if (changedSecrets.length > 0) {
for (const changedSecret of changedSecrets) {
await ctx.db
.update(integrationSecrets)
.set({
value: encryptSecret(changedSecret.value),
updatedAt: new Date(),
})
.where(
and(
eq(integrationSecrets.integrationId, input.id),
eq(integrationSecrets.kind, changedSecret.kind),
),
);
const secretInput = {
integrationId: input.id,
value: changedSecret.value,
kind: changedSecret.kind,
};
if (
!decryptedSecrets.some(
(secret) => secret.kind === changedSecret.kind,
)
) {
await addSecret(ctx.db, secretInput);
} else {
await updateSecret(ctx.db, secretInput);
}
}
}
}),
@@ -204,6 +207,17 @@ export const integrationRouter = createTRPCRouter({
});
}
}
const everySecretDefined = secretKinds.every((secretKind) =>
secrets.some((secret) => secret.kind === secretKind),
);
if (!everySecretDefined) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "SECRETS_NOT_DEFINED",
});
}
}
// TODO: actually test the connection
@@ -223,7 +237,7 @@ const key = Buffer.from(
); // TODO: generate with const data = crypto.randomBytes(32).toString('hex')
//Encrypting text
function encryptSecret(text: string): `${string}.${string}` {
export function encryptSecret(text: string): `${string}.${string}` {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, Buffer.from(key), iv);
let encrypted = cipher.update(text);
@@ -241,3 +255,37 @@ function decryptSecret(value: `${string}.${string}`) {
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
interface UpdateSecretInput {
integrationId: string;
value: string;
kind: IntegrationSecretKind;
}
const updateSecret = async (db: Database, input: UpdateSecretInput) => {
await db
.update(integrationSecrets)
.set({
value: encryptSecret(input.value),
updatedAt: new Date(),
})
.where(
and(
eq(integrationSecrets.integrationId, input.integrationId),
eq(integrationSecrets.kind, input.kind),
),
);
};
interface AddSecretInput {
integrationId: string;
value: string;
kind: IntegrationSecretKind;
}
const addSecret = async (db: Database, input: AddSecretInput) => {
await db.insert(integrationSecrets).values({
kind: input.kind,
value: encryptSecret(input.value),
updatedAt: new Date(),
integrationId: input.integrationId,
});
};

View File

@@ -0,0 +1,648 @@
import SuperJSON from "superjson";
import { describe, expect, it, vi } from "vitest";
import type { Session } from "@homarr/auth";
import type { Database } from "@homarr/db";
import { createId, eq } from "@homarr/db";
import {
boards,
integrationItems,
integrations,
items,
sections,
} from "@homarr/db/schema/sqlite";
import { createDb } from "@homarr/db/test";
import type { RouterOutputs } from "../../..";
import { boardRouter } from "../board";
// Mock the auth module to return an empty session
vi.mock("@homarr/auth", () => ({ auth: () => ({}) as Session }));
export const expectToBeDefined = <T>(value: T) => {
if (value === undefined) {
expect(value).toBeDefined();
}
if (value === null) {
expect(value).not.toBeNull();
}
return value as Exclude<T, undefined | null>;
};
describe("default should return default board", () => {
it("should return default board", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const fullBoardProps = await createFullBoardAsync(db, "default");
const result = await caller.default();
expectInputToBeFullBoardWithName(result, {
name: "default",
...fullBoardProps,
});
});
});
describe("byName should return board by name", () => {
it.each([["default"], ["something"]])(
"should return board by name %s when present",
async (name) => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const fullBoardProps = await createFullBoardAsync(db, name);
const result = await caller.byName({ name });
expectInputToBeFullBoardWithName(result, {
name,
...fullBoardProps,
});
},
);
it("should throw error when not present");
});
describe("saveGeneralSettings should save general settings", () => {
it("should save general settings", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const newPageTitle = "newPageTitle";
const newMetaTitle = "newMetaTitle";
const newLogoImageUrl = "http://logo.image/url.png";
const newFaviconImageUrl = "http://favicon.image/url.png";
const { boardId } = await createFullBoardAsync(db, "default");
await caller.saveGeneralSettings({
pageTitle: newPageTitle,
metaTitle: newMetaTitle,
logoImageUrl: newLogoImageUrl,
faviconImageUrl: newFaviconImageUrl,
boardId,
});
});
it("should throw error when board not found", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const act = async () =>
await caller.saveGeneralSettings({
pageTitle: "newPageTitle",
metaTitle: "newMetaTitle",
logoImageUrl: "http://logo.image/url.png",
faviconImageUrl: "http://favicon.image/url.png",
boardId: "nonExistentBoardId",
});
await expect(act()).rejects.toThrowError("Board not found");
});
});
describe("save should save full board", () => {
it("should remove section when not present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, sectionId } = await createFullBoardAsync(db, "default");
await caller.save({
boardId,
sections: [
{
id: createId(),
kind: "empty",
position: 0,
items: [],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: true,
},
});
const section = await db.query.boards.findFirst({
where: eq(sections.id, sectionId),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
expect(definedBoard.sections[0]?.id).not.toBe(sectionId);
expect(section).toBeUndefined();
});
it("should remove item when not present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, itemId, sectionId } = await createFullBoardAsync(
db,
"default",
);
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "empty",
position: 0,
items: [
{
id: createId(),
kind: "clock",
options: { is24HourFormat: true },
integrations: [],
height: 1,
width: 1,
xOffset: 0,
yOffset: 0,
},
],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: {
with: {
items: true,
},
},
},
});
const item = await db.query.items.findFirst({
where: eq(items.id, itemId),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
const firstSection = expectToBeDefined(definedBoard.sections[0]);
expect(firstSection.items.length).toBe(1);
expect(firstSection.items[0]?.id).not.toBe(itemId);
expect(item).toBeUndefined();
});
it("should remove integration reference when not present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const anotherIntegration = {
id: createId(),
kind: "adGuardHome",
name: "AdGuard Home",
url: "http://localhost:3000",
} as const;
const { boardId, itemId, integrationId, sectionId } =
await createFullBoardAsync(db, "default");
await db.insert(integrations).values(anotherIntegration);
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "empty",
position: 0,
items: [
{
id: itemId,
kind: "clock",
options: { is24HourFormat: true },
integrations: [anotherIntegration],
height: 1,
width: 1,
xOffset: 0,
yOffset: 0,
},
],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: {
with: {
items: {
with: {
integrations: true,
},
},
},
},
},
});
const integration = await db.query.integrationItems.findFirst({
where: eq(integrationItems.integrationId, integrationId),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
const firstSection = expectToBeDefined(definedBoard.sections[0]);
expect(firstSection.items.length).toBe(1);
const firstItem = expectToBeDefined(firstSection.items[0]);
expect(firstItem.integrations.length).toBe(1);
expect(firstItem.integrations[0]?.integrationId).not.toBe(integrationId);
expect(integration).toBeUndefined();
});
it.each([
[{ kind: "empty" as const }],
[{ kind: "category" as const, name: "My first category" }],
])("should add section when present in input", async (partialSection) => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, sectionId } = await createFullBoardAsync(db, "default");
const newSectionId = createId();
await caller.save({
boardId,
sections: [
{
id: newSectionId,
position: 1,
items: [],
...partialSection,
},
{
id: sectionId,
kind: "empty",
position: 0,
items: [],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: true,
},
});
const section = await db.query.sections.findFirst({
where: eq(sections.id, newSectionId),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(2);
const addedSection = expectToBeDefined(
definedBoard.sections.find((section) => section.id === newSectionId),
);
expect(addedSection).toBeDefined();
expect(addedSection.id).toBe(newSectionId);
expect(addedSection.kind).toBe(partialSection.kind);
expect(addedSection.position).toBe(1);
if ("name" in partialSection) {
expect(addedSection.name).toBe(partialSection.name);
}
expect(section).toBeDefined();
});
it("should add item when present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, sectionId } = await createFullBoardAsync(db, "default");
const newItemId = createId();
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "empty",
position: 0,
items: [
{
id: newItemId,
kind: "clock",
options: { is24HourFormat: true },
integrations: [],
height: 1,
width: 1,
xOffset: 3,
yOffset: 2,
},
],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: {
with: {
items: true,
},
},
},
});
const item = await db.query.items.findFirst({
where: eq(items.id, newItemId),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
const firstSection = expectToBeDefined(definedBoard.sections[0]);
expect(firstSection.items.length).toBe(1);
const addedItem = expectToBeDefined(
firstSection.items.find((item) => item.id === newItemId),
);
expect(addedItem).toBeDefined();
expect(addedItem.id).toBe(newItemId);
expect(addedItem.kind).toBe("clock");
expect(addedItem.options).toBe(
SuperJSON.stringify({ is24HourFormat: true }),
);
expect(addedItem.height).toBe(1);
expect(addedItem.width).toBe(1);
expect(addedItem.xOffset).toBe(3);
expect(addedItem.yOffset).toBe(2);
expect(item).toBeDefined();
});
it("should add integration reference when present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const integration = {
id: createId(),
kind: "plex",
name: "Plex",
url: "http://plex.local",
} as const;
const { boardId, itemId, sectionId } = await createFullBoardAsync(
db,
"default",
);
await db.insert(integrations).values(integration);
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "empty",
position: 0,
items: [
{
id: itemId,
kind: "clock",
options: { is24HourFormat: true },
integrations: [integration],
height: 1,
width: 1,
xOffset: 0,
yOffset: 0,
},
],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: {
with: {
items: {
with: {
integrations: true,
},
},
},
},
},
});
const integrationItem = await db.query.integrationItems.findFirst({
where: eq(integrationItems.integrationId, integration.id),
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
const firstSection = expectToBeDefined(definedBoard.sections[0]);
expect(firstSection.items.length).toBe(1);
const firstItem = expectToBeDefined(
firstSection.items.find((item) => item.id === itemId),
);
expect(firstItem.integrations.length).toBe(1);
expect(firstItem.integrations[0]?.integrationId).toBe(integration.id);
expect(integrationItem).toBeDefined();
});
it("should update section when present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, sectionId } = await createFullBoardAsync(db, "default");
const newSectionId = createId();
await db.insert(sections).values({
id: newSectionId,
kind: "category",
name: "Before",
position: 1,
boardId,
});
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "category",
position: 1,
name: "Test",
items: [],
},
{
id: newSectionId,
kind: "category",
name: "After",
position: 0,
items: [],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: true,
},
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(2);
const firstSection = expectToBeDefined(
definedBoard.sections.find((section) => section.id === sectionId),
);
expect(firstSection.id).toBe(sectionId);
expect(firstSection.kind).toBe("empty");
expect(firstSection.position).toBe(1);
expect(firstSection.name).toBe(null);
const secondSection = expectToBeDefined(
definedBoard.sections.find((section) => section.id === newSectionId),
);
expect(secondSection.id).toBe(newSectionId);
expect(secondSection.kind).toBe("category");
expect(secondSection.position).toBe(0);
expect(secondSection.name).toBe("After");
});
it("should update item when present in input", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const { boardId, itemId, sectionId } = await createFullBoardAsync(
db,
"default",
);
await caller.save({
boardId,
sections: [
{
id: sectionId,
kind: "empty",
position: 0,
items: [
{
id: itemId,
kind: "clock",
options: { is24HourFormat: false },
integrations: [],
height: 3,
width: 2,
xOffset: 7,
yOffset: 5,
},
],
},
],
});
const board = await db.query.boards.findFirst({
where: eq(boards.id, boardId),
with: {
sections: {
with: {
items: true,
},
},
},
});
const definedBoard = expectToBeDefined(board);
expect(definedBoard.sections.length).toBe(1);
const firstSection = expectToBeDefined(definedBoard.sections[0]);
expect(firstSection.items.length).toBe(1);
const firstItem = expectToBeDefined(
firstSection.items.find((item) => item.id === itemId),
);
expect(firstItem.id).toBe(itemId);
expect(firstItem.kind).toBe("clock");
expect(
SuperJSON.parse<{ is24HourFormat: boolean }>(firstItem.options)
.is24HourFormat,
).toBe(false);
expect(firstItem.height).toBe(3);
expect(firstItem.width).toBe(2);
expect(firstItem.xOffset).toBe(7);
expect(firstItem.yOffset).toBe(5);
});
it("should fail when board not found", async () => {
const db = createDb();
const caller = boardRouter.createCaller({ db, session: null });
const act = async () =>
await caller.save({
boardId: "nonExistentBoardId",
sections: [],
});
await expect(act()).rejects.toThrowError("Board not found");
});
});
const expectInputToBeFullBoardWithName = (
input: RouterOutputs["board"]["default"],
props: { name: string } & Awaited<ReturnType<typeof createFullBoardAsync>>,
) => {
expect(input.id).toBe(props.boardId);
expect(input.name).toBe(props.name);
expect(input.sections.length).toBe(1);
const firstSection = expectToBeDefined(input.sections[0]);
expect(firstSection.id).toBe(props.sectionId);
expect(firstSection.items.length).toBe(1);
const firstItem = expectToBeDefined(firstSection.items[0]);
expect(firstItem.id).toBe(props.itemId);
expect(firstItem.kind).toBe("clock");
if (firstItem.kind === "clock") {
expect(firstItem.options.is24HourFormat).toBe(true);
}
expect(firstItem.integrations.length).toBe(1);
const firstIntegration = expectToBeDefined(firstItem.integrations[0]);
expect(firstIntegration.id).toBe(props.integrationId);
expect(firstIntegration.kind).toBe("adGuardHome");
};
const createFullBoardAsync = async (db: Database, name: string) => {
const boardId = createId();
await db.insert(boards).values({
id: boardId,
name,
});
const sectionId = createId();
await db.insert(sections).values({
id: sectionId,
kind: "empty",
position: 0,
boardId,
});
const itemId = createId();
await db.insert(items).values({
id: itemId,
kind: "clock",
height: 1,
width: 1,
xOffset: 0,
yOffset: 0,
sectionId,
options: SuperJSON.stringify({ is24HourFormat: true }),
});
const integrationId = createId();
await db.insert(integrations).values({
id: integrationId,
kind: "adGuardHome",
name: "AdGuard Home",
url: "http://localhost:3000",
});
await db.insert(integrationItems).values({
integrationId,
itemId,
});
return {
boardId,
sectionId,
itemId,
integrationId,
};
};

View File

@@ -0,0 +1,503 @@
import { describe, expect, it, vi } from "vitest";
import type { Session } from "@homarr/auth";
import { createId } from "@homarr/db";
import { integrations, integrationSecrets } from "@homarr/db/schema/sqlite";
import { createDb } from "@homarr/db/test";
import type { RouterInputs } from "../../..";
import { encryptSecret, integrationRouter } from "../integration";
import { expectToBeDefined } from "./board.spec";
// Mock the auth module to return an empty session
vi.mock("@homarr/auth", () => ({ auth: () => ({}) as Session }));
describe("all should return all integrations", () => {
it("should return all integrations", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
await db.insert(integrations).values([
{
id: "1",
name: "Home assistant",
kind: "homeAssistant",
url: "http://homeassist.local",
},
{
id: "2",
name: "Home plex server",
kind: "plex",
url: "http://plex.local",
},
]);
const result = await caller.all();
expect(result.length).toBe(2);
expect(result[0]!.kind).toBe("plex");
expect(result[1]!.kind).toBe("homeAssistant");
});
});
describe("byId should return an integration by id", () => {
it("should return an integration by id", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
await db.insert(integrations).values([
{
id: "1",
name: "Home assistant",
kind: "homeAssistant",
url: "http://homeassist.local",
},
{
id: "2",
name: "Home plex server",
kind: "plex",
url: "http://plex.local",
},
]);
const result = await caller.byId({ id: "2" });
expect(result.kind).toBe("plex");
});
it("should throw an error if the integration does not exist", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const act = async () => await caller.byId({ id: "2" });
await expect(act()).rejects.toThrow("Integration not found");
});
it("should only return the public secret values", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
await db.insert(integrations).values([
{
id: "1",
name: "Home assistant",
kind: "homeAssistant",
url: "http://homeassist.local",
},
]);
await db.insert(integrationSecrets).values([
{
kind: "username",
value: encryptSecret("musterUser"),
integrationId: "1",
updatedAt: new Date(),
},
{
kind: "password",
value: encryptSecret("Password123!"),
integrationId: "1",
updatedAt: new Date(),
},
{
kind: "apiKey",
value: encryptSecret("1234567890"),
integrationId: "1",
updatedAt: new Date(),
},
]);
const result = await caller.byId({ id: "1" });
expect(result.secrets.length).toBe(3);
const username = expectToBeDefined(
result.secrets.find((secret) => secret.kind === "username"),
);
expect(username.value).not.toBeNull();
const password = expectToBeDefined(
result.secrets.find((secret) => secret.kind === "password"),
);
expect(password.value).toBeNull();
const apiKey = expectToBeDefined(
result.secrets.find((secret) => secret.kind === "apiKey"),
);
expect(apiKey.value).toBeNull();
});
});
describe("create should create a new integration", () => {
it("should create a new integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const input = {
name: "Jellyfin",
kind: "jellyfin" as const,
url: "http://jellyfin.local",
secrets: [{ kind: "apiKey" as const, value: "1234567890" }],
};
const fakeNow = new Date("2023-07-01T00:00:00Z");
vi.useFakeTimers();
vi.setSystemTime(fakeNow);
await caller.create(input);
vi.useRealTimers();
const dbIntegration = await db.query.integrations.findFirst();
const dbSecret = await db.query.integrationSecrets.findFirst();
expect(dbIntegration).toBeDefined();
expect(dbIntegration!.name).toBe(input.name);
expect(dbIntegration!.kind).toBe(input.kind);
expect(dbIntegration!.url).toBe(input.url);
expect(dbSecret!.integrationId).toBe(dbIntegration!.id);
expect(dbSecret).toBeDefined();
expect(dbSecret!.kind).toBe(input.secrets[0]!.kind);
expect(dbSecret!.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
expect(dbSecret!.updatedAt).toEqual(fakeNow);
});
});
describe("update should update an integration", () => {
it("should update an integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const lastWeek = new Date("2023-06-24T00:00:00Z");
const integrationId = createId();
const toInsert = {
id: integrationId,
name: "Pi Hole",
kind: "piHole" as const,
url: "http://hole.local",
};
await db.insert(integrations).values(toInsert);
const usernameToInsert = {
kind: "username" as const,
value: encryptSecret("musterUser"),
integrationId,
updatedAt: lastWeek,
};
const passwordToInsert = {
kind: "password" as const,
value: encryptSecret("Password123!"),
integrationId,
updatedAt: lastWeek,
};
await db
.insert(integrationSecrets)
.values([usernameToInsert, passwordToInsert]);
const input = {
id: integrationId,
name: "Milky Way Pi Hole",
kind: "piHole" as const,
url: "http://milkyway.local",
secrets: [
{ kind: "username" as const, value: "newUser" },
{ kind: "password" as const, value: null },
{ kind: "apiKey" as const, value: "1234567890" },
],
};
const fakeNow = new Date("2023-07-01T00:00:00Z");
vi.useFakeTimers();
vi.setSystemTime(fakeNow);
await caller.update(input);
vi.useRealTimers();
const dbIntegration = await db.query.integrations.findFirst();
const dbSecrets = await db.query.integrationSecrets.findMany();
expect(dbIntegration).toBeDefined();
expect(dbIntegration!.name).toBe(input.name);
expect(dbIntegration!.kind).toBe(input.kind);
expect(dbIntegration!.url).toBe(input.url);
expect(dbSecrets.length).toBe(3);
const username = expectToBeDefined(
dbSecrets.find((secret) => secret.kind === "username"),
);
const password = expectToBeDefined(
dbSecrets.find((secret) => secret.kind === "password"),
);
const apiKey = expectToBeDefined(
dbSecrets.find((secret) => secret.kind === "apiKey"),
);
expect(username.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
expect(password.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
expect(apiKey.value).toMatch(/^[a-f0-9]+.[a-f0-9]+$/);
expect(username.updatedAt).toEqual(fakeNow);
expect(password.updatedAt).toEqual(lastWeek);
expect(apiKey.updatedAt).toEqual(fakeNow);
expect(username.value).not.toEqual(usernameToInsert.value);
expect(password.value).toEqual(passwordToInsert.value);
expect(apiKey.value).not.toEqual(input.secrets[2]!.value);
});
it("should throw an error if the integration does not exist", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const act = async () =>
await caller.update({
id: createId(),
name: "Pi Hole",
url: "http://hole.local",
secrets: [],
});
await expect(act()).rejects.toThrow("Integration not found");
});
});
describe("delete should delete an integration", () => {
it("should delete an integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const integrationId = createId();
await db.insert(integrations).values({
id: integrationId,
name: "Home assistant",
kind: "homeAssistant",
url: "http://homeassist.local",
});
await db.insert(integrationSecrets).values([
{
kind: "username",
value: encryptSecret("example"),
integrationId,
updatedAt: new Date(),
},
]);
await caller.delete({ id: integrationId });
const dbIntegration = await db.query.integrations.findFirst();
expect(dbIntegration).toBeUndefined();
const dbSecrets = await db.query.integrationSecrets.findMany();
expect(dbSecrets.length).toBe(0);
});
});
describe("testConnection should test the connection to an integration", () => {
it.each([
[
"nzbGet" as const,
[
{ kind: "username" as const, value: null },
{ kind: "password" as const, value: "Password123!" },
],
],
[
"nzbGet" as const,
[
{ kind: "username" as const, value: "exampleUser" },
{ kind: "password" as const, value: null },
],
],
["sabNzbd" as const, [{ kind: "apiKey" as const, value: null }]],
[
"sabNzbd" as const,
[
{ kind: "username" as const, value: "exampleUser" },
{ kind: "password" as const, value: "Password123!" },
],
],
])(
"should fail when a required secret is missing when creating %s integration",
async (kind, secrets) => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const input: RouterInputs["integration"]["testConnection"] = {
id: null,
kind,
url: `http://${kind}.local`,
secrets,
};
const act = async () => await caller.testConnection(input);
await expect(act()).rejects.toThrow("SECRETS_NOT_DEFINED");
},
);
it.each([
[
"nzbGet" as const,
[
{ kind: "username" as const, value: "exampleUser" },
{ kind: "password" as const, value: "Password123!" },
],
],
["sabNzbd" as const, [{ kind: "apiKey" as const, value: "1234567890" }]],
])(
"should be successful when all required secrets are defined for creation of %s integration",
async (kind, secrets) => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const input: RouterInputs["integration"]["testConnection"] = {
id: null,
kind,
url: `http://${kind}.local`,
secrets,
};
const act = async () => await caller.testConnection(input);
await expect(act()).resolves.toBeUndefined();
},
);
it("should be successful when all required secrets are defined for updating an nzbGet integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const input: RouterInputs["integration"]["testConnection"] = {
id: createId(),
kind: "nzbGet",
url: "http://nzbGet.local",
secrets: [
{ kind: "username", value: "exampleUser" },
{ kind: "password", value: "Password123!" },
],
};
const act = async () => await caller.testConnection(input);
await expect(act()).resolves.toBeUndefined();
});
it("should be successful when overriding one of the secrets for an existing nzbGet integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const integrationId = createId();
await db.insert(integrations).values({
id: integrationId,
name: "NZBGet",
kind: "nzbGet",
url: "http://nzbGet.local",
});
await db.insert(integrationSecrets).values([
{
kind: "username",
value: encryptSecret("exampleUser"),
integrationId,
updatedAt: new Date(),
},
{
kind: "password",
value: encryptSecret("Password123!"),
integrationId,
updatedAt: new Date(),
},
]);
const input: RouterInputs["integration"]["testConnection"] = {
id: integrationId,
kind: "nzbGet",
url: "http://nzbGet.local",
secrets: [
{ kind: "username", value: "newUser" },
{ kind: "password", value: null },
],
};
const act = async () => await caller.testConnection(input);
await expect(act()).resolves.toBeUndefined();
});
it("should fail when a required secret is missing for an existing nzbGet integration", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const integrationId = createId();
await db.insert(integrations).values({
id: integrationId,
name: "NZBGet",
kind: "nzbGet",
url: "http://nzbGet.local",
});
await db.insert(integrationSecrets).values([
{
kind: "username",
value: encryptSecret("exampleUser"),
integrationId,
updatedAt: new Date(),
},
]);
const input: RouterInputs["integration"]["testConnection"] = {
id: integrationId,
kind: "nzbGet",
url: "http://nzbGet.local",
secrets: [
{ kind: "username", value: "newUser" },
{ kind: "apiKey", value: "1234567890" },
],
};
const act = async () => await caller.testConnection(input);
await expect(act()).rejects.toThrow("SECRETS_NOT_DEFINED");
});
it("should fail when the updating integration does not exist", async () => {
const db = createDb();
const caller = integrationRouter.createCaller({
db,
session: null,
});
const act = async () =>
await caller.testConnection({
id: createId(),
kind: "nzbGet",
url: "http://nzbGet.local",
secrets: [
{ kind: "username", value: null },
{ kind: "password", value: "Password123!" },
],
});
await expect(act()).rejects.toThrow("SECRETS_NOT_DEFINED");
});
});

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");
});
});

View File

@@ -0,0 +1,61 @@
import { cookies } from "next/headers";
import type { Adapter } from "@auth/core/adapters";
import type { NextAuthConfig } from "next-auth";
import {
expireDateAfter,
generateSessionToken,
sessionMaxAgeInSeconds,
sessionTokenCookieName,
} from "./session";
export const sessionCallback: NextAuthCallbackOf<"session"> = ({
session,
user,
}) => ({
...session,
user: {
...session.user,
id: user.id,
name: user.name,
},
});
export const createSignInCallback =
(
adapter: Adapter,
isCredentialsRequest: boolean,
): NextAuthCallbackOf<"signIn"> =>
async ({ user }) => {
if (!isCredentialsRequest) return true;
if (!user) return true;
// https://github.com/nextauthjs/next-auth/issues/6106
if (!adapter?.createSession) {
return false;
}
const sessionToken = generateSessionToken();
const sessionExpiry = expireDateAfter(sessionMaxAgeInSeconds);
await adapter.createSession({
sessionToken,
userId: user.id!,
expires: sessionExpiry,
});
cookies().set(sessionTokenCookieName, sessionToken, {
path: "/",
expires: sessionExpiry,
httpOnly: true,
sameSite: "lax",
secure: true,
});
return true;
};
type NextAuthCallbackRecord = Exclude<NextAuthConfig["callbacks"], undefined>;
export type NextAuthCallbackOf<TKey extends keyof NextAuthCallbackRecord> =
Exclude<NextAuthCallbackRecord[TKey], undefined>;

View File

@@ -5,55 +5,23 @@ import Credentials from "next-auth/providers/credentials";
import { db } from "@homarr/db";
import { credentialsConfiguration } from "./providers/credentials";
import { createSignInCallback, sessionCallback } from "./callbacks";
import { createCredentialsConfiguration } from "./providers/credentials";
import { EmptyNextAuthProvider } from "./providers/empty";
import { expireDateAfter, generateSessionToken } from "./session";
import { sessionMaxAgeInSeconds, sessionTokenCookieName } from "./session";
const adapter = DrizzleAdapter(db);
const sessionMaxAgeInSeconds = 30 * 24 * 60 * 60; // 30 days
export const createConfiguration = (isCredentialsRequest: boolean) =>
NextAuth({
adapter,
providers: [Credentials(credentialsConfiguration), EmptyNextAuthProvider()],
providers: [
Credentials(createCredentialsConfiguration(db)),
EmptyNextAuthProvider(),
],
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
name: user.name,
},
}),
signIn: async ({ user }) => {
if (!isCredentialsRequest) return true;
if (!user) return true;
const sessionToken = generateSessionToken();
const sessionExpiry = expireDateAfter(sessionMaxAgeInSeconds);
// https://github.com/nextauthjs/next-auth/issues/6106
if (!adapter?.createSession) {
return false;
}
await adapter.createSession({
sessionToken: sessionToken,
userId: user.id!,
expires: sessionExpiry,
});
cookies().set("next-auth.session-token", sessionToken, {
path: "/",
expires: sessionExpiry,
httpOnly: true,
sameSite: "lax",
secure: true,
});
return true;
},
session: sessionCallback,
signIn: createSignInCallback(adapter, isCredentialsRequest),
},
session: {
strategy: "database",
@@ -65,7 +33,7 @@ export const createConfiguration = (isCredentialsRequest: boolean) =>
},
jwt: {
encode() {
const cookie = cookies().get("next-auth.session-token")?.value;
const cookie = cookies().get(sessionTokenCookieName)?.value;
return cookie ?? "";
},

View File

@@ -1,6 +1,12 @@
{
"name": "@homarr/auth",
"version": "0.1.0",
"exports": {
".": "./index.ts",
"./security": "./security.ts",
"./client": "./client.ts",
"./env.mjs": "./env.mjs"
},
"private": true,
"main": "./index.ts",
"types": "./index.ts",

View File

@@ -1,49 +1,56 @@
import type Credentials from "@auth/core/providers/credentials";
import bcrypt from "bcrypt";
import { db, eq } from "@homarr/db";
import type { Database } from "@homarr/db";
import { eq } from "@homarr/db";
import { users } from "@homarr/db/schema/sqlite";
import { validation } from "@homarr/validation";
type CredentialsConfiguration = Parameters<typeof Credentials>[0];
export const credentialsConfiguration = {
type: "credentials",
name: "Credentials",
credentials: {
name: {
label: "Username",
type: "text",
export const createCredentialsConfiguration = (db: Database) =>
({
type: "credentials",
name: "Credentials",
credentials: {
name: {
label: "Username",
type: "text",
},
password: {
label: "Password",
type: "password",
},
},
password: {
label: "Password",
type: "password",
async authorize(credentials) {
const data = await validation.user.signIn.parseAsync(credentials);
const user = await db.query.users.findFirst({
where: eq(users.name, data.name),
});
if (!user?.password) {
return null;
}
console.log(
`user ${user.name} is trying to log in. checking password...`,
);
const isValidPassword = await bcrypt.compare(
data.password,
user.password,
);
if (!isValidPassword) {
console.log(`password for user ${user.name} was incorrect`);
return null;
}
console.log(`user ${user.name} successfully authorized`);
return {
id: user.id,
name: user.name,
};
},
},
async authorize(credentials) {
const data = await validation.user.signIn.parseAsync(credentials);
const user = await db.query.users.findFirst({
where: eq(users.name, data.name),
});
if (!user?.password) {
return null;
}
console.log(`user ${user.name} is trying to log in. checking password...`);
const isValidPassword = await bcrypt.compare(data.password, user.password);
if (!isValidPassword) {
console.log(`password for user ${user.name} was incorrect`);
return null;
}
console.log(`user ${user.name} successfully authorized`);
return {
id: user.id,
name: user.name,
};
},
} satisfies CredentialsConfiguration;
}) satisfies CredentialsConfiguration;

View File

@@ -0,0 +1,66 @@
import { describe, expect, it } from "vitest";
import { createId } from "@homarr/db";
import { users } from "@homarr/db/schema/sqlite";
import { createDb } from "@homarr/db/test";
import { createSalt, hashPassword } from "../../security";
import { createCredentialsConfiguration } from "../credentials";
describe("Credentials authorization", () => {
it("should authorize user with correct credentials", async () => {
const db = createDb();
const userId = createId();
const salt = await createSalt();
await db.insert(users).values({
id: userId,
name: "test",
password: await hashPassword("test", salt),
salt,
});
const result = await createCredentialsConfiguration(db).authorize({
name: "test",
password: "test",
});
expect(result).toEqual({ id: userId, name: "test" });
});
const passwordsThatShouldNotAuthorize = [
"wrong",
"Test",
"test ",
" test",
" test ",
];
passwordsThatShouldNotAuthorize.forEach((password) => {
it(`should not authorize user with incorrect credentials (${password})`, async () => {
const db = createDb();
const userId = createId();
const salt = await createSalt();
await db.insert(users).values({
id: userId,
name: "test",
password: await hashPassword("test", salt),
salt,
});
const result = await createCredentialsConfiguration(db).authorize({
name: "test",
password,
});
expect(result).toBeNull();
});
});
it("should not authorize user for not existing user", async () => {
const db = createDb();
const result = await createCredentialsConfiguration(db).authorize({
name: "test",
password: "test",
});
expect(result).toBeNull();
});
});

View File

@@ -1,5 +1,8 @@
import { randomUUID } from "crypto";
export const sessionMaxAgeInSeconds = 30 * 24 * 60 * 60; // 30 days
export const sessionTokenCookieName = "next-auth.session-token";
export const expireDateAfter = (seconds: number) => {
return new Date(Date.now() + seconds * 1000);
};

View File

@@ -0,0 +1,153 @@
import type { ResponseCookie } from "next/dist/compiled/@edge-runtime/cookies";
import type { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies";
import { cookies } from "next/headers";
import type { Adapter, AdapterUser } from "@auth/core/adapters";
import type { Account, User } from "next-auth";
import type { JWT } from "next-auth/jwt";
import { describe, expect, it, vi } from "vitest";
import { createSignInCallback, sessionCallback } from "../callbacks";
describe("session callback", () => {
it("should add id and name to session user", async () => {
const user: AdapterUser = {
id: "id",
name: "name",
email: "email",
emailVerified: new Date("2023-01-13"),
};
const token: JWT = {};
const result = await sessionCallback({
session: {
user: {
id: "no-id",
email: "no-email",
emailVerified: new Date("2023-01-13"),
},
expires: "2023-01-13" as Date & string,
sessionToken: "token",
userId: "no-id",
},
user,
token,
trigger: "update",
newSession: {},
});
expect(result.user).toBeDefined();
expect(result.user!.id).toEqual(user.id);
expect(result.user!.name).toEqual(user.name);
});
});
type AdapterSessionInput = Parameters<
Exclude<Adapter["createSession"], undefined>
>[0];
const createAdapter = () => {
const result = {
createSession: (input: AdapterSessionInput) => input,
};
vi.spyOn(result, "createSession");
return result;
};
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
type SessionExport = typeof import("../session");
const mockSessionToken = "e9ef3010-6981-4a81-b9d6-8495d09cf3b5" as const;
const mockSessionExpiry = new Date("2023-07-01");
vi.mock("../session", async (importOriginal) => {
const mod = await importOriginal<SessionExport>();
const generateSessionToken = () => mockSessionToken;
const expireDateAfter = (_seconds: number) => mockSessionExpiry;
return {
...mod,
generateSessionToken,
expireDateAfter,
} satisfies SessionExport;
});
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
type HeadersExport = typeof import("next/headers");
vi.mock("next/headers", async (importOriginal) => {
const mod = await importOriginal<HeadersExport>();
const result = {
set: (name: string, value: string, options: Partial<ResponseCookie>) =>
options as ResponseCookie,
} as unknown as ReadonlyRequestCookies;
vi.spyOn(result, "set");
const cookies = () => result;
return { ...mod, cookies } satisfies HeadersExport;
});
describe("createSignInCallback", () => {
it("should return true if not credentials request", async () => {
const isCredentialsRequest = false;
const signInCallback = createSignInCallback(
createAdapter(),
isCredentialsRequest,
);
const result = await signInCallback({
user: { id: "1", emailVerified: new Date("2023-01-13") },
account: {} as Account,
});
expect(result).toBe(true);
});
it("should return true if no user", async () => {
const isCredentialsRequest = true;
const signInCallback = createSignInCallback(
createAdapter(),
isCredentialsRequest,
);
const result = await signInCallback({
user: undefined as unknown as User,
account: {} as Account,
});
expect(result).toBe(true);
});
it("should return false if no adapter.createSession", async () => {
const isCredentialsRequest = true;
const signInCallback = createSignInCallback(
// https://github.com/nextauthjs/next-auth/issues/6106
undefined as unknown as Adapter,
isCredentialsRequest,
);
const result = await signInCallback({
user: { id: "1", emailVerified: new Date("2023-01-13") },
account: {} as Account,
});
expect(result).toBe(false);
});
it("should call adapter.createSession with correct input", async () => {
const adapter = createAdapter();
const isCredentialsRequest = true;
const signInCallback = createSignInCallback(adapter, isCredentialsRequest);
const user = { id: "1", emailVerified: new Date("2023-01-13") };
const account = {} as Account;
await signInCallback({ user, account });
expect(adapter.createSession).toHaveBeenCalledWith({
sessionToken: mockSessionToken,
userId: user.id,
expires: mockSessionExpiry,
});
expect(cookies().set).toHaveBeenCalledWith(
"next-auth.session-token",
mockSessionToken,
{
path: "/",
expires: mockSessionExpiry,
httpOnly: true,
sameSite: "lax",
secure: true,
},
);
});
});

View File

@@ -0,0 +1,47 @@
import { describe, expect, it } from "vitest";
import { createSalt, hashPassword } from "../security";
describe("createSalt should return a salt", () => {
it("should return a salt", async () => {
const result = await createSalt();
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(25);
});
it("should return a different salt each time", async () => {
const result1 = await createSalt();
const result2 = await createSalt();
expect(result1).not.toEqual(result2);
});
});
describe("hashPassword should return a hash", () => {
it("should return a hash", async () => {
const password = "password";
const salt = await createSalt();
const result = await hashPassword(password, salt);
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(55);
expect(result).not.toEqual(password);
});
it("should return a different hash each time", async () => {
const password = "password";
const password2 = "another password";
const salt = await createSalt();
const result1 = await hashPassword(password, salt);
const result2 = await hashPassword(password2, salt);
expect(result1).not.toEqual(result2);
});
it("should return a different hash for the same password with different salts", async () => {
const password = "password";
const salt1 = await createSalt();
const salt2 = await createSalt();
const result1 = await hashPassword(password, salt1);
const result2 = await hashPassword(password, salt2);
expect(result1).not.toEqual(result2);
});
});

View File

@@ -0,0 +1,43 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { z } from "@homarr/validation";
import { expireDateAfter, generateSessionToken } from "../session";
describe("expireDateAfter should calculate date after specified seconds", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it.each([
["2023-07-01T00:00:00Z", 60, "2023-07-01T00:01:00Z"], // 1 minute
["2023-07-01T00:00:00Z", 60 * 60, "2023-07-01T01:00:00Z"], // 1 hour
["2023-07-01T00:00:00Z", 60 * 60 * 24, "2023-07-02T00:00:00Z"], // 1 day
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 30, "2023-07-31T00:00:00Z"], // 30 days
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 365, "2024-06-30T00:00:00Z"], // 1 year
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 365 * 10, "2033-06-28T00:00:00Z"], // 10 years
])(
"should calculate date %s and after %i seconds to equal %s",
(initialDate, seconds, expectedDate) => {
vi.setSystemTime(new Date(initialDate));
const result = expireDateAfter(seconds);
expect(result).toEqual(new Date(expectedDate));
},
);
});
describe("generateSessionToken should return a random UUID", () => {
it("should return a random UUID", () => {
const result = generateSessionToken();
expect(z.string().uuid().safeParse(result).success).toBe(true);
});
it("should return a different token each time", () => {
const result1 = generateSessionToken();
const result2 = generateSessionToken();
expect(result1).not.toEqual(result2);
});
});

View File

@@ -1,10 +1,26 @@
import { describe, expect, it } from "vitest";
import { objectKeys } from "../object";
import { objectEntries, objectKeys } from "../object";
const testObjects = [
{ a: 1, c: 3, b: 2 },
{ a: 1, b: 2 },
{ a: 1 },
{},
] as const;
describe("objectKeys should return all keys of an object", () => {
it("should return all keys of an object", () => {
const obj = { a: 1, b: 2, c: 3 };
expect(objectKeys(obj)).toEqual(["a", "b", "c"]);
testObjects.forEach((obj) => {
it(`should return all keys of the object ${JSON.stringify(obj)}`, () => {
expect(objectKeys(obj)).toEqual(Object.keys(obj));
});
});
});
describe("objectEntries should return all entries of an object", () => {
testObjects.forEach((obj) => {
it(`should return all entries of the object ${JSON.stringify(obj)}`, () => {
expect(objectEntries(obj)).toEqual(Object.entries(obj));
});
});
});

View File

@@ -7,4 +7,5 @@ export default {
schema: "./schema",
driver: "better-sqlite",
dbCredentials: { url: process.env.DB_URL! },
out: "./migrations",
} satisfies Config;

View File

@@ -8,7 +8,7 @@ export const schema = sqliteSchema;
export * from "drizzle-orm";
const sqlite = new Database(process.env.DB_URL);
export const sqlite = new Database(process.env.DB_URL);
export const db = drizzle(sqlite, { schema });

View File

@@ -0,0 +1,112 @@
CREATE TABLE `account` (
`userId` text NOT NULL,
`type` text NOT NULL,
`provider` text NOT NULL,
`providerAccountId` text NOT NULL,
`refresh_token` text,
`access_token` text,
`expires_at` integer,
`token_type` text,
`scope` text,
`id_token` text,
`session_state` text,
PRIMARY KEY(`provider`, `providerAccountId`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `board` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`is_public` integer DEFAULT false NOT NULL,
`page_title` text,
`meta_title` text,
`logo_image_url` text,
`favicon_image_url` text,
`background_image_url` text,
`background_image_attachment` text DEFAULT 'fixed' NOT NULL,
`background_image_repeat` text DEFAULT 'no-repeat' NOT NULL,
`background_image_size` text DEFAULT 'cover' NOT NULL,
`primary_color` text DEFAULT 'red' NOT NULL,
`secondary_color` text DEFAULT 'orange' NOT NULL,
`primary_shade` integer DEFAULT 6 NOT NULL,
`app_opacity` integer DEFAULT 100 NOT NULL,
`custom_css` text,
`show_right_sidebar` integer DEFAULT false NOT NULL,
`show_left_sidebar` integer DEFAULT false NOT NULL,
`column_count` integer DEFAULT 10 NOT NULL
);
--> statement-breakpoint
CREATE TABLE `integration_item` (
`item_id` text NOT NULL,
`integration_id` text NOT NULL,
PRIMARY KEY(`integration_id`, `item_id`),
FOREIGN KEY (`item_id`) REFERENCES `item`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`integration_id`) REFERENCES `integration`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `integrationSecret` (
`kind` text NOT NULL,
`value` text NOT NULL,
`updated_at` integer NOT NULL,
`integration_id` text NOT NULL,
PRIMARY KEY(`integration_id`, `kind`),
FOREIGN KEY (`integration_id`) REFERENCES `integration`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `integration` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`url` text NOT NULL,
`kind` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `item` (
`id` text PRIMARY KEY NOT NULL,
`section_id` text NOT NULL,
`kind` text NOT NULL,
`x_offset` integer NOT NULL,
`y_offset` integer NOT NULL,
`width` integer NOT NULL,
`height` integer NOT NULL,
`options` text DEFAULT '{"json": {}}' NOT NULL,
FOREIGN KEY (`section_id`) REFERENCES `section`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `section` (
`id` text PRIMARY KEY NOT NULL,
`board_id` text NOT NULL,
`kind` text NOT NULL,
`position` integer NOT NULL,
`name` text,
FOREIGN KEY (`board_id`) REFERENCES `board`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `session` (
`sessionToken` text PRIMARY KEY NOT NULL,
`userId` text NOT NULL,
`expires` integer NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` text PRIMARY KEY NOT NULL,
`name` text,
`email` text,
`emailVerified` integer,
`image` text,
`password` text,
`salt` text
);
--> statement-breakpoint
CREATE TABLE `verificationToken` (
`identifier` text NOT NULL,
`token` text NOT NULL,
`expires` integer NOT NULL,
PRIMARY KEY(`identifier`, `token`)
);
--> statement-breakpoint
CREATE INDEX `userId_idx` ON `account` (`userId`);--> statement-breakpoint
CREATE INDEX `integration_secret__kind_idx` ON `integrationSecret` (`kind`);--> statement-breakpoint
CREATE INDEX `integration_secret__updated_at_idx` ON `integrationSecret` (`updated_at`);--> statement-breakpoint
CREATE INDEX `integration__kind_idx` ON `integration` (`kind`);--> statement-breakpoint
CREATE INDEX `user_id_idx` ON `session` (`userId`);

View File

@@ -0,0 +1,696 @@
{
"version": "5",
"dialect": "sqlite",
"id": "c9ea435a-5bbf-4439-84a1-55d3e2581b13",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"account": {
"name": "account",
"columns": {
"userId": {
"name": "userId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"type": {
"name": "type",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"provider": {
"name": "provider",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"providerAccountId": {
"name": "providerAccountId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"refresh_token": {
"name": "refresh_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"access_token": {
"name": "access_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expires_at": {
"name": "expires_at",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"token_type": {
"name": "token_type",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"scope": {
"name": "scope",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"id_token": {
"name": "id_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"session_state": {
"name": "session_state",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"userId_idx": {
"name": "userId_idx",
"columns": ["userId"],
"isUnique": false
}
},
"foreignKeys": {
"account_userId_user_id_fk": {
"name": "account_userId_user_id_fk",
"tableFrom": "account",
"tableTo": "user",
"columnsFrom": ["userId"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"account_provider_providerAccountId_pk": {
"columns": ["provider", "providerAccountId"],
"name": "account_provider_providerAccountId_pk"
}
},
"uniqueConstraints": {}
},
"board": {
"name": "board",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"is_public": {
"name": "is_public",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"page_title": {
"name": "page_title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"meta_title": {
"name": "meta_title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"logo_image_url": {
"name": "logo_image_url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"favicon_image_url": {
"name": "favicon_image_url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"background_image_url": {
"name": "background_image_url",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"background_image_attachment": {
"name": "background_image_attachment",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'fixed'"
},
"background_image_repeat": {
"name": "background_image_repeat",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'no-repeat'"
},
"background_image_size": {
"name": "background_image_size",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'cover'"
},
"primary_color": {
"name": "primary_color",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'red'"
},
"secondary_color": {
"name": "secondary_color",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'orange'"
},
"primary_shade": {
"name": "primary_shade",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 6
},
"app_opacity": {
"name": "app_opacity",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 100
},
"custom_css": {
"name": "custom_css",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"show_right_sidebar": {
"name": "show_right_sidebar",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"show_left_sidebar": {
"name": "show_left_sidebar",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"column_count": {
"name": "column_count",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 10
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"integration_item": {
"name": "integration_item",
"columns": {
"item_id": {
"name": "item_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"integration_id": {
"name": "integration_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"integration_item_item_id_item_id_fk": {
"name": "integration_item_item_id_item_id_fk",
"tableFrom": "integration_item",
"tableTo": "item",
"columnsFrom": ["item_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
},
"integration_item_integration_id_integration_id_fk": {
"name": "integration_item_integration_id_integration_id_fk",
"tableFrom": "integration_item",
"tableTo": "integration",
"columnsFrom": ["integration_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"integration_item_item_id_integration_id_pk": {
"columns": ["integration_id", "item_id"],
"name": "integration_item_item_id_integration_id_pk"
}
},
"uniqueConstraints": {}
},
"integrationSecret": {
"name": "integrationSecret",
"columns": {
"kind": {
"name": "kind",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"value": {
"name": "value",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"integration_id": {
"name": "integration_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"integration_secret__kind_idx": {
"name": "integration_secret__kind_idx",
"columns": ["kind"],
"isUnique": false
},
"integration_secret__updated_at_idx": {
"name": "integration_secret__updated_at_idx",
"columns": ["updated_at"],
"isUnique": false
}
},
"foreignKeys": {
"integrationSecret_integration_id_integration_id_fk": {
"name": "integrationSecret_integration_id_integration_id_fk",
"tableFrom": "integrationSecret",
"tableTo": "integration",
"columnsFrom": ["integration_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {
"integrationSecret_integration_id_kind_pk": {
"columns": ["integration_id", "kind"],
"name": "integrationSecret_integration_id_kind_pk"
}
},
"uniqueConstraints": {}
},
"integration": {
"name": "integration",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"url": {
"name": "url",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"kind": {
"name": "kind",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"integration__kind_idx": {
"name": "integration__kind_idx",
"columns": ["kind"],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"item": {
"name": "item",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"section_id": {
"name": "section_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"kind": {
"name": "kind",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"x_offset": {
"name": "x_offset",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"y_offset": {
"name": "y_offset",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"width": {
"name": "width",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"height": {
"name": "height",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"options": {
"name": "options",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "'{\"json\": {}}'"
}
},
"indexes": {},
"foreignKeys": {
"item_section_id_section_id_fk": {
"name": "item_section_id_section_id_fk",
"tableFrom": "item",
"tableTo": "section",
"columnsFrom": ["section_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"section": {
"name": "section",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"board_id": {
"name": "board_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"kind": {
"name": "kind",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"position": {
"name": "position",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"section_board_id_board_id_fk": {
"name": "section_board_id_board_id_fk",
"tableFrom": "section",
"tableTo": "board",
"columnsFrom": ["board_id"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"session": {
"name": "session",
"columns": {
"sessionToken": {
"name": "sessionToken",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"userId": {
"name": "userId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"user_id_idx": {
"name": "user_id_idx",
"columns": ["userId"],
"isUnique": false
}
},
"foreignKeys": {
"session_userId_user_id_fk": {
"name": "session_userId_user_id_fk",
"tableFrom": "session",
"tableTo": "user",
"columnsFrom": ["userId"],
"columnsTo": ["id"],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"emailVerified": {
"name": "emailVerified",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"salt": {
"name": "salt",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"verificationToken": {
"name": "verificationToken",
"columns": {
"identifier": {
"name": "identifier",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"token": {
"name": "token",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"verificationToken_identifier_token_pk": {
"columns": ["identifier", "token"],
"name": "verificationToken_identifier_token_pk"
}
},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1707511343363,
"tag": "0000_true_red_wolf",
"breakpoints": true
}
]
}

View File

@@ -4,7 +4,8 @@
"exports": {
".": "./index.ts",
"./client": "./client.ts",
"./schema/sqlite": "./schema/sqlite.ts"
"./schema/sqlite": "./schema/sqlite.ts",
"./test": "./test/index.ts"
},
"private": true,
"main": "./index.ts",
@@ -14,6 +15,7 @@
"clean": "rm -rf .turbo node_modules",
"lint": "eslint .",
"format": "prettier --check . --ignore-path ../../.gitignore",
"migration:generate": "drizzle-kit generate:sqlite",
"push": "drizzle-kit push:sqlite",
"studio": "drizzle-kit studio",
"typecheck": "tsc --noEmit"

View File

@@ -0,0 +1,14 @@
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import { schema } from "..";
export const createDb = () => {
const sqlite = new Database(":memory:");
const db = drizzle(sqlite, { schema });
migrate(db, {
migrationsFolder: "./packages/db/migrations",
});
return db;
};

View File

@@ -0,0 +1 @@
export * from "./db-mock";

View File

@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { objectEntries } from "@homarr/common";
import { integrationDefs } from "../integration";
describe("Icon url's of integrations should be valid and return 200", () => {
objectEntries(integrationDefs).forEach(([integration, { iconUrl }]) => {
it.concurrent(`should return 200 for ${integration}`, async () => {
const res = await fetch(iconUrl);
expect(res.status).toBe(200);
});
});
});

View File

@@ -2,3 +2,4 @@ export const supportedLanguages = ["en", "de"] as const;
export type SupportedLanguage = (typeof supportedLanguages)[number];
export const defaultLocale = "en";
export { languageMapping } from "./lang";

View File

@@ -29,10 +29,11 @@ const saveGeneralSettingsSchema = z.object({
.string()
.nullable()
.transform((value) => (value?.trim().length === 0 ? null : value)),
boardId: z.string(),
});
const saveSchema = z.object({
name: boardNameSchema,
boardId: z.string(),
sections: z.array(createSectionSchema(commonItemSchema)),
});

View File

@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { objectEntries } from "@homarr/common";
import { languageMapping } from "@homarr/translation";
import { widgetImports } from "..";
describe("Widget properties with description should have matching translations", async () => {
const enTranslation = await languageMapping().en();
objectEntries(widgetImports).forEach(([key, value]) => {
Object.entries(value.definition.options).forEach(
([optionKey, optionValue]: [string, { withDescription?: boolean }]) => {
it(`should have matching translations for ${optionKey} option description of ${key} widget`, () => {
const option = enTranslation.default.widget[key].option;
if (!(optionKey in option)) {
throw new Error(`Option ${optionKey} not found in translation`);
}
const value = option[optionKey as keyof typeof option];
expect("description" in value).toBe(optionValue.withDescription);
});
},
);
});
});
describe("Widget properties should have matching name translations", async () => {
const enTranslation = await languageMapping().en();
objectEntries(widgetImports).forEach(([key, value]) => {
Object.keys(value.definition.options).forEach((optionKey) => {
it(`should have matching translations for ${optionKey} option name of ${key} widget`, () => {
const option = enTranslation.default.widget[key].option;
if (!(optionKey in option)) {
throw new Error(`Option ${optionKey} not found in translation`);
}
const value = option[optionKey as keyof typeof option];
expect("label" in value).toBe(true);
});
});
});
});