mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-15 07:57:05 +03:00
add misc 400 tests
This commit is contained in:
@@ -2,10 +2,12 @@ import { db } from "../databases/databases";
|
|||||||
import { createMemoryCache } from "../utils/createMemoryCache";
|
import { createMemoryCache } from "../utils/createMemoryCache";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import { validateCategories } from "../utils/parseParams";
|
||||||
|
|
||||||
const MILLISECONDS_IN_MINUTE = 60000;
|
const MILLISECONDS_IN_MINUTE = 60000;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
const getTopCategoryUsersWithCache = createMemoryCache(generateTopCategoryUsersStats, config.getTopUsersCacheTimeMinutes * MILLISECONDS_IN_MINUTE);
|
const getTopCategoryUsersWithCache = createMemoryCache(generateTopCategoryUsersStats, config.getTopUsersCacheTimeMinutes * MILLISECONDS_IN_MINUTE);
|
||||||
|
/* istanbul ignore next */
|
||||||
const maxRewardTimePerSegmentInSeconds = config.maxRewardTimePerSegmentInSeconds ?? 86400;
|
const maxRewardTimePerSegmentInSeconds = config.maxRewardTimePerSegmentInSeconds ?? 86400;
|
||||||
|
|
||||||
interface DBSegment {
|
interface DBSegment {
|
||||||
@@ -38,7 +40,6 @@ async function generateTopCategoryUsersStats(sortBy: string, category: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
userNames,
|
userNames,
|
||||||
viewCounts,
|
viewCounts,
|
||||||
@@ -51,7 +52,7 @@ export async function getTopCategoryUsers(req: Request, res: Response): Promise<
|
|||||||
const sortType = parseInt(req.query.sortType as string);
|
const sortType = parseInt(req.query.sortType as string);
|
||||||
const category = req.query.category as string;
|
const category = req.query.category as string;
|
||||||
|
|
||||||
if (sortType == undefined || !config.categoryList.includes(category) ) {
|
if (sortType == undefined || !validateCategories([category]) ) {
|
||||||
//invalid request
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ function getStats(countContributingUsers: boolean): Promise<DBStatsData> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function updateExtensionUsers() {
|
function updateExtensionUsers() {
|
||||||
if (config.userCounterURL) {
|
if (config.userCounterURL) {
|
||||||
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
||||||
@@ -87,12 +86,13 @@ function updateExtensionUsers() {
|
|||||||
});
|
});
|
||||||
getCWSUsers(chromeExtId)
|
getCWSUsers(chromeExtId)
|
||||||
.then(res => chromeUsersCache = res)
|
.then(res => chromeUsersCache = res)
|
||||||
.catch(() =>
|
.catch(/* istanbul ignore next */ () =>
|
||||||
getChromeUsers(chromeExtensionUrl)
|
getChromeUsers(chromeExtensionUrl)
|
||||||
.then(res => chromeUsersCache = res)
|
.then(res => chromeUsersCache = res)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
function getChromeUsers(chromeExtensionUrl: string): Promise<number> {
|
function getChromeUsers(chromeExtensionUrl: string): Promise<number> {
|
||||||
return axios.get(chromeExtensionUrl)
|
return axios.get(chromeExtensionUrl)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
|||||||
@@ -73,3 +73,6 @@ export const parseActionTypes = (req: Request, fallback: ActionType[]): ActionTy
|
|||||||
|
|
||||||
export const parseRequiredSegments = (req: Request): SegmentUUID[] | undefined =>
|
export const parseRequiredSegments = (req: Request): SegmentUUID[] | undefined =>
|
||||||
syntaxErrorWrapper(getRequiredSegments, req, []); // never fall back
|
syntaxErrorWrapper(getRequiredSegments, req, []); // never fall back
|
||||||
|
|
||||||
|
export const validateCategories = (categories: string[]): boolean =>
|
||||||
|
categories.every((category: string) => config.categoryList.includes(category));
|
||||||
@@ -486,4 +486,13 @@ describe("getSkipSegments", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should get 400 for invalid category type", (done) => {
|
||||||
|
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", category: 1 } })
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 400);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe("getTopCategoryUsers", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should return 400 if invalid sortType provided", (done) => {
|
it("Should return 400 if invalid type of sortType provided", (done) => {
|
||||||
client.get(endpoint, { params: { sortType: "a" } })
|
client.get(endpoint, { params: { sortType: "a" } })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 400);
|
assert.strictEqual(res.status, 400);
|
||||||
@@ -38,6 +38,15 @@ describe("getTopCategoryUsers", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should return 400 if invalid sortType number provided", (done) => {
|
||||||
|
client.get(endpoint, { params: { sortType: 15, category: "sponsor" } })
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 400);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
it("Should return 400 if invalid category provided", (done) => {
|
it("Should return 400 if invalid category provided", (done) => {
|
||||||
client.get(endpoint, { params: { sortType: 1, category: "never_valid_category" } })
|
client.get(endpoint, { params: { sortType: 1, category: "never_valid_category" } })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
@@ -121,4 +130,16 @@ describe("getTopCategoryUsers", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should return no time saved for chapters", (done) => {
|
||||||
|
client.get(endpoint, { params: { sortType: 2, category: "chapter" } })
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
for (const timeSaved of res.data.minutesSaved) {
|
||||||
|
assert.strictEqual(timeSaved, 0, "Time saved should be 0");
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user