Remove warning expiry, save warning history

This commit is contained in:
mini-bomba
2025-09-10 18:51:08 +02:00
parent 1b99a8534c
commit 3e74a0da58
11 changed files with 267 additions and 114 deletions

View File

@@ -7,25 +7,65 @@ import { client } from "../utils/httpClient";
describe("postWarning", () => {
// constants
const endpoint = "/api/warnUser";
const getWarning = (userID: string, type = 0) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ? AND "type" = ?`, [userID, type]);
const getWarning = (userID: string, type = 0) => db.prepare("all", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ? AND "type" = ? ORDER BY "issueTime" ASC`, [userID, type]);
const warneduserOneID = "warning-0";
const warnedUserTwoID = "warning-1";
const warnedUserOnePublicID = getHash(warneduserOneID);
const warnedUserTwoPublicID = getHash(warnedUserTwoID);
const warningVipOne = "warning-vip-1";
const warningVipTwo = "warning-vip-2";
const userID0 = "warning-0";
const userID1 = "warning-1";
const userID2 = "warning-2";
const userID3 = "warning-3";
const userID4 = "warning-4";
const userID5 = "warning-5";
const userID6 = "warning-6";
const userID7 = "warning-7";
const userID8 = "warning-8";
const userID9 = "warning-9";
const userID10 = "warning-10";
const userID11 = "warning-11";
const userID12 = "warning-12";
const userID13 = "warning-13";
const publicUserID0 = getHash(userID0);
const publicUserID1 = getHash(userID1);
const publicUserID2 = getHash(userID2);
const publicUserID3 = getHash(userID3);
const publicUserID4 = getHash(userID4);
const publicUserID5 = getHash(userID5);
const publicUserID6 = getHash(userID6);
const publicUserID7 = getHash(userID7);
const publicUserID8 = getHash(userID8);
const publicUserID9 = getHash(userID9);
const publicUserID10 = getHash(userID10);
const publicUserID11 = getHash(userID11);
const publicUserID12 = getHash(userID12);
const publicUserID13 = getHash(userID13);
const vipID1 = "warning-vip-1";
const vipID2 = "warning-vip-2";
const publicVipID1 = getHash(vipID1);
const publicVipID2 = getHash(vipID2);
const nonVipUser = "warning-non-vip";
before(async () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipOne)]);
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipTwo)]);
const insertWarningQuery = 'INSERT INTO warnings ("userID", "issuerUserID", "enabled", "reason", "issueTime") VALUES(?, ?, ?, ?, ?)';
const HOUR = 60 * 60 * 1000;
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [publicVipID1]);
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [publicVipID2]);
await db.prepare("run", insertWarningQuery, [publicUserID1, publicVipID1, 1, "warn reason 1", (Date.now() - 24 * HOUR)]); // 24 hours is much past the edit deadline
await db.prepare("run", insertWarningQuery, [publicUserID2, publicVipID1, 1, "warn reason 2", (Date.now() - 24 * HOUR)]);
await db.prepare("run", insertWarningQuery, [publicUserID3, publicVipID1, 1, "warn reason 3", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID4, publicVipID1, 1, "warn reason 4", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID6, publicVipID1, 1, "warn reason 6", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID9, publicVipID1, 0, "warn reason 9", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID10, publicVipID1, 1, "warn reason 10", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID11, publicVipID1, 1, "warn reason 11", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID12, publicVipID1, 0, "warn reason 12", Date.now()]);
await db.prepare("run", insertWarningQuery, [publicUserID13, publicVipID1, 0, "warn reason 13", Date.now()]);
});
it("Should be able to create warning if vip (exp 200)", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserOnePublicID,
issuerUserID: vipID1,
userID: publicUserID0,
reason: "warning-reason-0"
};
client.post(endpoint, json)
@@ -37,16 +77,18 @@ describe("postWarning", () => {
issuerUserID: getHash(json.issuerUserID),
reason: json.reason,
};
assert.ok(partialDeepEquals(row, expected));
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should be not be able to create a duplicate warning if vip", (done) => {
it("Should be not be able to edit a warning if past deadline and same vip", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserOnePublicID,
issuerUserID: vipID1,
userID: publicUserID1,
reason: "edited reason 1",
};
client.post(endpoint, json)
@@ -55,18 +97,41 @@ describe("postWarning", () => {
const row = await getWarning(json.userID);
const expected = {
enabled: 1,
issuerUserID: getHash(json.issuerUserID),
issuerUserID: publicVipID1,
};
assert.ok(partialDeepEquals(row, expected));
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should be able to remove warning if vip", (done) => {
it("Should be not be able to edit a warning if past deadline and different vip", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserOnePublicID,
issuerUserID: vipID2,
userID: publicUserID2,
reason: "edited reason 2",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 409);
const row = await getWarning(json.userID);
const expected = {
enabled: 1,
issuerUserID: publicVipID1,
};
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should be able to remove warning if same vip as issuer", (done) => {
const json = {
issuerUserID: vipID1,
userID: publicUserID3,
enabled: false
};
@@ -77,7 +142,29 @@ describe("postWarning", () => {
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(row, expected));
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should be able to remove warning if not the same vip as issuer", (done) => {
const json = {
issuerUserID: vipID2,
userID: publicUserID4,
enabled: false
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getWarning(json.userID);
const expected = {
enabled: 0
};
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
@@ -86,7 +173,8 @@ describe("postWarning", () => {
it("Should not be able to create warning if not vip (exp 403)", (done) => {
const json = {
issuerUserID: nonVipUser,
userID: warnedUserOnePublicID,
userID: publicUserID5,
reason: "warn reason 5",
};
client.post(endpoint, json)
@@ -106,40 +194,21 @@ describe("postWarning", () => {
.catch(err => done(err));
});
it("Should re-enable disabled warning", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserOnePublicID,
enabled: true
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await getWarning(json.userID);
const expected = {
enabled: 1
};
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to remove your own warning", (done) => {
const json = {
userID: warneduserOneID,
userID: userID6,
enabled: false
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await getWarning(warnedUserOnePublicID);
const row = await getWarning(publicUserID6);
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(data, expected));
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
@@ -147,18 +216,16 @@ describe("postWarning", () => {
it("Should not be able to add your own warning", (done) => {
const json = {
userID: warneduserOneID,
enabled: true
userID: userID7,
enabled: true,
reason: "warn reason 7",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 403);
const data = await getWarning(warnedUserOnePublicID);
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(data, expected));
const data = await getWarning(publicUserID7);
assert.equal(data.length, 0);
done();
})
.catch(err => done(err));
@@ -166,8 +233,8 @@ describe("postWarning", () => {
it("Should not be able to warn a user without reason", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserTwoPublicID,
issuerUserID: vipID1,
userID: publicUserID8,
enabled: true
};
@@ -179,21 +246,128 @@ describe("postWarning", () => {
.catch(err => done(err));
});
it("Should be able to re-warn a user without reason", (done) => {
it("Should not be able to re-warn a user without reason", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserOnePublicID,
issuerUserID: vipID1,
userID: publicUserID9,
enabled: true
};
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should be able to edit a warning if within deadline and same vip", (done) => {
const json = {
issuerUserID: vipID1,
userID: publicUserID10,
enabled: true,
reason: "edited reason 10",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await getWarning(warnedUserOnePublicID);
const row = await getWarning(json.userID);
const expected = {
enabled: 1
enabled: 1,
issuerUserID: publicVipID1,
reason: json.reason,
};
assert.ok(partialDeepEquals(data, expected));
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should not be able to edit a warning if within deadline and different vip", (done) => {
const json = {
issuerUserID: vipID2,
userID: publicUserID11,
enabled: true,
reason: "edited reason 11",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 409);
const row = await getWarning(json.userID);
const expected = {
enabled: 1,
issuerUserID: publicVipID1,
reason: "warn reason 11",
};
assert.equal(row.length, 1);
assert.ok(partialDeepEquals(row[0], expected));
done();
})
.catch(err => done(err));
});
it("Should be able to warn a previously warned user again (same vip)", (done) => {
const json = {
issuerUserID: vipID1,
userID: publicUserID12,
enabled: true,
reason: "new reason 12",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getWarning(json.userID);
const expected = [
{
enabled: 0,
issuerUserID: publicVipID1,
reason: "warn reason 12",
},
{
enabled: 1,
issuerUserID: publicVipID1,
reason: "new reason 12",
}
];
assert.equal(row.length, 2);
assert.ok(partialDeepEquals(row[0], expected[0]));
assert.ok(partialDeepEquals(row[1], expected[1]));
done();
})
.catch(err => done(err));
});
it("Should be able to warn a previously warned user again (different vip)", (done) => {
const json = {
issuerUserID: vipID2,
userID: publicUserID13,
enabled: true,
reason: "new reason 13",
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getWarning(json.userID);
const expected = [
{
enabled: 0,
issuerUserID: publicVipID1,
reason: "warn reason 13",
},
{
enabled: 1,
issuerUserID: publicVipID2,
reason: "new reason 13",
}
];
assert.equal(row.length, 2);
assert.ok(partialDeepEquals(row[0], expected[0]));
assert.ok(partialDeepEquals(row[1], expected[1]));
done();
})
.catch(err => done(err));