mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
add getUserID and tests
This commit is contained in:
parent
e7fed0f3cf
commit
d4695f0192
3 changed files with 142 additions and 0 deletions
|
@ -32,6 +32,7 @@ import {endpoint as getSegmentInfo} from './routes/getSegmentInfo';
|
|||
import {postClearCache} from './routes/postClearCache';
|
||||
import { addUnlistedVideo } from './routes/addUnlistedVideo';
|
||||
import {postPurgeAllSegments} from './routes/postPurgeAllSegments';
|
||||
import {getUserID} from './routes/getUserID';
|
||||
|
||||
export function createServer(callback: () => void) {
|
||||
// Create a service (the app object is just a callback).
|
||||
|
@ -146,6 +147,9 @@ function setupRoutes(app: Express) {
|
|||
app.post('/api/purgeAllSegments', postPurgeAllSegments);
|
||||
|
||||
app.post('/api/unlistedVideo', addUnlistedVideo);
|
||||
|
||||
// get userID from username
|
||||
app.get('/api/userID', getUserID);
|
||||
|
||||
if (config.postgres) {
|
||||
app.get('/database', (req, res) => dumpDatabase(req, res, true));
|
||||
|
|
29
src/routes/getUserID.ts
Normal file
29
src/routes/getUserID.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import {db} from '../databases/databases';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export async function getUserID(req: Request, res: Response) {
|
||||
let username = req.query.username as string;
|
||||
|
||||
if (username == undefined) {
|
||||
//invalid request
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
// add wildcard to variable
|
||||
username = `%${username}%`
|
||||
try {
|
||||
let rows = await db.prepare('all', `SELECT "userName", "userID" FROM "userNames" WHERE "userName" LIKE ?`, [username]);
|
||||
if (rows.length === 0) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
} else {
|
||||
res.send(rows);
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
}
|
109
test/cases/getUserID.ts
Normal file
109
test/cases/getUserID.ts
Normal file
|
@ -0,0 +1,109 @@
|
|||
import fetch from 'node-fetch';
|
||||
import {Done, getbaseURL} from '../utils';
|
||||
import {db} from '../../src/databases/databases';
|
||||
import {getHash} from '../../src/utils/getHash';
|
||||
|
||||
describe('getUserID', () => {
|
||||
before(async () => {
|
||||
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_01"), 'fuzzy user 01']);
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_02"), 'fuzzy user 02']);
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_03"), 'specific user 03']);
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_04"), 'repeating']);
|
||||
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_05"), 'repeating']);
|
||||
});
|
||||
|
||||
it('Should be able to get a 200', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/userID?username=fuzzy+user+01')
|
||||
.then(async res => {
|
||||
const text = await res.text()
|
||||
if (res.status !== 200) done('non 200 (' + res.status + ')');
|
||||
else done(); // pass
|
||||
})
|
||||
.catch(err => done('couldn\'t call endpoint'));
|
||||
});
|
||||
|
||||
it('Should be able to get a 400 (No username parameter)', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/userID')
|
||||
.then(res => {
|
||||
if (res.status !== 400) done('non 400 (' + res.status + ')');
|
||||
else done(); // pass
|
||||
})
|
||||
.catch(err => done('couldn\'t call endpoint'));
|
||||
});
|
||||
|
||||
it('Should be able to get single username', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/userID?username=fuzzy+user+01')
|
||||
.then(async res => {
|
||||
if (res.status !== 200) {
|
||||
done("non 200");
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (data.length !== 1) {
|
||||
done('Returned incorrect number of users "' + data.length + '"');
|
||||
} else if (data[0].userName !== "fuzzy user 01") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[0].userID !== getHash("getuserinfo_user_01")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else {
|
||||
done(); // pass
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
});
|
||||
|
||||
it('Should be able to get multiple fuzzy user info from start', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/userID?username=fuzzy+user')
|
||||
.then(async res => {
|
||||
if (res.status !== 200) {
|
||||
done("non 200");
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (data.length !== 2) {
|
||||
done('Returned incorrect number of users "' + data.length + '"');
|
||||
} else if (data[0].userName !== "fuzzy user 01") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[0].userID !== getHash("getuserinfo_user_01")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else if (data[1].userName !== "fuzzy user 02") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[1].userID !== getHash("getuserinfo_user_02")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else {
|
||||
done(); // pass
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
});
|
||||
|
||||
it('Should be able to get multiple fuzzy user info from middle', (done: Done) => {
|
||||
fetch(getbaseURL() + '/api/userID?username=user')
|
||||
.then(async res => {
|
||||
if (res.status !== 200) {
|
||||
done("non 200");
|
||||
} else {
|
||||
const data = await res.json();
|
||||
if (data.length !== 3) {
|
||||
done('Returned incorrect number of users "' + data.length + '"');
|
||||
} else if (data[0].userName !== "fuzzy user 01") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[0].userID !== getHash("getuserinfo_user_01")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else if (data[1].userName !== "fuzzy user 02") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[1].userID !== getHash("getuserinfo_user_02")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else if (data[2].userName !== "specific user 03") {
|
||||
done('Returned incorrect username "' + data.userName + '"');
|
||||
} else if (data[2].userID !== getHash("getuserinfo_user_03")) {
|
||||
done('Returned incorrect userID "' + data.userID + '"');
|
||||
} else {
|
||||
done(); // pass
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue