⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
import * as email from "@kaneo/email";
import { eq } from "drizzle-orm";
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi,
} from "vite-plus/test";
import db, { schema } from "../../apps/api/src/database";
import { createApp } from "../../apps/api/src/index";
import getInstanceStatus from "../../apps/api/src/instance/controllers/get-instance-status";
import { promoteInitialAdministrator } from "../../apps/api/src/utils/instance-bootstrap";
import { assertUserRegistrationAllowed } from "../../apps/api/src/utils/registration-policy";
import { drainSignInEmails } from "../../apps/api/src/utils/sign-in-email-tasks";
import { resetTestDatabase } from "./helpers/database";
import { createWorkspaceMember } from "./helpers/fixtures";
const headers = {
"content-type": "application/json",
Origin: "http://localhost:5173",
};
const iphoneSafari =
"Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1";
async function post(
path: string,
body: unknown,
extraHeaders: Record<string, string> = {},
waitForEmail = true,
) {
const response = await createApp().app.request(`/api/auth${path}`, {
method: "POST",
headers: { ...headers, ...extraHeaders },
body: JSON.stringify(body),
});
if (waitForEmail) await drainSignInEmails();
return response;
}
function signup(email: string, extraHeaders?: Record<string, string>) {
return post(
"/sign-up/email",
{ name: "Test", email, password: "test-password-at-least-12" },
extraHeaders,
);
}
async function invitation(email: string) {
const inviter = await createWorkspaceMember({ role: "owner" });
const [row] = await db
.insert(schema.invitationTable)
.values({
email,
workspaceId: inviter.workspace.id,
inviterId: inviter.user.id,
role: "member",
status: "pending",
expiresAt: new Date(Date.now() + 60_000),
})
.returning();
return row;
}
describe("auth registration and bootstrap boundaries", () => {
beforeEach(async () => {
await resetTestDatabase();
});
afterEach(() => vi.unstubAllEnvs());
it("rejects anonymous bootstrap and ignores historical anonymous rows", async () => {
expect((await post("/sign-in/anonymous", {})).status).toBe(403);
expect(await db.select().from(schema.userTable)).toHaveLength(0);
await db.insert(schema.userTable).values({
id: "old-guest",
email: "[email protected]",
name: "Guest",
isAnonymous: true,
});
expect(await getInstanceStatus()).toEqual({
hasUsers: false,
hasAdmin: false,
});
vi.stubEnv("DISABLE_REGISTRATION", "true");
expect((await signup("[email protected]")).status).toBe(200);
expect(await getInstanceStatus()).toEqual({
hasUsers: true,
hasAdmin: true,
});
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.role, "admin")),
).toEqual([expect.objectContaining({ email: "[email protected]" })]);
});
it.each([
"DISABLE_REGISTRATION",
"DISABLE_PASSWORD_REGISTRATION",
"DISABLE_LOGIN_FORM",
"DISABLE_GUEST_ACCESS",
])("enforces %s on anonymous sign-in", async (flag) => {
await createWorkspaceMember();
vi.stubEnv(flag, "true");
expect((await post("/sign-in/anonymous", {})).status).toBe(403);
expect(await db.select().from(schema.userTable)).toHaveLength(1);
});
it("allows configured guest access on an initialized open instance", async () => {
await createWorkspaceMember();
expect((await post("/sign-in/anonymous", {})).status).toBe(200);
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.isAnonymous, true)),
).toHaveLength(1);
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.role, "admin")),
).toHaveLength(0);
});
it("elects exactly one admin when multiple first users already exist before the after-hooks", async () => {
await db.insert(schema.userTable).values(
["first", "second"].map((id) => ({
id,
name: id,
email: `${id}@example.com`,
createdAt: new Date("2026-01-01"),
})),
);
await Promise.all([
promoteInitialAdministrator("second"),
promoteInitialAdministrator("first"),
]);
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.role, "admin")),
).toEqual([expect.objectContaining({ id: "first" })]);
});
it("keeps one admin through concurrent HTTP first signups", async () => {
const responses = await Promise.all(
[1, 2, 3, 4].map((n) => signup(`race-${n}@example.com`)),
);
expect(responses.map((r) => r.status)).toEqual([200, 200, 200, 200]);
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.role, "admin")),
).toHaveLength(1);
});
it("does not give a new signup admin rights on an older instance lacking an admin", async () => {
const { user } = await createWorkspaceMember();
// Make the historical account unambiguously older across DB/app clocks.
await db
.update(schema.userTable)
.set({ createdAt: new Date("2020-01-01") })
.where(eq(schema.userTable.id, user.id));
expect((await signup("[email protected]")).status).toBe(200);
expect(
await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.role, "admin")),
).toHaveLength(0);
});
it("requires the matching invitation secret for password signup", async () => {
const invite = await invitation("[email protected]");
vi.stubEnv("DISABLE_REGISTRATION", "true");
expect((await signup(invite.email)).status).toBe(403);
expect(
(await signup(invite.email, { "x-invitation-id": "wrong" })).status,
).toBe(403);
expect(
(await signup("[email protected]", { "x-invitation-id": invite.id }))
.status,
).toBe(403);
expect(
(await signup(invite.email, { "x-invitation-id": invite.id })).status,
).toBe(200);
});
it("only accepts provider or mailbox verified email claims at the user-create boundary", async () => {
await invitation("[email protected]");
vi.stubEnv("DISABLE_REGISTRATION", "true");
for (const path of [
"/callback/github",
"/oauth2/callback/custom",
"/sign-in/email-otp",
"/magic-link/verify",
]) {
await expect(
assertUserRegistrationAllowed(
{ email: "[email protected]", emailVerified: false },
{ path },
),
).rejects.toMatchObject({ status: "FORBIDDEN" });
await expect(
assertUserRegistrationAllowed(
{ email: "[email protected]", emailVerified: true },
{ path },
),
).resolves.toBeUndefined();
}
await expect(
assertUserRegistrationAllowed(
{ email: "[email protected]", emailVerified: true },
{ path: "/sign-up/email" },
),
).rejects.toMatchObject({ status: "FORBIDDEN" });
});
it("prevents new OTP accounts under the local-registration restriction but preserves existing-user OTP login", async () => {
const member = await createWorkspaceMember();
vi.stubEnv("DISABLE_PASSWORD_REGISTRATION", "true");
const send = vi.spyOn(email, "sendOtpEmail").mockResolvedValue(undefined);
const sendMagicLink = vi
.spyOn(email, "sendMagicLinkEmail")
.mockResolvedValue(undefined);
for (const address of ["[email protected]", member.user.email]) {
expect(
(
await post(
"/email-otp/send-verification-otp",
{ email: address, type: "sign-in" },
{ "user-agent": iphoneSafari },
)
).status,
).toBe(200);
expect(
(
await post(
"/sign-in/magic-link",
{ email: address },
{ "user-agent": iphoneSafari },
)
).status,
).toBe(200);
}
expect(send).toHaveBeenCalledTimes(1);
expect(send.mock.lastCall?.[0]).toBe(member.user.email);
expect(sendMagicLink).toHaveBeenCalledTimes(1);
expect(sendMagicLink.mock.lastCall?.[0]).toBe(member.user.email);
const data = send.mock.lastCall?.[2] as { otp: string } | undefined;
expect(data?.otp).toBeTruthy();
const subject = send.mock.lastCall?.[1];
expect(typeof subject === "function" ? subject("Kaneo") : subject).toBe(
`${data?.otp} is your Kaneo sign-in code`,
);
const requestContext = expect.objectContaining({
device: { browser: "Safari", os: "iPhone" },
requestedAt: expect.any(Date),
});
expect(send.mock.lastCall?.[2]).toEqual(requestContext);
expect(sendMagicLink.mock.lastCall?.[2]).toEqual(requestContext);
const response = await post("/sign-in/email-otp", {
email: member.user.email,
otp: data?.otp,
});
expect(response.status).toBe(200);
expect(await db.select().from(schema.userTable)).toHaveLength(1);
});
it("still emails the first user's OTP and magic link for initial setup when registration is disabled", async () => {
vi.stubEnv("DISABLE_REGISTRATION", "true");
const sendOtp = vi
.spyOn(email, "sendOtpEmail")
.mockResolvedValue(undefined);
const sendMagicLink = vi
.spyOn(email, "sendMagicLinkEmail")
.mockResolvedValue(undefined);
expect(
(
await post("/email-otp/send-verification-otp", {
email: "[email protected]",
type: "sign-in",
})
).status,
).toBe(200);
expect(
(await post("/sign-in/magic-link", { email: "[email protected]" }))
.status,
).toBe(200);
expect(sendOtp.mock.calls.map((call) => call[0])).toEqual([
"[email protected]",
]);
expect(sendMagicLink.mock.calls.map((call) => call[0])).toEqual([
"[email protected]",
]);
const data = sendOtp.mock.lastCall?.[2] as { otp: string };
const response = await post("/sign-in/email-otp", {
email: "[email protected]",
otp: data.otp,
});
expect(response.status).toBe(200);
expect(await db.select().from(schema.userTable)).toHaveLength(1);
});
it("only emails sign-in OTPs and magic links to existing or invited addresses when registration is disabled", async () => {
const member = await createWorkspaceMember();
const invite = await invitation("[email protected]");
vi.stubEnv("DISABLE_REGISTRATION", "true");
const sendOtp = vi
.spyOn(email, "sendOtpEmail")
.mockResolvedValue(undefined);
const sendMagicLink = vi
.spyOn(email, "sendMagicLinkEmail")
.mockResolvedValue(undefined);
for (const address of [
member.user.email,
invite.email,
"[email protected]",
]) {
expect(
(
await post("/email-otp/send-verification-otp", {
email: address,
type: "sign-in",
})
).status,
).toBe(200);
expect(
(await post("/sign-in/magic-link", { email: address })).status,
).toBe(200);
}
const expectedRecipients = [invite.email, member.user.email].sort();
const recipients = (mock: {
mock: { calls: [to: string, subject: string, data: unknown][] };
}) => mock.mock.calls.map((call) => call[0]).sort();
expect(recipients(sendOtp)).toEqual(expectedRecipients);
expect(recipients(sendMagicLink)).toEqual(expectedRecipients);
});
it.each([
["/email-otp/send-verification-otp", "sendOtpEmail"],
["/sign-in/magic-link", "sendMagicLinkEmail"],
] as const)(
"returns from %s without waiting for SMTP",
async (path, method) => {
const member = await createWorkspaceMember();
const invite = await invitation("[email protected]");
vi.stubEnv("DISABLE_REGISTRATION", "true");
let releaseDelivery!: () => void;
const blockedDelivery = new Promise<void>((resolve) => {
releaseDelivery = resolve;
});
const send = vi.spyOn(email, method).mockReturnValue(blockedDelivery);
const responses: { status: number; body: unknown }[] = [];
const requests: Promise<Response>[] = [];
try {
for (const address of [
member.user.email,
invite.email,
"[email protected]",
]) {
const request = post(
path,
{ email: address, type: "sign-in" },
{},
false,
);
requests.push(request);
void request.then(async (response) => {
responses.push({
status: response.status,
body: await response.json(),
});
});
}
// The SMTP promise stays unresolved until all three HTTP responses arrive.
await vi.waitFor(() => expect(responses).toHaveLength(3));
expect(responses[0].status).toBe(200);
expect(responses).toEqual([responses[0], responses[0], responses[0]]);
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(2));
expect(send.mock.calls.map(([address]) => address).sort()).toEqual(
[member.user.email, invite.email].sort(),
);
} finally {
releaseDelivery();
await Promise.all(requests);
await drainSignInEmails();
}
},
);
it("stores a five-minute OTP expiry and rejects an expired code", async () => {
const member = await createWorkspaceMember();
const send = vi.spyOn(email, "sendOtpEmail").mockResolvedValue(undefined);
const before = Date.now();
expect(
(
await post("/email-otp/send-verification-otp", {
email: member.user.email,
type: "sign-in",
})
).status,
).toBe(200);
const rows = await db.select().from(schema.verificationTable);
expect(rows).toHaveLength(1);
expect(rows[0].expiresAt.getTime()).toBeGreaterThanOrEqual(
before + 300_000,
);
expect(rows[0].expiresAt.getTime()).toBeLessThanOrEqual(
Date.now() + 300_000,
);
const data = send.mock.lastCall?.[2] as { otp: string };
await db
.update(schema.verificationTable)
.set({ expiresAt: new Date(Date.now() - 1000) })
.where(eq(schema.verificationTable.id, rows[0].id));
expect(
(
await post("/sign-in/email-otp", {
email: member.user.email,
otp: data.otp,
})
).status,
).toBe(400);
expect(await db.select().from(schema.sessionTable)).toHaveLength(0);
});
describe("cloud email-code registration", () => {
beforeEach(() => {
vi.stubEnv("KANEO_CLOUD", "true");
vi.spyOn(email, "isSmtpConfigured").mockReturnValue(true);
});
it("creates a verified account with the submitted name from an email code", async () => {
await createWorkspaceMember();
const send = vi.spyOn(email, "sendOtpEmail").mockResolvedValue(undefined);
expect(
(
await post("/email-otp/send-verification-otp", {
email: "[email protected]",
type: "sign-in",
})
).status,
).toBe(200);
const data = send.mock.lastCall?.[2] as { otp: string };
const response = await post("/sign-in/email-otp", {
email: "[email protected]",
otp: data.otp,
name: "New Person",
});
expect(response.status).toBe(200);
const [user] = await db
.select()
.from(schema.userTable)
.where(eq(schema.userTable.email, "[email protected]"));
expect(user).toMatchObject({ name: "New Person", emailVerified: true });
});
it("refuses to send a code that would create an account for a disposable address", async () => {
await createWorkspaceMember();
const send = vi.spyOn(email, "sendOtpEmail").mockResolvedValue(undefined);
const sendMagicLink = vi
.spyOn(email, "sendMagicLinkEmail")
.mockResolvedValue(undefined);
const otp = await post("/email-otp/send-verification-otp", {
email: "[email protected]",
type: "sign-in",
});
expect(otp.status).toBe(400);
expect(await otp.json()).toMatchObject({
code: "DISPOSABLE_EMAIL_NOT_ALLOWED",
});
expect(
(
await post("/sign-in/magic-link", {
email: "[email protected]",
})
).status,
).toBe(400);
expect(send).not.toHaveBeenCalled();
expect(sendMagicLink).not.toHaveBeenCalled();
expect(await db.select().from(schema.verificationTable)).toHaveLength(0);
});
it("still sends codes to an existing account on a disposable address", async () => {
await createWorkspaceMember();
await db.insert(schema.userTable).values({
id: "existing-disposable",
email: "[email protected]",
name: "Kept",
});
const send = vi.spyOn(email, "sendOtpEmail").mockResolvedValue(undefined);
expect(
(
await post("/email-otp/send-verification-otp", {
email: "[email protected]",
type: "sign-in",
})
).status,
).toBe(200);
expect(send).toHaveBeenCalledTimes(1);
});
it("rejects disposable addresses at account creation on every path", async () => {
await createWorkspaceMember();
for (const path of ["/sign-in/email-otp", "/callback/github"]) {
await expect(
assertUserRegistrationAllowed(
{ email: "[email protected]", emailVerified: true },
{ path },
),
).rejects.toMatchObject({
body: expect.objectContaining({
code: "DISPOSABLE_EMAIL_NOT_ALLOWED",
}),
});
}
});
it("requires an email code instead of a password for new cloud accounts", async () => {
await createWorkspaceMember();
const response = await signup("[email protected]");
expect(response.status).toBe(403);
expect(await response.json()).toMatchObject({
code: "EMAIL_CODE_REGISTRATION_REQUIRED",
});
expect(await db.select().from(schema.userTable)).toHaveLength(1);
});
it("keeps password sign-up for the first account on a fresh instance", async () => {
expect((await signup("[email protected]")).status).toBe(200);
});
});
});