SponsorBlockServer/test/cases/getLockReason.ts

145 lines
7.5 KiB
TypeScript
Raw Normal View History

2021-09-23 00:52:35 +02:00
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases";
2021-09-19 05:24:23 +02:00
import assert from "assert";
2021-09-23 05:34:46 +02:00
import { client } from "../utils/httpClient";
2022-01-07 09:04:47 +01:00
import { partialDeepEquals } from "../utils/partialDeepEquals";
2021-09-19 05:24:23 +02:00
2021-09-23 05:34:46 +02:00
const endpoint = "/api/lockReason";
2021-09-19 05:24:23 +02:00
2021-10-24 11:04:18 +02:00
const vipUserName1 = "getLockReason-vipUserName_1";
const vipUserID1 = getHash("getLockReason-vipUserID_1");
const vipUserName2 = "getLockReason-vipUserName_2";
const vipUserID2 = getHash("getLockReason-vipUserID_2");
2021-09-19 05:24:23 +02:00
describe("getLockReason", () => {
before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
2021-10-24 11:04:18 +02:00
await db.prepare("run", insertVipUserQuery, [vipUserID1]);
await db.prepare("run", insertVipUserQuery, [vipUserID2]);
const insertVipUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES (?, ?)';
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
2021-09-19 05:24:23 +02:00
2022-01-07 09:04:47 +01:00
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason") VALUES (?, ?, ?, ?, ?)';
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "sponsor", "sponsor-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "interaction", "interaction-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "preview", "preview-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "mute", "music_offtopic", "nonmusic-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "mute", "outro", "outro-reason"]);
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "full", "selfpromo", "selfpromo-reason"]);
2021-10-24 11:04:18 +02:00
});
after(async () => {
const deleteUserNameQuery = 'DELETE FROM "userNames" WHERE "userID" = ? AND "userName" = ?';
2021-10-24 11:04:18 +02:00
await db.prepare("run", deleteUserNameQuery, [vipUserID1, vipUserName1]);
await db.prepare("run", deleteUserNameQuery, [vipUserID2, vipUserName2]);
2021-09-19 05:24:23 +02:00
});
it("Should update the database version when starting the application", async () => {
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
if (version > 20) return;
else return `Version isn't greater than 20. Version is ${version}`;
});
2021-09-23 05:34:46 +02:00
it("Should be able to get single reason", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", category: "sponsor" } })
.then(res => {
2021-09-19 05:24:23 +02:00
assert.strictEqual(res.status, 200);
const expected = [
2021-10-24 11:04:18 +02:00
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 }
2021-09-19 05:24:23 +02:00
];
2021-09-23 05:34:46 +02:00
assert.deepStrictEqual(res.data, expected);
2021-09-19 05:24:23 +02:00
done();
})
.catch(err => done(err));
});
2021-09-23 05:34:46 +02:00
it("Should be able to get empty locks", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } })
.then(res => {
2021-09-19 05:24:23 +02:00
assert.strictEqual(res.status, 200);
const expected = [
2021-10-24 11:04:18 +02:00
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
2021-09-19 05:24:23 +02:00
];
2021-09-23 05:34:46 +02:00
assert.deepStrictEqual(res.data, expected);
2021-09-19 05:24:23 +02:00
done();
})
.catch(err => done(err));
});
2021-09-23 05:34:46 +02:00
it("should get multiple locks with array", (done) => {
2021-10-24 11:04:18 +02:00
client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor","outro"]` } })
2021-09-23 05:34:46 +02:00
.then(res => {
2021-09-19 05:24:23 +02:00
assert.strictEqual(res.status, 200);
const expected = [
2021-10-24 11:04:18 +02:00
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 }
2021-09-19 05:24:23 +02:00
];
2021-09-23 05:34:46 +02:00
assert.deepStrictEqual(res.data, expected);
2021-09-19 05:24:23 +02:00
done();
})
.catch(err => done(err));
});
2021-09-23 05:34:46 +02:00
it("should get multiple locks with repeated category", (done) => {
client.get(`${endpoint}?videoID=getLockReason&category=interaction&category=music_offtopic&category=intro`)
.then(res => {
2021-09-19 05:24:23 +02:00
assert.strictEqual(res.status, 200);
const expected = [
2021-10-24 11:04:18 +02:00
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" }
2021-09-19 05:24:23 +02:00
];
2021-09-23 05:34:46 +02:00
assert.deepStrictEqual(res.data, expected);
2021-09-19 05:24:23 +02:00
done();
})
.catch(err => done(err));
});
2022-01-07 09:04:47 +01:00
it("should return all skip + mute categories if none specified", (done) => {
2021-09-23 05:34:46 +02:00
client.get(endpoint, { params: { videoID: "getLockReason" } })
.then(res => {
2021-09-19 05:24:23 +02:00
assert.strictEqual(res.status, 200);
const expected = [
2021-10-24 11:04:18 +02:00
{ category: "sponsor", locked: 1, reason: "sponsor-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "selfpromo", locked: 0, reason: "", userID: "", userName: "" },
2022-01-14 06:23:09 +01:00
{ category: "exclusive_access", locked: 0, reason: "", userID: "", userName: "" },
2021-10-24 11:04:18 +02:00
{ category: "interaction", locked: 1, reason: "interaction-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "intro", locked: 0, reason: "", userID: "", userName: "" },
{ category: "outro", locked: 1, reason: "outro-reason", userID: vipUserID2, userName: vipUserName2 },
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
2021-11-09 01:28:27 +01:00
{ category: "filler", locked: 0, reason: "", userID: "", userName: "" },
2021-09-19 05:24:23 +02:00
];
2022-01-07 09:04:47 +01:00
partialDeepEquals(res.data, expected, false);
2021-09-19 05:24:23 +02:00
done();
})
.catch(err => done(err));
});
2021-09-23 05:34:46 +02:00
it("should return 400 if no videoID specified", (done) => {
client.get(endpoint)
2021-09-19 05:24:23 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
2022-01-07 09:04:47 +01:00
it("should be able to get by actionType", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", actionType: "full" } })
2022-01-07 09:04:47 +01:00
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [
{ category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 },
{ category: "sponsor", locked: 0, reason: "", userID: "", userName: "" }
];
partialDeepEquals(res.data, expected);
done();
})
.catch(err => done(err));
});
2021-09-19 05:24:23 +02:00
});