mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
added tests and route
This commit is contained in:
@@ -29,6 +29,7 @@ import {apiCspMiddleware} from './middleware/apiCsp';
|
|||||||
import {rateLimitMiddleware} from './middleware/requestRateLimit';
|
import {rateLimitMiddleware} from './middleware/requestRateLimit';
|
||||||
import dumpDatabase, {redirectLink} from './routes/dumpDatabase';
|
import dumpDatabase, {redirectLink} from './routes/dumpDatabase';
|
||||||
import {endpoint as getSegmentInfo} from './routes/getSegmentInfo';
|
import {endpoint as getSegmentInfo} from './routes/getSegmentInfo';
|
||||||
|
import {postClearCache} from './routes/postClearCache';
|
||||||
|
|
||||||
export function createServer(callback: () => void) {
|
export function createServer(callback: () => void) {
|
||||||
// Create a service (the app object is just a callback).
|
// Create a service (the app object is just a callback).
|
||||||
@@ -136,6 +137,9 @@ function setupRoutes(app: Express) {
|
|||||||
//get segment info
|
//get segment info
|
||||||
app.get('/api/segmentInfo', getSegmentInfo);
|
app.get('/api/segmentInfo', getSegmentInfo);
|
||||||
|
|
||||||
|
//clear cache as VIP
|
||||||
|
app.post('/api/clearCache', postClearCache)
|
||||||
|
|
||||||
if (config.postgres) {
|
if (config.postgres) {
|
||||||
app.get('/database', (req, res) => dumpDatabase(req, res, true));
|
app.get('/database', (req, res) => dumpDatabase(req, res, true));
|
||||||
app.get('/database.json', (req, res) => dumpDatabase(req, res, false));
|
app.get('/database.json', (req, res) => dumpDatabase(req, res, false));
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import { UserID } from '../types/user.model';
|
|||||||
export async function postClearCache(req: Request, res: Response) {
|
export async function postClearCache(req: Request, res: Response) {
|
||||||
const videoID = req.query.videoID as VideoID;
|
const videoID = req.query.videoID as VideoID;
|
||||||
let userID = req.query.userID as UserID;
|
let userID = req.query.userID as UserID;
|
||||||
let service = req.query.service as Service ?? Service.YouTube;
|
const service = req.query.service as Service ?? Service.YouTube;
|
||||||
|
// hash the userID as early as possible
|
||||||
|
userID = getHash(userID);
|
||||||
|
|
||||||
const invalidFields = [];
|
const invalidFields = [];
|
||||||
if (typeof videoID !== 'string') {
|
if (typeof videoID !== 'string') {
|
||||||
@@ -26,8 +28,6 @@ export async function postClearCache(req: Request, res: Response) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash the userID
|
|
||||||
userID = getHash(userID);
|
|
||||||
// hash videoID
|
// hash videoID
|
||||||
const hashedVideoID = getHash(videoID, 1);
|
const hashedVideoID = getHash(videoID, 1);
|
||||||
|
|
||||||
|
|||||||
72
test/cases/postClearCache.ts
Normal file
72
test/cases/postClearCache.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import fetch from 'node-fetch';
|
||||||
|
import {Done, getbaseURL} from '../utils';
|
||||||
|
import {db} from '../../src/databases/databases';
|
||||||
|
import {getHash} from '../../src/utils/getHash';
|
||||||
|
|
||||||
|
describe('postClearCache', () => {
|
||||||
|
before(async () => {
|
||||||
|
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("clearing-vip") + "')");
|
||||||
|
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES';
|
||||||
|
await db.prepare("run", startOfQuery + "('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0, '" + getHash('clear-test', 1) + "')");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should be able to clear cache for existing video', (done: Done) => {
|
||||||
|
fetch(getbaseURL()
|
||||||
|
+ "/api/clearCache?userID=clearing-vip&videoID=clear-test", {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (res.status === 200) done();
|
||||||
|
else done("Status code was " + res.status);
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should be able to clear cache for nonexistent video', (done: Done) => {
|
||||||
|
fetch(getbaseURL()
|
||||||
|
+ "/api/clearCache?userID=clearing-vip&videoID=dne-video", {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (res.status === 200) done();
|
||||||
|
else done("Status code was " + res.status);
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should get 403 as non-vip', (done: Done) => {
|
||||||
|
fetch(getbaseURL()
|
||||||
|
+ "/api/clearCache?userID=regular-user&videoID=clear-tes", {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
if (res.status !== 403) done('non 403 (' + res.status + ')');
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should give 400 with missing videoID', (done: Done) => {
|
||||||
|
fetch(getbaseURL()
|
||||||
|
+ "/api/clearCache?userID=clearing-vip", {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
if (res.status !== 400) done('non 400 (' + res.status + ')');
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should give 400 with missing userID', (done: Done) => {
|
||||||
|
fetch(getbaseURL()
|
||||||
|
+ "/api/clearCache?userID=clearing-vip", {
|
||||||
|
method: 'POST'
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
if (res.status !== 400) done('non 400 (' + res.status + ')');
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user