Update tests to use promises

This commit is contained in:
Ajay Ramachandran
2021-03-01 21:37:35 -05:00
parent ff4af3786e
commit 46b42da5bd
19 changed files with 274 additions and 259 deletions

View File

@@ -4,92 +4,94 @@ import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
describe('getSkipSegments', () => {
before(() => {
before(async () => {
let startOfQuery = "INSERT INTO sponsorTimes (videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID) VALUES";
db.exec(startOfQuery + "('testtesttest', 1, 11, 2, 0, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')");
db.exec(startOfQuery + "('testtesttest', 20, 33, 2, 0, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')");
db.exec(startOfQuery + "('testtesttest,test', 1, 11, 2, 0, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')");
db.exec(startOfQuery + "('test3', 1, 11, 2, 0, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')");
db.exec(startOfQuery + "('test3', 7, 22, -3, 0, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')");
db.exec(startOfQuery + "('multiple', 1, 11, 2, 0, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')");
db.exec(startOfQuery + "('multiple', 20, 33, 2, 0, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')");
db.exec(startOfQuery + "('locked', 20, 33, 2, 1, '1-uuid-locked-8', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')");
db.exec(startOfQuery + "('locked', 20, 34, 100000, 0, '1-uuid-9', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')");
await db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, '1-uuid-0', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest', 1) + "')");
await db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, '1-uuid-2', 'testman', 0, 50, 'intro', 0, '" + getHash('testtesttest', 1) + "')");
await db.prepare("run", startOfQuery + "('testtesttest,test', 1, 11, 2, 0, '1-uuid-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('testtesttest,test', 1) + "')");
await db.prepare("run", startOfQuery + "('test3', 1, 11, 2, 0, '1-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')");
await db.prepare("run", startOfQuery + "('test3', 7, 22, -3, 0, '1-uuid-5', 'testman', 0, 50, 'sponsor', 0, '" + getHash('test3', 1) + "')");
await db.prepare("run", startOfQuery + "('multiple', 1, 11, 2, 0, '1-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')");
await db.prepare("run", startOfQuery + "('multiple', 20, 33, 2, 0, '1-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('multiple', 1) + "')");
await db.prepare("run", startOfQuery + "('locked', 20, 33, 2, 1, '1-uuid-locked-8', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')");
await db.prepare("run", startOfQuery + "('locked', 20, 34, 100000, 0, '1-uuid-9', 'testman', 0, 50, 'intro', 0, '" + getHash('locked', 1) + "')");
return;
});
it('Should be able to get a time by category 1', (done: Done) => {
it('Should be able to get a time by category 1', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=sponsor")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") {
done();
return;
} else {
done("Received incorrect body: " + (await res.text()));
return ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => "Couldn't call endpoint");
});
it('Should be able to get a time by category 2', (done: Done) => {
it('Should be able to get a time by category 2', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=intro")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-2") {
done();
return;
} else {
done("Received incorrect body: " + (await res.text()));
return ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get a time by categories array', (done: Done) => {
it('Should be able to get a time by categories array', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\"]")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") {
done();
return;
} else {
done("Received incorrect body: " + (await res.text()));
return ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get a time by categories array 2', (done: Done) => {
it('Should be able to get a time by categories array 2', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"intro\"]")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-2") {
done();
return;
} else {
done("Received incorrect body: " + (await res.text()));
return ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get multiple times by category', (done: Done) => {
it('Should be able to get multiple times by category', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=multiple&categories=[\"intro\"]")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
@@ -105,20 +107,20 @@ describe('getSkipSegments', () => {
}
}
if (success) done();
else done("Received incorrect body: " + body);
if (success) return;
else return ("Received incorrect body: " + body);
} else {
done("Received incorrect body: " + body);
return ("Received incorrect body: " + body);
}
}
})
.catch(err => done("Couldn't call endpoint\n\n" + err));
.catch(err => ("Couldn't call endpoint\n\n" + err));
});
it('Should be able to get multiple times by multiple categories', (done: Done) => {
it('Should be able to get multiple times by multiple categories', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\", \"intro\"]")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
@@ -135,95 +137,95 @@ describe('getSkipSegments', () => {
}
}
if (success) done();
else done("Received incorrect body: " + body);
if (success) return;
else return ("Received incorrect body: " + body);
} else {
done("Received incorrect body: " + body);
return ("Received incorrect body: " + body);
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should be possible to send unexpected query parameters', (done: Done) => {
it('Should be possible to send unexpected query parameters', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&fakeparam=hello&category=sponsor")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") {
done();
return;
} else {
done("Received incorrect body: " + body);
return ("Received incorrect body: " + body);
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Low voted submissions should be hidden', (done: Done) => {
it('Low voted submissions should be hidden', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=test3&category=sponsor")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-4") {
done();
return;
} else {
done("Received incorrect body: " + body);
return ("Received incorrect body: " + body);
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should return 404 if no segment found', (done: Done) => {
it('Should return 404 if no segment found', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=notarealvideo")
.then(res => {
if (res.status !== 404) done("non 404 respone code: " + res.status);
else done(); // pass
if (res.status !== 404) return ("non 404 respone code: " + res.status);
else return; // pass
})
.catch(err => done("couldn't call endpoint"));
.catch(err => ("couldn't call endpoint"));
});
it('Should be able send a comma in a query param', (done: Done) => {
it('Should be able send a comma in a query param', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest,test&category=sponsor")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-1") {
done();
return;
} else {
done("Received incorrect body: " + body);
return ("Received incorrect body: " + body);
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
it('Should always get locked segment', (done: Done) => {
it('Should always get locked segment', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=locked&category=intro")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-locked-8") {
done();
return;
} else {
done("Received incorrect body: " + (await res.text()));
return ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => done("Couldn't call endpoint"));
.catch(err => ("Couldn't call endpoint"));
});
});