SponsorBlockServer/test/cases/postWarning.ts

120 lines
3.8 KiB
TypeScript
Raw Normal View History

import { partialDeepEquals } from "../utils/partialDeepEquals";
2021-09-23 00:52:35 +02:00
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
2021-07-12 08:43:46 +02:00
import assert from "assert";
2021-09-23 05:18:31 +02:00
import { client } from "../utils/httpClient";
2020-10-17 20:56:54 +02:00
2021-07-12 08:43:46 +02:00
describe("postWarning", () => {
2021-09-17 05:05:16 +02:00
// constants
2021-09-23 05:18:31 +02:00
const endpoint = "/api/warnUser";
2021-09-17 06:03:42 +02:00
const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]);
2021-09-17 05:05:16 +02:00
2021-03-07 06:21:56 +01:00
before(async () => {
2021-05-07 01:51:11 +02:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]);
2020-10-17 20:56:54 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to create warning if vip (exp 200)", (done) => {
2021-07-04 07:34:31 +02:00
const json = {
2021-07-12 08:43:46 +02:00
issuerUserID: "warning-vip",
userID: "warning-0",
reason: "warning-reason-0"
2020-10-17 20:56:54 +02:00
};
2021-09-23 05:18:31 +02:00
client.post(endpoint, json)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
2021-09-17 05:05:16 +02:00
const row = await getWarning(json.userID);
2021-08-03 06:19:37 +02:00
const expected = {
enabled: 1,
issuerUserID: getHash(json.issuerUserID),
reason: json.reason,
};
assert.ok(partialDeepEquals(row, expected));
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2020-12-29 19:31:15 +01:00
});
2021-09-23 05:18:31 +02:00
it("Should be not be able to create a duplicate warning if vip", (done) => {
2021-07-04 07:34:31 +02:00
const json = {
2021-07-12 08:43:46 +02:00
issuerUserID: "warning-vip",
userID: "warning-0",
2020-12-29 19:31:15 +01:00
};
2021-09-23 05:18:31 +02:00
client.post(endpoint, json)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 409);
2021-09-17 05:05:16 +02:00
const row = await getWarning(json.userID);
2021-08-03 06:19:37 +02:00
const expected = {
enabled: 1,
issuerUserID: getHash(json.issuerUserID),
};
assert.ok(partialDeepEquals(row, expected));
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2020-12-29 06:18:50 +01:00
2021-09-23 05:18:31 +02:00
it("Should be able to remove warning if vip", (done) => {
2021-07-04 07:34:31 +02:00
const json = {
2021-07-12 08:43:46 +02:00
issuerUserID: "warning-vip",
userID: "warning-0",
2020-12-29 06:18:50 +01:00
enabled: false
};
2021-09-23 05:18:31 +02:00
client.post(endpoint, json)
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
2021-09-17 05:05:16 +02:00
const row = await getWarning(json.userID);
2021-08-03 06:19:37 +02:00
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(row, expected));
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2020-12-29 06:18:50 +01:00
});
2021-09-23 05:18:31 +02:00
it("Should not be able to create warning if not vip (exp 403)", (done) => {
2021-07-04 07:34:31 +02:00
const json = {
2021-07-12 08:43:46 +02:00
issuerUserID: "warning-not-vip",
userID: "warning-1",
2020-10-17 20:56:54 +02:00
};
2021-09-23 05:18:31 +02:00
client.post(endpoint, json)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-10 22:30:30 +02:00
2021-09-23 05:18:31 +02:00
it("Should return 400 if missing body", (done) => {
client.post(endpoint, {})
2021-07-22 23:50:39 +02:00
.then(async res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-07-10 22:30:30 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should re-enable disabled warning", (done) => {
const json = {
issuerUserID: "warning-vip",
userID: "warning-0",
enabled: true
};
2021-09-23 05:18:31 +02:00
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
2021-09-17 05:05:16 +02:00
const data = await getWarning(json.userID);
2021-08-03 06:19:37 +02:00
const expected = {
enabled: 1
};
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
});
2020-10-17 20:56:54 +02:00
});