From 0584492b8c157e582338a661873b614d1ee1e4e2 Mon Sep 17 00:00:00 2001 From: Michael C Date: Thu, 6 Jan 2022 23:56:27 -0500 Subject: [PATCH] getLockCategories --- src/routes/getLockCategories.ts | 15 +++--- test/cases/getLockCategories.ts | 91 ++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/routes/getLockCategories.ts b/src/routes/getLockCategories.ts index 693b7de..245d882 100644 --- a/src/routes/getLockCategories.ts +++ b/src/routes/getLockCategories.ts @@ -1,30 +1,33 @@ import { db } from "../databases/databases"; import { Logger } from "../utils/logger"; import { Request, Response } from "express"; -import { Category, VideoID } from "../types/segments.model"; +import { ActionType, Category, VideoID } from "../types/segments.model"; import { getService } from "../utils/getService"; export async function getLockCategories(req: Request, res: Response): Promise { const videoID = req.query.videoID as VideoID; const service = getService(req.query.service as string); + const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute]; - if (videoID == undefined) { + if (!videoID || !Array.isArray(actionTypes)) { //invalid request return res.sendStatus(400); } try { // Get existing lock categories markers - const row = await db.prepare("all", 'SELECT "category", "reason" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string}[]; + const row = await db.prepare("all", 'SELECT "category", "reason", "actionType" from "lockCategories" where "videoID" = ? AND "service" = ?', [videoID, service]) as {category: Category, reason: string, actionType: ActionType}[]; + const actionTypeMatches = row.filter((lock) => actionTypes.includes(lock.actionType)); // map categories to array in JS becaues of SQL incompatibilities - const categories = row.map(item => item.category); + const categories = actionTypeMatches.map(item => item.category); if (categories.length === 0 || !categories[0]) return res.sendStatus(404); // Get longest lock reason - const reason = row.map(item => item.reason) + const reason = actionTypeMatches.map(item => item.reason) .reduce((a,b) => (a.length > b.length) ? a : b); return res.send({ reason, - categories + categories, + actionTypes }); } catch (err) { Logger.error(err as string); diff --git a/test/cases/getLockCategories.ts b/test/cases/getLockCategories.ts index 9666bda..f05a821 100644 --- a/test/cases/getLockCategories.ts +++ b/test/cases/getLockCategories.ts @@ -4,27 +4,28 @@ import assert from "assert"; import { client } from "../utils/httpClient"; import { mixedDeepEquals } from "../utils/partialDeepEquals"; const endpoint = "/api/lockCategories"; -const getLockCategories = (videoID: string) => client.get(endpoint, { params: { videoID } }); -const getLockCategoriesWithService = (videoID: string, service: string) => client.get(endpoint, { params: { videoID, service } }); +const defaultActionTypes = ["skip", "mute"]; +const getLockCategories = (videoID: string, actionTypes = defaultActionTypes, service = "YouTube") => client.get(endpoint, { params: { videoID, actionTypes, service } }); describe("getLockCategories", () => { before(async () => { const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)'; await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]); - const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "service") VALUES (?, ?, ?, ?, ?)'; - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "sponsor", "1-short", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "interaction", "1-longer-reason", "YouTube"]); + const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType","category", "reason", "service") VALUES (?, ?, ?, ?, ?, ?)'; + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "skip", "sponsor", "1-short", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "mute", "interaction", "1-longer-reason", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory2", "preview", "2-reason", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory2", "skip", "preview", "2-reason", "YouTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "nonmusic", "3-reason", "PeerTube"]); - await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "sponsor", "3-reason", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "mute", "nonmusic", "3-reason", "PeerTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "skip", "sponsor", "3-reason", "YouTube"]); + await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "full", "outro", "3-longer-reason", "YouTube"]); }); it("Should update the database version when starting the application", async () => { const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value; - assert.ok(version > 20, `Version isn't greater than 20. Version is ${version}`); + assert.ok(version >= 29, `Version isn't greater than 29. Version is ${version}`); }); it("Should be able to get multiple locks", (done) => { @@ -36,7 +37,8 @@ describe("getLockCategories", () => { "sponsor", "interaction" ], - reason: "1-longer-reason" + reason: "1-longer-reason", + actionTypes: defaultActionTypes }; assert.ok(mixedDeepEquals(res.data, expected)); done(); @@ -52,7 +54,8 @@ describe("getLockCategories", () => { categories: [ "preview" ], - reason: "2-reason" + reason: "2-reason", + actionTypes: defaultActionTypes }; assert.deepStrictEqual(res.data, expected); done(); @@ -79,7 +82,7 @@ describe("getLockCategories", () => { }); it("Should be able to get multiple locks with service", (done) => { - getLockCategoriesWithService("getLockCategory1", "YouTube") + getLockCategories("getLockCategory1", defaultActionTypes, "YouTube") .then(res => { assert.strictEqual(res.status, 200); const expected = { @@ -87,7 +90,8 @@ describe("getLockCategories", () => { "sponsor", "interaction" ], - reason: "1-longer-reason" + reason: "1-longer-reason", + actionTypes: defaultActionTypes }; assert.ok(mixedDeepEquals(res.data, expected)); done(); @@ -96,14 +100,15 @@ describe("getLockCategories", () => { }); it("Should be able to get single locks with service", (done) => { - getLockCategoriesWithService("getLockCategory3", "PeerTube") + getLockCategories("getLockCategory3", defaultActionTypes, "PeerTube") .then(res => { assert.strictEqual(res.status, 200); const expected = { categories: [ "nonmusic" ], - reason: "3-reason" + reason: "3-reason", + actionTypes: defaultActionTypes }; assert.deepStrictEqual(res.data, expected); done(); @@ -112,14 +117,15 @@ describe("getLockCategories", () => { }); it("Should be able to get single locks with service", (done) => { - getLockCategoriesWithService("getLockCategory3", "Youtube") + getLockCategories("getLockCategory3", defaultActionTypes, "Youtube") .then(res => { assert.strictEqual(res.status, 200); const expected = { categories: [ "sponsor" ], - reason: "3-reason" + reason: "3-reason", + actionTypes: defaultActionTypes }; assert.deepStrictEqual(res.data, expected); done(); @@ -128,14 +134,61 @@ describe("getLockCategories", () => { }); it("should return result from Youtube service if service not match", (done) => { - getLockCategoriesWithService("getLockCategory3", "Dailymotion") + getLockCategories("getLockCategory3", defaultActionTypes, "Dailymotion") .then(res => { assert.strictEqual(res.status, 200); const expected = { categories: [ "sponsor" ], - reason: "3-reason" + reason: "3-reason", + actionTypes: defaultActionTypes + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("should return 404 if invalid actionTypes specified", (done) => { + getLockCategories("getLockCategory1", ["ban"]) + .then(res => { + assert.strictEqual(res.status, 404); + done(); + }) + .catch(err => done(err)); + }); + + it("should be able to get with specific actionType", (done) => { + getLockCategories("getLockCategory1", ["mute"]) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "sponsor", + "interaction" + ], + reason: "1-longer-reason", + actionTypes: ["mute"] + }; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get skip, mute, full", (done) => { + const actionTypes = [...defaultActionTypes, "full"] + getLockCategories("getLockCategory3",actionTypes ) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = { + categories: [ + "nonmusic", + "outro" + ], + reason: "3-longer-reason", + actionTypes }; assert.deepStrictEqual(res.data, expected); done();