diff --git a/.eslintrc.js b/.eslintrc.js index 8837f26..14b67b2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -26,5 +26,6 @@ module.exports = { "no-multiple-empty-lines": ["error", { max: 2, maxEOF: 0 }], "indent": ["warn", 4, { "SwitchCase": 1 }], "object-curly-spacing": ["warn", "always"], + "require-await": "warn", }, }; diff --git a/src/databases/Mysql.ts b/src/databases/Mysql.ts index c93aa9f..d15b692 100644 --- a/src/databases/Mysql.ts +++ b/src/databases/Mysql.ts @@ -10,6 +10,7 @@ export class Mysql implements IDatabase { constructor(private config: unknown) { } + // eslint-disable-next-line require-await async init(): Promise { this.connection = new MysqlInterface(this.config); } diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index dc3bd47..fc45bbe 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -12,6 +12,7 @@ export class Sqlite implements IDatabase { { } + // eslint-disable-next-line require-await async prepare(type: QueryType, query: string, params: any[] = []): Promise { // Logger.debug(`prepare (sqlite): type: ${type}, query: ${query}, params: ${params}`); const preparedQuery = this.db.prepare(query); @@ -30,6 +31,7 @@ export class Sqlite implements IDatabase { } } + // eslint-disable-next-line require-await async init(): Promise { // Make dirs if required if (!fs.existsSync(path.join(this.config.dbPath, "../"))) { diff --git a/src/routes/getSearchSegments.ts b/src/routes/getSearchSegments.ts index 684386b..6720df4 100644 --- a/src/routes/getSearchSegments.ts +++ b/src/routes/getSearchSegments.ts @@ -10,7 +10,7 @@ type searchSegmentResponse = { segments: DBSegment[] }; -async function getSegmentsFromDBByVideoID(videoID: VideoID, service: Service): Promise { +function getSegmentsFromDBByVideoID(videoID: VideoID, service: Service): Promise { return db.prepare( "all", `SELECT "UUID", "timeSubmitted", "startTime", "endTime", "category", "actionType", "votes", "views", "locked", "hidden", "shadowHidden" FROM "sponsorTimes" diff --git a/src/routes/getSkipSegmentsByHash.ts b/src/routes/getSkipSegmentsByHash.ts index a5ed122..48f916d 100644 --- a/src/routes/getSkipSegmentsByHash.ts +++ b/src/routes/getSkipSegmentsByHash.ts @@ -1,7 +1,7 @@ import { hashPrefixTester } from "../utils/hashPrefixTester"; import { getSegmentsByHash } from "./getSkipSegments"; import { Request, Response } from "express"; -import { ActionType, Category, SegmentUUID, Service, VideoIDHash } from "../types/segments.model"; +import { ActionType, Category, SegmentUUID, VideoIDHash } from "../types/segments.model"; import { getService } from "../utils/getService"; export async function getSkipSegmentsByHash(req: Request, res: Response): Promise { diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 9311ee8..ebca98c 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -116,7 +116,7 @@ const objSwitch = (cases: cases) => (defaultCase: string) => (key: string) => const functionSwitch = (cases: cases) => (defaultCase: string) => (key: string) => executeIfFunction(objSwitch(cases)(defaultCase)(key)); -const dbGetValue = async (userID: HashedUserID, property: string): Promise => { +const dbGetValue = (userID: HashedUserID, property: string): Promise => { return functionSwitch({ userID, userName: dbGetUsername(userID), diff --git a/src/routes/oldSubmitSponsorTimes.ts b/src/routes/oldSubmitSponsorTimes.ts index 592fb49..22cde5b 100644 --- a/src/routes/oldSubmitSponsorTimes.ts +++ b/src/routes/oldSubmitSponsorTimes.ts @@ -1,7 +1,7 @@ import { postSkipSegments } from "./postSkipSegments"; import { Request, Response } from "express"; -export async function oldSubmitSponsorTimes(req: Request, res: Response): Promise { +export function oldSubmitSponsorTimes(req: Request, res: Response): Promise { req.query.category = "sponsor"; return postSkipSegments(req, res); } diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 3dc4410..5e4ca67 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -274,7 +274,7 @@ async function autoModerateSubmission(apiVideoInfo: APIVideoInfo, } } -async function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise { +function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise { if (config.newLeafURLs !== null) { return YouTubeAPI.listVideos(videoID, ignoreCache); } else { diff --git a/src/routes/setUsername.ts b/src/routes/setUsername.ts index 90ffeee..dee0e99 100644 --- a/src/routes/setUsername.ts +++ b/src/routes/setUsername.ts @@ -4,7 +4,7 @@ import { db, privateDB } from "../databases/databases"; import { getHash } from "../utils/getHash"; import { Request, Response } from "express"; -async function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise { +function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise { return privateDB.prepare("run", `INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedByAdmin", "updatedAt") VALUES(?, ?, ?, ?, ?)`, [userID, newUserName, oldUserName, + updatedByAdmin, new Date().getTime()] diff --git a/test/cases/getLockReason.ts b/test/cases/getLockReason.ts index 5b40182..2941077 100644 --- a/test/cases/getLockReason.ts +++ b/test/cases/getLockReason.ts @@ -1,11 +1,9 @@ -import fetch from "node-fetch"; -import { Done } from "../utils/utils"; -import { getbaseURL } from "../utils/getBaseURL"; import { getHash } from "../../src/utils/getHash"; import { db } from "../../src/databases/databases"; import assert from "assert"; +import { client } from "../utils/httpClient"; -const endpoint = `${getbaseURL()}/api/lockReason`; +const endpoint = "/api/lockReason"; describe("getLockReason", () => { before(async () => { @@ -28,71 +26,65 @@ describe("getLockReason", () => { else return `Version isn't greater than 20. Version is ${version}`; }); - it("Should be able to get single reason", (done: Done) => { - fetch(`${endpoint}?videoID=getLockReason&category=sponsor`) - .then(async res => { + it("Should be able to get single reason", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", category: "sponsor" } }) + .then(res => { assert.strictEqual(res.status, 200); - const data = await res.json(); const expected = [ { category: "sponsor", locked: 1, reason: "sponsor-reason" } ]; - assert.deepStrictEqual(data, expected); + assert.deepStrictEqual(res.data, expected); done(); }) .catch(err => done(err)); }); - it("Should be able to get empty locks", (done: Done) => { - fetch(`${endpoint}?videoID=getLockReason&category=intro`) - .then(async res => { + it("Should be able to get empty locks", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } }) + .then(res => { assert.strictEqual(res.status, 200); - const data = await res.json(); const expected = [ { category: "intro", locked: 0, reason: "" } ]; - assert.deepStrictEqual(data, expected); + assert.deepStrictEqual(res.data, expected); done(); }) .catch(err => done(err)); }); - it("should get multiple locks with array", (done: Done) => { - fetch(`${endpoint}?videoID=getLockReason&categories=["intro","sponsor"]`) - .then(async res => { + it("should get multiple locks with array", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", categories: `["intro","sponsor"]` } }) + .then(res => { assert.strictEqual(res.status, 200); - const data = await res.json(); const expected = [ { category: "sponsor", locked: 1, reason: "sponsor-reason" }, { category: "intro", locked: 0, reason: "" } ]; - assert.deepStrictEqual(data, expected); + assert.deepStrictEqual(res.data, expected); done(); }) .catch(err => done(err)); }); - it("should get multiple locks with repeated category", (done: Done) => { - fetch(`${endpoint}?videoID=getLockReason&category=interaction&category=music_offtopic&category=intro`) - .then(async res => { + it("should get multiple locks with repeated category", (done) => { + client.get(`${endpoint}?videoID=getLockReason&category=interaction&category=music_offtopic&category=intro`) + .then(res => { assert.strictEqual(res.status, 200); - const data = await res.json(); const expected = [ { category: "interaction", locked: 1, reason: "interaction-reason" }, { category: "music_offtopic", locked: 1, reason: "nonmusic-reason" }, { category: "intro", locked: 0, reason: "" } ]; - assert.deepStrictEqual(data, expected); + assert.deepStrictEqual(res.data, expected); done(); }) .catch(err => done(err)); }); - it("should return all categories if none specified", (done: Done) => { - fetch(`${endpoint}?videoID=getLockReason`) - .then(async res => { + it("should return all categories if none specified", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason" } }) + .then(res => { assert.strictEqual(res.status, 200); - const data = await res.json(); - console.log(data); const expected = [ { category: "sponsor", locked: 1, reason: "sponsor-reason" }, { category: "interaction", locked: 1, reason: "interaction-reason" }, @@ -103,14 +95,14 @@ describe("getLockReason", () => { { category: "outro", locked: 0, reason: "" }, { category: "poi_highlight", locked: 0, reason: "" } ]; - assert.deepStrictEqual(data, expected); + assert.deepStrictEqual(res.data, expected); done(); }) .catch(err => done(err)); }); - it("should return 400 if no videoID specified", (done: Done) => { - fetch(endpoint) + it("should return 400 if no videoID specified", (done) => { + client.get(endpoint) .then(res => { assert.strictEqual(res.status, 400); done(); diff --git a/test/cases/postClearCache.ts b/test/cases/postClearCache.ts index 7269e3e..671a9e4 100644 --- a/test/cases/postClearCache.ts +++ b/test/cases/postClearCache.ts @@ -35,7 +35,7 @@ describe("postClearCache", () => { it("Should get 403 as non-vip", (done) => { postClearCache(regularUser, "clear-test") - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); done(); }) @@ -44,7 +44,7 @@ describe("postClearCache", () => { it("Should give 400 with missing videoID", (done) => { client.post(endpoint, { params: { userID: VIPUser } }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 400); done(); }) @@ -53,7 +53,7 @@ describe("postClearCache", () => { it("Should give 400 with missing userID", (done) => { client.post(endpoint, { params: { videoID: "clear-test" } }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 400); done(); }) diff --git a/test/cases/postSkipSegments.ts b/test/cases/postSkipSegments.ts index 3e0e89a..67c7de6 100644 --- a/test/cases/postSkipSegments.ts +++ b/test/cases/postSkipSegments.ts @@ -311,7 +311,7 @@ describe("postSkipSegments", () => { category: "sponsor", }], }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); done(); }) @@ -632,7 +632,7 @@ describe("postSkipSegments", () => { category: "sponsor", }], }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); const errorMessage = res.data; const reason = "Reason01"; @@ -707,7 +707,7 @@ describe("postSkipSegments", () => { category: "sponsor", }], }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); const errorMessage = res.data; assert.notStrictEqual(errorMessage, ""); diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index d4bb0c2..93730b3 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -90,7 +90,7 @@ describe("postWarning", () => { it("Should return 400 if missing body", (done) => { client.post(endpoint, {}) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 400); done(); }) diff --git a/test/cases/segmentShift.ts b/test/cases/segmentShift.ts index 33fca34..98629b1 100644 --- a/test/cases/segmentShift.ts +++ b/test/cases/segmentShift.ts @@ -41,7 +41,7 @@ describe("segmentShift", function () { const privateVipUserID = "VIPUser-segmentShift"; const vipUserID = getHash(privateVipUserID); const endpoint = "/api/segmentShift"; - const postSegmentShift = async (data: Record) => client({ + const postSegmentShift = (data: Record) => client({ method: "POST", url: endpoint, data, @@ -71,7 +71,7 @@ describe("segmentShift", function () { startTime: 20, endTime: 30, }) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); done(); }) diff --git a/test/cases/setUsername.ts b/test/cases/setUsername.ts index 482311b..f1f3f15 100644 --- a/test/cases/setUsername.ts +++ b/test/cases/setUsername.ts @@ -34,14 +34,14 @@ async function getUsernameInfo(userID: string): Promise<{ userName: string, lock return row; } -async function addLogUserNameChange(userID: string, newUserName: string, oldUserName = "") { +function addLogUserNameChange(userID: string, newUserName: string, oldUserName = "") { privateDB.prepare("run", `INSERT INTO "userNameLogs"("userID", "newUserName", "oldUserName", "updatedAt", "updatedByAdmin") VALUES(?, ?, ?, ?, ?)`, [getHash(userID), newUserName, oldUserName, new Date().getTime(), + true] ); } -async function getLastLogUserNameChange(userID: string) { +function getLastLogUserNameChange(userID: string) { return privateDB.prepare("get", `SELECT * FROM "userNameLogs" WHERE "userID" = ? ORDER BY "updatedAt" DESC LIMIT 1`, [getHash(userID)]); } @@ -105,7 +105,7 @@ describe("setUsername", () => { it("Should return 200", (done) => { const username = "Changed%20Username"; postSetUserName(user01PrivateUserID, username) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 200); testUserNameChangelog(user01PrivateUserID, username, username01, false, done); }) diff --git a/test/cases/testUtils.ts b/test/cases/testUtils.ts index fb252b6..78818c8 100644 --- a/test/cases/testUtils.ts +++ b/test/cases/testUtils.ts @@ -2,7 +2,7 @@ import assert from "assert"; import { partialDeepEquals } from "../utils/partialDeepEquals"; describe("Test utils ", () => { - it("objectContain", async () => { + it("objectContain", () => { assert(partialDeepEquals( { name: "John Wick", diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index d987b89..32f937b 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -310,7 +310,7 @@ describe("voteOnSponsorTime", () => { it("Should not be able to category-vote on an invalid UUID submission", (done) => { const UUID = "invalid-uuid"; postVoteCategory("randomID3", UUID, "intro") - .then(async res => { + .then(res => { assert.strictEqual(res.status, 400); done(); }) @@ -356,7 +356,7 @@ describe("voteOnSponsorTime", () => { it("Should not be able to upvote a segment (Too many warning)", (done) => { const UUID = "warnvote-uuid-0"; postVote("warn-voteuser01", UUID, 1) - .then(async res => { + .then(res => { assert.strictEqual(res.status, 403); done(); }) diff --git a/test/utils/getBaseURL.ts b/test/utils/getBaseURL.ts deleted file mode 100644 index 3632cb4..0000000 --- a/test/utils/getBaseURL.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { config } from "../../src/config"; - -export function getbaseURL(): string { - return `http://localhost:${config.port}`; -} \ No newline at end of file diff --git a/test/utils/httpClient.ts b/test/utils/httpClient.ts index 6385e02..b100f13 100644 --- a/test/utils/httpClient.ts +++ b/test/utils/httpClient.ts @@ -1,12 +1,8 @@ import { config } from "../../src/config"; import axios, { AxiosRequestConfig } from "axios"; -export function getbaseURL(): string { - return `http://localhost:${config.port}`; -} - -export const defaultConfig: AxiosRequestConfig = { - baseURL: getbaseURL(), +const defaultConfig: AxiosRequestConfig = { + baseURL: `http://localhost:${config.port}`, validateStatus: (status) => status < 500 }; diff --git a/test/utils/utils.ts b/test/utils/utils.ts deleted file mode 100644 index 4bbb8f4..0000000 --- a/test/utils/utils.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Duplicated from Mocha types. TypeScript doesn't infer that type by itself for some reason. - */ -export type Done = (err?: any) => void; - -export const postJSON = { - method: "POST", - headers: { - "Content-Type": "application/json", - }, -}; \ No newline at end of file diff --git a/test/youtubeMock.ts b/test/youtubeMock.ts index f0a91f1..39d8c8f 100644 --- a/test/youtubeMock.ts +++ b/test/youtubeMock.ts @@ -1,6 +1,7 @@ import { APIVideoData, APIVideoInfo } from "../src/types/youtubeApi.model"; export class YouTubeApiMock { + // eslint-disable-next-line require-await static async listVideos(videoID: string): Promise { const obj = { id: videoID