From 54e69b266d69e6ce2e1ae804e71fe70b04407d64 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sat, 6 Mar 2021 00:25:18 -0500 Subject: [PATCH] Fix tests --- src/databases/Sqlite.ts | 21 +++++---------------- src/routes/getSkipSegments.ts | 6 +++--- src/routes/getTopUsers.ts | 10 +++++----- src/routes/shadowBanUser.ts | 23 +++++++++++------------ src/routes/voteOnSponsorTime.ts | 12 ++++++------ test/cases/unBan.ts | 2 +- 6 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index b2880a2..ba80b0b 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -12,30 +12,19 @@ export class Sqlite implements IDatabase { { } - async prepare(type: QueryType, query: string, params?: any[]) { + async prepare(type: QueryType, query: string, params: any[] = []) { + // Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`); const preparedQuery = this.db.prepare(query); switch (type) { case 'get': { - if (params) { - return preparedQuery.get(...params); - } else { - return preparedQuery.get(); - } + return preparedQuery.get(...params); } case 'all': { - if (params) { - return preparedQuery.all(...params); - } else { - return preparedQuery.all(); - } + return preparedQuery.all(...params); } case 'run': { - if (params) { - preparedQuery.run(...params); - } else { - preparedQuery.run(); - } + preparedQuery.run(...params); break; } } diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 78ec5f3..54e74e2 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -52,7 +52,7 @@ async function getSegmentsByVideoID(req: Request, videoID: string, categories: C const segments: Segment[] = []; try { - categories.filter((category) => !/[^a-z|_|-]/.test(category)); + categories = categories.filter((category) => !/[^a-z|_|-]/.test(category)); const segmentsByCategory: SBRecord = (await db .prepare( @@ -87,12 +87,12 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, try { type SegmentWithHashPerVideoID = SBRecord}>; - categories.filter((category) => !/[^a-z|_|-]/.test(category)); + categories = categories.filter((category) => !(/[^a-z|_|-]/.test(category))); const segmentPerVideoID: SegmentWithHashPerVideoID = (await db .prepare( 'all', - `SELECT "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" + `SELECT "videoID", "startTime", "endTime", "votes", "locked", "UUID", "category", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" WHERE "hashedVideoID" LIKE ? AND "category" IN (${categories.map((c) => "'" + c + "'")}) ORDER BY "startTime"`, [hashedVideoIDPrefix + '%'] )).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => { diff --git a/src/routes/getTopUsers.ts b/src/routes/getTopUsers.ts index 15ff3ae..0e3acb2 100644 --- a/src/routes/getTopUsers.ts +++ b/src/routes/getTopUsers.ts @@ -24,13 +24,13 @@ async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boole } const rows = await db.prepare('all', `SELECT COUNT(*) as "totalSubmissions", SUM(views) as "viewCount", - SUM(("sponsorTimes.endTime" - "sponsorTimes.startTime") / 60 * "sponsorTimes.views") as "minutesSaved", + SUM(("sponsorTimes"."endTime" - "sponsorTimes"."startTime") / 60 * "sponsorTimes"."views") as "minutesSaved", SUM("votes") as "userVotes", ` + additionalFields + - `IFNULL("userNames.userName", "sponsorTimes.userID") as "userName" FROM "sponsorTimes" LEFT JOIN "userNames" ON "sponsorTimes.userID"="userNames.userID" - LEFT JOIN "privateDB.shadowBannedUsers" ON "sponsorTimes.userID"="privateDB.shadowBannedUsers.userID" - WHERE "sponsorTimes.votes" > -1 AND "sponsorTimes.shadowHidden" != 1 AND "privateDB.shadowBannedUsers.userID" IS NULL - GROUP BY IFNULL("userName", "sponsorTimes.userID") HAVING "userVotes" > 20 + `IFNULL("userNames"."userName", "sponsorTimes"."userID") as "userName" FROM "sponsorTimes" LEFT JOIN "userNames" ON "sponsorTimes"."userID"="userNames"."userID" + LEFT JOIN "privateDB"."shadowBannedUsers" ON "sponsorTimes"."userID"="privateDB"."shadowBannedUsers"."userID" + WHERE "sponsorTimes"."votes" > -1 AND "sponsorTimes"."shadowHidden" != 1 AND "privateDB"."shadowBannedUsers"."userID" IS NULL + GROUP BY IFNULL("userName", "sponsorTimes"."userID") HAVING "userVotes" > 20 ORDER BY "` + sortBy + `" DESC LIMIT 100`, []); for (let i = 0; i < rows.length; i++) { diff --git a/src/routes/shadowBanUser.ts b/src/routes/shadowBanUser.ts index 4007f7b..5ee22ee 100644 --- a/src/routes/shadowBanUser.ts +++ b/src/routes/shadowBanUser.ts @@ -44,29 +44,28 @@ export async function shadowBanUser(req: Request, res: Response) { if (unHideOldSubmissions) { await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "userID" = ? AND NOT EXISTS ( SELECT "videoID", "category" FROM "noSegments" WHERE - "sponsorTimes.videoID" = "noSegments.videoID" AND "sponsorTimes.category" = "noSegments.category")`, [userID]); + "sponsorTimes"."videoID" = "noSegments"."videoID" AND "sponsorTimes"."category" = "noSegments"."category")`, [userID]); } } else if (!enabled && row.userCount > 0) { //remove them from the shadow ban list - await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE userID = ?`, [userID]); + await privateDB.prepare('run', `DELETE FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); //find all previous submissions and unhide them if (unHideOldSubmissions) { let segmentsToIgnore = (await db.prepare('all', `SELECT UUID FROM "sponsorTimes" st - JOIN "noSegments" ns on "st.videoID" = "ns.videoID" AND st.category = ns.category WHERE "st.userID" = ?` + JOIN "noSegments" ns on "st"."videoID" = "ns"."videoID" AND st.category = ns.category WHERE "st"."userID" = ?` , [userID])).map((item: {UUID: string}) => item.UUID); - let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st.userID" = ?`, [userID])) + let allSegments = (await db.prepare('all', `SELECT "UUID" FROM "sponsorTimes" st WHERE "st"."userID" = ?`, [userID])) .map((item: {UUID: string}) => item.UUID); - allSegments.filter((item: {uuid: string}) => { + await Promise.all(allSegments.filter((item: {uuid: string}) => { return segmentsToIgnore.indexOf(item) === -1; - }).forEach((UUID: string) => { - db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE "UUID" = ?`, [UUID]); - }); + }).map((UUID: string) => { + return db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 0 WHERE "UUID" = ?`, [UUID]); + })); } } - } - else if (hashedIP) { + } else if (hashedIP) { //check to see if this user is already shadowbanned // let row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]); @@ -81,8 +80,8 @@ export async function shadowBanUser(req: Request, res: Response) { //find all previous submissions and hide them if (unHideOldSubmissions) { await db.prepare('run', `UPDATE "sponsorTimes" SET "shadowHidden" = 1 WHERE "timeSubmitted" IN - (SELECT "privateDB.timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB.sponsorTimes" as "privateDB" ON "sponsorTimes.timeSubmitted"="privateDB.timeSubmitted" - WHERE "privateDB.hashedIP" = ?)`, [hashedIP]); + (SELECT "privateDB"."timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB"."sponsorTimes" as "privateDB" ON "sponsorTimes"."timeSubmitted"="privateDB"."timeSubmitted" + WHERE "privateDB"."hashedIP" = ?)`, [hashedIP]); } } /*else if (!enabled && row.userCount > 0) { // //remove them from the shadow ban list diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 8ab4354..d74bc9b 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -36,10 +36,10 @@ interface VoteData { } async function sendWebhooks(voteData: VoteData) { - const submissionInfoRow = await db.prepare('get', `SELECT "s.videoID", "s.userID", "s.startTime", "s.endTime", "s.category", "u.userName", - (select count(1) from "sponsorTimes" where "userID" = "s.userID") count, - (select count(1) from "sponsorTimes" where "userID" = "s.userID" and votes <= -2) disregarded - FROM "sponsorTimes" s left join "userNames" u on "s.userID" = "u.userID" where "s.UUID"=?`, + const submissionInfoRow = await db.prepare('get', `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName", + (select count(1) from "sponsorTimes" where "userID" = s."userID") count, + (select count(1) from "sponsorTimes" where "userID" = s."userID" and votes <= -2) disregarded + FROM "sponsorTimes" s left join "userNames" u on s."userID" = u."userID" where s."UUID"=?`, [voteData.UUID]); const userSubmissionCountRow = await db.prepare('get', `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ?`, [voteData.nonAnonUserID]); @@ -254,8 +254,8 @@ export async function voteOnSponsorTime(req: Request, res: Response) { // If not upvote if (!isVIP && type !== 1) { const isSegmentLocked = async () => !!(await db.prepare('get', `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked; - const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT "noSegments.category" from "noSegments" left join "sponsorTimes"' + - ' on ("noSegments.videoID" = "sponsorTimes.videoID" and "noSegments.category" = "sponsorTimes.category")' + + const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT "noSegments".category from "noSegments" left join "sponsorTimes"' + + ' on ("noSegments"."videoID" = "sponsorTimes"."videoID" and "noSegments".category = "sponsorTimes".category)' + ' where "UUID" = ?', [UUID])); if (await isSegmentLocked() || await isVideoLocked()) { diff --git a/test/cases/unBan.ts b/test/cases/unBan.ts index fc0010c..99d3e87 100644 --- a/test/cases/unBan.ts +++ b/test/cases/unBan.ts @@ -31,7 +31,7 @@ describe('unBan', () => { }) .then(async res => { if (res.status === 200) { - let result = await db.prepare('all', 'SELECT * FROM sponsorTimes WHERE videoID = ? AND userID = ? AND shadowHidden = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); + let result = await db.prepare('all', 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', ['unBan-videoID-0', 'testMan-unBan', 1]); if (result.length !== 0) { console.log(result); done("Expected 0 banned entrys in db, got " + result.length);