mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
add tests
This commit is contained in:
@@ -37,6 +37,7 @@ import {getLockCategories} from "./routes/getLockCategories";
|
|||||||
import {getLockCategoriesByHash} from "./routes/getLockCategoriesByHash";
|
import {getLockCategoriesByHash} from "./routes/getLockCategoriesByHash";
|
||||||
import {endpoint as getSearchSegments } from "./routes/getSearchSegments";
|
import {endpoint as getSearchSegments } from "./routes/getSearchSegments";
|
||||||
import {getStatus } from "./routes/getStatus";
|
import {getStatus } from "./routes/getStatus";
|
||||||
|
import { getLockReason } from "./routes/getLockReason";
|
||||||
import ExpressPromiseRouter from "express-promise-router";
|
import ExpressPromiseRouter from "express-promise-router";
|
||||||
import { Server } from "http";
|
import { Server } from "http";
|
||||||
import { youtubeApiProxy } from "./routes/youtubeApiProxy";
|
import { youtubeApiProxy } from "./routes/youtubeApiProxy";
|
||||||
@@ -176,6 +177,8 @@ function setupRoutes(router: Router) {
|
|||||||
|
|
||||||
router.get("/api/youtubeApiProxy", youtubeApiProxy);
|
router.get("/api/youtubeApiProxy", youtubeApiProxy);
|
||||||
|
|
||||||
|
router.get("/api/lockReason", getLockReason);
|
||||||
|
|
||||||
if (config.postgres) {
|
if (config.postgres) {
|
||||||
router.get("/database", (req, res) => dumpDatabase(req, res, true));
|
router.get("/database", (req, res) => dumpDatabase(req, res, true));
|
||||||
router.get("/database.json", (req, res) => dumpDatabase(req, res, false));
|
router.get("/database.json", (req, res) => dumpDatabase(req, res, false));
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ interface lockArray {
|
|||||||
reason: string
|
reason: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLockCategories(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[] = [];
|
||||||
try {
|
try {
|
||||||
|
|||||||
119
test/cases/getLockReason.ts
Normal file
119
test/cases/getLockReason.ts
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
import fetch from "node-fetch";
|
||||||
|
import {Done, getbaseURL} from "../utils";
|
||||||
|
import {getHash} from "../../src/utils/getHash";
|
||||||
|
import {db} from "../../src/databases/databases";
|
||||||
|
import assert from "assert";
|
||||||
|
|
||||||
|
const endpoint = `${getbaseURL()}/api/lockReason`;
|
||||||
|
|
||||||
|
describe("getLockReason", () => {
|
||||||
|
before(async () => {
|
||||||
|
const vipUserID = "getLockReasonVIP";
|
||||||
|
const vipUserHash = getHash(vipUserID);
|
||||||
|
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
|
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
||||||
|
await db.prepare("run", insertVipUserQuery, [vipUserHash]);
|
||||||
|
|
||||||
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason") VALUES (?, ?, ?, ?)';
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "sponsor", "sponsor-reason"]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "interaction", "interaction-reason"]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "preview", "preview-reason"]);
|
||||||
|
await db.prepare("run", insertLockCategoryQuery, [vipUserHash, "getLockReason", "music_offtopic", "nonmusic-reason"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
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;
|
||||||
|
if (version > 20) return;
|
||||||
|
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 => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
const expected = [
|
||||||
|
{ category: "sponsor", locked: 1, reason: "sponsor-reason" }
|
||||||
|
];
|
||||||
|
assert.deepStrictEqual(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 => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const data = await res.json();
|
||||||
|
const expected = [
|
||||||
|
{ category: "intro", locked: 0, reason: "" }
|
||||||
|
];
|
||||||
|
assert.deepStrictEqual(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 => {
|
||||||
|
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);
|
||||||
|
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 => {
|
||||||
|
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);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return all categories if none specified", (done: Done) => {
|
||||||
|
fetch(`${endpoint}?videoID=getLockReason`)
|
||||||
|
.then(async 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" },
|
||||||
|
{ category: "preview", locked: 1, reason: "preview-reason" },
|
||||||
|
{ category: "music_offtopic", locked: 1, reason: "nonmusic-reason" },
|
||||||
|
{ category: "selfpromo", locked: 0, reason: "" },
|
||||||
|
{ category: "intro", locked: 0, reason: "" },
|
||||||
|
{ category: "outro", locked: 0, reason: "" },
|
||||||
|
{ category: "poi_highlight", locked: 0, reason: "" }
|
||||||
|
];
|
||||||
|
assert.deepStrictEqual(data, expected);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return 400 if no videoID specified", (done: Done) => {
|
||||||
|
fetch(endpoint)
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 400);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user