add 4xx tests

This commit is contained in:
Michael C
2022-09-25 03:31:25 -04:00
parent a469f2f382
commit c34de1baa4
5 changed files with 164 additions and 15 deletions

View File

@@ -104,6 +104,7 @@ describe("verifyToken mock tests", function() {
beforeEach(function() { beforeEach(function() {
mock = new MockAdapter(axios, { onNoMatch: "throwException" }); mock = new MockAdapter(axios, { onNoMatch: "throwException" });
mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, patreon.fakeOauth);
}); });
afterEach(function () { afterEach(function () {

View File

@@ -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 { db } from "../../src/databases/databases";
const endpoint = "/api/getDaysSavedFormatted"; const endpoint = "/api/getDaysSavedFormatted";
@@ -8,4 +10,18 @@ describe("getDaysSavedFormatted", () => {
const result = await client({ url: endpoint }); const result = await client({ url: endpoint });
assert.ok(result.data.daysSaved >= 0); 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();
});
}); });

View File

@@ -166,17 +166,77 @@ describe("getLockCategoriesByHash", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to get by actionType", (done) => { it("should return 400 if invalid actionTypes", (done) => {
getLockCategories(fakeHash.substring(0,5), [ActionType.Full]) 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 => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const expected = [{ const expected = [{
videoID: "fakehash-2", videoID,
hash: fakeHash, hash,
categories: [ 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); assert.deepStrictEqual(res.data, expected);
done(); done();

View File

@@ -55,6 +55,45 @@ describe("getLockReason", () => {
.catch(err => done(err)); .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) => { it("Should be able to get empty locks", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } }) client.get(endpoint, { params: { videoID: "getLockReason", category: "intro" } })
.then(res => { .then(res => {
@@ -118,8 +157,10 @@ describe("getLockReason", () => {
}) })
.catch(err => done(err)); .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) client.get(endpoint)
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
@@ -128,15 +169,37 @@ describe("getLockReason", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("should be able to get by actionType", (done) => { it("Should return 400 with invalid actionTypes ", (done) => {
client.get(endpoint, { params: { videoID: "getLockReason", actionType: "full" } }) client.get(endpoint, { params: { videoID: "valid-videoid", actionTypes: 3 } })
.then(res => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 400);
const expected = [ done();
{ category: "selfpromo", locked: 1, reason: "sponsor-reason", userID: vipUserID2, userName: vipUserName2 }, })
{ category: "sponsor", locked: 0, reason: "", userID: "", userName: "" } .catch(err => done(err));
]; });
partialDeepEquals(res.data, expected);
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(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));

View File

@@ -38,6 +38,15 @@ describe("getTopUsers", () => {
.catch(err => done(err)); .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) => { it("Should be able to get by all sortTypes", (done) => {
client.get(endpoint, { params: { sortType: 0 } })// minutesSaved client.get(endpoint, { params: { sortType: 0 } })// minutesSaved
.then(res => { .then(res => {