From 607b7cbb0a7bf64899743049e255f69e4b635212 Mon Sep 17 00:00:00 2001 From: Michael C Date: Tue, 15 Jun 2021 15:50:41 -0400 Subject: [PATCH 1/6] add ignored counts --- src/routes/getUserInfo.ts | 21 ++++++++++++++++++++- test/cases/getUserInfo.ts | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 8ea8dda..4ade24e 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -27,6 +27,15 @@ async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ min } } +async function dbGetIgnoredSegmentCount(userID: HashedUserID): Promise<{ ignoredSegmentCount: number }> { + try { + let row = await db.prepare("get", `SELECT COUNT(*) as "ignoredSegmentCount" FROM "sponsorTimes" WHERE "userID" = ? AND ( "votes" <= -2 OR "shadowHidden" = 1 )`, [userID]); + return row?.ignoredSegmentCount ?? 0 + } catch (err) { + return null; + } +} + async function dbGetUsername(userID: HashedUserID) { try { let row = await db.prepare('get', `SELECT "userName" FROM "userNames" WHERE "userID" = ?`, [userID]); @@ -50,6 +59,15 @@ async function dbGetViewsForUser(userID: HashedUserID) { } } +async function dbGetIgnoredViewsForUser(userID: HashedUserID) { + try { + let row = await db.prepare('get', `SELECT SUM("views") as "ignoredViewCount" FROM "sponsorTimes" WHERE "userID" = ? AND ( "votes" <= -2 OR "shadowHidden" = 1 )`, [userID]); + return row?.ignoredViewCount ?? 0; + } catch (err) { + return false; + } +} + async function dbGetWarningsForUser(userID: HashedUserID): Promise { try { let row = await db.prepare('get', `SELECT COUNT(*) as total FROM "warnings" WHERE "userID" = ? AND "enabled" = 1`, [userID]); @@ -70,7 +88,6 @@ export async function getUserInfo(req: Request, res: Response) { return; } - const segmentsSummary = await dbGetSubmittedSegmentSummary(hashedUserID); if (segmentsSummary) { res.send({ @@ -78,7 +95,9 @@ export async function getUserInfo(req: Request, res: Response) { userName: await dbGetUsername(hashedUserID), minutesSaved: segmentsSummary.minutesSaved, segmentCount: segmentsSummary.segmentCount, + ignoredSegmentCount: await dbGetIgnoredSegmentCount(hashedUserID), viewCount: await dbGetViewsForUser(hashedUserID), + ignoredViewCount: await dbGetIgnoredViewsForUser(hashedUserID), warnings: await dbGetWarningsForUser(hashedUserID), reputation: await getReputation(hashedUserID), vip: await isUserVIP(hashedUserID), diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index c809a49..f810d7a 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -54,8 +54,12 @@ describe('getUserInfo', () => { done('Returned incorrect minutesSaved "' + data.minutesSaved + '"'); } else if (data.viewCount !== 30) { done('Returned incorrect viewCount "' + data.viewCount + '"'); + } else if (data.ignoredViewCount !== 20) { + done('Returned incorrect ignoredViewCount "' + data.ignoredViewCount + '"'); } else if (data.segmentCount !== 3) { done('Returned incorrect segmentCount "' + data.segmentCount + '"'); + } else if (data.ignoredSegmentCount !== 2) { + done('Returned incorrect ignoredSegmentCount "' + data.ignoredSegmentCount + '"'); } else if (Math.abs(data.reputation - -0.928) > 0.001) { done('Returned incorrect reputation "' + data.reputation + '"'); } else { From 3b16cdb920d639c26206ee5d464a7269a73b19df Mon Sep 17 00:00:00 2001 From: Michael C Date: Tue, 15 Jun 2021 17:08:17 -0400 Subject: [PATCH 2/6] add last lastSegmentID --- src/routes/getUserInfo.ts | 13 ++++++++++++- test/cases/getUserInfo.ts | 34 +++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 4ade24e..8dc8ac9 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -5,6 +5,7 @@ import {Request, Response} from 'express'; import {Logger} from '../utils/logger'; import { HashedUserID, UserID } from '../types/user.model'; import { getReputation } from '../utils/reputation'; +import { SegmentUUID } from "../types/segments.model"; async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ minutesSaved: number, segmentCount: number }> { try { @@ -27,7 +28,7 @@ async function dbGetSubmittedSegmentSummary(userID: HashedUserID): Promise<{ min } } -async function dbGetIgnoredSegmentCount(userID: HashedUserID): Promise<{ ignoredSegmentCount: number }> { +async function dbGetIgnoredSegmentCount(userID: HashedUserID): Promise { try { let row = await db.prepare("get", `SELECT COUNT(*) as "ignoredSegmentCount" FROM "sponsorTimes" WHERE "userID" = ? AND ( "votes" <= -2 OR "shadowHidden" = 1 )`, [userID]); return row?.ignoredSegmentCount ?? 0 @@ -78,6 +79,15 @@ async function dbGetWarningsForUser(userID: HashedUserID): Promise { } } +async function dbGetLastSegmentForUser(userID: HashedUserID): Promise { + try { + let row = await db.prepare('get', `SELECT "timeSubmitted", "UUID" FROM "sponsorTimes" WHERE "userID" = ? ORDER BY "timeSubmitted" DESC LIMIT 1`, [userID]); + return row?.UUID ?? null; + } catch (err) { + return null; + } +} + export async function getUserInfo(req: Request, res: Response) { const userID = req.query.userID as UserID; const hashedUserID: HashedUserID = userID ? getHash(userID) : req.query.publicUserID as HashedUserID; @@ -101,6 +111,7 @@ export async function getUserInfo(req: Request, res: Response) { warnings: await dbGetWarningsForUser(hashedUserID), reputation: await getReputation(hashedUserID), vip: await isUserVIP(hashedUserID), + lastSegmentID: await dbGetLastSegmentForUser(hashedUserID), }); } else { res.status(400).send(); diff --git a/test/cases/getUserInfo.ts b/test/cases/getUserInfo.ts index f810d7a..3e63474 100644 --- a/test/cases/getUserInfo.ts +++ b/test/cases/getUserInfo.ts @@ -8,14 +8,14 @@ describe('getUserInfo', () => { let startOfUserNamesQuery = `INSERT INTO "userNames" ("userID", "userName") VALUES`; await db.prepare("run", startOfUserNamesQuery + "('" + getHash("getuserinfo_user_01") + "', 'Username user 01')"); let startOfSponsorTimesQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden") VALUES'; - await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)"); - await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -2, 'uuid000004', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); - await db.prepare("run", startOfSponsorTimesQuery + "('xzzzxxyyy', 1, 11, -5, 'uuid000005', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)"); - await db.prepare("run", startOfSponsorTimesQuery + "('zzzxxxyyy', 1, 11, 2, 'uuid000006', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 0)"); - await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000007', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); - await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 1, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 2, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 3, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -2, 'uuid000004', '" + getHash("getuserinfo_user_01") + "', 4, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xzzzxxyyy', 1, 11, -5, 'uuid000005', '" + getHash("getuserinfo_user_01") + "', 5, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('zzzxxxyyy', 1, 11, 2, 'uuid000006', '" + getHash("getuserinfo_user_02") + "', 6, 10, 'sponsor', 0)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000007', '" + getHash("getuserinfo_user_02") + "', 7, 10, 'sponsor', 1)"); + await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 8, 10, 'sponsor', 1)"); await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)"); @@ -62,6 +62,8 @@ describe('getUserInfo', () => { done('Returned incorrect ignoredSegmentCount "' + data.ignoredSegmentCount + '"'); } else if (Math.abs(data.reputation - -0.928) > 0.001) { done('Returned incorrect reputation "' + data.reputation + '"'); + } else if (data.lastSegmentID !== "uuid000005") { + done('Returned incorrect last segment "' + data.lastSegmentID + '"'); } else { done(); // pass } @@ -113,7 +115,7 @@ describe('getUserInfo', () => { .catch(err => ("couldn't call endpoint")); }); - it('Should not get warnings if noe', (done: Done) => { + it('Should not get warnings if none', (done: Done) => { fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_warning_2') .then(async res => { if (res.status !== 200) { @@ -142,4 +144,18 @@ describe('getUserInfo', () => { }) .catch(err => ('couldn\'t call endpoint')); }); + + it('Should return null segment if none', (done: Done) => { + fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_null') + .then(async res => { + if (res.status !== 200) { + done('non 200 (' + res.status + ')'); + } else { + const data = await res.json(); + if (data.lastSegmentID !== null) done('returned segment ' + data.warnings + ', not ' + null); + else done(); // pass + } + }) + .catch(err => ("couldn't call endpoint")); + }); }); From 13b105504baa79440423796a98ee20d135c9c37b Mon Sep 17 00:00:00 2001 From: Michael C Date: Tue, 15 Jun 2021 17:16:32 -0400 Subject: [PATCH 3/6] remove timeSubmitted from query --- src/routes/getUserInfo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 8dc8ac9..44db5d5 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -81,7 +81,7 @@ async function dbGetWarningsForUser(userID: HashedUserID): Promise { async function dbGetLastSegmentForUser(userID: HashedUserID): Promise { try { - let row = await db.prepare('get', `SELECT "timeSubmitted", "UUID" FROM "sponsorTimes" WHERE "userID" = ? ORDER BY "timeSubmitted" DESC LIMIT 1`, [userID]); + let row = await db.prepare('get', `SELECT "UUID" FROM "sponsorTimes" WHERE "userID" = ? ORDER BY "timeSubmitted" DESC LIMIT 1`, [userID]); return row?.UUID ?? null; } catch (err) { return null; From 8dcc1a4a5352636d79065e3c0759c2e05c8451fc Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 16 Jun 2021 00:33:51 -0400 Subject: [PATCH 4/6] add getSegmentInfo with tests --- src/app.ts | 5 +- src/routes/getSegmentInfo.ts | 75 +++++++++ test/cases/getSegmentInfo.ts | 302 +++++++++++++++++++++++++++++++++++ 3 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 src/routes/getSegmentInfo.ts create mode 100644 test/cases/getSegmentInfo.ts diff --git a/src/app.ts b/src/app.ts index a05a7cc..44a59f6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -28,7 +28,7 @@ import {corsMiddleware} from './middleware/cors'; import {apiCspMiddleware} from './middleware/apiCsp'; import {rateLimitMiddleware} from './middleware/requestRateLimit'; import dumpDatabase, {redirectLink} from './routes/dumpDatabase'; - +import {endpoint as getSegmentInfo} from './routes/getSegmentInfo'; export function createServer(callback: () => void) { // Create a service (the app object is just a callback). @@ -133,6 +133,9 @@ function setupRoutes(app: Express) { //get if user is a vip app.post('/api/segmentShift', postSegmentShift); + //get segment info + app.get('/api/segmentInfo', getSegmentInfo); + if (config.postgres) { app.get('/database', (req, res) => dumpDatabase(req, res, true)); app.get('/database.json', (req, res) => dumpDatabase(req, res, false)); diff --git a/src/routes/getSegmentInfo.ts b/src/routes/getSegmentInfo.ts new file mode 100644 index 0000000..a593d85 --- /dev/null +++ b/src/routes/getSegmentInfo.ts @@ -0,0 +1,75 @@ +import { Request, Response } from 'express'; +import { db } from '../databases/databases'; +import { DBSegment, SegmentUUID } from "../types/segments.model"; + +const isValidSegmentUUID = (str: string): Boolean => /^([a-f0-9]{64}|[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12})/.test(str) + +async function getSegmentFromDBByUUID(UUID: SegmentUUID): Promise { + try { + return await db.prepare('get', `SELECT * FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) + } catch (err) { + return null + } +} + +async function getSegmentsByUUID(UUIDs: SegmentUUID[]): Promise { + const DBSegments: DBSegment[] = []; + for (let UUID of UUIDs) { + // if UUID is invalid, skip + if (!isValidSegmentUUID(UUID)) { + continue; + } + DBSegments.push(await getSegmentFromDBByUUID(UUID as SegmentUUID)); + } + return DBSegments +} + +async function handleGetSegmentInfo(req: Request, res: Response) { + // If using params instead of JSON, only one UUID can be pulled + let UUIDs = req.query.UUIDs + ? JSON.parse(req.query.UUIDs as string) + : req.query.UUID + ? [req.query.UUID] + : null + // deduplicate with set + UUIDs = [ ...new Set(UUIDs)]; + // if more than 10 entries, slice + if (UUIDs.length > 10) UUIDs = UUIDs.slice(0, 10); + if (!Array.isArray(UUIDs) || !UUIDs) { + res.status(400).send("UUIDs parameter does not match format requirements."); + return false; + } + const DBSegments = await getSegmentsByUUID(UUIDs); + if (DBSegments === null || DBSegments === undefined) { + res.sendStatus(400); + return false; + } + if (DBSegments.length === 0) { + res.sendStatus(404); + return false; + } + return DBSegments; +} + +async function endpoint(req: Request, res: Response): Promise { + try { + const DBSegments = await handleGetSegmentInfo(req, res); + + // If false, res.send has already been called + if (DBSegments) { + //send result + res.send(DBSegments); + } + } catch (err) { + if (err instanceof SyntaxError) { // catch JSON.parse error + res.status(400).send("UUIDs parameter does not match format requirements."); + } else res.status(500).send(); + } +} + +export { + getSegmentFromDBByUUID, + getSegmentsByUUID, + handleGetSegmentInfo, + endpoint +}; diff --git a/test/cases/getSegmentInfo.ts b/test/cases/getSegmentInfo.ts new file mode 100644 index 0000000..0ab9250 --- /dev/null +++ b/test/cases/getSegmentInfo.ts @@ -0,0 +1,302 @@ +import fetch from 'node-fetch'; +import {db} from '../../src/databases/databases'; +import {Done, getbaseURL} from '../utils'; +import {getHash} from '../../src/utils/getHash'; + +const upvotedID = "a000000000000000000000000000000000000000000000000000000000000000" +const downvotedID = "b000000000000000000000000000000000000000000000000000000000000000" +const lockedupID = "c000000000000000000000000000000000000000000000000000000000000000" +const infvotesID = "d000000000000000000000000000000000000000000000000000000000000000" +const shadowhiddenID = "e000000000000000000000000000000000000000000000000000000000000000" +const lockeddownID = "f000000000000000000000000000000000000000000000000000000000000000" +const hiddenID = "1000000000000000000000000000000000000000000000000000000000000000" +const fillerID1 = "1100000000000000000000000000000000000000000000000000000000000000" +const fillerID2 = "1200000000000000000000000000000000000000000000000000000000000000" +const fillerID3 = "1300000000000000000000000000000000000000000000000000000000000000" +const fillerID4 = "1400000000000000000000000000000000000000000000000000000000000000" +const fillerID5 = "1500000000000000000000000000000000000000000000000000000000000000" +const oldID = "a0000000-0000-0000-0000-000000000000" + +describe('getSegmentInfo', () => { + before(async () => { + let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES'; + await db.prepare("run", startOfQuery + "('upvoted', 1, 10, 2, 0, '" + upvotedID+ "', 'testman', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '" + getHash('upvoted', 1) + "')"); + await db.prepare("run", startOfQuery + "('downvoted', 1, 10, -2, 0, '" + downvotedID+ "', 'testman', 0, 50, 'sponsor', 'YouTube', 120, 0, 0, '" + getHash('downvoted', 1) + "')"); + await db.prepare("run", startOfQuery + "('locked-up', 1, 10, 2, 1, '"+ lockedupID +"', 'testman', 0, 50, 'sponsor', 'YouTube', 101, 0, 0, '" + getHash('locked-up', 1) + "')"); + await db.prepare("run", startOfQuery + "('infvotes', 1, 10, 100000, 0, '"+infvotesID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 101, 0, 0, '" + getHash('infvotes', 1) + "')"); + await db.prepare("run", startOfQuery + "('hidden', 1, 10, 2, 0, '"+hiddenID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 140, 1, 0, '" + getHash('hidden', 1) + "')"); + await db.prepare("run", startOfQuery + "('shadowhidden', 1, 10, 2, 0, '"+shadowhiddenID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 140, 0, 1, '" + getHash('shadowhidden', 1) + "')"); + await db.prepare("run", startOfQuery + "('locked-down', 1, 10, -2, 1, '"+lockeddownID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 200, 0, 0, '" + getHash('locked-down', 1) + "')"); + await db.prepare("run", startOfQuery + "('oldID', 1, 10, 1, 0, '"+oldID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('oldID', 1) + "')"); + + await db.prepare("run", startOfQuery + "('filler', 1, 2, 1, 0, '"+fillerID1+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')"); + await db.prepare("run", startOfQuery + "('filler', 2, 3, 1, 0, '"+fillerID2+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')"); + await db.prepare("run", startOfQuery + "('filler', 3, 4, 1, 0, '"+fillerID3+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')"); + await db.prepare("run", startOfQuery + "('filler', 4, 5, 1, 0, '"+fillerID4+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')"); + await db.prepare("run", startOfQuery + "('filler', 5, 6, 1, 0, '"+fillerID5+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')"); + }); + + it('Should be able to retreive upvoted segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${upvotedID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "upvoted" && data[0].votes === 2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive downvoted segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${downvotedID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "downvoted" && data[0].votes === -2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive locked up segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${lockedupID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "locked-up" && data[0].locked === 1 && data[0].votes === 2) { + done(); + } else { + done ("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive infinite vote segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${infvotesID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "infvotes" && data[0].votes === 100000) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive shadowhidden segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${shadowhiddenID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "shadowhidden" && data[0].shadowHidden === 1) { + done(); + } else { + done ("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive locked down segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${lockeddownID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "locked-down" && data[0].votes === -2 && data[0].locked === 1) { + done(); + } else { + done ("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive hidden segment', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${hiddenID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "hidden" && data[0].hidden === 1) { + done(); + } else { + done ("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive segment with old ID', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${oldID}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "oldID" && data[0].votes === 1) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive single segment in array', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}"]`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data.length === 1 && data[0].videoID === "upvoted" && data[0].votes === 2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be able to retreive multiple segments in array', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "${downvotedID}"]`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data.length === 2 && + (data[0].videoID === "upvoted" && data[0].votes === 2) && + (data[1].videoID === "downvoted" && data[1].votes === -2)) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should be possible to send unexpected query parameters', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=${upvotedID}&fakeparam=hello&category=sponsor`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data[0].videoID === "upvoted" && data[0].votes === 2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => "Couldn't call endpoint"); + }); + + it('Should return 404 if array passed to UUID', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUID=["${upvotedID}", "${downvotedID}"]`) + .then(res => { + if (res.status !== 404) done("non 404 respone code: " + res.status); + else done(); // pass + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should return 400 if bad array passed to UUIDs', (done: Done) => { + fetch(getbaseURL() + "/api/segmentInfo?UUIDs=[not-quoted,not-quoted]") + .then(res => { + if (res.status !== 400) done("non 404 respone code: " + res.status); + else done(); // pass + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should return 404 if bad UUID passed', (done: Done) => { + fetch(getbaseURL() + "/api/segmentInfo?UUID=notarealuuid") + .then(res => { + if (res.status !== 404) done("non 404 respone code: " + res.status); + else done(); // pass + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should return 404 if bad UUIDs passed in array', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["notarealuuid", "anotherfakeuuid"]`) + .then(res => { + if (res.status !== 404) done("non 404 respone code: " + res.status); + else done(); // pass + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should return good UUID when mixed with bad UUIDs', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "anotherfakeuuid"]`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data.length === 1 && data[0].videoID === "upvoted" && data[0].votes === 2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should cut off array at 10', (done: Done) => { + const filledIDArray = `["${upvotedID}", "${downvotedID}", "${lockedupID}", "${shadowhiddenID}", "${lockeddownID}", "${hiddenID}", "${fillerID1}", "${fillerID2}", "${fillerID3}", "${fillerID4}", "${fillerID5}"]` + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=${filledIDArray}`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + // last segment should be fillerID4 + if (data.length === 10 && data[0].videoID === "upvoted" && data[0].votes === 2 && data[9].videoID === "filler" && data[9].UUID === fillerID4) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => ("couldn't call endpoint")); + }); + + it('Should not duplicate reponses', (done: Done) => { + fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${downvotedID}"]`) + .then(async res => { + if (res.status !== 200) done("Status code was: " + res.status); + else { + const data = await res.json(); + if (data.length === 2 && data[0].videoID === "upvoted" && data[0].votes === 2 && data[1].videoID === "downvoted" && data[1].votes === -2) { + done(); + } else { + done("Received incorrect body: " + (await res.text())); + } + } + }) + .catch(err => ("couldn't call endpoint")); + }); +}); From 7fe787c5ab1ab54d6dbb309be0f64db40bcb5853 Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 16 Jun 2021 00:53:34 -0400 Subject: [PATCH 5/6] remove extra properties --- src/routes/getSegmentInfo.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/routes/getSegmentInfo.ts b/src/routes/getSegmentInfo.ts index a593d85..af7063d 100644 --- a/src/routes/getSegmentInfo.ts +++ b/src/routes/getSegmentInfo.ts @@ -6,7 +6,11 @@ const isValidSegmentUUID = (str: string): Boolean => /^([a-f0-9]{64}|[a-f0-9]{8} async function getSegmentFromDBByUUID(UUID: SegmentUUID): Promise { try { - return await db.prepare('get', `SELECT * FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) + return await db.prepare('get', + `SELECT "videoID", "startTime", "endTime", "votes", "locked", + "UUID", "userID", "timeSubmitted", "views", "category", + "service", "videoDuration", "hidden", "reputation", "shadowHidden" FROM "sponsorTimes" + WHERE "UUID" = ?`, [UUID]) } catch (err) { return null } From 20ae560bb1ac42c73738710ad66014151b9c59bd Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Wed, 16 Jun 2021 13:23:25 -0400 Subject: [PATCH 6/6] Add semicolons --- src/routes/getSegmentInfo.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/routes/getSegmentInfo.ts b/src/routes/getSegmentInfo.ts index af7063d..4262998 100644 --- a/src/routes/getSegmentInfo.ts +++ b/src/routes/getSegmentInfo.ts @@ -10,9 +10,9 @@ async function getSegmentFromDBByUUID(UUID: SegmentUUID): Promise { `SELECT "videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "service", "videoDuration", "hidden", "reputation", "shadowHidden" FROM "sponsorTimes" - WHERE "UUID" = ?`, [UUID]) + WHERE "UUID" = ?`, [UUID]); } catch (err) { - return null + return null; } } @@ -25,7 +25,7 @@ async function getSegmentsByUUID(UUIDs: SegmentUUID[]): Promise { } DBSegments.push(await getSegmentFromDBByUUID(UUID as SegmentUUID)); } - return DBSegments + return DBSegments; } async function handleGetSegmentInfo(req: Request, res: Response) { @@ -34,7 +34,7 @@ async function handleGetSegmentInfo(req: Request, res: Response) { ? JSON.parse(req.query.UUIDs as string) : req.query.UUID ? [req.query.UUID] - : null + : null; // deduplicate with set UUIDs = [ ...new Set(UUIDs)]; // if more than 10 entries, slice