diff --git a/src/routes/addUserAsVIP.ts b/src/routes/addUserAsVIP.ts index 919d308..bd976f0 100644 --- a/src/routes/addUserAsVIP.ts +++ b/src/routes/addUserAsVIP.ts @@ -2,9 +2,12 @@ import {getHash} from "../utils/getHash"; import {db} from "../databases/databases"; import {config} from "../config"; 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 { - const userID = req.query.userID as string; + const userID = req.query.userID as HashedUserID; let adminUserIDInput = req.query.adminUserID as string; const enabled = req.query.enabled === undefined @@ -25,12 +28,12 @@ export async function addUserAsVIP(req: Request, res: Response): Promise 0) { + } else if (!enabled && userIsVIP) { //remove them from the shadow ban list await db.prepare("run", 'DELETE FROM "vipUsers" WHERE "userID" = ?', [userID]); } diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 98ae6e4..3ecb653 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -5,11 +5,12 @@ import { config } from "../config"; import { Category, Service, VideoID, VideoIDHash } from "../types/segments.model"; import { UserID } from "../types/user.model"; import { QueryCacher } from "../utils/queryCacher"; +import { isUserVIP } from "../utils/isUserVIP"; export async function shadowBanUser(req: Request, res: Response): Promise { - const userID = req.query.userID as string; + const userID = req.query.userID as UserID; 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 ? true @@ -27,9 +28,9 @@ export async function shadowBanUser(req: Request, res: Response): Promise 0; + const isVIP = await isUserVIP(adminUserID); if (!isVIP) { //not authorized return res.sendStatus(403); diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index a4c9628..02e8fbe 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -281,7 +281,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise 0; + const isVIP = await isUserVIP(nonAnonUserID); //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; diff --git a/test/cases/getSegmentInfo.ts b/test/cases/getSegmentInfo.ts index c49974b..20545f4 100644 --- a/test/cases/getSegmentInfo.ts +++ b/test/cases/getSegmentInfo.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch"; import {db} from "../../src/databases/databases"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {getHash} from "../../src/utils/getHash"; import assert from "assert"; @@ -53,9 +53,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); - assert.strictEqual(data[0].userAgent, userAgents.vanced); + const expected = [{ + videoID: "upvoted", + votes: 2, + userAgent: userAgents.vanced, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -66,9 +69,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "downvoted"); - assert.strictEqual(data[0].votes, -2); - assert.strictEqual(data[0].userAgent, userAgents.meabot); + const expected = [{ + videoID: "downvoted", + votes: -2, + userAgent: userAgents.meabot, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -79,10 +85,13 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "locked-up"); - assert.strictEqual(data[0].locked, 1); - assert.strictEqual(data[0].votes, 2); - assert.strictEqual(data[0].userAgent, userAgents.mpv); + const expected = [{ + videoID: "locked-up", + locked: 1, + votes: 2, + userAgent: userAgents.mpv, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -93,9 +102,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "infvotes"); - assert.strictEqual(data[0].votes, 100000); - assert.strictEqual(data[0].userAgent, userAgents.nodesb); + const expected = [{ + videoID: "infvotes", + votes: 100000, + userAgent: userAgents.nodesb, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -106,9 +118,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "shadowhidden"); - assert.strictEqual(data[0].shadowHidden, 1); - assert.strictEqual(data[0].userAgent, userAgents.blank); + const expected = [{ + videoID: "shadowhidden", + shadowHidden: 1, + userAgent: userAgents.blank, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -119,9 +134,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "locked-down"); - assert.strictEqual(data[0].votes, -2); - assert.strictEqual(data[0].locked, 1); + const expected = [{ + videoID: "locked-down", + locked: 1, + votes: -2, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -132,8 +150,11 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "hidden"); - assert.strictEqual(data[0].hidden, 1); + const expected = [{ + videoID: "hidden", + hidden: 1, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -144,8 +165,11 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "oldID"); - assert.strictEqual(data[0].votes, 1); + const expected = [{ + videoID: "oldID", + votes: 1, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -156,9 +180,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); + const expected = [{ + videoID: "upvoted", + votes: 2, + }]; assert.strictEqual(data.length, 1); - assert.strictEqual(data[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -169,11 +196,15 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); - assert.strictEqual(data[1].videoID, "downvoted"); - assert.strictEqual(data[1].votes, -2); done(); }) .catch(err => done(err)); @@ -184,8 +215,11 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); + const expected = [{ + videoID: "upvoted", + votes: 2, + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -232,9 +266,12 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); + const expected = [{ + videoID: "upvoted", + votes: 2, + }]; + assert.ok(partialDeepEquals(data, expected)); assert.strictEqual(data.length, 1); - assert.strictEqual(data[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); done(); }) .catch(err => done(err)); @@ -263,10 +300,6 @@ describe("getSegmentInfo", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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(); }) .catch(err => done(err)); @@ -286,11 +319,15 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[0].videoID, "upvoted"); - assert.strictEqual(data[1].videoID, "downvoted"); - assert.strictEqual(data[0].votes, 2); - assert.strictEqual(data[1].votes, -2); done(); }) .catch(err => done(err)); @@ -301,8 +338,11 @@ describe("getSegmentInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data[0].videoID, "upvoted"); - assert.strictEqual(data[0].votes, 2); + const expected = [{ + videoID: "upvoted", + votes: 2 + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/getSkipSegments.ts b/test/cases/getSkipSegments.ts index 6f710a6..ff1edb7 100644 --- a/test/cases/getSkipSegments.ts +++ b/test/cases/getSkipSegments.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch"; import {db} from "../../src/databases/databases"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {getHash} from "../../src/utils/getHash"; import assert from "assert"; @@ -48,12 +48,14 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -64,13 +66,16 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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].UUID, "1-uuid-0-2"); - assert.strictEqual(data[0].videoDuration, 100); + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -81,13 +86,15 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - 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].UUID, "1-uuid-0-2"); - assert.strictEqual(data[0].videoDuration, 100); + const expected = [{ + segment: [1, 11], + category: "sponsor", + UUID: "1-uuid-0", + videoDuration: 100 + }, { + UUID: "1-uuid-0-2" + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -98,12 +105,14 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -114,11 +123,13 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -129,12 +140,14 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -145,12 +158,14 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -171,14 +186,16 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); assert.strictEqual(data.length, 2); - assert.strictEqual(data[0].segment[0], 1); - assert.strictEqual(data[0].segment[1], 11); - assert.strictEqual(data[0].category, "intro"); - assert.strictEqual(data[0].UUID, "1-uuid-6"); - 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-7"); + const expected = [{ + segment: [1, 11], + category: "intro", + UUID: "1-uuid-6", + }, { + segment: [20, 33], + category: "intro", + UUID: "1-uuid-7", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -189,15 +206,17 @@ describe("getSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); 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[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(); }) .catch(err => done(err)); @@ -209,10 +228,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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"); + const expected = [{ + segment: [1, 11], + category: "sponsor", + UUID: "1-uuid-0", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -224,10 +245,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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-4"); + const expected = [{ + segment: [1, 11], + category: "sponsor", + UUID: "1-uuid-4", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -257,10 +280,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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-1"); + const expected = [{ + segment: [1, 11], + category: "sponsor", + UUID: "1-uuid-1", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -272,10 +297,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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-locked-8"); + const expected = [{ + segment: [20, 33], + category: "intro", + UUID: "1-uuid-locked-8", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -287,14 +314,16 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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"); + 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)); done(); }) .catch(err => done(err)); @@ -306,10 +335,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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"); + const expected = [{ + segment: [1, 11], + category: "sponsor", + UUID: "1-uuid-0", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -321,8 +352,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); assert.strictEqual(data.length, 2); - assert.strictEqual(data[0].UUID, "requiredSegmentVid-raw-2"); - assert.strictEqual(data[1].UUID, "requiredSegmentVid-raw-3"); + const expected = [{ + UUID: "requiredSegmentVid-raw-2", + }, { + UUID: "requiredSegmentVid-raw-3", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -334,8 +369,12 @@ describe("getSkipSegments", () => { assert.strictEqual(res.status, 200); const data = await res.json(); assert.strictEqual(data.length, 2); - assert.strictEqual(data[0].UUID, "requiredSegmentVid-raw-2"); - assert.strictEqual(data[1].UUID, "requiredSegmentVid-raw-3"); + const expected = [{ + UUID: "requiredSegmentVid-raw-2", + }, { + UUID: "requiredSegmentVid-raw-3", + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/getSkipSegmentsByHash.ts b/test/cases/getSkipSegmentsByHash.ts index f78e8bb..4c1634a 100644 --- a/test/cases/getSkipSegmentsByHash.ts +++ b/test/cases/getSkipSegmentsByHash.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch"; import {db} from "../../src/databases/databases"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {getHash} from "../../src/utils/getHash"; import {ImportMock,} from "ts-mock-imports"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; @@ -143,11 +143,19 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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[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(); }) .catch(err => done(err)); @@ -161,9 +169,17 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(data.length, 2); assert.strictEqual(data[0].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"); + const expected = [{ + segments: [{ + category: "sponsor", + UUID: "getSegmentsByHash-0-0", + }] + }, { + segments: [{ + category: "sponsor", + }] + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -177,10 +193,19 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(data.length, 2); assert.strictEqual(data[0].segments.length, 2); 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[0].segments[1].UUID, "getSegmentsByHash-0-2"); - assert.strictEqual(data[1].segments[0].category, "sponsor"); + const expected = [{ + segments: [{ + category: "sponsor", + UUID: "getSegmentsByHash-0-0", + }, { + UUID: "getSegmentsByHash-0-2", + }] + }, { + segments: [{ + category: "sponsor", + }] + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -192,12 +217,19 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); assert.strictEqual(data.length, 2); - assert.strictEqual(data[0].segments.length, 2); - 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[0].segments[1].UUID, "getSegmentsByHash-0-2"); - assert.strictEqual(data[1].segments[0].category, "sponsor"); + const expected = [{ + segments: [{ + category: "sponsor", + UUID: "getSegmentsByHash-0-0", + }, { + UUID: "getSegmentsByHash-0-2", + }] + }, { + segments: [{ + category: "sponsor", + }] + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -209,8 +241,13 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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[0].UUID, "getSegmentsByHash-0-0-1"); done(); }) .catch(err => done(err)); @@ -250,8 +287,13 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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[0].category, "sponsor"); done(); }) .catch(err => done(`(get) ${err}`)); @@ -265,14 +307,18 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); assert.strictEqual(data.length, 1); - assert.strictEqual(data[0].segments[0].segment[0], 1); - assert.strictEqual(data[0].segments[0].segment[1], 10); - assert.strictEqual(data[0].segments[0].category, "sponsor"); - assert.strictEqual(data[0].segments[0].UUID, "getSegmentsByHash-0-0"); - assert.strictEqual(data[0].segments[1].segment[0], 20); - assert.strictEqual(data[0].segments[1].segment[1], 30); - assert.strictEqual(data[0].segments[1].category, "intro"); - assert.strictEqual(data[0].segments[1].UUID, "getSegmentsByHash-0-1"); + const expected = [{ + segments: [{ + segment: [1, 10], + category: "sponsor", + UUID: "getSegmentsByHash-0-0", + }, { + segment: [20, 30], + category: "intro", + UUID: "getSegmentsByHash-0-1", + }] + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -284,9 +330,15 @@ describe("getSkipSegmentsByHash", () => { assert.strictEqual(res.status, 200); const data = await res.json(); 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[0].UUID, "requiredSegmentVid-2"); - assert.strictEqual(data[0].segments[0].UUID, "requiredSegmentVid-2"); done(); }) .catch(err => done(err)); @@ -299,8 +351,14 @@ describe("getSkipSegmentsByHash", () => { const data = await res.json(); assert.strictEqual(data.length, 1); assert.strictEqual(data[0].segments.length, 2); - assert.strictEqual(data[0].segments[0].UUID, "requiredSegmentVid-2"); - assert.strictEqual(data[0].segments[1].UUID, "requiredSegmentVid-3"); + const expected = [{ + segments: [{ + UUID: "requiredSegmentVid-2" + }, { + UUID: "requiredSegmentVid-3" + }] + }]; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index 5b1be3f..e6d8529 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {db} from "../../src/databases/databases"; import {getHash} from "../../src/utils/getHash"; import assert from "assert"; @@ -76,7 +76,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 1); + const expected = { + warnings: 1 + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -87,7 +90,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 1); + const expected = { + warnings: 1 + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -98,7 +104,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 2); + const expected = { + warnings: 2 + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -109,7 +118,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 0); + const expected = { + warnings: 0, + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -120,7 +132,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.userName, "c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f"); + const expected = { + userName: "c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f" + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -156,7 +171,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warningReason, "warning0-0"); + const expected = { + warningReason: "warning0-0" + }; + assert.ok(partialDeepEquals(data, expected)); done(); // pass }) .catch(err => done(err)); @@ -167,7 +185,10 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warningReason, "warning1-1"); + const expected = { + warningReason: "warning1-1" + }; + assert.ok(partialDeepEquals(data, expected)); done(); // pass }) .catch(err => done(err)); @@ -178,8 +199,11 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 0); - assert.strictEqual(data.warningReason, ""); + const expected = { + warnings: 0, + warningReason: "" + }; + assert.ok(partialDeepEquals(data, expected)); done(); // pass }) .catch(err => done(err)); @@ -190,8 +214,11 @@ describe("getUserInfo", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.warnings, 1); - assert.strictEqual(data.warningReason, "warning3-0"); + const expected = { + warnings: 1, + warningReason: "warning3-0" + }; + assert.ok(partialDeepEquals(data, expected)); done(); // pass }) .catch(err => done(err)); diff --git a/test/cases/lockCategoriesRecords.ts b/test/cases/lockCategoriesRecords.ts index 7248450..3a16307 100644 --- a/test/cases/lockCategoriesRecords.ts +++ b/test/cases/lockCategoriesRecords.ts @@ -515,7 +515,7 @@ describe("lockCategoriesRecords", () => { }); - it("Should be able to submit a segment to a video with a different no-segment record", (done: Done) => { + it("Should be able to submit a segment to a video with a different no-segment record", (done: Done) => { fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { method: "POST", headers: { diff --git a/test/cases/oldGetSponsorTime.ts b/test/cases/oldGetSponsorTime.ts index a6ed4d0..ef0bcf5 100644 --- a/test/cases/oldGetSponsorTime.ts +++ b/test/cases/oldGetSponsorTime.ts @@ -1,6 +1,6 @@ import fetch from "node-fetch"; import {db} from "../../src/databases/databases"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {getHash} from "../../src/utils/getHash"; import assert from "assert"; @@ -44,7 +44,10 @@ describe("getVideoSponsorTime (Old get method)", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.UUIDs[0], "uuid-1"); + const expected = { + UUIDs: ["uuid-1"], + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); @@ -55,9 +58,11 @@ describe("getVideoSponsorTime (Old get method)", () => { .then(async res => { assert.strictEqual(res.status, 200); const data = await res.json(); - assert.strictEqual(data.sponsorTimes[0][0], 1); - assert.strictEqual(data.sponsorTimes[0][1], 11); - assert.strictEqual(data.UUIDs[0], "uuid-0"); + const expected = { + sponsorTimes: [[1, 11]], + UUIDs: ["uuid-0"] + }; + assert.ok(partialDeepEquals(data, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/oldSubmitSponsorTimes.ts b/test/cases/oldSubmitSponsorTimes.ts index e15efe6..fac3555 100644 --- a/test/cases/oldSubmitSponsorTimes.ts +++ b/test/cases/oldSubmitSponsorTimes.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {db} from "../../src/databases/databases"; import assert from "assert"; @@ -10,9 +10,12 @@ describe("postVideoSponsorTime (Old submission method)", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcQ"]); - assert.strictEqual(row.startTime, 1); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.category, "sponsor"); + const expected = { + startTime: 1, + endTime: 10, + category: "sponsor" + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -29,9 +32,12 @@ describe("postVideoSponsorTime (Old submission method)", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcE"]); - assert.strictEqual(row.startTime, 1); - assert.strictEqual(row.endTime, 11); - assert.strictEqual(row.category, "sponsor"); + const expected = { + startTime: 1, + endTime: 11, + category: "sponsor" + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index d1e496f..6faa921 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -1,7 +1,7 @@ import fetch from "node-fetch"; import {config} from "../../src/config"; import {getHash} from "../../src/utils/getHash"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {db} from "../../src/databases/databases"; import {ImportMock} from "ts-mock-imports"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; @@ -65,9 +65,12 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcR"]); - assert.strictEqual(row.startTime, 2); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.category, "sponsor"); + const expected = { + startTime: 2, + endTime: 10, + category: "sponsor", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -91,10 +94,13 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcF"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); + const expected = { + startTime: 0, + endTime: 10, + locked: 0, + category: "sponsor", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -119,11 +125,13 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "actionType" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcV"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); - assert.strictEqual(row.actionType, "mute"); + const expected = { + startTime: 0, + endTime: 10, + category: "sponsor", + actionType: "mute", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -148,11 +156,13 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZX"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); - assert.strictEqual(row.videoDuration, 4980); + const expected = { + startTime: 0, + endTime: 10, + category: "sponsor", + videoDuration: 4980, + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -177,11 +187,14 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZH"]); - assert.strictEqual(row.startTime, 1); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); - assert.strictEqual(row.videoDuration, 4980.20); + const expected = { + startTime: 1, + endTime: 10, + locked: 0, + category: "sponsor", + videoDuration: 4980.20, + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -206,11 +219,14 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["noDuration"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); - assert.strictEqual(row.videoDuration, 100); + const expected = { + startTime: 0, + endTime: 10, + locked: 0, + category: "sponsor", + videoDuration: 100, + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -240,17 +256,19 @@ describe("postSkipSegments", () => { const lockCategoriesRow = await db.prepare("get", `SELECT * from "lockCategories" WHERE videoID = ?`, ["noDuration"]); const videoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 0`, ["noDuration"]); - const videoRow = videoRows[0]; const hiddenVideoRows = await db.prepare("all", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 1`, ["noDuration"]); 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(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) { return e; } @@ -287,11 +305,14 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "service" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXcG"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 0); - assert.strictEqual(row.category, "sponsor"); - assert.strictEqual(row.service, "PeerTube"); + const expected = { + startTime: 0, + endTime: 10, + locked: 0, + category: "sponsor", + service: "PeerTube", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -315,10 +336,13 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, ["vipuserIDSubmission"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.locked, 1); - assert.strictEqual(row.category, "sponsor"); + const expected = { + startTime: 0, + endTime: 10, + locked: 1, + category: "sponsor", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -865,9 +889,12 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-1"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.userAgent, "Vanced/5.0"); + const expected = { + startTime: 0, + endTime: 10, + userAgent: "Vanced/5.0", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -892,9 +919,12 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-3"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.userAgent, ""); + const expected = { + startTime: 0, + endTime: 10, + userAgent: "", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -919,9 +949,12 @@ describe("postSkipSegments", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-4"]); - assert.strictEqual(row.startTime, 0); - assert.strictEqual(row.endTime, 10); - assert.strictEqual(row.userAgent, "MeaBot/5.0"); + const expected = { + startTime: 0, + endTime: 10, + userAgent: "MeaBot/5.0", + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index 7fdcb52..1cdd6f6 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch"; -import {Done, getbaseURL} from "../utils"; +import {Done, getbaseURL, partialDeepEquals} from "../utils"; import {db} from "../../src/databases/databases"; import {getHash} from "../../src/utils/getHash"; import assert from "assert"; @@ -25,9 +25,12 @@ describe("postWarning", () => { .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]); - assert.strictEqual(row.enabled, 1); - assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID)); - assert.strictEqual(row.reason, json.reason); + const expected = { + enabled: 1, + issuerUserID: getHash(json.issuerUserID), + reason: json.reason, + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -49,8 +52,11 @@ describe("postWarning", () => { .then(async res => { assert.strictEqual(res.status, 409); const row = await db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]); - assert.strictEqual(row.enabled, 1); - assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID)); + const expected = { + enabled: 1, + issuerUserID: getHash(json.issuerUserID), + }; + assert.ok(partialDeepEquals(row, expected)); done(); }) .catch(err => done(err)); @@ -73,7 +79,10 @@ describe("postWarning", () => { .then(async res => { assert.strictEqual(res.status, 200); 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(); }) .catch(err => done(err)); @@ -130,7 +139,10 @@ describe("postWarning", () => { .then(async res => { assert.strictEqual(res.status, 200); 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(); }) .catch(err => done(err)); diff --git a/test/cases/setUsername.ts b/test/cases/setUsername.ts index b86af99..048f750 100644 --- a/test/cases/setUsername.ts +++ b/test/cases/setUsername.ts @@ -1,5 +1,5 @@ import fetch from "node-fetch"; -import { Done, getbaseURL } from "../utils"; +import { Done, getbaseURL, partialDeepEquals } from "../utils"; import { db, privateDB } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import assert from "assert"; diff --git a/test/cases/testUtils.ts b/test/cases/testUtils.ts new file mode 100644 index 0000000..3807dae --- /dev/null +++ b/test/cases/testUtils.ts @@ -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"); + }); +}); \ No newline at end of file diff --git a/test/utils.ts b/test/utils.ts index b82942b..e70532f 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -7,4 +7,20 @@ export function getbaseURL(): string { /** * Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason. */ -export type Done = (err?: any) => void; \ No newline at end of file +export type Done = (err?: any) => void; + +/** + * + * Check object contains expected properties + */ +export const partialDeepEquals = (actual: Record, expected: Record): 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; +};