Add an authenticated route that sends Discord direct messages for trusted apps
Some checks failed
Deploy Palico Bot / deploy (push) Failing after 3s
Some checks failed
Deploy Palico Bot / deploy (push) Failing after 3s
- POST /api/notifications/dm takes a user id and a text, sends it as a direct message with no active mentions, and answers 204, 400, 404 or 422. - The send logic lives in src/features/directMessages.js with its own tests; the route gets its own per-minute rate limit behind the existing API key. - Sokko TCG uses it for its opt-in Expedition alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
20624a5c9c
commit
3655b1ad74
6 changed files with 183 additions and 56 deletions
46
test/directMessages.test.js
Normal file
46
test/directMessages.test.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { DirectMessageError, sendDirectMessage } from "../src/features/directMessages.js";
|
||||
|
||||
const USER_ID = "123456789012345678";
|
||||
|
||||
function fakeClient({ fetchError = null, sendError = null } = {}) {
|
||||
const sent = [];
|
||||
return {
|
||||
sent,
|
||||
users: {
|
||||
async fetch(userId) {
|
||||
if (fetchError) throw fetchError;
|
||||
return {
|
||||
id: userId,
|
||||
async send(message) {
|
||||
if (sendError) throw sendError;
|
||||
sent.push({ userId, ...message });
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test("sendDirectMessage sends the text to the user without active mentions", async () => {
|
||||
const client = fakeClient();
|
||||
await sendDirectMessage(client, { userId: USER_ID, content: "Votre expédition est de retour !" });
|
||||
assert.deepEqual(client.sent, [
|
||||
{ userId: USER_ID, content: "Votre expédition est de retour !", allowedMentions: { parse: [] } },
|
||||
]);
|
||||
});
|
||||
|
||||
test("sendDirectMessage rejects invalid users and contents before calling Discord", async () => {
|
||||
const client = fakeClient();
|
||||
for (const body of [{ userId: "dev-admin", content: "x" }, { userId: USER_ID, content: " " }, { userId: USER_ID, content: "x".repeat(2001) }, undefined]) {
|
||||
await assert.rejects(sendDirectMessage(client, body), (error) => error instanceof DirectMessageError && error.status === 400);
|
||||
}
|
||||
assert.equal(client.sent.length, 0);
|
||||
});
|
||||
|
||||
test("sendDirectMessage maps unknown users and closed messages to client errors", async () => {
|
||||
await assert.rejects(sendDirectMessage(fakeClient({ fetchError: { code: 10013 } }), { userId: USER_ID, content: "x" }), (error) => error.status === 404 && error.code === "UNKNOWN_USER");
|
||||
await assert.rejects(sendDirectMessage(fakeClient({ sendError: { code: 50007 } }), { userId: USER_ID, content: "x" }), (error) => error.status === 422 && error.code === "DM_CLOSED");
|
||||
await assert.rejects(sendDirectMessage(fakeClient({ sendError: new Error("network") }), { userId: USER_ID, content: "x" }), /network/);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue