diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 0e9def1..2286a7f 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -131,7 +131,7 @@ function sendWebhooks(voteData: VoteData) { } } -function categoryVote(UUID: string, userID: string, isVIP: boolean, category: any, hashedIP: string, res: Response) { +function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) { // Check if they've already made a vote const previousVoteInfo = privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]); @@ -198,7 +198,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, category: an //TODO: In the future, raise this number from zero to make it harder to change categories // VIPs change it every time - if (nextCategoryCount - currentCategoryCount >= (submissionInfo ? Math.max(Math.ceil(submissionInfo.votes / 2), 1) : 1) || isVIP) { + if (nextCategoryCount - currentCategoryCount >= (submissionInfo ? Math.max(Math.ceil(submissionInfo.votes / 2), 1) : 1) || isVIP || isOwnSubmission) { // Replace the category db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]); } @@ -247,7 +247,7 @@ async function voteOnSponsorTime(req: Request, res: Response) { } if (type === undefined && category !== undefined) { - return categoryVote(UUID, nonAnonUserID, isVIP, category, hashedIP, res); + return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, res); } if (type == 1 && !isVIP && !isOwnSubmission) { diff --git a/test/cases/voteOnSponsorTime.ts b/test/cases/voteOnSponsorTime.ts index 8e95094..11c0f70 100644 --- a/test/cases/voteOnSponsorTime.ts +++ b/test/cases/voteOnSponsorTime.ts @@ -30,6 +30,7 @@ describe('voteOnSponsorTime', () => { db.exec(startOfQuery + "('vote-testtesttest,test', 1, 11, 100, 'vote-uuid-3', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-testtesttest,test', 1) + "')"); db.exec(startOfQuery + "('vote-test3', 1, 11, 2, 'vote-uuid-4', 'testman', 0, 50, 'sponsor', 0, '" + getHash('vote-test3', 1) + "')"); db.exec(startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); + db.exec(startOfQuery + "('vote-test3', 7, 22, -3, 'vote-uuid-5_1', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-test3', 1) + "')"); db.exec(startOfQuery + "('vote-multiple', 1, 11, 2, 'vote-uuid-6', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); db.exec(startOfQuery + "('vote-multiple', 20, 33, 2, 'vote-uuid-7', 'testman', 0, 50, 'intro', 0, '" + getHash('vote-multiple', 1) + "')"); db.exec(startOfQuery + "('voter-submitter', 1, 11, 2, 'vote-uuid-8', '" + getHash("randomID") + "', 0, 50, 'sponsor', 0, '" + getHash('voter-submitter', 1) + "')"); @@ -323,6 +324,24 @@ describe('voteOnSponsorTime', () => { }); }); + it('Submitter should be able to vote for a category and it should immediately change', (done: Done) => { + request.get(getbaseURL() + + "/api/voteOnSponsorTime?userID=testman&UUID=vote-uuid-5_1&category=outro", null, + (err, res) => { + if (err) done(err); + else if (res.statusCode === 200) { + let row = db.prepare('get', "SELECT category FROM sponsorTimes WHERE UUID = ?", ["vote-uuid-5"]); + if (row.category === "outro") { + done(); + } else { + done("Vote did not succeed. Submission went from intro to " + row.category + "."); + } + } else { + done("Status code was " + res.statusCode); + } + }); + }); + it('Should not be able to category-vote on an invalid UUID submission', (done: Done) => { request.get(getbaseURL() + "/api/voteOnSponsorTime?userID=randomID3&UUID=invalid-uuid&category=intro", null,