add more tests for coverage

This commit is contained in:
Michael C 2023-02-20 22:21:46 -05:00
parent d04230a1c4
commit 72fb4eb6ec
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
5 changed files with 66 additions and 4 deletions

View file

@ -34,11 +34,11 @@ async function handleGetSegmentInfo(req: Request, res: Response): Promise<DBSegm
// deduplicate with set
UUIDs = [ ...new Set(UUIDs)];
// if more than 10 entries, slice
if (UUIDs.length > 10) UUIDs = UUIDs.slice(0, 10);
if (!Array.isArray(UUIDs) || !UUIDs) {
if (!Array.isArray(UUIDs) || !UUIDs?.length) {
res.status(400).send("UUIDs parameter does not match format requirements.");
return;
}
if (UUIDs.length > 10) UUIDs = UUIDs.slice(0, 10);
const DBSegments = await getSegmentsByUUID(UUIDs);
// all uuids failed lookup
if (!DBSegments?.length) {

View file

@ -33,7 +33,7 @@ describe("304 etag validation", () => {
if (!config.redis?.enabled) this.skip();
});
const endpoint = "/api/skipSegments";
const endpoint = "/etag";
for (const hashType of ["skipSegments", "skipSegmentsHash", "videoLabel", "videoLabelHash"]) {
it(`${hashType} etag should return 304`, (done) => {
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));
});
});

View file

@ -338,4 +338,13 @@ describe("getSegmentInfo", () => {
})
.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));
});
});

View file

@ -81,4 +81,14 @@ describe("getTopUsers", () => {
})
.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));
});
});

View file

@ -1,5 +1,7 @@
import assert from "assert";
import { client } from "../utils/httpClient";
import sinon from "sinon";
import * as getCWSUsers from "../../src/utils/getCWSUsers";
const endpoint = "/api/getTotalStats";
@ -7,11 +9,36 @@ describe("getTotalStats", () => {
it("Can get total stats", async () => {
const result = await client({ url: endpoint });
const data = result.data;
assert.ok(data?.userCount ?? true);
assert.ok(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 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();
});
});