SponsorBlockServer/test/cases/postWarning.ts

139 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-07-12 08:43:46 +02:00
import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import assert from "assert";
2020-10-17 20:56:54 +02:00
2021-07-12 08:43:46 +02:00
describe("postWarning", () => {
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-07-12 08:43:46 +02:00
it("Should be able to create warning if vip (exp 200)", (done: 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-07-12 08:43:46 +02:00
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify(json),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1);
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID));
assert.strictEqual(row.reason, json.reason);
done();
})
.catch(err => done(err));
2020-12-29 19:31:15 +01:00
});
2021-07-12 08:43:46 +02:00
it("Should be not be able to create a duplicate warning if vip", (done: 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-07-12 08:43:46 +02:00
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify(json),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 409);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1);
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID));
done();
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2020-12-29 06:18:50 +01:00
2021-07-12 08:43:46 +02:00
it("Should be able to remove warning if vip", (done: 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-07-12 08:43:46 +02:00
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify(json),
})
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 0);
done();
})
.catch(err => done(err));
2020-12-29 06:18:50 +01:00
});
2021-07-12 08:43:46 +02:00
it("Should not be able to create warning if not vip (exp 403)", (done: 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-07-12 08:43:46 +02:00
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
2021-01-06 01:43:28 +01:00
headers: {
2021-07-12 08:43:46 +02:00
"Content-Type": "application/json",
2021-01-06 01:43:28 +01:00
},
body: JSON.stringify(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-07-22 23:50:39 +02:00
it("Should return 400 if missing body", (done: Done) => {
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
2021-07-10 22:30:30 +02:00
headers: {
2021-07-22 23:50:39 +02:00
"Content-Type": "application/json",
2021-07-10 22:30:30 +02:00
}
})
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
});
it("Should re-enable disabled warning", (done: Done) => {
const json = {
issuerUserID: "warning-vip",
userID: "warning-0",
enabled: true
};
fetch(`${getbaseURL()}/api/warnUser`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(json),
})
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(data.enabled, 1);
done();
})
.catch(err => done(err));
});
2020-10-17 20:56:54 +02:00
});