public
anord
read
Ledger
Why work hard when you can work easier?
Languages
Repository composition by tracked source files.
TypeScript
86%
CSS
10%
SQL
3%
Shell
1%
HTML
0%
Trace
apps/api/src/test/http.test.ts
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
import request from "supertest";
2
import { beforeEach, describe, expect, it, vi } from "vitest";
4
const queryMock = vi.fn();
5
const enqueueWebhookEvent = vi.fn();
6
const verifyPassword = vi.fn();
7
const getUserForSession = vi.fn();
8
const createSessionToken = vi.fn(() => "token");
10
vi.mock("../db/pool.js", () => ({
11
pool: {
12
query: queryMock
13
}
14
}));
16
vi.mock("../services/webhooks.js", () => ({
17
enqueueWebhookEvent
18
}));
20
vi.mock("../services/auth.js", () => ({
21
verifyPassword,
22
getUserForSession,
23
createSessionToken,
24
verifySessionToken: vi.fn(),
25
hashPassword: vi.fn()
26
}));
28
describe("http flows", () => {
29
beforeEach(() => {
30
queryMock.mockReset();
31
enqueueWebhookEvent.mockReset();
32
verifyPassword.mockReset();
33
getUserForSession.mockReset();
34
createSessionToken.mockClear();
35
});
37
it("logs in with valid credentials", async () => {
38
queryMock
39
.mockResolvedValueOnce({
40
rowCount: 1,
41
rows: [{ id: "user-1", password_hash: "hash" }]
42
})
43
.mockResolvedValueOnce({ rowCount: 1, rows: [] });
44
verifyPassword.mockResolvedValueOnce(true);
45
getUserForSession.mockResolvedValueOnce({
46
id: "user-1",
47
email: "[email protected]",
48
displayName: "Owner",
49
role: "owner",
50
groupIds: []
51
});
53
const { createApp } = await import("../app.js");
54
const response = await request(createApp()).post("/api/auth/login").send({
55
email: "[email protected]",
56
password: "Password123!"
57
});
59
expect(response.status).toBe(200);
60
expect(response.body.user.email).toBe("[email protected]");
61
expect(response.headers["set-cookie"]).toBeTruthy();
62
});
64
it("blocks register before initial setup is completed", async () => {
65
queryMock.mockResolvedValueOnce({
66
rowCount: 1,
67
rows: [{ count: 0 }]
68
});
70
const { createApp } = await import("../app.js");
71
const response = await request(createApp()).post("/api/auth/register").send({
72
email: "[email protected]",
73
password: "Password123!",
74
displayName: "First User"
75
});
77
expect(response.status).toBe(403);
78
expect(response.body.error).toContain("initial Ledger setup");
79
});
81
it("creates feedback records", async () => {
82
queryMock
83
.mockResolvedValueOnce({ rowCount: 1, rows: [{ id: "feedback-1" }] })
84
.mockResolvedValueOnce({ rowCount: 1, rows: [] });
86
const { createApp } = await import("../app.js");
87
const response = await request(createApp()).post("/api/feedback").send({
88
pageId: "27ef6842-a7e5-41ab-a18a-f2d0ca9d1d85",
89
revisionId: "8f8381fd-e2d3-4093-a2c6-59c59f119abc",
90
helpful: true,
91
comment: "Solved it"
92
});
94
expect(response.status).toBe(201);
95
expect(response.body.feedbackId).toBe("feedback-1");
96
expect(enqueueWebhookEvent).toHaveBeenCalledWith(
97
"feedback.created",
98
expect.objectContaining({ feedbackId: "feedback-1" })
99
);
100
});
101
});