From 4e50f0ab4b63817f1e5c92ed4b8e85382bd0fc96 Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 22 Sep 2021 23:18:31 -0400 Subject: [PATCH] remaining tests --- test/cases/postClearCache.ts | 37 +- test/cases/postPurgeAllSegments.ts | 35 +- test/cases/postSkipSegments.ts | 826 ++++++++++++++--------------- test/cases/postWarning.ts | 45 +- test/cases/segmentShift.ts | 101 ++-- test/cases/setUsername.ts | 133 ++--- test/cases/shadowBanUser.ts | 95 +++- test/cases/unBan.ts | 27 +- test/cases/voteOnSponsorTime.ts | 206 +++---- 9 files changed, 743 insertions(+), 762 deletions(-) diff --git a/test/cases/postClearCache.ts b/test/cases/postClearCache.ts index bcaf141..7269e3e 100644 --- a/test/cases/postClearCache.ts +++ b/test/cases/postClearCache.ts @@ -1,13 +1,12 @@ -import fetch from "node-fetch"; -import { Done } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { db } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import assert from "assert"; +import { client } from "../utils/httpClient"; const VIPUser = "clearCacheVIP"; const regularUser = "regular-user"; -const endpoint = `${getbaseURL()}/api/clearCache`; +const endpoint = "/api/clearCache"; +const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } }); describe("postClearCache", () => { before(async () => { @@ -16,10 +15,8 @@ describe("postClearCache", () => { await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`); }); - it("Should be able to clear cache for existing video", (done: Done) => { - fetch(`${endpoint}?userID=${VIPUser}&videoID=clear-test`, { - method: "POST" - }) + it("Should be able to clear cache for existing video", (done) => { + postClearCache(VIPUser, "clear-test") .then(res => { assert.strictEqual(res.status, 200); done(); @@ -27,10 +24,8 @@ describe("postClearCache", () => { .catch(err => done(err)); }); - it("Should be able to clear cache for nonexistent video", (done: Done) => { - fetch(`${endpoint}?userID=${VIPUser}&videoID=dne-video`, { - method: "POST" - }) + it("Should be able to clear cache for nonexistent video", (done) => { + postClearCache(VIPUser, "dne-video") .then(res => { assert.strictEqual(res.status, 200); done(); @@ -38,10 +33,8 @@ describe("postClearCache", () => { .catch(err => done(err)); }); - it("Should get 403 as non-vip", (done: Done) => { - fetch(`${endpoint}?userID=${regularUser}&videoID=clear-tes`, { - method: "POST" - }) + it("Should get 403 as non-vip", (done) => { + postClearCache(regularUser, "clear-test") .then(async res => { assert.strictEqual(res.status, 403); done(); @@ -49,10 +42,8 @@ describe("postClearCache", () => { .catch(err => done(err)); }); - it("Should give 400 with missing videoID", (done: Done) => { - fetch(`${endpoint}?userID=${VIPUser}`, { - method: "POST" - }) + it("Should give 400 with missing videoID", (done) => { + client.post(endpoint, { params: { userID: VIPUser } }) .then(async res => { assert.strictEqual(res.status, 400); done(); @@ -60,10 +51,8 @@ describe("postClearCache", () => { .catch(err => done(err)); }); - it("Should give 400 with missing userID", (done: Done) => { - fetch(`${endpoint}?userID=${VIPUser}`, { - method: "POST" - }) + it("Should give 400 with missing userID", (done) => { + client.post(endpoint, { params: { videoID: "clear-test" } }) .then(async res => { assert.strictEqual(res.status, 400); done(); diff --git a/test/cases/postPurgeAllSegments.ts b/test/cases/postPurgeAllSegments.ts index 4fc0874..9d9c675 100644 --- a/test/cases/postPurgeAllSegments.ts +++ b/test/cases/postPurgeAllSegments.ts @@ -1,10 +1,8 @@ -import fetch from "node-fetch"; -import { Done, postJSON } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { db } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import { IDatabase } from "../../src/databases/IDatabase"; import assert from "assert"; +import { client } from "../utils/httpClient"; async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) { const votes = 0, @@ -34,7 +32,8 @@ async function dbSponsorTimesCompareExpect(db: IDatabase, videoId: string, expec describe("postPurgeAllSegments", function () { const privateVipUserID = "VIPUser-purgeAll"; const vipUserID = getHash(privateVipUserID); - const endpoint = `${getbaseURL()}/api/purgeAllSegments`; + const endpoint = "/api/purgeAllSegments"; + const postSegmentShift = (videoID: string, userID: string) => client.post(endpoint, { videoID, userID }); before(async function () { // startTime and endTime get set in beforeEach for consistency @@ -46,29 +45,17 @@ describe("postPurgeAllSegments", function () { await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]); }); - it("Reject non-VIP user", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegpurge01", - userID: "segshift_randomuser001", - }), - }) - .then(async res => { + it("Reject non-VIP user", function (done) { + postSegmentShift("vsegpurge01", "segshift_randomuser001") + .then(res => { assert.strictEqual(res.status, 403); done(); }) .catch(err => done(err)); }); - it("Purge all segments success", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegpurge01", - userID: privateVipUserID, - }), - }) + it("Purge all segments success", function (done) { + postSegmentShift("vsegpurge01", privateVipUserID) .then(async res => { assert.strictEqual(res.status, 200); done(await dbSponsorTimesCompareExpect(db, "vsegpurge01", 1) || await dbSponsorTimesCompareExpect(db, "vseg-not-purged01", 0)); @@ -76,9 +63,9 @@ describe("postPurgeAllSegments", function () { .catch(err => done(err)); }); - it("Should return 400 if missing body", function (done: Done) { - fetch(endpoint, { ...postJSON }) - .then(async res => { + it("Should return 400 if missing body", function (done) { + client.post(endpoint, {}) + .then(res => { assert.strictEqual(res.status, 400); done(); }) diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index 829eed6..3e0e89a 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -1,14 +1,12 @@ -import fetch from "node-fetch"; import { config } from "../../src/config"; import { getHash } from "../../src/utils/getHash"; -import { Done, postJSON } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { partialDeepEquals } from "../utils/partialDeepEquals"; import { db } from "../../src/databases/databases"; import { ImportMock } from "ts-mock-imports"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import { YouTubeApiMock } from "../youtubeMock"; import assert from "assert"; +import { client } from "../utils/httpClient"; const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI"); const sinonStub = mockManager.mock("listVideos"); @@ -38,7 +36,17 @@ describe("postSkipSegments", () => { const queryDatabaseActionType = (videoID: string) => db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "actionType" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoID]); const queryDatabaseDuration = (videoID: string) => db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoID]); - const endpoint = `${getbaseURL()}/api/skipSegments`; + const endpoint = "/api/skipSegments"; + const postSkipSegmentJSON = (data: Record) => client({ + method: "POST", + url: endpoint, + data + }); + const postSkipSegmentParam = (params: Record) => client({ + method: "POST", + url: endpoint, + params + }); before(() => { const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; @@ -81,10 +89,14 @@ describe("postSkipSegments", () => { db.prepare("run", insertVipUserQuery, [getHash(submitVIPuser)]); }); - it("Should be able to submit a single time (Params method)", (done: Done) => { + it("Should be able to submit a single time (Params method)", (done) => { const videoID = "dQw4w9WgXcR"; - fetch(`${endpoint}?videoID=${videoID}&startTime=2&endTime=10&userID=${submitUserOne}&category=sponsor`, { - method: "POST" + postSkipSegmentParam({ + videoID, + startTime: 2, + endTime: 10, + userID: submitUserOne, + category: "sponsor" }) .then(async res => { assert.strictEqual(res.status, 200); @@ -100,18 +112,15 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time (JSON method)", (done: Done) => { + it("Should be able to submit a single time (JSON method)", (done) => { const videoID = "dQw4w9WgXcF"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -128,19 +137,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time with an action type (JSON method)", (done: Done) => { + it("Should be able to submit a single time with an action type (JSON method)", (done) => { const videoID = "dQw4w9WgXcV"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [0, 10], - category: "sponsor", - actionType: "mute" - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [0, 10], + category: "sponsor", + actionType: "mute" + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -157,19 +163,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should not be able to submit an intro with mute action type (JSON method)", (done: Done) => { + it("Should not be able to submit an intro with mute action type (JSON method)", (done) => { const videoID = "dQw4w9WgXpQ"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [0, 10], - category: "intro", - actionType: "mute" - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [0, 10], + category: "intro", + actionType: "mute" + }], }) .then(async res => { assert.strictEqual(res.status, 400); @@ -180,19 +183,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time with a duration from the YouTube API (JSON method)", (done: Done) => { + it("Should be able to submit a single time with a duration from the YouTube API (JSON method)", (done) => { const videoID = "dQw4w9WgXZX"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - videoDuration: 100, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + videoDuration: 100, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -209,19 +209,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time with a precise duration close to the one from the YouTube API (JSON method)", (done: Done) => { + it("Should be able to submit a single time with a precise duration close to the one from the YouTube API (JSON method)", (done) => { const videoID = "dQw4w9WgXZH"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - videoDuration: 4980.20, - segments: [{ - segment: [1, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + videoDuration: 4980.20, + segments: [{ + segment: [1, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -239,19 +236,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time with a duration in the body (JSON method)", (done: Done) => { + it("Should be able to submit a single time with a duration in the body (JSON method)", (done) => { const videoID = "noDuration"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - videoDuration: 100, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + videoDuration: 100, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -275,17 +269,14 @@ describe("postSkipSegments", () => { VALUES(?, ?, ?)`, [getHash("VIPUser-lockCategories"), videoID, "sponsor"]); try { - const res = await fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - videoDuration: 100, - segments: [{ - segment: [1, 10], - category: "sponsor", - }], - }), + const res = await postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + videoDuration: 100, + segments: [{ + segment: [1, 10], + category: "sponsor", + }], }); assert.strictEqual(res.status, 200); const lockCategoriesRow = await db.prepare("get", `SELECT * from "lockCategories" WHERE videoID = ?`, [videoID]); @@ -309,10 +300,16 @@ describe("postSkipSegments", () => { } }); - it("Should still not be allowed if youtube thinks duration is 0", (done: Done) => { + it("Should still not be allowed if youtube thinks duration is 0", (done) => { const videoID= "noDuration"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=10000&userID=${submitUserThree}&category=sponsor`, { - method: "POST", + postSkipSegmentJSON({ + userID: submitUserThree, + videoID, + videoDuration: 100, + segments: [{ + segment: [30, 10000], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 403); @@ -321,19 +318,16 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit a single time under a different service (JSON method)", (done: Done) => { + it("Should be able to submit a single time under a different service (JSON method)", (done) => { const videoID = "dQw4w9WgXcG"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - service: "PeerTube", - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + service: "PeerTube", + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -351,18 +345,15 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("VIP submission should start locked", (done: Done) => { + it("VIP submission should start locked", (done) => { const videoID = "vipuserIDSubmission"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitVIPuser, - videoID, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitVIPuser, + videoID, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -379,21 +370,18 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit multiple times (JSON method)", (done: Done) => { + it("Should be able to submit multiple times (JSON method)", (done) => { const videoID = "dQw4w9WgXcT"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [3, 10], - category: "sponsor", - }, { - segment: [30, 60], - category: "intro", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [3, 10], + category: "sponsor", + }, { + segment: [30, 60], + category: "intro", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -413,27 +401,24 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }).timeout(5000); - it("Should allow multiple times if total is under 80% of video(JSON method)", (done: Done) => { + it("Should allow multiple times if total is under 80% of video(JSON method)", (done) => { const videoID = "L_jWHffIx5E"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [3, 3000], - category: "sponsor", - }, { - segment: [3002, 3050], - category: "intro", - }, { - segment: [45, 100], - category: "interaction", - }, { - segment: [99, 170], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [3, 3000], + category: "sponsor", + }, { + segment: [3002, 3050], + category: "intro", + }, { + segment: [45, 100], + category: "interaction", + }, { + segment: [99, 170], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 200); @@ -461,27 +446,24 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }).timeout(5000); - it("Should reject multiple times if total is over 80% of video (JSON method)", (done: Done) => { + it("Should reject multiple times if total is over 80% of video (JSON method)", (done) => { const videoID = "n9rIGdXnSJc"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [0, 2000], - category: "interaction", - }, { - segment: [3000, 4000], - category: "sponsor", - }, { - segment: [1500, 2750], - category: "sponsor", - }, { - segment: [4050, 4750], - category: "intro", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [0, 2000], + category: "interaction", + }, { + segment: [3000, 4000], + category: "sponsor", + }, { + segment: [1500, 2750], + category: "sponsor", + }, { + segment: [4050, 4750], + category: "intro", + }], }) .then(async res => { assert.strictEqual(res.status, 403); @@ -492,24 +474,21 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }).timeout(5000); - it("Should reject multiple times if total is over 80% of video including previosuly submitted times(JSON method)", (done: Done) => { + it("Should reject multiple times if total is over 80% of video including previosuly submitted times(JSON method)", (done) => { const videoID = "80percent_video"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [2000, 4000], - category: "sponsor", - }, { - segment: [1500, 2750], - category: "sponsor", - }, { - segment: [4050, 4750], - category: "sponsor", - }], - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [2000, 4000], + category: "sponsor", + }, { + segment: [1500, 2750], + category: "sponsor", + }, { + segment: [4050, 4750], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 403); @@ -533,10 +512,14 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }).timeout(5000); - it("Should be accepted if a non-sponsor is less than 1 second", (done: Done) => { + it("Should be accepted if a non-sponsor is less than 1 second", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=30.5&userID=${submitUserTwo}&category=intro`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 30, + endTime: 30.5, + userID: submitUserTwo, + category: "intro" }) .then(res => { assert.strictEqual(res.status, 200); @@ -545,10 +528,14 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be rejected if segment starts and ends at the same time", (done: Done) => { + it("Should be rejected if segment starts and ends at the same time", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=90&endTime=90&userID=${submitUserTwo}&category=intro`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 90, + endTime: 90, + userID: submitUserTwo, + category: "intro" }) .then(res => { assert.strictEqual(res.status, 400); @@ -557,10 +544,14 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be accepted if highlight segment starts and ends at the same time", (done: Done) => { + it("Should be accepted if highlight segment starts and ends at the same time", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=30&userID=${submitUserTwo}&category=poi_highlight`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 30, + endTime: 30, + userID: submitUserTwo, + category: "poi_highlight" }) .then(res => { assert.strictEqual(res.status, 200); @@ -569,10 +560,14 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be rejected if highlight segment doesn't start and end at the same time", (done: Done) => { + it("Should be rejected if highlight segment doesn't start and end at the same time", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=30.5&userID=${submitUserTwo}&category=poi_highlight`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 30, + endTime: 30.5, + userID: submitUserTwo, + category: "poi_highlight" }) .then(res => { assert.strictEqual(res.status, 400); @@ -581,10 +576,13 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be rejected if a sponsor is less than 1 second", (done: Done) => { + it("Should be rejected if a sponsor is less than 1 second", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=30.5&userID=${submitUserTwo}`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 30, + endTime: 30.5, + userID: submitUserTwo }) .then(res => { assert.strictEqual(res.status, 400); @@ -593,45 +591,50 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be rejected if over 80% of the video", (done: Done) => { + it("Should be rejected if over 80% of the video", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=30&endTime=1000000&userID=${submitUserTwo}&category=sponsor`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 30, + endTime: 1000000, + userID: submitUserTwo, + category: "sponsor" }) - .then( async res => { + .then(res => { assert.strictEqual(res.status, 403); done(); }) .catch(err => done(err)); }); - it("Should be rejected if NB's predicted probability is <70%.", (done: Done) => { + it("Should be rejected if NB's predicted probability is <70%.", (done) => { const videoID = "LevkAjUE6d4"; - fetch(`${endpoint}?videoID=${videoID}&startTime=40&endTime=60&userID=${submitUserTwo}&category=sponsor`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 40, + endTime: 60, + userID: submitUserTwo, + category: "sponsor" }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 200); done(); }) .catch(err => done(err)); }); - it("Should be rejected with custom message if user has to many active warnings", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: warnUser01, - videoID: warnVideoID, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + it("Should be rejected with custom message if user has to many active warnings", (done) => { + postSkipSegmentJSON({ + userID: warnUser01, + videoID: warnVideoID, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 403); - const errorMessage = await res.text(); + const errorMessage = res.data; const reason = "Reason01"; const expected = "Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes" + " that are not malicious, and we just want to clarify the rules. " @@ -644,97 +647,85 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be accepted if user has some active warnings", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: warnUser02, - videoID: warnVideoID, - segments: [{ - segment: [50, 60], - category: "sponsor", - }], - }), + it("Should be accepted if user has some active warnings", (done) => { + postSkipSegmentJSON({ + userID: warnUser02, + videoID: warnVideoID, + segments: [{ + segment: [50, 60], + category: "sponsor", + }], }) - .then(async res => { + .then(res => { if (res.status === 200) { done(); // success } else { - const body = await res.text(); - done(`Status code was ${res.status} ${body}`); + done(`Status code was ${res.status} ${res.data}`); } }) .catch(err => done(err)); }); - it("Should be accepted if user has some warnings removed", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: warnUser03, - videoID: warnVideoID, - segments: [{ - segment: [53, 60], - category: "sponsor", - }], - }), + it("Should be accepted if user has some warnings removed", (done) => { + postSkipSegmentJSON({ + userID: warnUser03, + videoID: warnVideoID, + segments: [{ + segment: [53, 60], + category: "sponsor", + }], }) - .then(async res => { + .then(res => { if (res.status === 200) { done(); // success } else { - const body = await res.text(); - done(`Status code was ${res.status} ${body}`); + done(`Status code was ${res.status} ${res.data}`); } }) .catch(err => done(err)); }); - it("Should return 400 for missing params (Params method)", (done: Done) => { - fetch(`${endpoint}?startTime=9&endTime=10&userID=${submitUserOne}`, { - method: "POST", + it("Should return 400 for missing params (Params method)", (done) => { + postSkipSegmentParam({ + startTime: 9, + endTime: 10, + userID: submitUserOne }) - .then(async res => { + .then(res => { if (res.status === 400) done(); else done(true); }) .catch(err => done(err)); }); - it("Should be rejected with default message if user has to many active warnings", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: warnUser01, - videoID: warnVideoID, - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - }), + it("Should be rejected with default message if user has to many active warnings", (done) => { + postSkipSegmentJSON({ + userID: warnUser01, + videoID: warnVideoID, + segments: [{ + segment: [0, 10], + category: "sponsor", + }], }) .then(async res => { assert.strictEqual(res.status, 403); - const errorMessage = await res.text(); + const errorMessage = res.data; assert.notStrictEqual(errorMessage, ""); done(); }) .catch(err => done(err)); }); - it("Should return 400 for missing params (JSON method) 1", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - segments: [{ - segment: [9, 10], - category: "sponsor", - }, { - segment: [31, 60], - category: "intro", - }], - }), + it("Should return 400 for missing params (JSON method) 1", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + segments: [{ + segment: [9, 10], + category: "sponsor", + }, { + segment: [31, 60], + category: "intro", + }], }) .then(res => { assert.strictEqual(res.status, 400); @@ -742,13 +733,10 @@ describe("postSkipSegments", () => { }) .catch(err => done(err)); }); - it("Should return 400 for missing params (JSON method) 2", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID: badInputVideoID, - }), + it("Should return 400 for missing params (JSON method) 2", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + videoID: badInputVideoID, }) .then(res => { assert.strictEqual(res.status, 400); @@ -756,54 +744,17 @@ describe("postSkipSegments", () => { }) .catch(err => done(err)); }); - it("Should return 400 for missing params (JSON method) 3", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID: badInputVideoID, - segments: [{ - segment: [0], - category: "sponsor", - }, { - segment: [31, 60], - category: "intro", - }], - }), - }) - .then(res => { - assert.strictEqual(res.status, 400); - done(); - }) - .catch(err => done(err)); - }); - it("Should return 400 for missing params (JSON method) 4", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID: badInputVideoID, - segments: [{ - segment: [9, 10], - }, { - segment: [31, 60], - category: "intro", - }], - }), - }) - .then(res => { - assert.strictEqual(res.status, 400); - done(); - }) - .catch(err => done(err)); - }); - it("Should return 400 for missing params (JSON method) 5", (done: Done) => { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID: badInputVideoID, - }), + it("Should return 400 for missing params (JSON method) 3", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + videoID: badInputVideoID, + segments: [{ + segment: [0], + category: "sponsor", + }, { + segment: [31, 60], + category: "intro", + }], }) .then(res => { assert.strictEqual(res.status, 400); @@ -812,73 +763,94 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should return 403 and custom reason for submiting in lockedCategory", async () => { + it("Should return 400 for missing params (JSON method) 4", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + videoID: badInputVideoID, + segments: [{ + segment: [9, 10], + }, { + segment: [31, 60], + category: "intro", + }], + }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should return 400 for missing params (JSON method) 5", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + videoID: badInputVideoID, + }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should return 403 and custom reason for submiting in lockedCategory", (done) => { const videoID = "lockedVideo"; - await db.prepare("run", `INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") - VALUES(?, ?, ?, ?)`, [getHash("VIPUser-lockCategories"), videoID, "sponsor", "Custom Reason"]); - - try { - const res = await fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [1, 10], - category: "sponsor", - }], - }), - }); - - assert.strictEqual(res.status, 403); - assert.match(await res.text(), /Lock reason: /); - assert.match(await res.text(), /Custom Reason/); - } catch (e) { - return e; - } + db.prepare("run", `INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") + VALUES(?, ?, ?, ?)`, [getHash("VIPUser-lockCategories"), videoID, "sponsor", "Custom Reason"]) + .then(() => postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [1, 10], + category: "sponsor", + }], + })) + .then(res => { + assert.strictEqual(res.status, 403); + assert.match(res.data, /Lock reason: /); + assert.match(res.data, /Custom Reason/); + done(); + }) + .catch(err => done(err)); }); - it("Should return 403 for submiting in lockedCategory", async () => { + it("Should return 403 for submiting in lockedCategory", (done) => { const videoID = "lockedVideo1"; - await db.prepare("run", `INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") - VALUES(?, ?, ?, ?)`, [getHash("VIPUser-lockCategories"), videoID, "intro", ""]); + db.prepare("run", `INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") + VALUES(?, ?, ?, ?)`, [getHash("VIPUser-lockCategories"), videoID, "intro", ""]) + .then(() => postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: [1, 10], + category: "intro", + }], + })) + .then(res => { + assert.strictEqual(res.status, 403); + assert.doesNotMatch(res.data, /Lock reason: /); + assert.doesNotMatch(res.data, /Custom Reason/); + done(); + }) + .catch(err => done(err)); + }); - try { - const res = await fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: [1, 10], - category: "intro", - }], - }), - }); - - assert.strictEqual(res.status, 403); - assert.doesNotMatch(await res.text(), /Lock reason: /); - assert.doesNotMatch(await res.text(), /Custom Reason/); - } catch (e) { - return e; - } - }).timeout(5000); - - it("Should be able to submit with custom user-agent 1", (done: Done) => { - fetch(endpoint, { + it("Should be able to submit with custom user-agent 1", (done) => { + client({ + url: endpoint, method: "POST", headers: { "Content-Type": "application/json", "User-Agent": "com.google.android.youtube/5.0" }, - body: JSON.stringify({ + data: { userID: submitUserOne, videoID: "userAgent-1", segments: [{ segment: [0, 10], category: "sponsor", }], - }), + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -894,21 +866,19 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit with empty user-agent", (done: Done) => { - fetch(endpoint, { + it("Should be able to submit with empty user-agent", (done) => { + client({ + url: endpoint, method: "POST", - headers: { - "Content-Type": "application/json", - "User-Agent": "" - }, - body: JSON.stringify({ + data: { userID: submitUserOne, videoID: "userAgent-3", segments: [{ segment: [0, 10], category: "sponsor", }], - }), + userAgent: "", + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -924,21 +894,15 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit with custom userAgent in body", (done: Done) => { - fetch(endpoint, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - userID: submitUserOne, - videoID: "userAgent-4", - segments: [{ - segment: [0, 10], - category: "sponsor", - }], - userAgent: "MeaBot/5.0" - }), + it("Should be able to submit with custom userAgent in body", (done) => { + postSkipSegmentJSON({ + userID: submitUserOne, + videoID: "userAgent-4", + segments: [{ + segment: [0, 10], + category: "sponsor", + }], + userAgent: "MeaBot/5.0" }) .then(async res => { assert.strictEqual(res.status, 200); @@ -954,18 +918,15 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be able to submit with commas in timestamps", (done: Done) => { + it("Should be able to submit with commas in timestamps", (done) => { const videoID = "commas-1"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: ["0,2", "10,392"], - category: "sponsor", - }] - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: ["0,2", "10,392"], + category: "sponsor", + }] }) .then(async res => { assert.strictEqual(res.status, 200); @@ -980,10 +941,14 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should be rejected if a POI is at less than 1 second", (done: Done) => { + it("Should be rejected if a POI is at less than 1 second", (done) => { const videoID = "qqwerty"; - fetch(`${endpoint}?videoID=${videoID}&startTime=0.5&endTime=0.5&category=poi_highlight&userID=${submitUserTwo}`, { - method: "POST", + postSkipSegmentParam({ + videoID, + startTime: 0.5, + endTime: 0.5, + category: "poi_highlight", + userID: submitUserTwo }) .then(res => { assert.strictEqual(res.status, 400); @@ -992,18 +957,15 @@ describe("postSkipSegments", () => { .catch(err => done(err)); }); - it("Should not be able to submit with colons in timestamps", (done: Done) => { + it("Should not be able to submit with colons in timestamps", (done) => { const videoID = "colon-1"; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - userID: submitUserOne, - videoID, - segments: [{ - segment: ["0:2.000", "3:10.392"], - category: "sponsor", - }] - }), + postSkipSegmentJSON({ + userID: submitUserOne, + videoID, + segments: [{ + segment: ["0:2.000", "3:10.392"], + category: "sponsor", + }] }) .then(res => { assert.strictEqual(res.status, 400); diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index 81ea56e..d4bb0c2 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -1,30 +1,25 @@ -import fetch from "node-fetch"; -import { Done, postJSON } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { partialDeepEquals } from "../utils/partialDeepEquals"; import { db } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import assert from "assert"; +import { client } from "../utils/httpClient"; describe("postWarning", () => { // constants - const endpoint = `${getbaseURL()}/api/warnUser`; + const endpoint = "/api/warnUser"; const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]); before(async () => { await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]); }); - it("Should be able to create warning if vip (exp 200)", (done: Done) => { + it("Should be able to create warning if vip (exp 200)", (done) => { const json = { issuerUserID: "warning-vip", userID: "warning-0", reason: "warning-reason-0" }; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify(json), - }) + client.post(endpoint, json) .then(async res => { assert.strictEqual(res.status, 200); const row = await getWarning(json.userID); @@ -39,16 +34,13 @@ describe("postWarning", () => { .catch(err => done(err)); }); - it("Should be not be able to create a duplicate warning if vip", (done: Done) => { + it("Should be not be able to create a duplicate warning if vip", (done) => { const json = { issuerUserID: "warning-vip", userID: "warning-0", }; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify(json), - }) + client.post(endpoint, json) .then(async res => { assert.strictEqual(res.status, 409); const row = await getWarning(json.userID); @@ -62,17 +54,14 @@ describe("postWarning", () => { .catch(err => done(err)); }); - it("Should be able to remove warning if vip", (done: Done) => { + it("Should be able to remove warning if vip", (done) => { const json = { issuerUserID: "warning-vip", userID: "warning-0", enabled: false }; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify(json), - }) + client.post(endpoint, json) .then(async res => { assert.strictEqual(res.status, 200); const row = await getWarning(json.userID); @@ -85,16 +74,13 @@ describe("postWarning", () => { .catch(err => done(err)); }); - it("Should not be able to create warning if not vip (exp 403)", (done: Done) => { + it("Should not be able to create warning if not vip (exp 403)", (done) => { const json = { issuerUserID: "warning-not-vip", userID: "warning-1", }; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify(json), - }) + client.post(endpoint, json) .then(res => { assert.strictEqual(res.status, 403); done(); @@ -102,8 +88,8 @@ describe("postWarning", () => { .catch(err => done(err)); }); - it("Should return 400 if missing body", (done: Done) => { - fetch(endpoint, postJSON) + it("Should return 400 if missing body", (done) => { + client.post(endpoint, {}) .then(async res => { assert.strictEqual(res.status, 400); done(); @@ -111,17 +97,14 @@ describe("postWarning", () => { .catch(err => done(err)); }); - it("Should re-enable disabled warning", (done: Done) => { + it("Should re-enable disabled warning", (done) => { const json = { issuerUserID: "warning-vip", userID: "warning-0", enabled: true }; - fetch(endpoint, { - ...postJSON, - body: JSON.stringify(json), - }) + client.post(endpoint, json) .then(async res => { assert.strictEqual(res.status, 200); const data = await getWarning(json.userID); diff --git a/test/cases/segmentShift.ts b/test/cases/segmentShift.ts index 5c1c80c..33fca34 100644 --- a/test/cases/segmentShift.ts +++ b/test/cases/segmentShift.ts @@ -1,10 +1,8 @@ -import fetch from "node-fetch"; -import { Done, postJSON } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { db } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import { IDatabase } from "../../src/databases/IDatabase"; import assert from "assert"; +import { client } from "../utils/httpClient"; describe("segmentShift", function () { // functions @@ -42,7 +40,12 @@ describe("segmentShift", function () { // constants const privateVipUserID = "VIPUser-segmentShift"; const vipUserID = getHash(privateVipUserID); - const endpoint = `${getbaseURL()}/api/segmentShift`; + const endpoint = "/api/segmentShift"; + const postSegmentShift = async (data: Record) => client({ + method: "POST", + url: endpoint, + data, + }); before(async function () { // startTime and endTime get set in beforeEach for consistency @@ -61,15 +64,12 @@ describe("segmentShift", function () { await dbSponsorTimesSetByUUID(db, "vsegshifttest01uuid04", 120, 140); }); - it("Reject none VIP user", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: "segshift_randomuser001", - startTime: 20, - endTime: 30, - }), + it("Reject non VIP user", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: "segshift_randomuser001", + startTime: 20, + endTime: 30, }) .then(async res => { assert.strictEqual(res.status, 403); @@ -78,15 +78,12 @@ describe("segmentShift", function () { .catch(err => done(err)); }); - it("Shift is outside segments", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: privateVipUserID, - startTime: 20, - endTime: 30, - }), + it("Shift is outside segments", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: privateVipUserID, + startTime: 20, + endTime: 30, }) .then(async res => { assert.strictEqual(res.status, 200); @@ -112,15 +109,12 @@ describe("segmentShift", function () { .catch(err => done(err)); }); - it("Shift is inside segment", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: privateVipUserID, - startTime: 65, - endTime: 75, - }), + it("Shift is inside segment", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: privateVipUserID, + startTime: 65, + endTime: 75, }) .then(async res => { assert.strictEqual(res.status, 200); @@ -146,15 +140,12 @@ describe("segmentShift", function () { .catch(err => done(err)); }); - it("Shift is overlaping startTime of segment", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: privateVipUserID, - startTime: 32, - endTime: 42, - }), + it("Shift is overlaping startTime of segment", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: privateVipUserID, + startTime: 32, + endTime: 42, }) .then(async res => { assert.strictEqual(res.status, 200); @@ -180,15 +171,12 @@ describe("segmentShift", function () { .catch(err => done(err)); }); - it("Shift is overlaping endTime of segment", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: privateVipUserID, - startTime: 85, - endTime: 95, - }), + it("Shift is overlaping endTime of segment", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: privateVipUserID, + startTime: 85, + endTime: 95, }) .then(async res => { assert.strictEqual(res.status, 200); @@ -214,15 +202,12 @@ describe("segmentShift", function () { .catch(err => done(err)); }); - it("Shift is overlaping segment", function (done: Done) { - fetch(endpoint, { - ...postJSON, - body: JSON.stringify({ - videoID: "vsegshift01", - userID: privateVipUserID, - startTime: 35, - endTime: 55, - }), + it("Shift is overlaping segment", (done) => { + postSegmentShift({ + videoID: "vsegshift01", + userID: privateVipUserID, + startTime: 35, + endTime: 55, }) .then(async res => { assert.strictEqual(res.status, 200); diff --git a/test/cases/setUsername.ts b/test/cases/setUsername.ts index 75f87dd..482311b 100644 --- a/test/cases/setUsername.ts +++ b/test/cases/setUsername.ts @@ -1,9 +1,7 @@ -import fetch from "node-fetch"; -import { Done } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { db, privateDB } from "../../src/databases/databases"; import { getHash } from "../../src/utils/getHash"; import assert from "assert"; +import { client } from "../utils/httpClient"; const adminPrivateUserID = "testUserId"; const user00PrivateUserID = "setUsername_00"; @@ -52,7 +50,7 @@ function wellFormatUserName(userName: string) { return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, ""); } -async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Done) { +async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Mocha.Done) { const log = await getLastLogUserNameChange(userID); assert.strictEqual(newUserName, log.newUserName); assert.strictEqual(oldUserName, log.oldUserName); @@ -60,8 +58,27 @@ async function testUserNameChangelog(userID: string, newUserName: string, oldUse return done(); } +const endpoint = "/api/setUsername"; +const postSetUserName = (userID: string, username: string) => client({ + method: "POST", + url: endpoint, + params: { + userID, + username, + } +}); + +const postSetUserNameAdmin = (userID: string, username: string, adminUserID: string) => client({ + method: "POST", + url: endpoint, + params: { + userID, + username, + adminUserID, + } +}); + describe("setUsername", () => { - const endpoint = `${getbaseURL()}/api/setUsername`; before(async () => { await addUsername(getHash(user01PrivateUserID), username01, 0); await addUsername(getHash(user02PrivateUserID), username02, 0); @@ -72,157 +89,147 @@ describe("setUsername", () => { await addUsername(getHash(user07PrivateUserID), username07, 1); }); - it("Should be able to set username that has never been set", (done: Done) => { - fetch(`${endpoint}?userID=${user00PrivateUserID}&username=${username00}`, { - method: "POST", - }) + it("Should be able to set username that has never been set", (done) => { + postSetUserName(user00PrivateUserID, username00) .then(async res => { + console.log(res.data); const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID)); assert.strictEqual(res.status, 200); assert.strictEqual(usernameInfo.userName, username00); assert.notStrictEqual(usernameInfo.locked, 1, "username should not be locked"); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Should return 200", (done: Done) => { - fetch(`${endpoint}?userID=${user01PrivateUserID}&username=Changed%20Username`, { - method: "POST", - }) + it("Should return 200", (done) => { + const username = "Changed%20Username"; + postSetUserName(user01PrivateUserID, username) .then(async res => { assert.strictEqual(res.status, 200); - testUserNameChangelog(user01PrivateUserID, decodeURIComponent("Changed%20Username"), username01, false, done); + testUserNameChangelog(user01PrivateUserID, username, username01, false, done); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it('Should return 400 for missing param "userID"', (done: Done) => { - fetch(`${endpoint}?username=MyUsername`, { + it('Should return 400 for missing param "userID"', (done) => { + client({ method: "POST", + url: endpoint, + data: { + userName: "MyUsername" + } }) .then(res => { assert.strictEqual(res.status, 400); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it('Should return 400 for missing param "username"', (done: Done) => { - fetch(`${endpoint}?userID=test`, { + it('Should return 400 for missing param "username"', (done) => { + client({ method: "POST", + url: endpoint, + data: { + userID: "test" + } }) .then(res => { assert.strictEqual(res.status, 400); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it('Should return 400 for "username" longer then 64 characters', (done: Done) => { + it('Should return 400 for "username" longer then 64 characters', (done) => { const username65 = "0000000000000000000000000000000000000000000000000000000000000000X"; - fetch(`${endpoint}?userID=test&username=${encodeURIComponent(username65)}`, { - method: "POST", - }) + postSetUserName("test", username65) .then(res => { assert.strictEqual(res.status, 400); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it('Should not change username if it contains "discord"', (done: Done) => { + it('Should not change username if it contains "discord"', (done) => { const newUsername = "discord.me"; - fetch(`${endpoint}?userID=${user02PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserName(user02PrivateUserID, newUsername) .then(async res => { assert.strictEqual(res.status, 200); const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID)); assert.notStrictEqual(userNameInfo.userName, newUsername); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Should be able to change username", (done: Done) => { + it("Should be able to change username", (done) => { const newUsername = "newUsername"; - fetch(`${endpoint}?userID=${user03PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserName(user03PrivateUserID, newUsername) .then(async () => { const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID)); assert.strictEqual(usernameInfo.userName, newUsername, "Username should change"); assert.notStrictEqual(usernameInfo.locked, 1, "Username should not be locked"); testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Should not be able to change locked username", (done: Done) => { + it("Should not be able to change locked username", (done) => { const newUsername = "newUsername"; - fetch(`${endpoint}?userID=${user04PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserName(user04PrivateUserID, newUsername) .then(async () => { const usernameInfo = await getUsernameInfo(getHash(user04PrivateUserID)); assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not be changed"); assert.strictEqual(usernameInfo.locked, 1, "username should be locked"); done(); }) - .catch((err) => done(`couldn't call endpoint: ${err}`)); + .catch((err) => done(err)); }); - it("Should filter out unicode control characters", (done: Done) => { + it("Should filter out unicode control characters", (done) => { const newUsername = "This\nUsername+has\tInvalid+Characters"; - fetch(`${endpoint}?userID=${user05PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserName(user05PrivateUserID, newUsername) .then(async () => { const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID)); assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not contain control characters"); testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Incorrect adminUserID should return 403", (done: Done) => { + it("Incorrect adminUserID should return 403", (done) => { const newUsername = "New Username"; - fetch(`${endpoint}?adminUserID=invalidAdminID&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) - .then(async res => { + postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername,"invalidAdminID") + .then(res => { assert.strictEqual(res.status, 403); done(); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Admin should be able to change username", (done: Done) => { + it("Admin should be able to change username", (done) => { const newUsername = "New Username"; - fetch(`${endpoint}?adminUserID=${adminPrivateUserID}&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername, adminPrivateUserID) .then(async () => { const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID)); assert.strictEqual(usernameInfo.userName, newUsername, "username should be changed"); assert.strictEqual(usernameInfo.locked, 1, "Username should be locked"); testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); - it("Admin should be able to change locked username", (done: Done) => { + it("Admin should be able to change locked username", (done) => { const newUsername = "New Username"; - fetch(`${endpoint}?adminUserID=${adminPrivateUserID}&userID=${getHash(user07PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { - method: "POST", - }) + postSetUserNameAdmin(getHash(user07PrivateUserID), newUsername, adminPrivateUserID) .then(async () => { const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID)); assert.strictEqual(usernameInfo.userName, newUsername, "Username should be changed"); assert.strictEqual(usernameInfo.locked, 1, "Username should be locked"); testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done); }) - .catch(() => done(`couldn't call endpoint`)); + .catch((err) => done(err)); }); }); diff --git a/test/cases/shadowBanUser.ts b/test/cases/shadowBanUser.ts index d78ed78..834382c 100644 --- a/test/cases/shadowBanUser.ts +++ b/test/cases/shadowBanUser.ts @@ -1,17 +1,15 @@ -import fetch from "node-fetch"; import { db } from "../../src/databases/databases"; -import { Done } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { getHash } from "../../src/utils/getHash"; import assert from "assert"; import { Category } from "../../src/types/segments.model"; +import { client } from "../utils/httpClient"; describe("shadowBanUser", () => { - const endpoint = `${getbaseURL()}/api/shadowBanUser`; const getShadowBan = (userID: string) => db.prepare("get", `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]); const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]); + const endpoint = "/api/shadowBanUser"; const VIPuserID = "shadow-ban-vip"; before(async () => { @@ -36,10 +34,15 @@ describe("shadowBanUser", () => { await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]); }); - it("Should be able to ban user and hide submissions", (done: Done) => { + it("Should be able to ban user and hide submissions", (done) => { const userID = "shadowBanned"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -52,10 +55,17 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should be able to unban user without unhiding submissions", (done: Done) => { + it("Should be able to unban user without unhiding submissions", (done) => { const userID = "shadowBanned"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false&unHideOldSubmissions=false`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + enabled: false, + unHideOldSubmissions: false, + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -68,10 +78,16 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should be able to ban user and hide submissions from only some categories", (done: Done) => { + it("Should be able to ban user and hide submissions from only some categories", (done) => { const userID = "shadowBanned2"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&categories=["sponsor"]`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + categories: `["sponsor"]` + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -85,10 +101,16 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should be able to unban user and unhide submissions", (done: Done) => { + it("Should be able to unban user and unhide submissions", (done) => { const userID = "shadowBanned2"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + enabled: false, + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -101,10 +123,17 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should be able to unban user and unhide some submissions", (done: Done) => { + it("Should be able to unban user and unhide some submissions", (done) => { const userID = "shadowBanned3"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false&categories=["sponsor"]`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + enabled: false, + categories: `["sponsor"]` + } }) .then(async res => { assert.strictEqual(res.status, 200); @@ -118,10 +147,18 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should get 409 when re-shadowbanning user", (done: Done) => { + it("Should get 409 when re-shadowbanning user", (done) => { const userID = "shadowBanned4"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=true&categories=["sponsor"]&unHideOldSubmissions=false`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + enabled: true, + categories: `["sponsor"]`, + unHideOldSubmissions: false + } }) .then(async res => { assert.strictEqual(res.status, 409); @@ -135,10 +172,18 @@ describe("shadowBanUser", () => { .catch(err => done(err)); }); - it("Should be able to re-shadowban user to hide old submissions", (done: Done) => { + it("Should be able to re-shadowban user to hide old submissions", (done) => { const userID = "shadowBanned4"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=true&categories=["sponsor"]&unHideOldSubmissions=true`, { - method: "POST" + client({ + method: "POST", + url: endpoint, + params: { + userID, + adminUserID: VIPuserID, + enabled: true, + categories: `["sponsor"]`, + unHideOldSubmissions: true + } }) .then(async res => { assert.strictEqual(res.status, 200); diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index cb34d0b..2f3fec8 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -1,13 +1,20 @@ -import fetch from "node-fetch"; -import { postJSON } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { getHash } from "../../src/utils/getHash"; import { db } from "../../src/databases/databases"; +import { client } from "../utils/httpClient"; import assert from "assert"; describe("unBan", () => { - const endpoint = `${getbaseURL()}/api/shadowBanUser`; + const endpoint = "/api/shadowBanUser"; const VIPuser = "VIPUser-unBan"; + const postUnBan = (userID: string, adminUserID: string, enabled: boolean) => client({ + url: endpoint, + method: "POST", + params: { + userID, + adminUserID, + enabled + } + }); const videoIDUnBanCheck = (videoID: string, userID: string, status: number) => db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', [videoID, userID, status]); before(async () => { const insertShadowBannedUserQuery = 'INSERT INTO "shadowBannedUsers" VALUES(?)'; @@ -30,9 +37,7 @@ describe("unBan", () => { it("Should be able to unban a user and re-enable shadow banned segments", (done) => { const userID = "testMan-unBan"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { - ...postJSON - }) + postUnBan(userID, VIPuser, false) .then(async res => { assert.strictEqual(res.status, 200); const result = await videoIDUnBanCheck("unBan-videoID-0", userID, 1); @@ -44,9 +49,7 @@ describe("unBan", () => { it("Should be able to unban a user and re-enable shadow banned segments without lockCategories entrys", (done) => { const userID = "testWoman-unBan"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { - ...postJSON - }) + postUnBan(userID, VIPuser, false) .then(async res => { assert.strictEqual(res.status, 200); const result = await videoIDUnBanCheck("unBan-videoID-1", userID, 1); @@ -58,9 +61,7 @@ describe("unBan", () => { it("Should be able to unban a user and re-enable shadow banned segments with a mix of lockCategories entrys", (done) => { const userID = "testEntity-unBan"; - fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { - ...postJSON - }) + postUnBan(userID, VIPuser, false) .then(async res => { assert.strictEqual(res.status, 200); const result = await db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?', [userID, 1]); diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 28a740b..d987b89 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -1,17 +1,17 @@ -import fetch from "node-fetch"; import { config } from "../../src/config"; import { db } from "../../src/databases/databases"; -import { Done } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { getHash } from "../../src/utils/getHash"; import { ImportMock } from "ts-mock-imports"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import { YouTubeApiMock } from "../youtubeMock"; import assert from "assert"; +import { client } from "../utils/httpClient"; const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI"); const sinonStub = mockManager.mock("listVideos"); sinonStub.callsFake(YouTubeApiMock.listVideos); +const vipUser = "VIPUser"; +const randomID2 = "randomID2"; describe("voteOnSponsorTime", () => { before(async () => { @@ -35,7 +35,7 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]); - await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-9", getHash("randomID2"), 0, 50, "sponsor", 0, 0]); + await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-9", getHash(randomID2), 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", 0, 0]); await db.prepare("run", insertSponsorTimeQuery, ["own-submission-video", 1, 11, 500, "own-submission-uuid", getHash("own-submission-id"), 0, 50, "sponsor", 0, 0]); @@ -59,19 +59,38 @@ describe("voteOnSponsorTime", () => { await db.prepare("run", insertWarningQuery, [warnUser02Hash, (now - (warningExpireTime + 2000)), warnVip01Hash, 1]); - await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash("VIPUser")]); + await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]); await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]); await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ["no-sponsor-segments-video", "someUser", "sponsor"]); }); // constants - const endpoint = `${getbaseURL()}/api/voteOnSponsorTime`; + const endpoint = "/api/voteOnSponsorTime"; + const postVote = (userID: string, UUID: string, type: number) => client({ + method: "POST", + url: endpoint, + params: { + userID, + UUID, + type + } + }); + const postVoteCategory = (userID: string, UUID: string, category: string) => client({ + method: "POST", + url: endpoint, + params: { + userID, + UUID, + category + } + }); + const getSegmentVotes = (UUID: string) => db.prepare("get", `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const getSegmentCategory = (UUID: string) => db.prepare("get", `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); - it("Should be able to upvote a segment", (done: Done) => { + it("Should be able to upvote a segment", (done) => { const UUID = "vote-uuid-0"; - fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`) + postVote("randomID", UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -81,9 +100,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should be able to downvote a segment", (done: Done) => { + it("Should be able to downvote a segment", (done) => { const UUID = "vote-uuid-2"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`) + postVote(randomID2, UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -93,9 +112,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to downvote the same segment when voting from a different user on the same IP", (done: Done) => { + it("Should not be able to downvote the same segment when voting from a different user on the same IP", (done) => { const UUID = "vote-uuid-2"; - fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&type=0`) + postVote("randomID3", UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -105,75 +124,81 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to downvote a segment if the user is shadow banned", (done: Done) => { - fetch(`${endpoint}?userID=randomID4&UUID=vote-uuid-1.6&type=0`) + it("Should not be able to downvote a segment if the user is shadow banned", (done) => { + const UUID = "vote-uuid-1.6"; + postVote("randomID4", UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("vote-uuid-1.6"); + const row = await getSegmentVotes(UUID); assert.strictEqual(row.votes, 10); done(); }) .catch(err => done(err)); }); - it("Should not be able to upvote a segment if the user hasn't submitted yet", (done: Done) => { - fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1`) + it("Should not be able to upvote a segment if the user hasn't submitted yet", (done) => { + const UUID = "vote-uuid-1"; + postVote("hasNotSubmittedID", UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("vote-uuid-1"); + const row = await getSegmentVotes(UUID); assert.strictEqual(row.votes, 2); done(); }) .catch(err => done(err)); }); - it("Should not be able to downvote a segment if the user hasn't submitted yet", (done: Done) => { - fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0`) + it("Should not be able to downvote a segment if the user hasn't submitted yet", (done) => { + const UUID = "vote-uuid-1.5"; + postVote("hasNotSubmittedID", UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("vote-uuid-1.5"); + const row = await getSegmentVotes(UUID); assert.strictEqual(row.votes, 10); done(); }) .catch(err => done(err)); }); - it("VIP should be able to completely downvote a segment", (done: Done) => { - fetch(`${endpoint}?userID=VIPUser&UUID=vote-uuid-3&type=0`) + it("VIP should be able to completely downvote a segment", (done) => { + const UUID = "vote-uuid-3"; + postVote(vipUser, UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("vote-uuid-3"); + const row = await getSegmentVotes(UUID); assert.ok(row.votes <= -2); done(); }) .catch(err => done(err)); }); - it("should be able to completely downvote your own segment", (done: Done) => { - fetch(`${endpoint}?userID=own-submission-id&UUID=own-submission-uuid&type=0`) + it("should be able to completely downvote your own segment", (done) => { + const UUID = "own-submission-uuid"; + postVote("own-submission-id", UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("own-submission-uuid"); + const row = await getSegmentVotes(UUID); assert.ok(row.votes <= -2); done(); }) .catch(err => done(err)); }); - it("should not be able to completely downvote somebody elses segment", (done: Done) => { - fetch(`${endpoint}?userID=randomID2&UUID=not-own-submission-uuid&type=0`) + it("should not be able to completely downvote somebody elses segment", (done) => { + const UUID = "not-own-submission-uuid"; + postVote(randomID2, UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); - const row = await getSegmentVotes("not-own-submission-uuid"); + const row = await getSegmentVotes(UUID); assert.strictEqual(row.votes, 499); done(); }) .catch(err => done(err)); }); - it("Should be able to vote for a category and it should add your vote to the database", (done: Done) => { + it("Should be able to vote for a category and it should add your vote to the database", (done) => { const UUID = "vote-uuid-4"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=intro`) + postVoteCategory(randomID2, UUID, "intro") .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); @@ -189,9 +214,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not able to change to an invalid category", (done: Done) => { + it("Should not able to change to an invalid category", (done) => { const UUID = "incorrect-category"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=fakecategory`) + postVoteCategory(randomID2, UUID, "fakecategory") .then(async res => { assert.strictEqual(res.status, 400); const row = await getSegmentCategory(UUID); @@ -201,9 +226,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not able to change to highlight category", (done: Done) => { + it("Should not able to change to highlight category", (done) => { const UUID = "incorrect-category"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=highlight`) + postVoteCategory(randomID2, UUID, "highlight") .then(async res => { assert.strictEqual(res.status, 400); const row = await getSegmentCategory(UUID); @@ -213,39 +238,36 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should be able to change your vote for a category and it should add your vote to the database", (done: Done) => { + it("Should be able to change your vote for a category and it should add your vote to the database", (done) => { const UUID = "vote-uuid-4"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=outro`) + postVoteCategory(randomID2, UUID, "outro") .then(async res => { - if (res.status === 200) { - const submissionRow = await getSegmentCategory(UUID); - const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]); - let introVotes = 0; - let outroVotes = 0; - let sponsorVotes = 0; - for (const row of categoryRows) { - if (row?.category === "intro") introVotes += row?.votes; - if (row?.category === "outro") outroVotes += row?.votes; - if (row?.category === "sponsor") sponsorVotes += row?.votes; - } - assert.strictEqual(submissionRow.category, "sponsor"); - assert.strictEqual(categoryRows.length, 3); - assert.strictEqual(introVotes, 0); - assert.strictEqual(outroVotes, 1); - assert.strictEqual(sponsorVotes, 1); - done(); - } else { - done(`Status code was ${res.status}`); + assert.strictEqual(res.status, 200, "Status code should be 200"); + const submissionRow = await getSegmentCategory(UUID); + const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]); + let introVotes = 0; + let outroVotes = 0; + let sponsorVotes = 0; + for (const row of categoryRows) { + if (row?.category === "intro") introVotes += row?.votes; + if (row?.category === "outro") outroVotes += row?.votes; + if (row?.category === "sponsor") sponsorVotes += row?.votes; } + assert.strictEqual(submissionRow.category, "sponsor"); + assert.strictEqual(categoryRows.length, 3); + assert.strictEqual(introVotes, 0); + assert.strictEqual(outroVotes, 1); + assert.strictEqual(sponsorVotes, 1); + done(); }) .catch(err => done(err)); }); - it("Should not be able to change your vote to an invalid category", (done: Done) => { + it("Should not be able to change your vote to an invalid category", (done) => { const UUID = "incorrect-category-change"; - const vote = (inputCat: string, assertCat: string, callback: Done) => { - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=${inputCat}`) + const vote = (inputCat: string, assertCat: string, callback: Mocha.Done) => { + postVoteCategory(randomID2, UUID, inputCat) .then(async () => { const row = await getSegmentCategory(UUID); assert.strictEqual(row.category, assertCat); @@ -259,9 +281,9 @@ describe("voteOnSponsorTime", () => { }); - it("VIP should be able to vote for a category and it should immediately change", (done: Done) => { + it("VIP should be able to vote for a category and it should immediately change", (done) => { const UUID = "vote-uuid-5"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&category=outro`) + postVoteCategory(vipUser, UUID, "outro") .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); @@ -273,9 +295,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Submitter should be able to vote for a category and it should immediately change", (done: Done) => { + it("Submitter should be able to vote for a category and it should immediately change", (done) => { const UUID = "vote-uuid-5_1"; - fetch(`${endpoint}?userID=testman&UUID=${UUID}&category=outro`) + postVoteCategory("testman", UUID, "outro") .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory("vote-uuid-5"); @@ -285,9 +307,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to category-vote on an invalid UUID submission", (done: Done) => { + it("Should not be able to category-vote on an invalid UUID submission", (done) => { const UUID = "invalid-uuid"; - fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&category=intro`) + postVoteCategory("randomID3", UUID, "intro") .then(async res => { assert.strictEqual(res.status, 400); done(); @@ -295,9 +317,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it('Non-VIP should not be able to upvote "dead" submission', (done: Done) => { + it('Non-VIP should not be able to upvote "dead" submission', (done) => { const UUID = "vote-uuid-5"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=1`) + postVote(randomID2, UUID, 1) .then(async res => { assert.strictEqual(res.status, 403); const row = await getSegmentVotes(UUID); @@ -307,9 +329,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it('Non-VIP should not be able to downvote "dead" submission', (done: Done) => { + it('Non-VIP should not be able to downvote "dead" submission', (done) => { const UUID = "vote-uuid-5"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`) + postVote(randomID2, UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -319,9 +341,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it('VIP should be able to upvote "dead" submission', (done: Done) => { + it('VIP should be able to upvote "dead" submission', (done) => { const UUID = "vote-uuid-5"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) + postVote(vipUser, UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -331,9 +353,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to upvote a segment (Too many warning)", (done: Done) => { + it("Should not be able to upvote a segment (Too many warning)", (done) => { const UUID = "warnvote-uuid-0"; - fetch(`${endpoint}?userID=warn-voteuser01&UUID=${UUID}&type=1`) + postVote("warn-voteuser01", UUID, 1) .then(async res => { assert.strictEqual(res.status, 403); done(); @@ -341,9 +363,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Non-VIP should not be able to downvote on a segment with no-segments category", (done: Done) => { + it("Non-VIP should not be able to downvote on a segment with no-segments category", (done) => { const UUID = "no-sponsor-segments-uuid-0"; - fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=0`) + postVote("randomID", UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -353,9 +375,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Non-VIP should be able to upvote on a segment with no-segments category", (done: Done) => { + it("Non-VIP should be able to upvote on a segment with no-segments category", (done) => { const UUID = "no-sponsor-segments-uuid-0"; - fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`) + postVote("randomID", UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -365,9 +387,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Non-VIP should not be able to category vote on a segment with no-segments category", (done: Done) => { + it("Non-VIP should not be able to category vote on a segment with no-segments category", (done) => { const UUID = "no-sponsor-segments-uuid-0"; - fetch(`${endpoint}?userID=randomID&UUID=${UUID}&category=outro`) + postVoteCategory("randomID", UUID, "outro") .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentCategory(UUID); @@ -377,9 +399,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("VIP upvote should lock segment", (done: Done) => { + it("VIP upvote should lock segment", (done) => { const UUID = "segment-locking-uuid-1"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) + postVote(vipUser, UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); @@ -389,9 +411,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("VIP downvote should unlock segment", (done: Done) => { + it("VIP downvote should unlock segment", (done) => { const UUID = "segment-locking-uuid-1"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=0`) + postVote(vipUser, UUID, 0) .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); @@ -401,9 +423,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("VIP upvote should unhide segment", (done: Done) => { + it("VIP upvote should unhide segment", (done) => { const UUID = "segment-hidden-uuid-1"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) + postVote(vipUser, UUID, 1) .then(async res => { assert.strictEqual(res.status, 200); const row = await db.prepare("get", `SELECT "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); @@ -413,9 +435,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should be able to undo-vote a segment", (done: Done) => { + it("Should be able to undo-vote a segment", (done) => { const UUID = "vote-uuid-2"; - fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=20`) + postVote(randomID2, UUID, 20) .then(async res => { assert.strictEqual(res.status, 200); const row = await getSegmentVotes(UUID); @@ -425,9 +447,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to vote with type 10", (done: Done) => { + it("Should not be able to vote with type 10", (done) => { const UUID = "segment-locking-uuid-1"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=10`) + postVote(vipUser, UUID, 10) .then(res => { assert.strictEqual(res.status, 400); done(); @@ -435,9 +457,9 @@ describe("voteOnSponsorTime", () => { .catch(err => done(err)); }); - it("Should not be able to vote with type 11", (done: Done) => { + it("Should not be able to vote with type 11", (done) => { const UUID = "segment-locking-uuid-1"; - fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=11`) + postVote(vipUser, UUID, 11) .then(res => { assert.strictEqual(res.status, 400); done();