mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 14:07:09 +03:00
Merge pull request #444 from mchangrh/full-tests
Tests for full video locks
This commit is contained in:
@@ -1,30 +1,32 @@
|
|||||||
import { db } from "../databases/databases";
|
import { db } from "../databases/databases";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
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";
|
import { getService } from "../utils/getService";
|
||||||
|
|
||||||
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
export async function getLockCategories(req: Request, res: Response): Promise<Response> {
|
||||||
const videoID = req.query.videoID as VideoID;
|
const videoID = req.query.videoID as VideoID;
|
||||||
const service = getService(req.query.service as string);
|
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
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// 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
|
// 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);
|
if (categories.length === 0 || !categories[0]) return res.sendStatus(404);
|
||||||
// Get longest lock reason
|
// 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);
|
.reduce((a,b) => (a.length > b.length) ? a : b);
|
||||||
return res.send({
|
return res.send({
|
||||||
reason,
|
reason,
|
||||||
categories
|
categories,
|
||||||
|
actionTypes
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { db } from "../databases/databases";
|
|||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { hashPrefixTester } from "../utils/hashPrefixTester";
|
import { hashPrefixTester } from "../utils/hashPrefixTester";
|
||||||
import { Category, VideoID, VideoIDHash } from "../types/segments.model";
|
import { ActionType, Category, VideoID, VideoIDHash } from "../types/segments.model";
|
||||||
|
|
||||||
interface LockResultByHash {
|
interface LockResultByHash {
|
||||||
videoID: VideoID,
|
videoID: VideoID,
|
||||||
@@ -16,11 +16,13 @@ interface DBLock {
|
|||||||
hash: VideoIDHash,
|
hash: VideoIDHash,
|
||||||
category: Category,
|
category: Category,
|
||||||
reason: string,
|
reason: string,
|
||||||
|
actionType: ActionType,
|
||||||
}
|
}
|
||||||
|
|
||||||
const mergeLocks = (source: DBLock[]) => {
|
const mergeLocks = (source: DBLock[], actionTypes: ActionType[]) => {
|
||||||
const dest: LockResultByHash[] = [];
|
const dest: LockResultByHash[] = [];
|
||||||
for (const obj of source) {
|
for (const obj of source) {
|
||||||
|
if (!actionTypes.includes(obj.actionType)) continue;
|
||||||
// videoID already exists
|
// videoID already exists
|
||||||
const destMatch = dest.find(s => s.videoID === obj.videoID);
|
const destMatch = dest.find(s => s.videoID === obj.videoID);
|
||||||
if (destMatch) {
|
if (destMatch) {
|
||||||
@@ -42,6 +44,7 @@ const mergeLocks = (source: DBLock[]) => {
|
|||||||
|
|
||||||
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
export async function getLockCategoriesByHash(req: Request, res: Response): Promise<Response> {
|
||||||
let hashPrefix = req.params.prefix as VideoIDHash;
|
let hashPrefix = req.params.prefix as VideoIDHash;
|
||||||
|
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Mute, ActionType.Skip];
|
||||||
if (!hashPrefixTester(req.params.prefix)) {
|
if (!hashPrefixTester(req.params.prefix)) {
|
||||||
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
return res.status(400).send("Hash prefix does not match format requirements."); // Exit early on faulty prefix
|
||||||
}
|
}
|
||||||
@@ -49,10 +52,10 @@ export async function getLockCategoriesByHash(req: Request, res: Response): Prom
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// Get existing lock categories markers
|
||||||
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
|
const lockedRows = await db.prepare("all", 'SELECT "videoID", "hashedVideoID" as "hash", "category", "reason", "actionType" from "lockCategories" where "hashedVideoID" LIKE ?', [`${hashPrefix}%`]) as DBLock[];
|
||||||
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
|
if (lockedRows.length === 0 || !lockedRows[0]) return res.sendStatus(404);
|
||||||
// merge all locks
|
// merge all locks
|
||||||
return res.send(mergeLocks(lockedRows));
|
return res.send(mergeLocks(lockedRows, actionTypes));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { db } from "../databases/databases";
|
import { db } from "../databases/databases";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { Category, VideoID } from "../types/segments.model";
|
import { Category, VideoID, ActionType } from "../types/segments.model";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
|
|
||||||
const possibleCategoryList = config.categoryList;
|
const categorySupportList = config.categorySupport;
|
||||||
interface lockArray {
|
interface lockArray {
|
||||||
category: Category;
|
category: Category;
|
||||||
locked: number,
|
locked: number,
|
||||||
@@ -13,9 +13,23 @@ interface lockArray {
|
|||||||
userName: string,
|
userName: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filterActionType = (actionTypes: ActionType[]) => {
|
||||||
|
const filterCategories = new Set();
|
||||||
|
for (const [key, value] of Object.entries(categorySupportList)) {
|
||||||
|
for (const type of actionTypes) {
|
||||||
|
if (value.includes(type)) {
|
||||||
|
filterCategories.add(key as Category);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [...filterCategories];
|
||||||
|
};
|
||||||
|
|
||||||
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
export async function getLockReason(req: Request, res: Response): Promise<Response> {
|
||||||
const videoID = req.query.videoID as VideoID;
|
const videoID = req.query.videoID as VideoID;
|
||||||
let categories: Category[] = [];
|
let categories: Category[] = [];
|
||||||
|
const actionTypes = req.query.actionTypes as ActionType[] || [ActionType.Skip, ActionType.Mute];
|
||||||
|
const possibleCategories = filterActionType(actionTypes);
|
||||||
try {
|
try {
|
||||||
categories = req.query.categories
|
categories = req.query.categories
|
||||||
? JSON.parse(req.query.categories as string)
|
? JSON.parse(req.query.categories as string)
|
||||||
@@ -31,28 +45,32 @@ export async function getLockReason(req: Request, res: Response): Promise<Respon
|
|||||||
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
||||||
}
|
}
|
||||||
// only take valid categories
|
// only take valid categories
|
||||||
const searchCategories = (categories.length === 0 ) ? possibleCategoryList : categories.filter(x => possibleCategoryList.includes(x));
|
const searchCategories = (categories.length === 0 )
|
||||||
|
? possibleCategories
|
||||||
|
: categories.filter(x =>
|
||||||
|
possibleCategories.includes(x));
|
||||||
|
|
||||||
if (videoID == undefined) {
|
if (!videoID || !Array.isArray(actionTypes)) {
|
||||||
//invalid request
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get existing lock categories markers
|
// Get existing lock categories markers
|
||||||
const row = await db.prepare("all", 'SELECT "category", "reason", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, userID: string }[];
|
const row = await db.prepare("all", 'SELECT "category", "reason", "actionType", "userID" from "lockCategories" where "videoID" = ?', [videoID]) as {category: Category, reason: string, actionType: ActionType, userID: string }[];
|
||||||
// map to object array
|
// map to object array
|
||||||
const locks = [];
|
const locks = [];
|
||||||
const userIDs = new Set();
|
const userIDs = new Set();
|
||||||
// get all locks for video, check if requested later
|
// get all locks for video, check if requested later
|
||||||
for (const lock of row) {
|
for (const lock of row) {
|
||||||
locks.push({
|
if (actionTypes.includes(lock.actionType))
|
||||||
category: lock.category,
|
locks.push({
|
||||||
locked: 1,
|
category: lock.category,
|
||||||
reason: lock.reason,
|
locked: 1,
|
||||||
userID: lock?.userID || "",
|
reason: lock.reason,
|
||||||
userName: "",
|
userID: lock?.userID || "",
|
||||||
} as lockArray);
|
userName: "",
|
||||||
|
} as lockArray);
|
||||||
userIDs.add(lock.userID);
|
userIDs.add(lock.userID);
|
||||||
}
|
}
|
||||||
// all userName from userIDs
|
// all userName from userIDs
|
||||||
|
|||||||
@@ -389,7 +389,7 @@ async function checkEachSegmentValid(rawIP: IPAddress, paramUserID: UserID, user
|
|||||||
|| (getCategoryActionType(segments[i].category) === CategoryActionType.POI && startTime !== endTime)
|
|| (getCategoryActionType(segments[i].category) === CategoryActionType.POI && startTime !== endTime)
|
||||||
|| (segments[i].actionType === ActionType.Full && (startTime !== 0 || endTime !== 0))) {
|
|| (segments[i].actionType === ActionType.Full && (startTime !== 0 || endTime !== 0))) {
|
||||||
//invalid request
|
//invalid request
|
||||||
return { pass: false, errorMessage: "One of your segments times are invalid (too short, startTime before endTime, etc.)", errorCode: 400 };
|
return { pass: false, errorMessage: "One of your segments times are invalid (too short, endTime before startTime, etc.)", errorCode: 400 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for POI segments before some seconds
|
// Check for POI segments before some seconds
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export interface SBSConfig {
|
|||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
webhooks: WebhookConfig[];
|
webhooks: WebhookConfig[];
|
||||||
categoryList: string[];
|
categoryList: string[];
|
||||||
|
categorySupport: Record<string, string[]>;
|
||||||
getTopUsersCacheTimeMinutes: number;
|
getTopUsersCacheTimeMinutes: number;
|
||||||
maxNumberOfActiveWarnings: number;
|
maxNumberOfActiveWarnings: number;
|
||||||
hoursAfterWarningExpires: number;
|
hoursAfterWarningExpires: number;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { HashedValue } from "./hash.model";
|
import { HashedValue } from "./hash.model";
|
||||||
import { SBRecord } from "./lib.model";
|
import { SBRecord } from "./lib.model";
|
||||||
import { UserID } from "./user.model";
|
import { HashedUserID, UserID } from "./user.model";
|
||||||
|
|
||||||
export type SegmentUUID = string & { __segmentUUIDBrand: unknown };
|
export type SegmentUUID = string & { __segmentUUIDBrand: unknown };
|
||||||
export type VideoID = string & { __videoIDBrand: unknown };
|
export type VideoID = string & { __videoIDBrand: unknown };
|
||||||
@@ -107,11 +107,14 @@ export enum CategoryActionType {
|
|||||||
POI
|
POI
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LockCategory {
|
export interface DBLock {
|
||||||
category: Category,
|
|
||||||
reason: string,
|
|
||||||
videoID: VideoID,
|
videoID: VideoID,
|
||||||
userID: UserID
|
userID: HashedUserID,
|
||||||
|
actionType: ActionType,
|
||||||
|
category: Category,
|
||||||
|
hashedVideoID: VideoIDHash,
|
||||||
|
reason: string,
|
||||||
|
service: Service,
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum SortableFields {
|
export enum SortableFields {
|
||||||
|
|||||||
@@ -4,27 +4,28 @@ import assert from "assert";
|
|||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
import { mixedDeepEquals } from "../utils/partialDeepEquals";
|
import { mixedDeepEquals } from "../utils/partialDeepEquals";
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const getLockCategories = (videoID: string) => client.get(endpoint, { params: { videoID } });
|
const defaultActionTypes = ["skip", "mute"];
|
||||||
const getLockCategoriesWithService = (videoID: string, service: string) => client.get(endpoint, { params: { videoID, service } });
|
const getLockCategories = (videoID: string, actionTypes = defaultActionTypes, service = "YouTube") => client.get(endpoint, { params: { videoID, actionTypes, service } });
|
||||||
|
|
||||||
describe("getLockCategories", () => {
|
describe("getLockCategories", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]);
|
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]);
|
||||||
|
|
||||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "service") VALUES (?, ?, ?, ?, ?)';
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType","category", "reason", "service") VALUES (?, ?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "sponsor", "1-short", "YouTube"]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "skip", "sponsor", "1-short", "YouTube"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "interaction", "1-longer-reason", "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", "mute", "nonmusic", "3-reason", "PeerTube"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "sponsor", "3-reason", "YouTube"]);
|
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 () => {
|
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;
|
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) => {
|
it("Should be able to get multiple locks", (done) => {
|
||||||
@@ -36,7 +37,8 @@ describe("getLockCategories", () => {
|
|||||||
"sponsor",
|
"sponsor",
|
||||||
"interaction"
|
"interaction"
|
||||||
],
|
],
|
||||||
reason: "1-longer-reason"
|
reason: "1-longer-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.ok(mixedDeepEquals(res.data, expected));
|
assert.ok(mixedDeepEquals(res.data, expected));
|
||||||
done();
|
done();
|
||||||
@@ -52,7 +54,8 @@ describe("getLockCategories", () => {
|
|||||||
categories: [
|
categories: [
|
||||||
"preview"
|
"preview"
|
||||||
],
|
],
|
||||||
reason: "2-reason"
|
reason: "2-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -79,7 +82,7 @@ describe("getLockCategories", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get multiple locks with service", (done) => {
|
it("Should be able to get multiple locks with service", (done) => {
|
||||||
getLockCategoriesWithService("getLockCategory1", "YouTube")
|
getLockCategories("getLockCategory1", defaultActionTypes, "YouTube")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = {
|
const expected = {
|
||||||
@@ -87,7 +90,8 @@ describe("getLockCategories", () => {
|
|||||||
"sponsor",
|
"sponsor",
|
||||||
"interaction"
|
"interaction"
|
||||||
],
|
],
|
||||||
reason: "1-longer-reason"
|
reason: "1-longer-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.ok(mixedDeepEquals(res.data, expected));
|
assert.ok(mixedDeepEquals(res.data, expected));
|
||||||
done();
|
done();
|
||||||
@@ -96,14 +100,15 @@ describe("getLockCategories", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get single locks with service", (done) => {
|
it("Should be able to get single locks with service", (done) => {
|
||||||
getLockCategoriesWithService("getLockCategory3", "PeerTube")
|
getLockCategories("getLockCategory3", defaultActionTypes, "PeerTube")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = {
|
const expected = {
|
||||||
categories: [
|
categories: [
|
||||||
"nonmusic"
|
"nonmusic"
|
||||||
],
|
],
|
||||||
reason: "3-reason"
|
reason: "3-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -112,14 +117,15 @@ describe("getLockCategories", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get single locks with service", (done) => {
|
it("Should be able to get single locks with service", (done) => {
|
||||||
getLockCategoriesWithService("getLockCategory3", "Youtube")
|
getLockCategories("getLockCategory3", defaultActionTypes, "Youtube")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = {
|
const expected = {
|
||||||
categories: [
|
categories: [
|
||||||
"sponsor"
|
"sponsor"
|
||||||
],
|
],
|
||||||
reason: "3-reason"
|
reason: "3-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
@@ -128,18 +134,65 @@ describe("getLockCategories", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should return result from Youtube service if service not match", (done) => {
|
it("should return result from Youtube service if service not match", (done) => {
|
||||||
getLockCategoriesWithService("getLockCategory3", "Dailymotion")
|
getLockCategories("getLockCategory3", defaultActionTypes, "Dailymotion")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
const expected = {
|
const expected = {
|
||||||
categories: [
|
categories: [
|
||||||
"sponsor"
|
"sponsor"
|
||||||
],
|
],
|
||||||
reason: "3-reason"
|
reason: "3-reason",
|
||||||
|
actionTypes: defaultActionTypes
|
||||||
};
|
};
|
||||||
assert.deepStrictEqual(res.data, expected);
|
assert.deepStrictEqual(res.data, expected);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.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"]
|
||||||
|
};
|
||||||
|
mixedDeepEquals(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
|
||||||
|
};
|
||||||
|
mixedDeepEquals(res.data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,34 +2,36 @@ import { getHash } from "../../src/utils/getHash";
|
|||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
|
import { ActionType } from "../../src/types/segments.model";
|
||||||
|
|
||||||
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
|
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const getLockCategories = (hash: string) => client.get(`${endpoint}/${hash}`);
|
const getLockCategories = (hash: string, actionTypes = [ActionType.Mute, ActionType.Skip]) => client.get(`${endpoint}/${hash}`, { params: { actionTypes } });
|
||||||
|
|
||||||
describe("getLockCategoriesByHash", () => {
|
describe("getLockCategoriesByHash", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
|
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
|
||||||
|
|
||||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "preview", "2-reason", getHash("getLockHash2", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "skip", "preview", "2-reason", getHash("getLockHash2", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "skip", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
|
||||||
|
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "outro", "fake1-reason", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "mute", "outro", "fake1-reason", fakeHash]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "intro", "fake2-longer-reason", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "intro", "fake2-longer-reason", fakeHash]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "preview", "fake2-short", fakeHash]);
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "preview", "fake2-short", fakeHash]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "full", "sponsor", "fake2-notshown", fakeHash]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Database should be greater or equal to version 20", async () => {
|
it("Database should be greater or equal to version 29", async () => {
|
||||||
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
|
||||||
assert(
|
assert(
|
||||||
version >= 20,
|
version >= 29,
|
||||||
`Version isn't greater than 20. Version is ${version}`);
|
`Version isn't greater than 29. Version is ${version}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get multiple locks in one object", (done) => {
|
it("Should be able to get multiple locks in one object", (done) => {
|
||||||
@@ -163,4 +165,22 @@ describe("getLockCategoriesByHash", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to get by actionType", (done) => {
|
||||||
|
getLockCategories(fakeHash.substring(0,5), [ActionType.Full])
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const expected = [{
|
||||||
|
videoID: "fakehash-2",
|
||||||
|
hash: fakeHash,
|
||||||
|
categories: [
|
||||||
|
"sponsor"
|
||||||
|
],
|
||||||
|
reason: "fake2-notshown"
|
||||||
|
}];
|
||||||
|
assert.deepStrictEqual(res.data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { getHash } from "../../src/utils/getHash";
|
|||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
|
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||||
|
|
||||||
const endpoint = "/api/lockReason";
|
const endpoint = "/api/lockReason";
|
||||||
|
|
||||||
@@ -20,12 +21,13 @@ describe("getLockReason", () => {
|
|||||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
|
await db.prepare("run", insertVipUserNameQuery, [vipUserID1, vipUserName1]);
|
||||||
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
|
await db.prepare("run", insertVipUserNameQuery, [vipUserID2, vipUserName2]);
|
||||||
|
|
||||||
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)';
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason") VALUES (?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "sponsor", "sponsor-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "sponsor", "sponsor-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "interaction", "interaction-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "interaction", "interaction-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "preview", "preview-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "skip", "preview", "preview-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "music_offtopic", "nonmusic-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID1, "getLockReason", "mute", "music_offtopic", "nonmusic-reason"]);
|
||||||
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "outro", "outro-reason"]);
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "mute", "outro", "outro-reason"]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserID2, "getLockReason", "full", "selfpromo", "selfpromo-reason"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
after(async () => {
|
after(async () => {
|
||||||
@@ -96,7 +98,7 @@ describe("getLockReason", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return all categories if none specified", (done) => {
|
it("should return all skip + mute categories if none specified", (done) => {
|
||||||
client.get(endpoint, { params: { videoID: "getLockReason" } })
|
client.get(endpoint, { params: { videoID: "getLockReason" } })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 200);
|
assert.strictEqual(res.status, 200);
|
||||||
@@ -110,10 +112,8 @@ describe("getLockReason", () => {
|
|||||||
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
{ category: "preview", locked: 1, reason: "preview-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason", userID: vipUserID1, userName: vipUserName1 },
|
||||||
{ category: "filler", locked: 0, reason: "", userID: "", userName: "" },
|
{ category: "filler", locked: 0, reason: "", userID: "", userName: "" },
|
||||||
{ category: "poi_highlight", locked: 0, reason: "", userID: "", userName: "" },
|
|
||||||
{ category: "chapter", locked: 0, reason: "", userID: "", userName: "" }
|
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(res.data, expected);
|
partialDeepEquals(res.data, expected, false);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
@@ -127,4 +127,18 @@ describe("getLockReason", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should be able to get by actionType", (done) => {
|
||||||
|
client.get(endpoint, { params: { videoID: "getLockReason", actionTypes: ["full"] } })
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const expected = [
|
||||||
|
{ category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 },
|
||||||
|
{ category: "sponsor", locked: 0, reason: "", userID: "", userName: "" }
|
||||||
|
];
|
||||||
|
partialDeepEquals(res.data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { getHash } from "../../src/utils/getHash";
|
import { getHash } from "../../src/utils/getHash";
|
||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { LockCategory } from "../../src/types/segments.model";
|
import { UserID } from "../../src/types/user.model";
|
||||||
|
import { Category, VideoID } from "../../src/types/segments.model";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
import { partialDeepEquals } from "../utils/partialDeepEquals";
|
||||||
|
|
||||||
@@ -13,6 +14,13 @@ const stringDeepEquals = (a: string[], b: string[]): boolean => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface LockCategory {
|
||||||
|
category: Category,
|
||||||
|
reason: string,
|
||||||
|
videoID: VideoID,
|
||||||
|
userID: UserID
|
||||||
|
}
|
||||||
|
|
||||||
const endpoint = "/api/lockCategories";
|
const endpoint = "/api/lockCategories";
|
||||||
const submitEndpoint = "/api/skipSegments";
|
const submitEndpoint = "/api/skipSegments";
|
||||||
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
|
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
|
||||||
|
|||||||
Reference in New Issue
Block a user