mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
feat(admin): add email activity and investigation workflow
This commit is contained in:
1 parent
8f272b7c7b
commit
99bf9f173e
10 files changed
+571
-169
No files matched your search
+113
-5
@@ -1,3 +1,4 @@
|
||||
const { URL, URLSearchParams } = require("node:url");
|
||||
const { expect } = require("chai");
|
||||
const fs = require("node:fs");
|
||||
const vm = require("node:vm");
|
||||
@@ -7,11 +8,11 @@ const sandbox = { console, Date, Intl }; vm.runInNewContext(script, sandbox);
|
||||
const tick = () => new Promise(resolve => require("node:timers").setImmediate(resolve));
|
||||
|
||||
describe("email admin UI requests", () => {
|
||||
let state, pending;
|
||||
let state, pending, summaries, dispose;
|
||||
beforeEach(() => {
|
||||
pending = [];
|
||||
state = { user: {}, on() {} };
|
||||
sandbox.controller(state, { get: (url, options) => new Promise((resolve, reject) => pending.push({ url, options, resolve, reject })) }, { url() {} });
|
||||
pending = []; summaries = [];
|
||||
state = { user: {}, on(event, callback) { if (event === "dispose") dispose = callback; } };
|
||||
sandbox.controller(state, { get: (url, options) => new Promise((resolve, reject) => (url.endsWith("/summary") ? summaries : pending).push({ url, options, resolve, reject })) }, { url() {} });
|
||||
});
|
||||
const response = (entries, nextCursor = null) => ({ data: { entries, nextCursor } });
|
||||
it("keeps visible history when a refresh fails", async () => {
|
||||
@@ -44,6 +45,113 @@ describe("email admin UI requests", () => {
|
||||
state.emailKind = "owner-access"; expect(state.hasEmailFilters()).to.equal(true);
|
||||
const clear = state.clearEmailFilters();
|
||||
expect(state.hasEmailFilters()).to.equal(false);
|
||||
pending[1].resolve(response([])); await clear;
|
||||
pending[1].resolve(response([])); summaries[1].resolve({ data: { counts: {}, total: 0, series: [] } }); await clear;
|
||||
});
|
||||
it("loads bounded pages and fetches older history only at the loaded boundary", async () => {
|
||||
pending[0].resolve(response(Array.from({ length: 50 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
|
||||
expect(state.emailVisibleRows()).to.have.length(25);
|
||||
await state.emailNextPage(); expect(state.emailPage).to.equal(2); expect(pending).to.have.length(1);
|
||||
const next = state.emailNextPage();
|
||||
expect(pending[1].options.params.before).to.equal("older");
|
||||
pending[1].resolve(response([{ _id: "50" }])); await next;
|
||||
expect(state.emailPage).to.equal(3); expect(state.emailVisibleRows()[0]._id).to.equal("50");
|
||||
state.emailPreviousPage(); expect(state.emailVisibleRows()).to.have.length(25);
|
||||
});
|
||||
it("does not turn unavailable summary data into zero counts", async () => {
|
||||
summaries[0].reject(Error("offline")); await tick();
|
||||
expect(state.emailTotal()).to.equal(null); expect(state.emailPageCount()).to.equal(null);
|
||||
expect(state.emailSummaryError).not.to.equal("");
|
||||
});
|
||||
it("ignores stale summary responses and applies search only on submit", async () => {
|
||||
state.emailSearch = " author@example.com ";
|
||||
expect(pending[0].options.params.search).to.equal(undefined);
|
||||
const current = state.applyEmailFilters();
|
||||
expect(pending[1].options.params.search).to.equal("author@example.com");
|
||||
expect(summaries[1].options.params.state).to.equal(undefined);
|
||||
pending[1].resolve(response([])); summaries[1].resolve({ data: { counts: { rejected: 3 }, total: 3, series: [] } }); await current;
|
||||
summaries[0].resolve({ data: { counts: {}, total: 99, series: [] } }); await tick();
|
||||
expect(state.emailTotal()).to.equal(3);
|
||||
const status = state.setEmailStatus("rejected"); expect(summaries).to.have.length(2);
|
||||
pending[2].resolve(response([])); await status; expect(state.emailTotal()).to.equal(3);
|
||||
});
|
||||
it("ignores responses after disposal", async () => {
|
||||
dispose(); pending[0].resolve(response([{ _id: "late" }]));
|
||||
summaries[0].resolve({ data: { total: 99 } }); await tick();
|
||||
expect(state.emailRows).to.have.length(0); expect(state.emailSummary).to.equal(null);
|
||||
});
|
||||
it("retains summary totals and the current page when requests fail", async () => {
|
||||
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older"));
|
||||
summaries[0].resolve({ data: { counts: { accepted: 30 }, total: 30, series: [] } }); await tick();
|
||||
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
|
||||
expect(state.emailPage).to.equal(1); expect(state.emailVisibleRows()).to.have.length(25);
|
||||
const summary = state.loadEmailSummary(); summaries[1].reject(Error("offline")); await summary;
|
||||
expect(state.emailTotal()).to.equal(30); expect(state.emailSummaryError).to.include("Previously loaded");
|
||||
});
|
||||
it("restores valid URL filters and preserves unrelated URL state", async () => {
|
||||
let saved;
|
||||
const local = { console, Date, Intl, URL, URLSearchParams, window: {
|
||||
location: { search: "?state=rejected&period=30d&search=author%40example.com&other=keep", href: "http://localhost/admin/emails?state=rejected&period=30d&search=author%40example.com&other=keep#history" },
|
||||
history: { replaceState(a, b, value) { saved = value; } },
|
||||
} };
|
||||
vm.runInNewContext(script, local);
|
||||
const restored = { user: {}, on() {} };
|
||||
local.controller(restored, { get: () => new Promise(() => {}) }, { url() {} });
|
||||
expect(restored.emailState).to.equal("rejected"); expect(restored.emailPeriod).to.equal("30d");
|
||||
expect(restored.emailSearch).to.equal("author@example.com");
|
||||
expect(saved).to.include("other=keep"); expect(saved).to.include("#history");
|
||||
});
|
||||
it("advances to the intended page after retrying a failed Next", async () => {
|
||||
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
|
||||
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
|
||||
expect(state.emailPage).to.equal(1);
|
||||
const retry = state.retryEmails(); pending[2].resolve(response([{ _id: "next" }])); await retry;
|
||||
expect(state.emailPage).to.equal(2); expect(state.emailVisibleRows()[0]._id).to.equal("next");
|
||||
expect(state.emailAnnouncement).to.equal("Page 2. Showing attempts 26 to 26.");
|
||||
state.emailPreviousPage(); expect(state.emailAnnouncement).to.equal("Page 1. Showing attempts 1 to 25.");
|
||||
await state.emailNextPage(); expect(state.emailAnnouncement).to.equal("Page 2. Showing attempts 26 to 26.");
|
||||
});
|
||||
it("resets a failed page target on filter changes", async () => {
|
||||
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
|
||||
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
|
||||
const filtered = state.setEmailStatus("rejected"); pending[2].resolve(response([{ _id: "filtered" }], "filteredOlder")); await filtered;
|
||||
const more = state.loadEmails(true); pending[3].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: "extra" + i })))); await more;
|
||||
expect(state.emailPage).to.equal(1);
|
||||
});
|
||||
it("handles empty chart date endpoints", () => {
|
||||
expect(state.emailDayLabel(undefined)).to.equal(""); expect(state.emailDayLabel({})).to.equal("");
|
||||
expect(state.emailDayLabel({ date: "not-a-date" })).to.equal("");
|
||||
expect(state.emailDayLabel({ date: "2026-11-14" })).not.to.equal("");
|
||||
});
|
||||
it("focuses and scrolls the history heading only on explicit paging", async () => {
|
||||
let focused = 0, scrolled = 0;
|
||||
const heading = { setAttribute(key, value) { expect([key, value]).to.deep.equal(["tabindex", "-1"]); }, focus(options) { expect(options.preventScroll).to.equal(true); focused++; }, scrollIntoView() { scrolled++; } };
|
||||
const local = { console, Date, Intl, URL, URLSearchParams, window: {
|
||||
location: { search: "", href: "http://localhost/admin/emails" }, history: { replaceState() {} },
|
||||
document: { querySelector(selector) { return selector === "#email-history-title" ? heading : null; } },
|
||||
} };
|
||||
vm.runInNewContext(script, local);
|
||||
const view = { user: {}, on() {} };
|
||||
local.controller(view, { get: url => Promise.resolve(url.endsWith("/summary") ? { data: { counts: {}, total: 50, series: [] } } : response(Array.from({ length: 50 }, (_, i) => ({ _id: String(i) })))) }, { url() {} });
|
||||
await tick(); expect(focused).to.equal(0);
|
||||
await view.emailNextPage(); expect(focused).to.equal(1); expect(scrolled).to.equal(1);
|
||||
await view.refreshEmails(); expect(focused).to.equal(1);
|
||||
});
|
||||
it("shows years for older dates and all-time history", () => {
|
||||
const currentYear = new Date().getFullYear();
|
||||
expect(state.emailShortTime(`${currentYear - 1}-06-01T12:00:00Z`)).to.include(String(currentYear - 1));
|
||||
state.emailPeriod = "all";
|
||||
expect(state.emailShortTime(`${currentYear}-06-01T12:00:00Z`)).to.include(String(currentYear));
|
||||
});
|
||||
it("explains outcomes without inventing delivery evidence", () => {
|
||||
expect(state.emailExplanation({ state: "accepted" })).to.include("not confirmed");
|
||||
expect(state.emailExplanation({ state: "rejected", httpStatus: 429 })).to.include("rate limiting");
|
||||
expect(state.emailExplanation({ state: "unknown" })).to.include("avoid duplicates");
|
||||
});
|
||||
it("uses displayed chart activity independently of all-time totals", () => {
|
||||
state.emailSummary = {total: 100, series: [{date: "2026-11-14", accepted: 0, rejected: 0, unknown: 0, sending: 0}]};
|
||||
expect(state.emailChartTotal()).to.equal(0);
|
||||
state.emailSummary.series[0].unknown = 3;
|
||||
expect(state.emailChartTotal()).to.equal(3);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -4,7 +4,7 @@ require("ts-node/register/transpile-only");
|
||||
const Users = require("../src/core/model/users/users.model").default;
|
||||
const history = require("../src/core/email-history");
|
||||
const admin = require("../src/server/routes/admin").default;
|
||||
const { emailHistoryQuery } = require("../src/server/routes/admin-emails");
|
||||
const { emailHistoryQuery, emailHistoryPeriod, emailHistorySummary } = require("../src/server/routes/admin-emails");
|
||||
|
||||
describe("email delivery history", () => {
|
||||
const restores = [];
|
||||
@@ -65,12 +65,13 @@ describe("email delivery history", () => {
|
||||
const router = require("../src/server/routes/admin-emails").default;
|
||||
const handler = router.stack.find(layer => layer.route?.path === "/").route.stack[0].handle;
|
||||
const records = Array.from({ length: 51 }, (_, index) => ({ _id: String(index), createdAt: new Date("2026-01-01"), state: "accepted" }));
|
||||
let limit, filter, sort, result, cache;
|
||||
stub(history, "emailHistoryCollection", () => ({ find: query => {
|
||||
filter = query;
|
||||
let limit, filter, sort, result, cache, options;
|
||||
stub(history, "emailHistoryCollection", () => ({ find: (query, value) => {
|
||||
filter = query; options = value;
|
||||
return { sort: value => { sort = value; return { limit: count => { limit = count; return { toArray: async () => records }; } }; } };
|
||||
} }));
|
||||
await handler({ query: { state: "accepted" } }, { set: (key, value) => { cache = [key, value]; }, json: value => { result = value; } });
|
||||
expect(options.maxTimeMS).to.equal(5000);
|
||||
expect(limit).to.equal(51); expect(sort).to.deep.equal({ createdAt: -1, _id: -1 });
|
||||
expect(filter).to.deep.equal({ state: "accepted" });
|
||||
expect(result.entries).to.have.length(50);
|
||||
@@ -88,6 +89,75 @@ describe("email delivery history", () => {
|
||||
await handler({ query: {} }, res);
|
||||
expect(code).to.equal(503); expect(body.error).not.to.include("private");
|
||||
});
|
||||
it("uses UTC calendar periods and rejects malformed period or search input", () => {
|
||||
const now = new Date("2026-11-14T23:59:00.000Z");
|
||||
expect(emailHistoryPeriod("7d", now)).to.deep.equal({ period: "7d", chartDays: 7, start: new Date("2026-11-08T00:00:00.000Z") });
|
||||
expect(emailHistoryQuery({ period: "30d" }, now).createdAt.$gte.toISOString()).to.equal("2026-10-16T00:00:00.000Z");
|
||||
expect(emailHistoryQuery({ period: "all" }, now)).to.deep.equal({});
|
||||
expect(emailHistoryQuery({}, now)).to.deep.equal({});
|
||||
for (const query of [{ period: "1d" }, { period: [] }, { period: "" }, { search: [] }, { search: { $ne: "" } }, { search: "a".repeat(201) }]) {
|
||||
expect(() => emailHistoryQuery(query)).to.throw();
|
||||
}
|
||||
});
|
||||
it("searches literal recipient text and exact IDs without losing the cursor or period", () => {
|
||||
const now = new Date("2026-11-14T12:00:00.000Z");
|
||||
const before = "2026-11-12T12:00:00.000Z|aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
|
||||
const query = emailHistoryQuery({ search: " a+b.[test]@example.com ", before, period: "7d" }, now);
|
||||
const regex = new RegExp(query.$or[0].recipient.$regex, query.$or[0].recipient.$options);
|
||||
expect(regex.test("A+B.[test]@example.com")).to.equal(true);
|
||||
expect(regex.test("abxt@example.com")).to.equal(false);
|
||||
expect(query.$or.slice(1)).to.deep.equal([{ _id: "a+b.[test]@example.com" }, { ownerId: "a+b.[test]@example.com" }, { resourceId: "a+b.[test]@example.com" }]);
|
||||
expect(query.$and[0].$or).to.have.length(2);
|
||||
expect(query.createdAt.$gte.toISOString()).to.equal("2026-11-08T00:00:00.000Z");
|
||||
expect(emailHistoryQuery({ search: " " })).to.deep.equal({});
|
||||
});
|
||||
it("zero fills UTC daily series and keeps all-time totals independent of the chart", () => {
|
||||
const period = emailHistoryPeriod("all", new Date("2026-11-14T12:00:00.000Z"));
|
||||
const result = emailHistorySummary([{ _id: "accepted", count: 75 }, { _id: "unknown", count: 3 }], [
|
||||
{ _id: { date: "2026-11-14", state: "accepted" }, count: 2 },
|
||||
{ _id: { date: "2025-01-01", state: "accepted" }, count: 50 },
|
||||
], period);
|
||||
expect(result.total).to.equal(78);
|
||||
expect(result.counts).to.deep.equal({ accepted: 75, rejected: 0, unknown: 3, sending: 0 });
|
||||
expect(result.series).to.have.length(30);
|
||||
expect(result.series[0]).to.deep.equal({ date: "2026-10-16", accepted: 0, rejected: 0, unknown: 0, sending: 0 });
|
||||
expect(result.series.at(-1).accepted).to.equal(2);
|
||||
expect(result.chartDays).to.equal(30);
|
||||
expect(result.period).to.equal("all");
|
||||
});
|
||||
it("aggregates the whole filtered dataset independently of state and cursor with bounded database time", async () => {
|
||||
const router = require("../src/server/routes/admin-emails").default;
|
||||
const handler = router.stack.find(layer => layer.route?.path === "/summary").route.stack[0].handle;
|
||||
let pipeline, options, result, cache;
|
||||
stub(history, "emailHistoryCollection", () => ({ aggregate: (stages, opts) => {
|
||||
pipeline = stages; options = opts;
|
||||
return { toArray: async () => [{ counts: [{ _id: "accepted", count: 120 }, { _id: "rejected", count: 8 }], daily: [] }] };
|
||||
} }));
|
||||
await handler({ query: { state: "rejected", before: "ignored", period: "7d", kind: "owner-access", search: "research" } }, {
|
||||
set: (key, value) => { cache = [key, value]; }, json: value => { result = value; },
|
||||
});
|
||||
expect(pipeline[0].$match).not.to.have.property("state");
|
||||
expect(pipeline[0].$match).not.to.have.property("$and");
|
||||
expect(pipeline[0].$match.kind).to.equal("owner-access");
|
||||
expect(pipeline[0].$match.$or[0].recipient.$regex).to.equal("research");
|
||||
expect(pipeline[1].$facet.daily[1].$group._id.date.$dateToString.timezone).to.equal("UTC");
|
||||
expect(options.maxTimeMS).to.equal(5000);
|
||||
expect(result.total).to.equal(128);
|
||||
expect(result.series).to.have.length(7);
|
||||
expect(cache).to.deep.equal(["Cache-Control", "no-store"]);
|
||||
});
|
||||
it("reports invalid summary filters and aggregate failures without exposing database errors", async () => {
|
||||
const router = require("../src/server/routes/admin-emails").default;
|
||||
const handler = router.stack.find(layer => layer.route?.path === "/summary").route.stack[0].handle;
|
||||
let code, body;
|
||||
const res = { set() {}, status(value) { code = value; return this; }, json(value) { body = value; } };
|
||||
await handler({ query: { period: "wrong" } }, res);
|
||||
expect(code).to.equal(400);
|
||||
stub(history, "emailHistoryCollection", () => ({ aggregate: () => ({ toArray: async () => { throw Error("private timeout info"); } }) }));
|
||||
await handler({ query: {} }, res);
|
||||
expect(code).to.equal(503);
|
||||
expect(body.error).not.to.include("private");
|
||||
});
|
||||
it("mounts history behind authentication and administrator checks", () => {
|
||||
const emails = require("../src/server/routes/admin-emails").default;
|
||||
const index = admin.stack.findIndex(layer => layer.handle === emails);
|
||||
|
||||
Reference in new issue
Block a user