v3: the backend gets a test suite, and the bugs it turned up are fixed — deno task test runs 186 tests over api/ in about 18s, against a throwaway database and uploads directory (GERBEUR_DB_PATH and GERBEUR_UPLOADS_DIR now override the defaults; api/test/setup.ts sets them before any app module loads), with requests sent through the whole middleware stack in-process via app.handle, outbound fetches and DNS stubbed, and the socket tests on a real Oak listener since an upgraded request has no response for handle() to return. It covers auth (token purposes, revocation after a password change, live roles, login rate limiting, invites claimed once even under parallel registrations, password resets), dumps end to end (create, privacy, by-url matching on the canonical key, backlinks, categories, edits, file replacement, thumbnails, refresh, delete), file serving headers and ranges, attachments, avatars, comments and likes, playlists, follows and feeds, notifications and mentions, categories, search, chat cursors and read markers, link previews and the SSRF guard on every redirect hop, the Bandcamp and YouTube resolvers, migrations (a fresh schema is left untouched; the data rewrites in 0004/0005 and 0010–0013 do what they say), CORS, OG tags, static caching, and the WebSocket protocol. Fixed along the way: an unknown category id on a new dump returned 400 but kept the dump row, and for a file dump deleted the file while keeping the row; an edit or file replacement with one was half-applied before the 400 — categories are now checked before anything is written. A suffix range (bytes=-3) was read as an end offset and got a 416 instead of the last three bytes. Reordering a playlist accepted a list naming a member twice. verifyPassword threw on a stored hash without a salt instead of answering false. And a request with no Accept-Encoding, which RFC 9110 lets a server treat as accepting anything, was sent the brotli sibling; curl and bots now get the plain file.
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 46s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 46s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
260
api/test/comments.test.ts
Normal file
260
api/test/comments.test.ts
Normal file
@@ -0,0 +1,260 @@
|
||||
import {
|
||||
count,
|
||||
createUser,
|
||||
db,
|
||||
del,
|
||||
get,
|
||||
insertComment,
|
||||
insertDump,
|
||||
patch,
|
||||
post,
|
||||
test,
|
||||
} from "./helpers.ts";
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
castCommentLike,
|
||||
removeCommentLike,
|
||||
} from "../services/comment-like-service.ts";
|
||||
import { APIException } from "../model/interfaces.ts";
|
||||
|
||||
test("comments: post, list in order, count on the dump", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
const { id } = insertDump({ userId: owner.id });
|
||||
const first = await post(`/api/dumps/${id}/comments`, {
|
||||
auth: other.auth,
|
||||
json: { body: " first " },
|
||||
});
|
||||
assertEquals(first.status, 201);
|
||||
assertEquals(first.body.data.body, "first");
|
||||
assertEquals(first.body.data.authorUsername, other.username);
|
||||
const reply = await post(`/api/dumps/${id}/comments`, {
|
||||
auth: owner.auth,
|
||||
json: { body: "reply", parentId: first.body.data.id },
|
||||
});
|
||||
assertEquals(reply.body.data.parentId, first.body.data.id);
|
||||
const list = await get(`/api/dumps/${id}/comments`);
|
||||
assertEquals(list.body.data.map((c: { body: string }) => c.body), [
|
||||
"first",
|
||||
"reply",
|
||||
]);
|
||||
assertEquals((await get(`/api/dumps/${id}`)).body.data.commentCount, 2);
|
||||
});
|
||||
|
||||
test("comments: validation, auth and unknown dumps", async () => {
|
||||
const user = await createUser();
|
||||
const { id } = insertDump({ userId: user.id });
|
||||
for (const body of ["", " ", "x".repeat(5001)]) {
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/${id}/comments`, {
|
||||
auth: user.auth,
|
||||
json: { body },
|
||||
}))
|
||||
.status,
|
||||
400,
|
||||
);
|
||||
}
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/${id}/comments`, { json: { body: "hi" } })).status,
|
||||
401,
|
||||
);
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/nope/comments`, {
|
||||
auth: user.auth,
|
||||
json: { body: "hi" },
|
||||
}))
|
||||
.status,
|
||||
404,
|
||||
);
|
||||
});
|
||||
|
||||
test("comments on a private dump: owner only", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
const { id } = insertDump({ userId: owner.id, isPrivate: true });
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/${id}/comments`, {
|
||||
auth: other.auth,
|
||||
json: { body: "x" },
|
||||
}))
|
||||
.status,
|
||||
404,
|
||||
);
|
||||
assertEquals(
|
||||
(await post(`/api/dumps/${id}/comments`, {
|
||||
auth: owner.auth,
|
||||
json: { body: "x" },
|
||||
}))
|
||||
.status,
|
||||
201,
|
||||
);
|
||||
assertEquals(
|
||||
(await get(`/api/dumps/${id}/comments`, { auth: other.auth })).status,
|
||||
404,
|
||||
);
|
||||
});
|
||||
|
||||
test("comments: the dump's owner is notified, the commenter isn't", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
const { id } = insertDump({ userId: owner.id, title: "Mine" });
|
||||
await post(`/api/dumps/${id}/comments`, {
|
||||
auth: owner.auth,
|
||||
json: { body: "self" },
|
||||
});
|
||||
assertEquals(count(`SELECT COUNT(*) as n FROM notifications;`), 0);
|
||||
const c = await post(`/api/dumps/${id}/comments`, {
|
||||
auth: other.auth,
|
||||
json: { body: "nice" },
|
||||
});
|
||||
const note = db.prepare(`SELECT user_id, type, data FROM notifications;`)
|
||||
.get() as {
|
||||
user_id: string;
|
||||
type: string;
|
||||
data: string;
|
||||
};
|
||||
assertEquals(note.user_id, owner.id);
|
||||
assertEquals(note.type, "dump_commented");
|
||||
assertEquals(JSON.parse(note.data).commentId, c.body.data.id);
|
||||
assertEquals(JSON.parse(note.data).dumpTitle, "Mine");
|
||||
});
|
||||
|
||||
test("PATCH /api/comments/:id: author and moderator edit, others can't", async () => {
|
||||
const author = await createUser();
|
||||
const other = await createUser();
|
||||
const mod = await createUser({ role: "moderator" });
|
||||
const { id: dumpId } = insertDump({ userId: author.id });
|
||||
const commentId = insertComment({ dumpId, userId: author.id, body: "orig" });
|
||||
assertEquals(
|
||||
(await patch(`/api/comments/${commentId}`, {
|
||||
auth: other.auth,
|
||||
json: { body: "x" },
|
||||
}))
|
||||
.status,
|
||||
401,
|
||||
);
|
||||
const edited = await patch(`/api/comments/${commentId}`, {
|
||||
auth: author.auth,
|
||||
json: { body: " edited " },
|
||||
});
|
||||
assertEquals(edited.status, 200);
|
||||
assertEquals(edited.body.data.body, "edited");
|
||||
assertExists(edited.body.data.updatedAt);
|
||||
assertEquals(
|
||||
(await patch(`/api/comments/${commentId}`, {
|
||||
auth: mod.auth,
|
||||
json: { body: "m" },
|
||||
}))
|
||||
.status,
|
||||
200,
|
||||
);
|
||||
assertEquals(
|
||||
(await patch(`/api/comments/${commentId}`, {
|
||||
auth: author.auth,
|
||||
json: { body: "" },
|
||||
}))
|
||||
.status,
|
||||
400,
|
||||
);
|
||||
assertEquals(
|
||||
(await patch(`/api/comments/${crypto.randomUUID()}`, {
|
||||
auth: author.auth,
|
||||
json: { body: "x" },
|
||||
})).status,
|
||||
404,
|
||||
);
|
||||
});
|
||||
|
||||
test("DELETE /api/comments/:id soft-deletes, and a deleted comment can't be edited", async () => {
|
||||
const author = await createUser();
|
||||
const other = await createUser();
|
||||
const { id: dumpId } = insertDump({ userId: author.id });
|
||||
const commentId = insertComment({
|
||||
dumpId,
|
||||
userId: author.id,
|
||||
body: "gone soon",
|
||||
});
|
||||
assertEquals(
|
||||
(await del(`/api/comments/${commentId}`, { auth: other.auth })).status,
|
||||
401,
|
||||
);
|
||||
assertEquals(
|
||||
(await del(`/api/comments/${commentId}`, { auth: author.auth })).status,
|
||||
200,
|
||||
);
|
||||
const [c] = (await get(`/api/dumps/${dumpId}/comments`)).body.data;
|
||||
assertEquals(c.deleted, true);
|
||||
assertEquals(c.body, "");
|
||||
assertEquals((await get(`/api/dumps/${dumpId}`)).body.data.commentCount, 0);
|
||||
assertEquals(
|
||||
(await patch(`/api/comments/${commentId}`, {
|
||||
auth: author.auth,
|
||||
json: { body: "back" },
|
||||
}))
|
||||
.status,
|
||||
400,
|
||||
);
|
||||
});
|
||||
|
||||
test("comment likes: count, conflicts, likers list and notifications", async () => {
|
||||
const author = await createUser();
|
||||
const liker = await createUser();
|
||||
const { id: dumpId } = insertDump({ userId: author.id });
|
||||
const commentId = insertComment({ dumpId, userId: author.id });
|
||||
|
||||
assertEquals(castCommentLike(commentId, liker.id).likeCount, 1);
|
||||
assertEquals(castCommentLike(commentId, author.id).likeCount, 2);
|
||||
try {
|
||||
castCommentLike(commentId, liker.id);
|
||||
throw new Error("expected a conflict");
|
||||
} catch (err) {
|
||||
assertEquals((err as APIException).status, 409);
|
||||
}
|
||||
// Only the other user's like notifies the author.
|
||||
assertEquals(
|
||||
count(
|
||||
`SELECT COUNT(*) as n FROM notifications WHERE type = 'comment_liked';`,
|
||||
),
|
||||
1,
|
||||
);
|
||||
|
||||
const likers = await get(`/api/comments/${commentId}/likers`);
|
||||
assertEquals(likers.body.data.total, 2);
|
||||
assertEquals(likers.body.data.items[0].email, undefined);
|
||||
|
||||
assertEquals(removeCommentLike(commentId, liker.id).likeCount, 1);
|
||||
try {
|
||||
removeCommentLike(commentId, liker.id);
|
||||
throw new Error("expected a 404");
|
||||
} catch (err) {
|
||||
assertEquals((err as APIException).status, 404);
|
||||
}
|
||||
assertEquals(
|
||||
(db.prepare(`SELECT like_count FROM comments WHERE id = ?;`).get(
|
||||
commentId,
|
||||
) as {
|
||||
like_count: number;
|
||||
}).like_count,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test("comment likes on a private dump: owner only", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
const { id: dumpId } = insertDump({ userId: owner.id, isPrivate: true });
|
||||
const commentId = insertComment({ dumpId, userId: owner.id });
|
||||
try {
|
||||
castCommentLike(commentId, other.id);
|
||||
throw new Error("expected a 404");
|
||||
} catch (err) {
|
||||
assertEquals((err as APIException).status, 404);
|
||||
}
|
||||
assertEquals(castCommentLike(commentId, owner.id).isPrivate, true);
|
||||
assertEquals((await get(`/api/comments/${commentId}/likers`)).status, 404);
|
||||
assertEquals(
|
||||
(await get(`/api/comments/${commentId}/likers`, { auth: owner.auth }))
|
||||
.status,
|
||||
200,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user