simplify postWarning

This commit is contained in:
Michael C
2023-02-21 03:25:02 -05:00
parent c6795a783d
commit 820a7eb02f

View File

@@ -9,16 +9,21 @@ describe("postWarning", () => {
const endpoint = "/api/warnUser"; const endpoint = "/api/warnUser";
const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]); const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]);
const warnedUser = getHash("warning-0"); const warneduserID = "warning-0";
const warnedUserPublicID = getHash(warneduserID);
const warningVipOne = "warning-vip-1";
const warningVipTwo = "warning-vip-2";
const nonVipUser = "warning-non-vip";
before(async () => { before(async () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]); await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipOne)]);
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipTwo)]);
}); });
it("Should be able to create warning if vip (exp 200)", (done) => { it("Should be able to create warning if vip (exp 200)", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: warningVipOne,
userID: warnedUser, userID: warnedUserPublicID,
reason: "warning-reason-0" reason: "warning-reason-0"
}; };
client.post(endpoint, json) client.post(endpoint, json)
@@ -38,8 +43,8 @@ describe("postWarning", () => {
it("Should be not be able to create a duplicate warning if vip", (done) => { it("Should be not be able to create a duplicate warning if vip", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: warningVipOne,
userID: warnedUser, userID: warnedUserPublicID,
}; };
client.post(endpoint, json) client.post(endpoint, json)
@@ -58,8 +63,8 @@ describe("postWarning", () => {
it("Should be able to remove warning if vip", (done) => { it("Should be able to remove warning if vip", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: warningVipOne,
userID: warnedUser, userID: warnedUserPublicID,
enabled: false enabled: false
}; };
@@ -78,8 +83,8 @@ describe("postWarning", () => {
it("Should not be able to create warning if not vip (exp 403)", (done) => { it("Should not be able to create warning if not vip (exp 403)", (done) => {
const json = { const json = {
issuerUserID: "warning-not-vip", issuerUserID: nonVipUser,
userID: "warning-1", userID: warnedUserPublicID,
}; };
client.post(endpoint, json) client.post(endpoint, json)
@@ -101,8 +106,8 @@ describe("postWarning", () => {
it("Should re-enable disabled warning", (done) => { it("Should re-enable disabled warning", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: warningVipOne,
userID: warnedUser, userID: warnedUserPublicID,
enabled: true enabled: true
}; };
@@ -121,14 +126,14 @@ describe("postWarning", () => {
it("Should be able to remove your own warning", (done) => { it("Should be able to remove your own warning", (done) => {
const json = { const json = {
userID: "warning-0", userID: warneduserID,
enabled: false enabled: false
}; };
client.post(endpoint, json) client.post(endpoint, json)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await getWarning(warnedUser); const data = await getWarning(warnedUserPublicID);
const expected = { const expected = {
enabled: 0 enabled: 0
}; };
@@ -138,15 +143,16 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to add your own warning", (done) => { it("Should not be able to add your own warning", (done) => {
const json = { const json = {
userID: "warning-0" userID: warneduserID,
enabled: true
}; };
client.post(endpoint, json) client.post(endpoint, json)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
const data = await getWarning(warnedUser); const data = await getWarning(warnedUserPublicID);
const expected = { const expected = {
enabled: 0 enabled: 0
}; };