mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 09:07:47 +01:00
add more tests for coverage
This commit is contained in:
parent
d04230a1c4
commit
72fb4eb6ec
5 changed files with 66 additions and 4 deletions
|
@ -34,11 +34,11 @@ async function handleGetSegmentInfo(req: Request, res: Response): Promise<DBSegm
|
||||||
// deduplicate with set
|
// deduplicate with set
|
||||||
UUIDs = [ ...new Set(UUIDs)];
|
UUIDs = [ ...new Set(UUIDs)];
|
||||||
// if more than 10 entries, slice
|
// if more than 10 entries, slice
|
||||||
if (UUIDs.length > 10) UUIDs = UUIDs.slice(0, 10);
|
if (!Array.isArray(UUIDs) || !UUIDs?.length) {
|
||||||
if (!Array.isArray(UUIDs) || !UUIDs) {
|
|
||||||
res.status(400).send("UUIDs parameter does not match format requirements.");
|
res.status(400).send("UUIDs parameter does not match format requirements.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (UUIDs.length > 10) UUIDs = UUIDs.slice(0, 10);
|
||||||
const DBSegments = await getSegmentsByUUID(UUIDs);
|
const DBSegments = await getSegmentsByUUID(UUIDs);
|
||||||
// all uuids failed lookup
|
// all uuids failed lookup
|
||||||
if (!DBSegments?.length) {
|
if (!DBSegments?.length) {
|
||||||
|
|
|
@ -33,7 +33,7 @@ describe("304 etag validation", () => {
|
||||||
if (!config.redis?.enabled) this.skip();
|
if (!config.redis?.enabled) this.skip();
|
||||||
});
|
});
|
||||||
|
|
||||||
const endpoint = "/api/skipSegments";
|
const endpoint = "/etag";
|
||||||
for (const hashType of ["skipSegments", "skipSegmentsHash", "videoLabel", "videoLabelHash"]) {
|
for (const hashType of ["skipSegments", "skipSegmentsHash", "videoLabel", "videoLabelHash"]) {
|
||||||
it(`${hashType} etag should return 304`, (done) => {
|
it(`${hashType} etag should return 304`, (done) => {
|
||||||
const etagKey = `${hashType};${genRandom};YouTube;${Date.now()}`;
|
const etagKey = `${hashType};${genRandom};YouTube;${Date.now()}`;
|
||||||
|
@ -47,4 +47,20 @@ describe("304 etag validation", () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it(`other etag type should not return 304`, (done) => {
|
||||||
|
const etagKey = `invalidHashType;${genRandom};YouTube;${Date.now()}`;
|
||||||
|
client.get(endpoint, { headers: { "If-None-Match": etagKey } }).then(res => {
|
||||||
|
assert.strictEqual(res.status, 404);
|
||||||
|
done();
|
||||||
|
}).catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it(`outdated etag type should not return 304`, (done) => {
|
||||||
|
const etagKey = `skipSegments;${genRandom};YouTube;5000`;
|
||||||
|
client.get(endpoint, { headers: { "If-None-Match": etagKey } }).then(res => {
|
||||||
|
assert.strictEqual(res.status, 404);
|
||||||
|
done();
|
||||||
|
}).catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -338,4 +338,13 @@ describe("getSegmentInfo", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should return 400 if no UUIDs not sent", (done) => {
|
||||||
|
client.get(endpoint)
|
||||||
|
.then(res => {
|
||||||
|
if (res.status !== 400) done(`non 400 response code: ${res.status}`);
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -81,4 +81,14 @@ describe("getTopUsers", () => {
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to get cached result", (done) => {
|
||||||
|
client.get(endpoint, { params: { sortType: 0 } })// minutesSaved
|
||||||
|
.then(res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
assert.ok(res.data.userNames.indexOf(user1) < res.data.userNames.indexOf(user2), `Actual Order: ${res.data.userNames}`);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
|
import sinon from "sinon";
|
||||||
|
import * as getCWSUsers from "../../src/utils/getCWSUsers";
|
||||||
|
|
||||||
const endpoint = "/api/getTotalStats";
|
const endpoint = "/api/getTotalStats";
|
||||||
|
|
||||||
|
@ -7,11 +9,36 @@ describe("getTotalStats", () => {
|
||||||
it("Can get total stats", async () => {
|
it("Can get total stats", async () => {
|
||||||
const result = await client({ url: endpoint });
|
const result = await client({ url: endpoint });
|
||||||
const data = result.data;
|
const data = result.data;
|
||||||
assert.ok(data?.userCount ?? true);
|
assert.ok(data.userCount >= 0);
|
||||||
assert.ok(data.activeUsers >= 0);
|
assert.ok(data.activeUsers >= 0);
|
||||||
assert.ok(data.apiUsers >= 0);
|
assert.ok(data.apiUsers >= 0);
|
||||||
assert.ok(data.viewCount >= 0);
|
assert.ok(data.viewCount >= 0);
|
||||||
assert.ok(data.totalSubmissions >= 0);
|
assert.ok(data.totalSubmissions >= 0);
|
||||||
assert.ok(data.minutesSaved >= 0);
|
assert.ok(data.minutesSaved >= 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Can get total stats without contributing users", async () => {
|
||||||
|
const result = await client({ url: `${endpoint}?countContributingUsers=false` });
|
||||||
|
const data = result.data;
|
||||||
|
assert.strictEqual(data.userCount, 0);
|
||||||
|
assert.ok(data.activeUsers >= 0);
|
||||||
|
assert.ok(data.apiUsers >= 0);
|
||||||
|
assert.ok(data.viewCount >= 0);
|
||||||
|
assert.ok(data.totalSubmissions >= 0);
|
||||||
|
assert.ok(data.minutesSaved >= 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Can get total stats with old cws method", async () => {
|
||||||
|
const stub = sinon.stub(getCWSUsers, "getCWSUsers");
|
||||||
|
stub.resolves(undefined);
|
||||||
|
const result = await client({ url: `${endpoint}?countContributingUsers=false` });
|
||||||
|
const data = result.data;
|
||||||
|
assert.strictEqual(data.userCount, 0);
|
||||||
|
assert.ok(data.activeUsers >= 0);
|
||||||
|
assert.ok(data.apiUsers >= 0);
|
||||||
|
assert.ok(data.viewCount >= 0);
|
||||||
|
assert.ok(data.totalSubmissions >= 0);
|
||||||
|
assert.ok(data.minutesSaved >= 0);
|
||||||
|
stub.restore();
|
||||||
|
});
|
||||||
});
|
});
|
Loading…
Reference in a new issue