mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-01-31 00:21:20 -05:00
* test: backups service * refactor: create hono app in a separate file To avoid side effects like db migration or startup scripts when testing test(backups): add security tests to the backups controller * ci: run typechecks, build and tests on PR * test: controllers security tests * chore: update lock file * refactor: pr feedbacks
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import { test, describe, expect } from "bun:test";
|
|
import { createApp } from "~/server/app";
|
|
import { createTestSession } from "~/test/helpers/auth";
|
|
|
|
const app = createApp();
|
|
|
|
describe("system security", () => {
|
|
test("should return 401 if no session cookie is provided", async () => {
|
|
const res = await app.request("/api/v1/system/info");
|
|
expect(res.status).toBe(401);
|
|
const body = await res.json();
|
|
expect(body.message).toBe("Authentication required");
|
|
});
|
|
|
|
test("should return 401 if session is invalid", async () => {
|
|
const res = await app.request("/api/v1/system/info", {
|
|
headers: {
|
|
Cookie: "session_id=invalid-session",
|
|
},
|
|
});
|
|
expect(res.status).toBe(401);
|
|
const body = await res.json();
|
|
expect(body.message).toBe("Invalid or expired session");
|
|
|
|
expect(res.headers.get("Set-Cookie")).toContain("session_id=;");
|
|
});
|
|
|
|
test("should return 200 if session is valid", async () => {
|
|
const { sessionId } = await createTestSession();
|
|
|
|
const res = await app.request("/api/v1/system/info", {
|
|
headers: {
|
|
Cookie: `session_id=${sessionId}`,
|
|
},
|
|
});
|
|
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
describe("unauthenticated access", () => {
|
|
const endpoints: { method: string; path: string }[] = [
|
|
{ method: "GET", path: "/api/v1/system/info" },
|
|
{ method: "POST", path: "/api/v1/system/restic-password" },
|
|
];
|
|
|
|
for (const { method, path } of endpoints) {
|
|
test(`${method} ${path} should return 401`, async () => {
|
|
const res = await app.request(path, { method });
|
|
expect(res.status).toBe(401);
|
|
const body = await res.json();
|
|
expect(body.message).toBe("Authentication required");
|
|
});
|
|
}
|
|
});
|
|
|
|
describe("input validation", () => {
|
|
test("should return 400 for invalid payload on restic-password", async () => {
|
|
const { sessionId } = await createTestSession();
|
|
const res = await app.request("/api/v1/system/restic-password", {
|
|
method: "POST",
|
|
headers: {
|
|
Cookie: `session_id=${sessionId}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({}),
|
|
});
|
|
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
test("should return 401 for incorrect password on restic-password", async () => {
|
|
const { sessionId } = await createTestSession();
|
|
const res = await app.request("/api/v1/system/restic-password", {
|
|
method: "POST",
|
|
headers: {
|
|
Cookie: `session_id=${sessionId}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
password: "wrong-password",
|
|
}),
|
|
});
|
|
|
|
expect(res.status).toBe(401);
|
|
const body = await res.json();
|
|
expect(body.message).toBe("Incorrect password");
|
|
});
|
|
});
|
|
});
|