This commit is contained in:
Ajay Ramachandran 2021-08-05 16:50:41 -04:00
commit 9f0f9054d1
15 changed files with 631 additions and 257 deletions

View file

@ -2,9 +2,12 @@ import {getHash} from "../utils/getHash";
import {db} from "../databases/databases"; import {db} from "../databases/databases";
import {config} from "../config"; import {config} from "../config";
import {Request, Response} from "express"; import {Request, Response} from "express";
import { isUserVIP } from "../utils/isUserVIP";
import { HashedUserID } from "../types/user.model";
export async function addUserAsVIP(req: Request, res: Response): Promise<Response> { export async function addUserAsVIP(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as string; const userID = req.query.userID as HashedUserID;
let adminUserIDInput = req.query.adminUserID as string; let adminUserIDInput = req.query.adminUserID as string;
const enabled = req.query.enabled === undefined const enabled = req.query.enabled === undefined
@ -25,12 +28,12 @@ export async function addUserAsVIP(req: Request, res: Response): Promise<Respons
} }
//check to see if this user is already a vip //check to see if this user is already a vip
const row = await db.prepare("get", 'SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?', [userID]); const userIsVIP = await isUserVIP(userID);
if (enabled && row.userCount == 0) { if (enabled && !userIsVIP) {
//add them to the vip list //add them to the vip list
await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]); await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]);
} else if (!enabled && row.userCount > 0) { } else if (!enabled && userIsVIP) {
//remove them from the shadow ban list //remove them from the shadow ban list
await db.prepare("run", 'DELETE FROM "vipUsers" WHERE "userID" = ?', [userID]); await db.prepare("run", 'DELETE FROM "vipUsers" WHERE "userID" = ?', [userID]);
} }

View file

