From c34de1baa41db22f59f18345a498f70886076f94 Mon Sep 17 00:00:00 2001 From: Michael C Date: Sun, 25 Sep 2022 03:31:25 -0400 Subject: [PATCH] add 4xx tests --- test/cases/generateVerifyToken.ts | 1 + test/cases/getDaysSavedFormatted.ts | 16 ++++++ test/cases/getLockCategoriesByHash.ts | 72 ++++++++++++++++++++++-- test/cases/getLockReason.ts | 81 ++++++++++++++++++++++++--- test/cases/getTopUsers.ts | 9 +++ 5 files changed, 164 insertions(+), 15 deletions(-) diff --git a/test/cases/generateVerifyToken.ts b/test/cases/generateVerifyToken.ts index fc11cc5..b1f5630 100644 --- a/test/cases/generateVerifyToken.ts +++ b/test/cases/generateVerifyToken.ts @@ -104,6 +104,7 @@ describe("verifyToken mock tests", function() { beforeEach(function() { mock = new MockAdapter(axios, { onNoMatch: "throwException" }); + mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, patreon.fakeOauth); }); afterEach(function () { diff --git a/test/cases/getDaysSavedFormatted.ts b/test/cases/getDaysSavedFormatted.ts index 8414b73..2b2ae23 100644 --- a/test/cases/getDaysSavedFormatted.ts +++ b/test/cases/getDaysSavedFormatted.ts @@ -1,5 +1,7 @@ import assert from "assert"; import { client } from "../utils/httpClient"; +import sinon from "sinon"; +import { db } from "../../src/databases/databases"; const endpoint = "/api/getDaysSavedFormatted"; @@ -8,4 +10,18 @@ describe("getDaysSavedFormatted", () => { const result = await client({ url: endpoint }); assert.ok(result.data.daysSaved >= 0); }); + + it("returns 0 days saved if no segments", async () => { + const stub = sinon.stub(db, "prepare").resolves(undefined); + const result = await client({ url: endpoint }); + assert.ok(result.data.daysSaved >= 0); + stub.restore(); + }); + + it("returns days saved to 2 fixed points", async () => { + const stub = sinon.stub(db, "prepare").resolves({ daysSaved: 1.23456789 }); + const result = await client({ url: endpoint }); + assert.strictEqual(result.data.daysSaved, "1.23"); + stub.restore(); + }); }); \ No newline at end of file diff --git a/test/cases/getLockCategoriesByHash.ts b/test/cases/getLockCategoriesByHash.ts index 9695731..f6b2757 100644 --- a/test/cases/getLockCategoriesByHash.ts +++ b/test/cases/getLockCategoriesByHash.ts @@ -166,17 +166,77 @@ describe("getLockCategoriesByHash", () => { .catch(err => done(err)); }); - it("Should be able to get by actionType", (done) => { - getLockCategories(fakeHash.substring(0,5), [ActionType.Full]) + it("should return 400 if invalid actionTypes", (done) => { + client.get(`${endpoint}/aaaa`, { params: { actionTypes: 3 } }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("should return 400 if invalid actionTypes JSON", (done) => { + client.get(`${endpoint}/aaaa`, { params: { actionTypes: "{3}" } }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get single lock", (done) => { + const videoID = "getLockHash2"; + const hash = getHash(videoID, 1); + getLockCategories(hash.substring(0,6)) .then(res => { assert.strictEqual(res.status, 200); const expected = [{ - videoID: "fakehash-2", - hash: fakeHash, + videoID, + hash, categories: [ - "sponsor" + "preview" ], - reason: "fake2-notshown" + reason: "2-reason" + }]; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get by actionType not in array", (done) => { + const videoID = "getLockHash2"; + const hash = getHash(videoID, 1); + client.get(`${endpoint}/${hash.substring(0,6)}`, { params: { actionType: ActionType.Skip } }) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [{ + videoID, + hash, + categories: [ + "preview" + ], + reason: "2-reason" + }]; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get by no actionType", (done) => { + const videoID = "getLockHash2"; + const hash = getHash(videoID, 1); + client.get(`${endpoint}/${hash.substring(0,6)}`) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [{ + videoID, + hash, + categories: [ + "preview" + ], + reason: "2-reason" }]; assert.deepStrictEqual(res.data, expected); done(); diff --git a/test/cases/getLockReason.ts b/test/cases/getLockReason.ts index 9bbb35b..1b3c36b 100644 --- a/test/cases/getLockReason.ts +++ b/test/cases/getLockReason.ts @@ -55,6 +55,45 @@ describe("getLockReason", () => { .catch(err => done(err)); }); + it("Should be able to get with actionTypes array", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", category: "selfpromo", actionTypes: '["full"]' } }) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [ + { category: "selfpromo", locked: 1, reason: "selfpromo-reason", userID: vipUserID2, userName: vipUserName2 } + ]; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get with actionType", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", category: "selfpromo", actionType: "full" } }) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [ + { category: "selfpromo", locked: 1, reason: "selfpromo-reason", userID: vipUserID2, userName: vipUserName2 } + ]; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to get with actionType array", (done) => { + client.get(endpoint, { params: { videoID: "getLockReason", category: "selfpromo", actionType: ["full"] } }) + .then(res => { + assert.strictEqual(res.status, 200); + const expected = [ + { category: "selfpromo", locked: 1, reason: "selfpromo-reason", userID: vipUserID2, userName: vipUserName2 } + ]; + assert.deepStrictEqual(res.data, expected); + done(); + }) + .catch(err => done(err)); + }); + it("Should be able to get empty locks", (done) => { client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } }) .then(res => { @@ -118,8 +157,10 @@ describe("getLockReason", () => { }) .catch(err => done(err)); }); +}); - it("should return 400 if no videoID specified", (done) => { +describe("getLockReason 400", () => { + it("Should return 400 with missing videoID", (done) => { client.get(endpoint) .then(res => { assert.strictEqual(res.status, 400); @@ -128,15 +169,37 @@ describe("getLockReason", () => { .catch(err => done(err)); }); - it("should be able to get by actionType", (done) => { - client.get(endpoint, { params: { videoID: "getLockReason", actionType: "full" } }) + it("Should return 400 with invalid actionTypes ", (done) => { + client.get(endpoint, { params: { videoID: "valid-videoid", actionTypes: 3 } }) .then(res => { - assert.strictEqual(res.status, 200); - const expected = [ - { category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 }, - { category: "sponsor", locked: 0, reason: "", userID: "", userName: "" } - ]; - partialDeepEquals(res.data, expected); + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should return 400 with invalid actionTypes JSON ", (done) => { + client.get(endpoint, { params: { videoID: "valid-videoid", actionTypes: "{3}" } }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should return 400 with invalid categories", (done) => { + client.get(endpoint, { params: { videoID: "valid-videoid", categories: 3 } }) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + + it("Should return 400 with invalid categories JSON", (done) => { + client.get(endpoint, { params: { videoID: "valid-videoid", categories: "{3}" } }) + .then(res => { + assert.strictEqual(res.status, 400); done(); }) .catch(err => done(err)); diff --git a/test/cases/getTopUsers.ts b/test/cases/getTopUsers.ts index 8808910..07ef80c 100644 --- a/test/cases/getTopUsers.ts +++ b/test/cases/getTopUsers.ts @@ -38,6 +38,15 @@ describe("getTopUsers", () => { .catch(err => done(err)); }); + it("Should return 400 if undefined sortType provided", (done) => { + client.get(endpoint) + .then(res => { + assert.strictEqual(res.status, 400); + done(); + }) + .catch(err => done(err)); + }); + it("Should be able to get by all sortTypes", (done) => { client.get(endpoint, { params: { sortType: 0 } })// minutesSaved .then(res => {