remaining tests

This commit is contained in:
Michael C
2021-09-22 23:18:31 -04:00
parent a028eaa41a
commit 4e50f0ab4b
9 changed files with 743 additions and 762 deletions

View File

@@ -1,17 +1,17 @@
import fetch from "node-fetch";
import { config } from "../../src/config";
import { db } from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { getHash } from "../../src/utils/getHash";
import { ImportMock } from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
import { YouTubeApiMock } from "../youtubeMock";
import assert from "assert";
import { client } from "../utils/httpClient";
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI");
const sinonStub = mockManager.mock("listVideos");
sinonStub.callsFake(YouTubeApiMock.listVideos);
const vipUser = "VIPUser";
const randomID2 = "randomID2";
describe("voteOnSponsorTime", () => {
before(async () => {
@@ -35,7 +35,7 @@ describe("voteOnSponsorTime", () => {
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, "vote-uuid-6", "testman", 0, 50, "intro", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, "vote-uuid-7", "testman", 0, 50, "intro", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-9", getHash("randomID2"), 0, 50, "sponsor", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-9", getHash(randomID2), 0, 50, "sponsor", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", 0, 0]);
await db.prepare("run", insertSponsorTimeQuery, ["own-submission-video", 1, 11, 500, "own-submission-uuid", getHash("own-submission-id"), 0, 50, "sponsor", 0, 0]);
@@ -59,19 +59,38 @@ describe("voteOnSponsorTime", () => {
await db.prepare("run", insertWarningQuery, [warnUser02Hash, (now - (warningExpireTime + 2000)), warnVip01Hash, 1]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash("VIPUser")]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]);
await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ["no-sponsor-segments-video", "someUser", "sponsor"]);
});
// constants
const endpoint = `${getbaseURL()}/api/voteOnSponsorTime`;
const endpoint = "/api/voteOnSponsorTime";
const postVote = (userID: string, UUID: string, type: number) => client({
method: "POST",
url: endpoint,
params: {
userID,
UUID,
type
}
});
const postVoteCategory = (userID: string, UUID: string, category: string) => client({
method: "POST",
url: endpoint,
params: {
userID,
UUID,
category
}
});
const getSegmentVotes = (UUID: string) => db.prepare("get", `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
const getSegmentCategory = (UUID: string) => db.prepare("get", `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
it("Should be able to upvote a segment", (done: Done) => {
it("Should be able to upvote a segment", (done) => {
const UUID = "vote-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`)
postVote("randomID", UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -81,9 +100,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should be able to downvote a segment", (done: Done) => {
it("Should be able to downvote a segment", (done) => {
const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`)
postVote(randomID2, UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -93,9 +112,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to downvote the same segment when voting from a different user on the same IP", (done: Done) => {
it("Should not be able to downvote the same segment when voting from a different user on the same IP", (done) => {
const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&type=0`)
postVote("randomID3", UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -105,75 +124,81 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to downvote a segment if the user is shadow banned", (done: Done) => {
fetch(`${endpoint}?userID=randomID4&UUID=vote-uuid-1.6&type=0`)
it("Should not be able to downvote a segment if the user is shadow banned", (done) => {
const UUID = "vote-uuid-1.6";
postVote("randomID4", UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1.6");
const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 10);
done();
})
.catch(err => done(err));
});
it("Should not be able to upvote a segment if the user hasn't submitted yet", (done: Done) => {
fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1`)
it("Should not be able to upvote a segment if the user hasn't submitted yet", (done) => {
const UUID = "vote-uuid-1";
postVote("hasNotSubmittedID", UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1");
const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 2);
done();
})
.catch(err => done(err));
});
it("Should not be able to downvote a segment if the user hasn't submitted yet", (done: Done) => {
fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0`)
it("Should not be able to downvote a segment if the user hasn't submitted yet", (done) => {
const UUID = "vote-uuid-1.5";
postVote("hasNotSubmittedID", UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1.5");
const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 10);
done();
})
.catch(err => done(err));
});
it("VIP should be able to completely downvote a segment", (done: Done) => {
fetch(`${endpoint}?userID=VIPUser&UUID=vote-uuid-3&type=0`)
it("VIP should be able to completely downvote a segment", (done) => {
const UUID = "vote-uuid-3";
postVote(vipUser, UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-3");
const row = await getSegmentVotes(UUID);
assert.ok(row.votes <= -2);
done();
})
.catch(err => done(err));
});
it("should be able to completely downvote your own segment", (done: Done) => {
fetch(`${endpoint}?userID=own-submission-id&UUID=own-submission-uuid&type=0`)
it("should be able to completely downvote your own segment", (done) => {
const UUID = "own-submission-uuid";
postVote("own-submission-id", UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("own-submission-uuid");
const row = await getSegmentVotes(UUID);
assert.ok(row.votes <= -2);
done();
})
.catch(err => done(err));
});
it("should not be able to completely downvote somebody elses segment", (done: Done) => {
fetch(`${endpoint}?userID=randomID2&UUID=not-own-submission-uuid&type=0`)
it("should not be able to completely downvote somebody elses segment", (done) => {
const UUID = "not-own-submission-uuid";
postVote(randomID2, UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("not-own-submission-uuid");
const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 499);
done();
})
.catch(err => done(err));
});
it("Should be able to vote for a category and it should add your vote to the database", (done: Done) => {
it("Should be able to vote for a category and it should add your vote to the database", (done) => {
const UUID = "vote-uuid-4";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=intro`)
postVoteCategory(randomID2, UUID, "intro")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID);
@@ -189,9 +214,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not able to change to an invalid category", (done: Done) => {
it("Should not able to change to an invalid category", (done) => {
const UUID = "incorrect-category";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=fakecategory`)
postVoteCategory(randomID2, UUID, "fakecategory")
.then(async res => {
assert.strictEqual(res.status, 400);
const row = await getSegmentCategory(UUID);
@@ -201,9 +226,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not able to change to highlight category", (done: Done) => {
it("Should not able to change to highlight category", (done) => {
const UUID = "incorrect-category";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=highlight`)
postVoteCategory(randomID2, UUID, "highlight")
.then(async res => {
assert.strictEqual(res.status, 400);
const row = await getSegmentCategory(UUID);
@@ -213,39 +238,36 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should be able to change your vote for a category and it should add your vote to the database", (done: Done) => {
it("Should be able to change your vote for a category and it should add your vote to the database", (done) => {
const UUID = "vote-uuid-4";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=outro`)
postVoteCategory(randomID2, UUID, "outro")
.then(async res => {
if (res.status === 200) {
const submissionRow = await getSegmentCategory(UUID);
const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]);
let introVotes = 0;
let outroVotes = 0;
let sponsorVotes = 0;
for (const row of categoryRows) {
if (row?.category === "intro") introVotes += row?.votes;
if (row?.category === "outro") outroVotes += row?.votes;
if (row?.category === "sponsor") sponsorVotes += row?.votes;
}
assert.strictEqual(submissionRow.category, "sponsor");
assert.strictEqual(categoryRows.length, 3);
assert.strictEqual(introVotes, 0);
assert.strictEqual(outroVotes, 1);
assert.strictEqual(sponsorVotes, 1);
done();
} else {
done(`Status code was ${res.status}`);
assert.strictEqual(res.status, 200, "Status code should be 200");
const submissionRow = await getSegmentCategory(UUID);
const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]);
let introVotes = 0;
let outroVotes = 0;
let sponsorVotes = 0;
for (const row of categoryRows) {
if (row?.category === "intro") introVotes += row?.votes;
if (row?.category === "outro") outroVotes += row?.votes;
if (row?.category === "sponsor") sponsorVotes += row?.votes;
}
assert.strictEqual(submissionRow.category, "sponsor");
assert.strictEqual(categoryRows.length, 3);
assert.strictEqual(introVotes, 0);
assert.strictEqual(outroVotes, 1);
assert.strictEqual(sponsorVotes, 1);
done();
})
.catch(err => done(err));
});
it("Should not be able to change your vote to an invalid category", (done: Done) => {
it("Should not be able to change your vote to an invalid category", (done) => {
const UUID = "incorrect-category-change";
const vote = (inputCat: string, assertCat: string, callback: Done) => {
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=${inputCat}`)
const vote = (inputCat: string, assertCat: string, callback: Mocha.Done) => {
postVoteCategory(randomID2, UUID, inputCat)
.then(async () => {
const row = await getSegmentCategory(UUID);
assert.strictEqual(row.category, assertCat);
@@ -259,9 +281,9 @@ describe("voteOnSponsorTime", () => {
});
it("VIP should be able to vote for a category and it should immediately change", (done: Done) => {
it("VIP should be able to vote for a category and it should immediately change", (done) => {
const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&category=outro`)
postVoteCategory(vipUser, UUID, "outro")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID);
@@ -273,9 +295,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Submitter should be able to vote for a category and it should immediately change", (done: Done) => {
it("Submitter should be able to vote for a category and it should immediately change", (done) => {
const UUID = "vote-uuid-5_1";
fetch(`${endpoint}?userID=testman&UUID=${UUID}&category=outro`)
postVoteCategory("testman", UUID, "outro")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentCategory("vote-uuid-5");
@@ -285,9 +307,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to category-vote on an invalid UUID submission", (done: Done) => {
it("Should not be able to category-vote on an invalid UUID submission", (done) => {
const UUID = "invalid-uuid";
fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&category=intro`)
postVoteCategory("randomID3", UUID, "intro")
.then(async res => {
assert.strictEqual(res.status, 400);
done();
@@ -295,9 +317,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it('Non-VIP should not be able to upvote "dead" submission', (done: Done) => {
it('Non-VIP should not be able to upvote "dead" submission', (done) => {
const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=1`)
postVote(randomID2, UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 403);
const row = await getSegmentVotes(UUID);
@@ -307,9 +329,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it('Non-VIP should not be able to downvote "dead" submission', (done: Done) => {
it('Non-VIP should not be able to downvote "dead" submission', (done) => {
const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`)
postVote(randomID2, UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -319,9 +341,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it('VIP should be able to upvote "dead" submission', (done: Done) => {
it('VIP should be able to upvote "dead" submission', (done) => {
const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`)
postVote(vipUser, UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -331,9 +353,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to upvote a segment (Too many warning)", (done: Done) => {
it("Should not be able to upvote a segment (Too many warning)", (done) => {
const UUID = "warnvote-uuid-0";
fetch(`${endpoint}?userID=warn-voteuser01&UUID=${UUID}&type=1`)
postVote("warn-voteuser01", UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 403);
done();
@@ -341,9 +363,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Non-VIP should not be able to downvote on a segment with no-segments category", (done: Done) => {
it("Non-VIP should not be able to downvote on a segment with no-segments category", (done) => {
const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=0`)
postVote("randomID", UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -353,9 +375,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Non-VIP should be able to upvote on a segment with no-segments category", (done: Done) => {
it("Non-VIP should be able to upvote on a segment with no-segments category", (done) => {
const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`)
postVote("randomID", UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -365,9 +387,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Non-VIP should not be able to category vote on a segment with no-segments category", (done: Done) => {
it("Non-VIP should not be able to category vote on a segment with no-segments category", (done) => {
const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&category=outro`)
postVoteCategory("randomID", UUID, "outro")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID);
@@ -377,9 +399,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("VIP upvote should lock segment", (done: Done) => {
it("VIP upvote should lock segment", (done) => {
const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`)
postVote(vipUser, UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -389,9 +411,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("VIP downvote should unlock segment", (done: Done) => {
it("VIP downvote should unlock segment", (done) => {
const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=0`)
postVote(vipUser, UUID, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -401,9 +423,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("VIP upvote should unhide segment", (done: Done) => {
it("VIP upvote should unhide segment", (done) => {
const UUID = "segment-hidden-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`)
postVote(vipUser, UUID, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -413,9 +435,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should be able to undo-vote a segment", (done: Done) => {
it("Should be able to undo-vote a segment", (done) => {
const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=20`)
postVote(randomID2, UUID, 20)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID);
@@ -425,9 +447,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to vote with type 10", (done: Done) => {
it("Should not be able to vote with type 10", (done) => {
const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=10`)
postVote(vipUser, UUID, 10)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@@ -435,9 +457,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err));
});
it("Should not be able to vote with type 11", (done: Done) => {
it("Should not be able to vote with type 11", (done) => {
const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=11`)
postVote(vipUser, UUID, 11)
.then(res => {
assert.strictEqual(res.status, 400);
done();