SponsorBlockServer/test/cases/shadowBanUser.ts

200 lines
9 KiB
TypeScript
Raw Normal View History

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-17 05:05:16 +02:00
import { Category } from "../../src/types/segments.model";
2021-09-23 05:18:31 +02:00
import { client } from "../utils/httpClient";
2021-06-14 22:23:39 +02:00
2021-07-12 08:43:46 +02:00
describe("shadowBanUser", () => {
2021-09-17 05:05:16 +02:00
const getShadowBan = (userID: string) => db.prepare("get", `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]);
const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
2021-09-23 05:18:31 +02:00
const endpoint = "/api/shadowBanUser";
2021-09-17 05:05:16 +02:00
const VIPuserID = "shadow-ban-vip";
2021-06-22 00:59:56 +02:00
before(async () => {
const insertQuery = `INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
2021-07-12 08:43:46 +02:00
await db.prepare("run", insertQuery, ["testtesttest", 1, 11, 2, 0, "shadow-1-uuid-0", "shadowBanned", 0, 50, "sponsor", "YouTube", 100, 0, 0, getHash("testtesttest", 1)]);
await db.prepare("run", insertQuery, ["testtesttest2", 1, 11, 2, 0, "shadow-1-uuid-0-1", "shadowBanned", 0, 50, "sponsor", "PeerTube", 120, 0, 0, getHash("testtesttest2", 1)]);
await db.prepare("run", insertQuery, ["testtesttest", 20, 33, 2, 0, "shadow-1-uuid-2", "shadowBanned", 0, 50, "intro", "YouTube", 101, 0, 0, getHash("testtesttest", 1)]);
2021-06-14 22:23:39 +02:00
2021-07-12 08:43:46 +02:00
await db.prepare("run", insertQuery, ["testtesttest", 1, 11, 2, 0, "shadow-2-uuid-0", "shadowBanned2", 0, 50, "sponsor", "YouTube", 100, 0, 0, getHash("testtesttest", 1)]);
await db.prepare("run", insertQuery, ["testtesttest2", 1, 11, 2, 0, "shadow-2-uuid-0-1", "shadowBanned2", 0, 50, "sponsor", "PeerTube", 120, 0, 0, getHash("testtesttest2", 1)]);
await db.prepare("run", insertQuery, ["testtesttest", 20, 33, 2, 0, "shadow-2-uuid-2", "shadowBanned2", 0, 50, "intro", "YouTube", 101, 0, 0, getHash("testtesttest", 1)]);
2021-06-14 22:23:39 +02:00
2021-07-12 08:43:46 +02:00
await db.prepare("run", insertQuery, ["testtesttest", 1, 11, 2, 0, "shadow-3-uuid-0", "shadowBanned3", 0, 50, "sponsor", "YouTube", 100, 0, 1, getHash("testtesttest", 1)]);
await db.prepare("run", insertQuery, ["testtesttest2", 1, 11, 2, 0, "shadow-3-uuid-0-1", "shadowBanned3", 0, 50, "sponsor", "PeerTube", 120, 0, 1, getHash("testtesttest2", 1)]);
await db.prepare("run", insertQuery, ["testtesttest", 20, 33, 2, 0, "shadow-3-uuid-2", "shadowBanned3", 0, 50, "intro", "YouTube", 101, 0, 1, getHash("testtesttest", 1)]);
await db.prepare("run", insertQuery, ["testtesttest", 21, 34, 2, 0, "shadow-4-uuid-1", "shadowBanned4", 0, 50, "sponsor", "YouTube", 101, 0, 0, getHash("testtesttest", 1)]);
2021-07-12 08:43:46 +02:00
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned3"]);
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned4"]);
2021-06-14 22:23:39 +02:00
2021-09-17 05:05:16 +02:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]);
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to ban user and hide submissions", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
}
2021-06-14 22:23:39 +02:00
})
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 videoRow = await getShadowBanSegments(userID, 1);
const shadowRow = await getShadowBan(userID);
2021-07-12 08:43:46 +02:00
assert.ok(shadowRow);
assert.strictEqual(videoRow.length, 3);
done();
})
.catch(err => done(err));
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to unban user without unhiding submissions", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
unHideOldSubmissions: false,
}
2021-06-14 22:23:39 +02:00
})
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 videoRow = await getShadowBanSegments(userID, 1);
const shadowRow = await getShadowBan(userID);
2021-07-12 08:43:46 +02:00
assert.ok(!shadowRow);
assert.strictEqual(videoRow.length, 3);
done();
})
.catch(err => done(err));
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to ban user and hide submissions from only some categories", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned2";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
categories: `["sponsor"]`
}
2021-06-14 22:23:39 +02:00
})
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 videoRow = await getShadowBanSegmentCategory(userID, 1);
const shadowRow = await getShadowBan(userID);
2021-07-12 08:43:46 +02:00
assert.ok(shadowRow);
assert.strictEqual(videoRow.length, 2);
assert.strictEqual(videoRow.filter((elem) => elem.category === "sponsor").length, 2);
done();
})
.catch(err => done(err));
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to unban user and unhide submissions", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned2";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
}
2021-06-14 22:23:39 +02:00
})
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 videoRow = await getShadowBanSegments(userID, 1);
const shadowRow = await getShadowBan(userID);
2021-07-12 08:43:46 +02:00
assert.ok(!shadowRow);
assert.strictEqual(videoRow?.length, 0);
done();
})
.catch(err => done(err));
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to unban user and unhide some submissions", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned3";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
categories: `["sponsor"]`
}
2021-06-14 22:23:39 +02:00
})
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 videoRow = await getShadowBanSegmentCategory(userID, 1);
const shadowRow = await getShadowBan(userID);
2021-07-12 08:43:46 +02:00
assert.ok(!shadowRow);
assert.strictEqual(videoRow.length, 1);
assert.strictEqual(videoRow[0].category, "intro");
done();
})
.catch(err => done(err));
2021-06-14 22:23:39 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should get 409 when re-shadowbanning user", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned4";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: true,
categories: `["sponsor"]`,
unHideOldSubmissions: false
}
})
.then(async res => {
assert.strictEqual(res.status, 409);
2021-09-17 05:05:16 +02:00
const videoRow = await getShadowBanSegmentCategory(userID, 0);
const shadowRow = await getShadowBan(userID);
assert.ok(shadowRow); // ban still exists
assert.strictEqual(videoRow.length, 1); // videos should not be hidden
assert.strictEqual(videoRow[0].category, "sponsor");
done();
})
.catch(err => done(err));
});
2021-09-23 05:18:31 +02:00
it("Should be able to re-shadowban user to hide old submissions", (done) => {
2021-09-17 05:05:16 +02:00
const userID = "shadowBanned4";
2021-09-23 05:18:31 +02:00
client({
method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: true,
categories: `["sponsor"]`,
unHideOldSubmissions: true
}
})
.then(async res => {
assert.strictEqual(res.status, 200);
2021-09-17 05:05:16 +02:00
const videoRow = await getShadowBanSegmentCategory(userID, 1);
const shadowRow = await getShadowBan(userID);
assert.ok(shadowRow); // ban still exists
assert.strictEqual(videoRow.length, 1); // videos should be hidden
assert.strictEqual(videoRow[0].category, "sponsor");
done();
})
.catch(err => done(err));
});
2021-06-14 22:23:39 +02:00
});