@ -5,11 +5,12 @@ import { config } from "../config";
import { Category, Service, VideoID, VideoIDHash } from "../types/segments.model"; import { Category, Service, VideoID, VideoIDHash } from "../types/segments.model";
import { UserID } from "../types/user.model"; import { UserID } from "../types/user.model";
import { QueryCacher } from "../utils/queryCacher"; import { QueryCacher } from "../utils/queryCacher";
import { isUserVIP } from "../utils/isUserVIP";
export async function shadowBanUser(req: Request, res: Response): Promise<Response> { export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as string; const userID = req.query.userID as UserID;
const hashedIP = req.query.hashedIP as string; const hashedIP = req.query.hashedIP as string;
let adminUserIDInput = req.query.adminUserID as string; const adminUserIDInput = req.query.adminUserID as UserID;
const enabled = req.query.enabled === undefined const enabled = req.query.enabled === undefined
? true ? true
@ -27,9 +28,9 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
} }
//hash the userID //hash the userID
adminUserIDInput = getHash(adminUserIDInput); const adminUserID = getHash(adminUserIDInput);
const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [adminUserIDInput])).userCount > 0; const isVIP = await isUserVIP(adminUserID);
if (!isVIP) { if (!isVIP) {
//not authorized //not authorized
return res.sendStatus(403); return res.sendStatus(403);

View file

@ -281,7 +281,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
const hashedIP: HashedIP = getHash((ip + config.globalSalt) as IPAddress); const hashedIP: HashedIP = getHash((ip + config.globalSalt) as IPAddress);
//check if this user is on the vip list //check if this user is on the vip list
const isVIP = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "vipUsers" WHERE "userID" = ?`, [nonAnonUserID])).userCount > 0; const isVIP = await isUserVIP(nonAnonUserID);
//check if user voting on own submission //check if user voting on own submission
const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined; const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined;

View file

@ -1,6 +1,6 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
@ -53,9 +53,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "upvoted"); const expected = [{
assert.strictEqual(data[0].votes, 2); videoID: "upvoted",
assert.strictEqual(data[0].userAgent, userAgents.vanced); votes: 2,
userAgent: userAgents.vanced,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -66,9 +69,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "downvoted"); const expected = [{
assert.strictEqual(data[0].votes, -2); videoID: "downvoted",
assert.strictEqual(data[0].userAgent, userAgents.meabot); votes: -2,
userAgent: userAgents.meabot,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -79,10 +85,13 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "locked-up"); const expected = [{
assert.strictEqual(data[0].locked, 1); videoID: "locked-up",
assert.strictEqual(data[0].votes, 2); locked: 1,
assert.strictEqual(data[0].userAgent, userAgents.mpv); votes: 2,
userAgent: userAgents.mpv,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -93,9 +102,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "infvotes"); const expected = [{
assert.strictEqual(data[0].votes, 100000); videoID: "infvotes",
assert.strictEqual(data[0].userAgent, userAgents.nodesb); votes: 100000,
userAgent: userAgents.nodesb,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -106,9 +118,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "shadowhidden"); const expected = [{
assert.strictEqual(data[0].shadowHidden, 1); videoID: "shadowhidden",
assert.strictEqual(data[0].userAgent, userAgents.blank); shadowHidden: 1,
userAgent: userAgents.blank,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -119,9 +134,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "locked-down"); const expected = [{
assert.strictEqual(data[0].votes, -2); videoID: "locked-down",
assert.strictEqual(data[0].locked, 1); locked: 1,
votes: -2,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -132,8 +150,11 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "hidden"); const expected = [{
assert.strictEqual(data[0].hidden, 1); videoID: "hidden",
hidden: 1,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -144,8 +165,11 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "oldID"); const expected = [{
assert.strictEqual(data[0].votes, 1); videoID: "oldID",
votes: 1,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -156,9 +180,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
videoID: "upvoted",
votes: 2,
}];
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].videoID, "upvoted"); assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].votes, 2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -169,11 +196,15 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
videoID: "upvoted",
votes: 2,
}, {
videoID: "downvoted",
votes: -2,
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].videoID, "upvoted");
assert.strictEqual(data[0].votes, 2);
assert.strictEqual(data[1].videoID, "downvoted");
assert.strictEqual(data[1].votes, -2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -184,8 +215,11 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "upvoted"); const expected = [{
assert.strictEqual(data[0].votes, 2); videoID: "upvoted",
votes: 2,
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -232,9 +266,12 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
videoID: "upvoted",
votes: 2,
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].videoID, "upvoted");
assert.strictEqual(data[0].votes, 2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -263,10 +300,6 @@ describe("getSegmentInfo", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].videoID, "upvoted");
assert.strictEqual(data[0].votes, 2);
assert.strictEqual(data[1].videoID, "downvoted");
assert.strictEqual(data[1].votes, -2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -286,11 +319,15 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
videoID: "upvoted",
votes: 2,
}, {
videoID: "downvoted",
votes: -2,
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].videoID, "upvoted");
assert.strictEqual(data[1].videoID, "downvoted");
assert.strictEqual(data[0].votes, 2);
assert.strictEqual(data[1].votes, -2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -301,8 +338,11 @@ describe("getSegmentInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data[0].videoID, "upvoted"); const expected = [{
assert.strictEqual(data[0].votes, 2); videoID: "upvoted",
votes: 2
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,6 +1,6 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
@ -48,12 +48,14 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [12, 14],
category: "sponsor",
UUID: "1-uuid-0-2",
videoDuration: 100
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 12);
assert.strictEqual(data[0].segment[1], 14);
assert.strictEqual(data[0].category, "sponsor");
assert.strictEqual(data[0].UUID, "1-uuid-0-2");
assert.strictEqual(data[0].videoDuration, 100);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -64,13 +66,16 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [1, 11],
category: "sponsor",
UUID: "1-uuid-0",
videoDuration: 100
}, {
UUID: "1-uuid-0-2"
}];
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segment[0], 1); assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segment[1], 11);
assert.strictEqual(data[0].category, "sponsor");
assert.strictEqual(data[0].UUID, "1-uuid-0");
assert.strictEqual(data[1].UUID, "1-uuid-0-2");
assert.strictEqual(data[0].videoDuration, 100);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -81,13 +86,15 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); const expected = [{
assert.strictEqual(data[0].segment[0], 1); segment: [1, 11],
assert.strictEqual(data[0].segment[1], 11); category: "sponsor",
assert.strictEqual(data[0].category, "sponsor"); UUID: "1-uuid-0",
assert.strictEqual(data[0].UUID, "1-uuid-0"); videoDuration: 100
assert.strictEqual(data[1].UUID, "1-uuid-0-2"); }, {
assert.strictEqual(data[0].videoDuration, 100); UUID: "1-uuid-0-2"
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -98,12 +105,14 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [1, 11],
category: "sponsor",
UUID: "1-uuid-0-1",
videoDuration: 120
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1);
assert.strictEqual(data[0].segment[1], 11);
assert.strictEqual(data[0].category, "sponsor");
assert.strictEqual(data[0].UUID, "1-uuid-0-1");
assert.strictEqual(data[0].videoDuration, 120);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -114,11 +123,13 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [20, 33],
category: "intro",
UUID: "1-uuid-2"
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 20);
assert.strictEqual(data[0].segment[1], 33);
assert.strictEqual(data[0].category, "intro");
assert.strictEqual(data[0].UUID, "1-uuid-2");
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -129,12 +140,14 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [1, 11],
category: "sponsor",
UUID: "1-uuid-0",
videoDuration: 100
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1);
assert.strictEqual(data[0].segment[1], 11);
assert.strictEqual(data[0].category, "sponsor");
assert.strictEqual(data[0].UUID, "1-uuid-0");
assert.strictEqual(data[0].videoDuration, 100);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -145,12 +158,14 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [20, 33],
category: "intro",
UUID: "1-uuid-2",
videoDuration: 101
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 20);
assert.strictEqual(data[0].segment[1], 33);
assert.strictEqual(data[0].category, "intro");
assert.strictEqual(data[0].UUID, "1-uuid-2");
assert.strictEqual(data[0].videoDuration, 101);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -171,14 +186,16 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "intro"); category: "intro",
assert.strictEqual(data[0].UUID, "1-uuid-6"); UUID: "1-uuid-6",
assert.strictEqual(data[1].segment[0], 20); }, {
assert.strictEqual(data[1].segment[1], 33); segment: [20, 33],
assert.strictEqual(data[1].category, "intro"); category: "intro",
assert.strictEqual(data[1].UUID, "1-uuid-7"); UUID: "1-uuid-7",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -189,15 +206,17 @@ describe("getSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
const expected = [{
segment: [1, 11],
category: "sponsor",
UUID: "1-uuid-0",
}, {
segment: [20, 33],
category: "intro",
UUID: "1-uuid-2",
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segment[0], 1);
assert.strictEqual(data[0].segment[1], 11);
assert.strictEqual(data[0].category, "sponsor");
assert.strictEqual(data[0].UUID, "1-uuid-0");
assert.strictEqual(data[1].segment[0], 20);
assert.strictEqual(data[1].segment[1], 33);
assert.strictEqual(data[1].category, "intro");
assert.strictEqual(data[1].UUID, "1-uuid-2");
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -209,10 +228,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].UUID, "1-uuid-0"); UUID: "1-uuid-0",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -224,10 +245,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].UUID, "1-uuid-4"); UUID: "1-uuid-4",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -257,10 +280,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].UUID, "1-uuid-1"); UUID: "1-uuid-1",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -272,10 +297,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 20); const expected = [{
assert.strictEqual(data[0].segment[1], 33); segment: [20, 33],
assert.strictEqual(data[0].category, "intro"); category: "intro",
assert.strictEqual(data[0].UUID, "1-uuid-locked-8"); UUID: "1-uuid-locked-8",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -287,14 +314,16 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].UUID, "1-uuid-0"); UUID: "1-uuid-0",
assert.strictEqual(data[1].segment[0], 20); }, {
assert.strictEqual(data[1].segment[1], 33); segment: [20, 33],
assert.strictEqual(data[1].category, "intro"); category: "intro",
assert.strictEqual(data[1].UUID, "1-uuid-2"); UUID: "1-uuid-2",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -306,10 +335,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segment[1], 11); segment: [1, 11],
assert.strictEqual(data[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].UUID, "1-uuid-0"); UUID: "1-uuid-0",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -321,8 +352,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].UUID, "requiredSegmentVid-raw-2"); const expected = [{
assert.strictEqual(data[1].UUID, "requiredSegmentVid-raw-3"); UUID: "requiredSegmentVid-raw-2",
}, {
UUID: "requiredSegmentVid-raw-3",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -334,8 +369,12 @@ describe("getSkipSegments", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].UUID, "requiredSegmentVid-raw-2"); const expected = [{
assert.strictEqual(data[1].UUID, "requiredSegmentVid-raw-3"); UUID: "requiredSegmentVid-raw-2",
}, {
UUID: "requiredSegmentVid-raw-3",
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,6 +1,6 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import {ImportMock,} from "ts-mock-imports"; import {ImportMock,} from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
@ -143,11 +143,19 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
const expected = [{
segments: [{
category: "sponsor",
UUID: "getSegmentsByHash-0-0",
}]
}, {
segments: [{
category: "sponsor",
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 1); assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[1].segments.length, 1); assert.strictEqual(data[1].segments.length, 1);
assert.strictEqual(data[0].segments[0].category, "sponsor");
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0");
assert.strictEqual(data[1].segments[0].category, "sponsor");
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -161,9 +169,17 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 1); assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[1].segments.length, 1); assert.strictEqual(data[1].segments.length, 1);
assert.strictEqual(data[0].segments[0].category, "sponsor"); const expected = [{
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0"); segments: [{
assert.strictEqual(data[1].segments[0].category, "sponsor"); category: "sponsor",
UUID: "getSegmentsByHash-0-0",
}]
}, {
segments: [{
category: "sponsor",
}]
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -177,10 +193,19 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 2); assert.strictEqual(data[0].segments.length, 2);
assert.strictEqual(data[1].segments.length, 1); assert.strictEqual(data[1].segments.length, 1);
assert.strictEqual(data[0].segments[0].category, "sponsor"); const expected = [{
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0"); segments: [{
assert.strictEqual(data[0].segments[1].UUID, "getSegmentsByHash-0-2"); category: "sponsor",
assert.strictEqual(data[1].segments[0].category, "sponsor"); UUID: "getSegmentsByHash-0-0",
}, {
UUID: "getSegmentsByHash-0-2",
}]
}, {
segments: [{
category: "sponsor",
}]
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -192,12 +217,19 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 2); assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 2); const expected = [{
assert.strictEqual(data[1].segments.length, 1); segments: [{
assert.strictEqual(data[0].segments[0].category, "sponsor"); category: "sponsor",
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0"); UUID: "getSegmentsByHash-0-0",
assert.strictEqual(data[0].segments[1].UUID, "getSegmentsByHash-0-2"); }, {
assert.strictEqual(data[1].segments[0].category, "sponsor"); UUID: "getSegmentsByHash-0-2",
}]
}, {
segments: [{
category: "sponsor",
}]
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -209,8 +241,13 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: "getSegmentsByHash-0-0-1"
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 1); assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0-1");
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -250,8 +287,13 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
category: "sponsor",
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 1); assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[0].segments[0].category, "sponsor");
done(); done();
}) })
.catch(err => done(`(get) ${err}`)); .catch(err => done(`(get) ${err}`));
@ -265,14 +307,18 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments[0].segment[0], 1); const expected = [{
assert.strictEqual(data[0].segments[0].segment[1], 10); segments: [{
assert.strictEqual(data[0].segments[0].category, "sponsor"); segment: [1, 10],
assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0"); category: "sponsor",
assert.strictEqual(data[0].segments[1].segment[0], 20); UUID: "getSegmentsByHash-0-0",
assert.strictEqual(data[0].segments[1].segment[1], 30); }, {
assert.strictEqual(data[0].segments[1].category, "intro"); segment: [20, 30],
assert.strictEqual(data[0].segments[1].UUID, "getSegmentsByHash-0-1"); category: "intro",
UUID: "getSegmentsByHash-0-1",
}]
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -284,9 +330,15 @@ describe("getSkipSegmentsByHash", () => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
UUID: "requiredSegmentVid-2"
}, {
UUID: "requiredSegmentVid-3"
}]
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 2); assert.strictEqual(data[0].segments.length, 2);
assert.strictEqual(data[0].segments[0].UUID, "requiredSegmentVid-2");
assert.strictEqual(data[0].segments[0].UUID, "requiredSegmentVid-2");
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -299,8 +351,14 @@ describe("getSkipSegmentsByHash", () => {
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.length, 1); assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments.length, 2); assert.strictEqual(data[0].segments.length, 2);
assert.strictEqual(data[0].segments[0].UUID, "requiredSegmentVid-2"); const expected = [{
assert.strictEqual(data[0].segments[1].UUID, "requiredSegmentVid-3"); segments: [{
UUID: "requiredSegmentVid-2"
}, {
UUID: "requiredSegmentVid-3"
}]
}];
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
@ -76,7 +76,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 1); const expected = {
warnings: 1
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -87,7 +90,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 1); const expected = {
warnings: 1
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -98,7 +104,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 2); const expected = {
warnings: 2
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -109,7 +118,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 0); const expected = {
warnings: 0,
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -120,7 +132,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.userName, "c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f"); const expected = {
userName: "c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f"
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -156,7 +171,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warningReason, "warning0-0"); const expected = {
warningReason: "warning0-0"
};
assert.ok(partialDeepEquals(data, expected));
done(); // pass done(); // pass
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -167,7 +185,10 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warningReason, "warning1-1"); const expected = {
warningReason: "warning1-1"
};
assert.ok(partialDeepEquals(data, expected));
done(); // pass done(); // pass
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -178,8 +199,11 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 0); const expected = {
assert.strictEqual(data.warningReason, ""); warnings: 0,
warningReason: ""
};
assert.ok(partialDeepEquals(data, expected));
done(); // pass done(); // pass
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -190,8 +214,11 @@ describe("getUserInfo", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.warnings, 1); const expected = {
assert.strictEqual(data.warningReason, "warning3-0"); warnings: 1,
warningReason: "warning3-0"
};
assert.ok(partialDeepEquals(data, expected));
done(); // pass done(); // pass
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,6 +1,6 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
@ -44,7 +44,10 @@ describe("getVideoSponsorTime (Old get method)", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.UUIDs[0], "uuid-1"); const expected = {
UUIDs: ["uuid-1"],
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -55,9 +58,11 @@ describe("getVideoSponsorTime (Old get method)", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await res.json(); const data = await res.json();
assert.strictEqual(data.sponsorTimes[0][0], 1); const expected = {
assert.strictEqual(data.sponsorTimes[0][1], 11); sponsorTimes: [[1, 11]],
assert.strictEqual(data.UUIDs[0], "uuid-0"); UUIDs: ["uuid-0"]
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import assert from "assert"; import assert from "assert";
@ -10,9 +10,12 @@ describe("postVideoSponsorTime (Old submission method)", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcQ"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcQ"]);
assert.strictEqual(row.startTime, 1); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 1,
assert.strictEqual(row.category, "sponsor"); endTime: 10,
category: "sponsor"
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -29,9 +32,12 @@ describe("postVideoSponsorTime (Old submission method)", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcE"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcE"]);
assert.strictEqual(row.startTime, 1); const expected = {
assert.strictEqual(row.endTime, 11); startTime: 1,
assert.strictEqual(row.category, "sponsor"); endTime: 11,
category: "sponsor"
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,7 +1,7 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {config} from "../../src/config"; import {config} from "../../src/config";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {ImportMock} from "ts-mock-imports"; import {ImportMock} from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
@ -65,9 +65,12 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcR"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcR"]);
assert.strictEqual(row.startTime, 2); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 2,
assert.strictEqual(row.category, "sponsor"); endTime: 10,
category: "sponsor",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -91,10 +94,13 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcF"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcF"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); locked: 0,
category: "sponsor",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -119,11 +125,13 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "actionType" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcV"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "actionType" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcV"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); category: "sponsor",
assert.strictEqual(row.actionType, "mute"); actionType: "mute",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -148,11 +156,13 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZX"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZX"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); category: "sponsor",
assert.strictEqual(row.videoDuration, 4980); videoDuration: 4980,
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -177,11 +187,14 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZH"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZH"]);
assert.strictEqual(row.startTime, 1); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 1,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); locked: 0,
assert.strictEqual(row.videoDuration, 4980.20); category: "sponsor",
videoDuration: 4980.20,
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -206,11 +219,14 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["noDuration"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["noDuration"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); locked: 0,
assert.strictEqual(row.videoDuration, 100); category: "sponsor",
videoDuration: 100,
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -240,17 +256,19 @@ describe("postSkipSegments", () => {
const lockCategoriesRow = await db.prepare("get", `SELECT * from "lockCategories" WHERE videoID = ?`, ["noDuration"]); const lockCategoriesRow = await db.prepare("get", `SELECT * from "lockCategories" WHERE videoID = ?`, ["noDuration"]);
const videoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" const videoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration"
FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 0`, ["noDuration"]); FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 0`, ["noDuration"]);
const videoRow = videoRows[0];
const hiddenVideoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" const hiddenVideoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration"
FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 1`, ["noDuration"]); FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 1`, ["noDuration"]);
assert.ok(!lockCategoriesRow); assert.ok(!lockCategoriesRow);
const expected = {
startTime: 1,
endTime: 10,
locked: 0,
category: "sponsor",
videoDuration: 100,
};
assert.ok(partialDeepEquals(videoRows[0], expected));
assert.strictEqual(videoRows.length, 1); assert.strictEqual(videoRows.length, 1);
assert.strictEqual(hiddenVideoRows.length, 1); assert.strictEqual(hiddenVideoRows.length, 1);
assert.strictEqual(videoRow.startTime, 1);
assert.strictEqual(videoRow.endTime, 10);
assert.strictEqual(videoRow.locked, 0);
assert.strictEqual(videoRow.category, "sponsor");
assert.strictEqual(videoRow.videoDuration, 100);
} catch (e) { } catch (e) {
return e; return e;
} }
@ -287,11 +305,14 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "service" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcG"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "service" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcG"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 0); endTime: 10,
assert.strictEqual(row.category, "sponsor"); locked: 0,
assert.strictEqual(row.service, "PeerTube"); category: "sponsor",
service: "PeerTube",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -315,10 +336,13 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["vipuserIDSubmission"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["vipuserIDSubmission"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.locked, 1); endTime: 10,
assert.strictEqual(row.category, "sponsor"); locked: 1,
category: "sponsor",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -865,9 +889,12 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-1"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-1"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.userAgent, "Vanced/5.0"); endTime: 10,
userAgent: "Vanced/5.0",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -892,9 +919,12 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-3"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-3"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.userAgent, ""); endTime: 10,
userAgent: "",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -919,9 +949,12 @@ describe("postSkipSegments", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-4"]); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-4"]);
assert.strictEqual(row.startTime, 0); const expected = {
assert.strictEqual(row.endTime, 10); startTime: 0,
assert.strictEqual(row.userAgent, "MeaBot/5.0"); endTime: 10,
userAgent: "MeaBot/5.0",
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils"; import {Done, getbaseURL, partialDeepEquals} from "../utils";
import {db} from "../../src/databases/databases"; import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash"; import {getHash} from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
@ -25,9 +25,12 @@ describe("postWarning", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]); const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1); const expected = {
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID)); enabled: 1,
assert.strictEqual(row.reason, json.reason); issuerUserID: getHash(json.issuerUserID),
reason: json.reason,
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -49,8 +52,11 @@ describe("postWarning", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 409); assert.strictEqual(res.status, 409);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1); const expected = {
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID)); enabled: 1,
issuerUserID: getHash(json.issuerUserID),
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -73,7 +79,10 @@ describe("postWarning", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 0); const expected = {
enabled: 0
};
assert.ok(partialDeepEquals(row, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
@ -130,7 +139,10 @@ describe("postWarning", () => {
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); const data = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(data.enabled, 1); const expected = {
enabled: 1
};
assert.ok(partialDeepEquals(data, expected));
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View file

@ -1,5 +1,5 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import { Done, getbaseURL } from "../utils"; import { Done, getbaseURL, partialDeepEquals } from "../utils";
import { db, privateDB } from "../../src/databases/databases"; import { db, privateDB } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";

134
test/cases/testUtils.ts Normal file
View file

@ -0,0 +1,134 @@
import assert from "assert";
import { partialDeepEquals } from "../utils";
describe("Test utils ", () => {
it("objectContain", async () => {
assert(partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true
},
{
}
), "Did not match empty expect");
assert(partialDeepEquals(
[{a: [1,2,3]}, {a: [1,2]}],
[{a: [1,2,3]}, {a: [1,2]}]
), "Did not match same arrays");
assert(partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true
},
{
vip: true,
old: 20
}
), "Did not match same partial properties");
// do not match incorrect propeties
assert(!partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true
},
{
vip: false,
old: 19
}
), "Matched different properties");
// do not match missing property
assert(!partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true
},
{
vip: true,
child: {
name: ""
}
}
));
assert(!partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true,
child: {
name: "a"
}
},
{
vip: true,
child: {
name: ""
}
}
), "Matched incorrect child property");
assert(!partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true,
child: {
name: "a",
child: {
name: "a",
}
}
},
{
vip: true,
child: {
name: "a",
child: {
name: "b",
}
}
}
), "Matched incorrected 2-nested property");
assert(partialDeepEquals(
{
name: "John Wick",
old: 20,
vip: true,
child: {
name: "a",
child: {
name: "b",
}
}
},
{
vip: true,
child: {
name: "a",
child: {
name: "b",
}
}
}
), "Did not match exact child properties");
assert(partialDeepEquals(
{
name: "John Wick",
values: [{
name: "a",
}, {
name: "b",
}]
},
{
values: [{
name: "a",
}]
}
), "Did not match partial child array");
});
});

View file

@ -8,3 +8,19 @@ export function getbaseURL(): string {
* Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason. * Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason.
*/ */
export type Done = (err?: any) => void; export type Done = (err?: any) => void;
/**
*
* Check object contains expected properties
*/
export const partialDeepEquals = (actual: Record<string, any>, expected: Record<string, any>): boolean => {
// loop over key, value of expected
for (const [ key, value ] of Object.entries(expected)) {
// if value is object or array, recurse
if (Array.isArray(value) || typeof value === "object") {
if (!partialDeepEquals(actual?.[key], value)) return false;
}
else if (actual?.[key] !== value) return false;
}
return true;
};