SponsorBlockServer/test/cases/oldGetSponsorTime.ts

71 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-01-06 01:43:28 +01:00
import fetch from 'node-fetch';
2020-10-17 20:56:54 +02:00
import {db} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
describe('getVideoSponsorTime (Old get method)', () => {
before(() => {
2021-05-07 01:51:11 +02:00
const insertSponsorTimes = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
db.prepare("run", insertSponsorTimes, ['old-testtesttest', 1, 11, 2, 'uuid-0', 'testman', 0, 50, 'sponsor', 0, getHash('old-testtesttest', 1)]);
db.prepare("run", insertSponsorTimes, ['old-testtesttest,test', 1, 11, 2, 'uuid-1', 'testman', 0, 50, 'sponsor', 0, getHash('old-testtesttest,test', 1)]);
2020-10-17 20:56:54 +02:00
});
it('Should be able to get a time', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + "/api/getVideoSponsorTimes?videoID=old-testtesttest")
.then(res => {
if (res.status !== 200) done("non 200 (" + res.status + ")");
else done(); // pass
})
.catch(err => done("Couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should return 404 if no segment found', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + "/api/getVideoSponsorTimes?videoID=notarealvideo")
.then(res => {
if (res.status !== 404) done("non 404 respone code: " + res.status);
else done(); // pass
})
.catch(err => done("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should be possible to send unexpected query parameters', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + "/api/getVideoSponsorTimes?videoID=old-testtesttest&fakeparam=hello")
.then(res => {
if (res.status !== 200) done("non 200");
else done(); // pass
})
.catch(err => done("couldn't callendpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should be able send a comma in a query param', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + "/api/getVideoSponsorTimes?videoID=old-testtesttest,test")
.then(async res => {
const body = await res.text();
if (res.status !== 200) done("non 200 response: " + res.status);
else if (JSON.parse(body).UUIDs[0] === 'uuid-1') done(); // pass
else done("couldn't parse response");
})
.catch(err => done("couln't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should be able to get the correct time', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + "/api/getVideoSponsorTimes?videoID=old-testtesttest")
.then(async res => {
if (res.status !== 200) done("non 200");
else {
const parsedBody = await res.json();
if (parsedBody.sponsorTimes[0][0] === 1
&& parsedBody.sponsorTimes[0][1] === 11
&& parsedBody.UUIDs[0] === 'uuid-0') {
done(); // pass
} else {
done("Wrong data was returned + " + JSON.stringify(parsedBody));
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
}
2020-10-17 20:56:54 +02:00
2021-01-06 01:43:28 +01:00
})
.catch(err => done("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
});