2021-07-12 08:43:46 +02:00
|
|
|
import fetch from "node-fetch";
|
2021-09-21 08:02:17 +02:00
|
|
|
import {Done, postJSON} from "../utils/utils";
|
|
|
|
import { getbaseURL } from "../utils/getBaseURL";
|
2021-07-12 08:43:46 +02:00
|
|
|
import {db} from "../../src/databases/databases";
|
|
|
|
import {getHash} from "../../src/utils/getHash";
|
|
|
|
import {IDatabase} from "../../src/databases/IDatabase";
|
|
|
|
import assert from "assert";
|
2021-06-24 07:27:21 +02:00
|
|
|
|
|
|
|
async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) {
|
|
|
|
const votes = 0,
|
|
|
|
userID = 0,
|
|
|
|
timeSubmitted = 0,
|
|
|
|
views = 0,
|
|
|
|
shadowHidden = 0,
|
|
|
|
hidden = 0,
|
|
|
|
hashedVideoID = `hash_${UUID}`;
|
|
|
|
await db.prepare("run", `INSERT INTO
|
|
|
|
"sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID",
|
|
|
|
"userID", "timeSubmitted", "views", "category", "shadowHidden", "hashedVideoID", "hidden")
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
|
|
[videoID, startTime, endTime, votes, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID, hidden]);
|
|
|
|
}
|
|
|
|
|
2021-06-24 07:36:54 +02:00
|
|
|
async function dbSponsorTimesCompareExpect(db: IDatabase, videoId: string, expectdHidden: number) {
|
2021-07-12 08:43:46 +02:00
|
|
|
const seg = await db.prepare("get", `SELECT "hidden", "UUID" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoId]);
|
2021-06-24 07:27:21 +02:00
|
|
|
for (let i = 0, len = seg.length; i < len; i++) {
|
2021-06-24 07:36:54 +02:00
|
|
|
if (seg.hidden !== expectdHidden) {
|
|
|
|
return `${seg.UUID} hidden expected to be ${expectdHidden} but found ${seg.hidden}`;
|
2021-06-24 07:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-12 08:43:46 +02:00
|
|
|
describe("postPurgeAllSegments", function () {
|
|
|
|
const privateVipUserID = "VIPUser-purgeAll";
|
2021-06-24 07:27:21 +02:00
|
|
|
const vipUserID = getHash(privateVipUserID);
|
2021-09-17 05:05:16 +02:00
|
|
|
const endpoint = `${getbaseURL()}/api/purgeAllSegments`;
|
2021-06-24 07:27:21 +02:00
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
// startTime and endTime get set in beforeEach for consistency
|
2021-07-12 08:43:46 +02:00
|
|
|
await dbSponsorTimesAdd(db, "vsegpurge01", 0, 1, "vsegpurgetest01uuid01", "intro");
|
|
|
|
await dbSponsorTimesAdd(db, "vsegpurge01", 0, 2, "vsegpurgetest01uuid02", "sponsor");
|
|
|
|
await dbSponsorTimesAdd(db, "vsegpurge01", 0, 3, "vsegpurgetest01uuid03", "interaction");
|
|
|
|
await dbSponsorTimesAdd(db, "vsegpurge01", 0, 4, "vsegpurgetest01uuid04", "outro");
|
|
|
|
await dbSponsorTimesAdd(db, "vseg-not-purged01", 0, 5, "vsegpurgetest01uuid05", "outro");
|
2021-06-24 07:27:21 +02:00
|
|
|
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]);
|
|
|
|
});
|
|
|
|
|
2021-07-12 08:43:46 +02:00
|
|
|
it("Reject non-VIP user", function (done: Done) {
|
2021-09-17 05:05:16 +02:00
|
|
|
fetch(endpoint, {
|
|
|
|
...postJSON,
|
2021-06-24 07:27:21 +02:00
|
|
|
body: JSON.stringify({
|
2021-07-12 08:43:46 +02:00
|
|
|
videoID: "vsegpurge01",
|
|
|
|
userID: "segshift_randomuser001",
|
2021-06-24 07:27:21 +02:00
|
|
|
}),
|
|
|
|
})
|
2021-07-12 08:43:46 +02:00
|
|
|
.then(async res => {
|
|
|
|
assert.strictEqual(res.status, 403);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(err => done(err));
|
2021-06-24 07:27:21 +02:00
|
|
|
});
|
|
|
|
|
2021-07-12 08:43:46 +02:00
|
|
|
it("Purge all segments success", function (done: Done) {
|
2021-09-17 05:05:16 +02:00
|
|
|
fetch(endpoint, {
|
|
|
|
...postJSON,
|
2021-06-24 07:27:21 +02:00
|
|
|
body: JSON.stringify({
|
2021-07-12 08:43:46 +02:00
|
|
|
videoID: "vsegpurge01",
|
2021-06-24 07:27:21 +02:00
|
|
|
userID: privateVipUserID,
|
|
|
|
}),
|
|
|
|
})
|
2021-07-12 08:43:46 +02:00
|
|
|
.then(async res => {
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
done(await dbSponsorTimesCompareExpect(db, "vsegpurge01", 1) || await dbSponsorTimesCompareExpect(db, "vseg-not-purged01", 0));
|
|
|
|
})
|
|
|
|
.catch(err => done(err));
|
2021-06-24 07:27:21 +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", function (done: Done) {
|
2021-09-17 05:05:16 +02:00
|
|
|
fetch(endpoint, { ...postJSON })
|
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-06-24 07:27:21 +02:00
|
|
|
});
|