SponsorBlockServer/test/cases/postWarning.ts

202 lines
6.5 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";
const getWarning = (userID: string, type = 0) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ? AND "type" = ?`, [userID, type]);
2021-09-17 05:05:16 +02:00
2023-09-27 21:09:35 +02:00
const warneduserOneID = "warning-0";
const warnedUserTwoID = "warning-1";
const warnedUserOnePublicID = getHash(warneduserOneID);
const warnedUserTwoPublicID = getHash(warnedUserTwoID);
2023-02-21 09:25:02 +01:00
const warningVipOne = "warning-vip-1";
const warningVipTwo = "warning-vip-2";
const nonVipUser = "warning-non-vip";
2022-07-21 00:52:27 +02:00
2021-03-07 06:21:56 +01:00
before(async () => {
2023-02-21 09:25:02 +01:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipOne)]);
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash(warningVipTwo)]);
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 = {
2023-02-21 09:25:02 +01:00
issuerUserID: warningVipOne,
2023-09-27 21:09:35 +02:00
userID: warnedUserOnePublicID,
2021-07-12 08:43:46 +02:00
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 = {
2023-02-21 09:25:02 +01:00
issuerUserID: warningVipOne,
2023-09-27 21:09:35 +02:00
userID: warnedUserOnePublicID,
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 = {
2023-02-21 09:25:02 +01:00
issuerUserID: warningVipOne,
2023-09-27 21:09:35 +02:00
userID: warnedUserOnePublicID,
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 = {
2023-02-21 09:25:02 +01:00
issuerUserID: nonVipUser,
2023-09-27 21:09:35 +02:00
userID: warnedUserOnePublicID,
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-09-23 05:34:46 +02:00
.then(res => {
2021-07-22 23:50:39 +02:00
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 = {
2023-02-21 09:25:02 +01:00
issuerUserID: warningVipOne,
2023-09-27 21:09:35 +02:00
userID: warnedUserOnePublicID,
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));
});
2022-07-21 00:52:27 +02:00
it("Should be able to remove your own warning", (done) => {
const json = {
2023-09-27 21:09:35 +02:00
userID: warneduserOneID,
2022-07-21 00:52:27 +02:00
enabled: false
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
2023-09-27 21:09:35 +02:00
const data = await getWarning(warnedUserOnePublicID);
2022-07-21 00:52:27 +02:00
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
});
2022-07-21 20:19:41 +02:00
2023-02-21 09:25:02 +01:00
it("Should not be able to add your own warning", (done) => {
2022-07-21 20:19:41 +02:00
const json = {
2023-09-27 21:09:35 +02:00
userID: warneduserOneID,
2023-02-21 09:25:02 +01:00
enabled: true
2022-07-21 20:19:41 +02:00
};
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 403);
2023-09-27 21:09:35 +02:00
const data = await getWarning(warnedUserOnePublicID);
2022-07-21 20:19:41 +02:00
const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
});
2023-09-27 21:09:35 +02:00
it("Should not be able to warn a user without reason", (done) => {
const json = {
issuerUserID: warningVipOne,
userID: warnedUserTwoPublicID,
enabled: true
};
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should be able to re-warn a user without reason", (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(warnedUserOnePublicID);
const expected = {
enabled: 1
};
assert.ok(partialDeepEquals(data, expected));
done();
})
.catch(err => done(err));
});
2020-10-17 20:56:54 +02:00
});