diff --git a/src/routes/getSegmentInfo.ts b/src/routes/getSegmentInfo.ts index d4df1aa..f6625af 100644 --- a/src/routes/getSegmentInfo.ts +++ b/src/routes/getSegmentInfo.ts @@ -34,11 +34,11 @@ async function handleGetSegmentInfo(req: Request, res: Response): Promise 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) { diff --git a/test/cases/eTag.ts b/test/cases/eTag.ts index 3c01c2a..82976e8 100644 --- a/test/cases/eTag.ts +++ b/test/cases/eTag.ts @@ -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)); + }); }); \ No newline at end of file diff --git a/test/cases/getSegmentInfo.ts b/test/cases/getSegmentInfo.ts index 87245cc..d9420c0 100644 --- a/test/cases/getSegmentInfo.ts +++ b/test/cases/getSegmentInfo.ts @@ -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)); + }); }); diff --git a/test/cases/getTopUsers.ts b/test/cases/getTopUsers.ts index 07ef80c..c4f0633 100644 --- a/test/cases/getTopUsers.ts +++ b/test/cases/getTopUsers.ts @@ -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)); + }); }); diff --git a/test/cases/getTotalStats.ts b/test/cases/getTotalStats.ts index f95c292..3b53a84 100644 --- a/test/cases/getTotalStats.ts +++ b/test/cases/getTotalStats.ts @@ -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(); + }); }); \ No newline at end of file