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,13 +1,12 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
const VIPUser = "clearCacheVIP"; const VIPUser = "clearCacheVIP";
const regularUser = "regular-user"; const regularUser = "regular-user";
const endpoint = `${getbaseURL()}/api/clearCache`; const endpoint = "/api/clearCache";
const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } });
describe("postClearCache", () => { describe("postClearCache", () => {
before(async () => { before(async () => {
@@ -16,10 +15,8 @@ describe("postClearCache", () => {
await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`); await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`);
}); });
it("Should be able to clear cache for existing video", (done: Done) => { it("Should be able to clear cache for existing video", (done) => {
fetch(`${endpoint}?userID=${VIPUser}&videoID=clear-test`, { postClearCache(VIPUser, "clear-test")
method: "POST"
})
.then(res => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
done(); done();
@@ -27,10 +24,8 @@ describe("postClearCache", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to clear cache for nonexistent video", (done: Done) => { it("Should be able to clear cache for nonexistent video", (done) => {
fetch(`${endpoint}?userID=${VIPUser}&videoID=dne-video`, { postClearCache(VIPUser, "dne-video")
method: "POST"
})
.then(res => { .then(res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
done(); done();
@@ -38,10 +33,8 @@ describe("postClearCache", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should get 403 as non-vip", (done: Done) => { it("Should get 403 as non-vip", (done) => {
fetch(`${endpoint}?userID=${regularUser}&videoID=clear-tes`, { postClearCache(regularUser, "clear-test")
method: "POST"
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
done(); done();
@@ -49,10 +42,8 @@ describe("postClearCache", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should give 400 with missing videoID", (done: Done) => { it("Should give 400 with missing videoID", (done) => {
fetch(`${endpoint}?userID=${VIPUser}`, { client.post(endpoint, { params: { userID: VIPUser } })
method: "POST"
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
@@ -60,10 +51,8 @@ describe("postClearCache", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should give 400 with missing userID", (done: Done) => { it("Should give 400 with missing userID", (done) => {
fetch(`${endpoint}?userID=${VIPUser}`, { client.post(endpoint, { params: { videoID: "clear-test" } })
method: "POST"
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();

View File

@@ -1,10 +1,8 @@
import fetch from "node-fetch";
import { Done, postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import { IDatabase } from "../../src/databases/IDatabase"; import { IDatabase } from "../../src/databases/IDatabase";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) { async function dbSponsorTimesAdd(db: IDatabase, videoID: string, startTime: number, endTime: number, UUID: string, category: string) {
const votes = 0, const votes = 0,
@@ -34,7 +32,8 @@ async function dbSponsorTimesCompareExpect(db: IDatabase, videoId: string, expec
describe("postPurgeAllSegments", function () { describe("postPurgeAllSegments", function () {
const privateVipUserID = "VIPUser-purgeAll"; const privateVipUserID = "VIPUser-purgeAll";
const vipUserID = getHash(privateVipUserID); const vipUserID = getHash(privateVipUserID);
const endpoint = `${getbaseURL()}/api/purgeAllSegments`; const endpoint = "/api/purgeAllSegments";
const postSegmentShift = (videoID: string, userID: string) => client.post(endpoint, { videoID, userID });
before(async function () { before(async function () {
// startTime and endTime get set in beforeEach for consistency // startTime and endTime get set in beforeEach for consistency
@@ -46,29 +45,17 @@ describe("postPurgeAllSegments", function () {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]); await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]);
}); });
it("Reject non-VIP user", function (done: Done) { it("Reject non-VIP user", function (done) {
fetch(endpoint, { postSegmentShift("vsegpurge01", "segshift_randomuser001")
...postJSON, .then(res => {
body: JSON.stringify({
videoID: "vsegpurge01",
userID: "segshift_randomuser001",
}),
})
.then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Purge all segments success", function (done: Done) { it("Purge all segments success", function (done) {
fetch(endpoint, { postSegmentShift("vsegpurge01", privateVipUserID)
...postJSON,
body: JSON.stringify({
videoID: "vsegpurge01",
userID: privateVipUserID,
}),
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
done(await dbSponsorTimesCompareExpect(db, "vsegpurge01", 1) || await dbSponsorTimesCompareExpect(db, "vseg-not-purged01", 0)); done(await dbSponsorTimesCompareExpect(db, "vsegpurge01", 1) || await dbSponsorTimesCompareExpect(db, "vseg-not-purged01", 0));
@@ -76,9 +63,9 @@ describe("postPurgeAllSegments", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should return 400 if missing body", function (done: Done) { it("Should return 400 if missing body", function (done) {
fetch(endpoint, { ...postJSON }) client.post(endpoint, {})
.then(async res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
}) })

File diff suppressed because it is too large Load Diff

View File

@@ -1,30 +1,25 @@
import fetch from "node-fetch";
import { Done, postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals"; import { partialDeepEquals } from "../utils/partialDeepEquals";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
describe("postWarning", () => { describe("postWarning", () => {
// constants // constants
const endpoint = `${getbaseURL()}/api/warnUser`; const endpoint = "/api/warnUser";
const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]); const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]);
before(async () => { before(async () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]); await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]);
}); });
it("Should be able to create warning if vip (exp 200)", (done: Done) => { it("Should be able to create warning if vip (exp 200)", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: "warning-vip",
userID: "warning-0", userID: "warning-0",
reason: "warning-reason-0" reason: "warning-reason-0"
}; };
fetch(endpoint, { client.post(endpoint, json)
...postJSON,
body: JSON.stringify(json),
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getWarning(json.userID); const row = await getWarning(json.userID);
@@ -39,16 +34,13 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be not be able to create a duplicate warning if vip", (done: Done) => { it("Should be not be able to create a duplicate warning if vip", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: "warning-vip",
userID: "warning-0", userID: "warning-0",
}; };
fetch(endpoint, { client.post(endpoint, json)
...postJSON,
body: JSON.stringify(json),
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 409); assert.strictEqual(res.status, 409);
const row = await getWarning(json.userID); const row = await getWarning(json.userID);
@@ -62,17 +54,14 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to remove warning if vip", (done: Done) => { it("Should be able to remove warning if vip", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: "warning-vip",
userID: "warning-0", userID: "warning-0",
enabled: false enabled: false
}; };
fetch(endpoint, { client.post(endpoint, json)
...postJSON,
body: JSON.stringify(json),
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getWarning(json.userID); const row = await getWarning(json.userID);
@@ -85,16 +74,13 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should not be able to create warning if not vip (exp 403)", (done: Done) => { it("Should not be able to create warning if not vip (exp 403)", (done) => {
const json = { const json = {
issuerUserID: "warning-not-vip", issuerUserID: "warning-not-vip",
userID: "warning-1", userID: "warning-1",
}; };
fetch(endpoint, { client.post(endpoint, json)
...postJSON,
body: JSON.stringify(json),
})
.then(res => { .then(res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
done(); done();
@@ -102,8 +88,8 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should return 400 if missing body", (done: Done) => { it("Should return 400 if missing body", (done) => {
fetch(endpoint, postJSON) client.post(endpoint, {})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
@@ -111,17 +97,14 @@ describe("postWarning", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should re-enable disabled warning", (done: Done) => { it("Should re-enable disabled warning", (done) => {
const json = { const json = {
issuerUserID: "warning-vip", issuerUserID: "warning-vip",
userID: "warning-0", userID: "warning-0",
enabled: true enabled: true
}; };
fetch(endpoint, { client.post(endpoint, json)
...postJSON,
body: JSON.stringify(json),
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const data = await getWarning(json.userID); const data = await getWarning(json.userID);

View File

@@ -1,10 +1,8 @@
import fetch from "node-fetch";
import { Done, postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import { IDatabase } from "../../src/databases/IDatabase"; import { IDatabase } from "../../src/databases/IDatabase";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
describe("segmentShift", function () { describe("segmentShift", function () {
// functions // functions
@@ -42,7 +40,12 @@ describe("segmentShift", function () {
// constants // constants
const privateVipUserID = "VIPUser-segmentShift"; const privateVipUserID = "VIPUser-segmentShift";
const vipUserID = getHash(privateVipUserID); const vipUserID = getHash(privateVipUserID);
const endpoint = `${getbaseURL()}/api/segmentShift`; const endpoint = "/api/segmentShift";
const postSegmentShift = async (data: Record<string, any>) => client({
method: "POST",
url: endpoint,
data,
});
before(async function () { before(async function () {
// startTime and endTime get set in beforeEach for consistency // startTime and endTime get set in beforeEach for consistency
@@ -61,15 +64,12 @@ describe("segmentShift", function () {
await dbSponsorTimesSetByUUID(db, "vsegshifttest01uuid04", 120, 140); await dbSponsorTimesSetByUUID(db, "vsegshifttest01uuid04", 120, 140);
}); });
it("Reject none VIP user", function (done: Done) { it("Reject non VIP user", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: "segshift_randomuser001",
videoID: "vsegshift01", startTime: 20,
userID: "segshift_randomuser001", endTime: 30,
startTime: 20,
endTime: 30,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
@@ -78,15 +78,12 @@ describe("segmentShift", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Shift is outside segments", function (done: Done) { it("Shift is outside segments", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: privateVipUserID,
videoID: "vsegshift01", startTime: 20,
userID: privateVipUserID, endTime: 30,
startTime: 20,
endTime: 30,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -112,15 +109,12 @@ describe("segmentShift", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Shift is inside segment", function (done: Done) { it("Shift is inside segment", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: privateVipUserID,
videoID: "vsegshift01", startTime: 65,
userID: privateVipUserID, endTime: 75,
startTime: 65,
endTime: 75,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -146,15 +140,12 @@ describe("segmentShift", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Shift is overlaping startTime of segment", function (done: Done) { it("Shift is overlaping startTime of segment", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: privateVipUserID,
videoID: "vsegshift01", startTime: 32,
userID: privateVipUserID, endTime: 42,
startTime: 32,
endTime: 42,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -180,15 +171,12 @@ describe("segmentShift", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Shift is overlaping endTime of segment", function (done: Done) { it("Shift is overlaping endTime of segment", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: privateVipUserID,
videoID: "vsegshift01", startTime: 85,
userID: privateVipUserID, endTime: 95,
startTime: 85,
endTime: 95,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -214,15 +202,12 @@ describe("segmentShift", function () {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Shift is overlaping segment", function (done: Done) { it("Shift is overlaping segment", (done) => {
fetch(endpoint, { postSegmentShift({
...postJSON, videoID: "vsegshift01",
body: JSON.stringify({ userID: privateVipUserID,
videoID: "vsegshift01", startTime: 35,
userID: privateVipUserID, endTime: 55,
startTime: 35,
endTime: 55,
}),
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);

View File

@@ -1,9 +1,7 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { db, privateDB } from "../../src/databases/databases"; import { db, privateDB } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
const adminPrivateUserID = "testUserId"; const adminPrivateUserID = "testUserId";
const user00PrivateUserID = "setUsername_00"; const user00PrivateUserID = "setUsername_00";
@@ -52,7 +50,7 @@ function wellFormatUserName(userName: string) {
return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, ""); return userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
} }
async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Done) { async function testUserNameChangelog(userID: string, newUserName: string, oldUserName: string, byAdmin: boolean, done: Mocha.Done) {
const log = await getLastLogUserNameChange(userID); const log = await getLastLogUserNameChange(userID);
assert.strictEqual(newUserName, log.newUserName); assert.strictEqual(newUserName, log.newUserName);
assert.strictEqual(oldUserName, log.oldUserName); assert.strictEqual(oldUserName, log.oldUserName);
@@ -60,8 +58,27 @@ async function testUserNameChangelog(userID: string, newUserName: string, oldUse
return done(); return done();
} }
const endpoint = "/api/setUsername";
const postSetUserName = (userID: string, username: string) => client({
method: "POST",
url: endpoint,
params: {
userID,
username,
}
});
const postSetUserNameAdmin = (userID: string, username: string, adminUserID: string) => client({
method: "POST",
url: endpoint,
params: {
userID,
username,
adminUserID,
}
});
describe("setUsername", () => { describe("setUsername", () => {
const endpoint = `${getbaseURL()}/api/setUsername`;
before(async () => { before(async () => {
await addUsername(getHash(user01PrivateUserID), username01, 0); await addUsername(getHash(user01PrivateUserID), username01, 0);
await addUsername(getHash(user02PrivateUserID), username02, 0); await addUsername(getHash(user02PrivateUserID), username02, 0);
@@ -72,157 +89,147 @@ describe("setUsername", () => {
await addUsername(getHash(user07PrivateUserID), username07, 1); await addUsername(getHash(user07PrivateUserID), username07, 1);
}); });
it("Should be able to set username that has never been set", (done: Done) => { it("Should be able to set username that has never been set", (done) => {
fetch(`${endpoint}?userID=${user00PrivateUserID}&username=${username00}`, { postSetUserName(user00PrivateUserID, username00)
method: "POST",
})
.then(async res => { .then(async res => {
console.log(res.data);
const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user00PrivateUserID));
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
assert.strictEqual(usernameInfo.userName, username00); assert.strictEqual(usernameInfo.userName, username00);
assert.notStrictEqual(usernameInfo.locked, 1, "username should not be locked"); assert.notStrictEqual(usernameInfo.locked, 1, "username should not be locked");
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Should return 200", (done: Done) => { it("Should return 200", (done) => {
fetch(`${endpoint}?userID=${user01PrivateUserID}&username=Changed%20Username`, { const username = "Changed%20Username";
method: "POST", postSetUserName(user01PrivateUserID, username)
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
testUserNameChangelog(user01PrivateUserID, decodeURIComponent("Changed%20Username"), username01, false, done); testUserNameChangelog(user01PrivateUserID, username, username01, false, done);
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it('Should return 400 for missing param "userID"', (done: Done) => { it('Should return 400 for missing param "userID"', (done) => {
fetch(`${endpoint}?username=MyUsername`, { client({
method: "POST", method: "POST",
url: endpoint,
data: {
userName: "MyUsername"
}
}) })
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it('Should return 400 for missing param "username"', (done: Done) => { it('Should return 400 for missing param "username"', (done) => {
fetch(`${endpoint}?userID=test`, { client({
method: "POST", method: "POST",
url: endpoint,
data: {
userID: "test"
}
}) })
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it('Should return 400 for "username" longer then 64 characters', (done: Done) => { it('Should return 400 for "username" longer then 64 characters', (done) => {
const username65 = "0000000000000000000000000000000000000000000000000000000000000000X"; const username65 = "0000000000000000000000000000000000000000000000000000000000000000X";
fetch(`${endpoint}?userID=test&username=${encodeURIComponent(username65)}`, { postSetUserName("test", username65)
method: "POST",
})
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it('Should not change username if it contains "discord"', (done: Done) => { it('Should not change username if it contains "discord"', (done) => {
const newUsername = "discord.me"; const newUsername = "discord.me";
fetch(`${endpoint}?userID=${user02PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { postSetUserName(user02PrivateUserID, newUsername)
method: "POST",
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID)); const userNameInfo = await getUsernameInfo(getHash(user02PrivateUserID));
assert.notStrictEqual(userNameInfo.userName, newUsername); assert.notStrictEqual(userNameInfo.userName, newUsername);
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Should be able to change username", (done: Done) => { it("Should be able to change username", (done) => {
const newUsername = "newUsername"; const newUsername = "newUsername";
fetch(`${endpoint}?userID=${user03PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { postSetUserName(user03PrivateUserID, newUsername)
method: "POST",
})
.then(async () => { .then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user03PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "Username should change"); assert.strictEqual(usernameInfo.userName, newUsername, "Username should change");
assert.notStrictEqual(usernameInfo.locked, 1, "Username should not be locked"); assert.notStrictEqual(usernameInfo.locked, 1, "Username should not be locked");
testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done); testUserNameChangelog(user03PrivateUserID, newUsername, username03, false, done);
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Should not be able to change locked username", (done: Done) => { it("Should not be able to change locked username", (done) => {
const newUsername = "newUsername"; const newUsername = "newUsername";
fetch(`${endpoint}?userID=${user04PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { postSetUserName(user04PrivateUserID, newUsername)
method: "POST",
})
.then(async () => { .then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user04PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user04PrivateUserID));
assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not be changed"); assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not be changed");
assert.strictEqual(usernameInfo.locked, 1, "username should be locked"); assert.strictEqual(usernameInfo.locked, 1, "username should be locked");
done(); done();
}) })
.catch((err) => done(`couldn't call endpoint: ${err}`)); .catch((err) => done(err));
}); });
it("Should filter out unicode control characters", (done: Done) => { it("Should filter out unicode control characters", (done) => {
const newUsername = "This\nUsername+has\tInvalid+Characters"; const newUsername = "This\nUsername+has\tInvalid+Characters";
fetch(`${endpoint}?userID=${user05PrivateUserID}&username=${encodeURIComponent(newUsername)}`, { postSetUserName(user05PrivateUserID, newUsername)
method: "POST",
})
.then(async () => { .then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user05PrivateUserID));
assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not contain control characters"); assert.notStrictEqual(usernameInfo.userName, newUsername, "Username should not contain control characters");
testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done); testUserNameChangelog(user05PrivateUserID, wellFormatUserName(newUsername), username05, false, done);
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Incorrect adminUserID should return 403", (done: Done) => { it("Incorrect adminUserID should return 403", (done) => {
const newUsername = "New Username"; const newUsername = "New Username";
fetch(`${endpoint}?adminUserID=invalidAdminID&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername,"invalidAdminID")
method: "POST", .then(res => {
})
.then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
done(); done();
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Admin should be able to change username", (done: Done) => { it("Admin should be able to change username", (done) => {
const newUsername = "New Username"; const newUsername = "New Username";
fetch(`${endpoint}?adminUserID=${adminPrivateUserID}&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { postSetUserNameAdmin(getHash(user06PrivateUserID), newUsername, adminPrivateUserID)
method: "POST",
})
.then(async () => { .then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "username should be changed"); assert.strictEqual(usernameInfo.userName, newUsername, "username should be changed");
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked"); assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done); testUserNameChangelog(user06PrivateUserID, newUsername, username06, true, done);
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
it("Admin should be able to change locked username", (done: Done) => { it("Admin should be able to change locked username", (done) => {
const newUsername = "New Username"; const newUsername = "New Username";
fetch(`${endpoint}?adminUserID=${adminPrivateUserID}&userID=${getHash(user07PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, { postSetUserNameAdmin(getHash(user07PrivateUserID), newUsername, adminPrivateUserID)
method: "POST",
})
.then(async () => { .then(async () => {
const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID)); const usernameInfo = await getUsernameInfo(getHash(user06PrivateUserID));
assert.strictEqual(usernameInfo.userName, newUsername, "Username should be changed"); assert.strictEqual(usernameInfo.userName, newUsername, "Username should be changed");
assert.strictEqual(usernameInfo.locked, 1, "Username should be locked"); assert.strictEqual(usernameInfo.locked, 1, "Username should be locked");
testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done); testUserNameChangelog(user07PrivateUserID, newUsername, username07, true, done);
}) })
.catch(() => done(`couldn't call endpoint`)); .catch((err) => done(err));
}); });
}); });

View File

@@ -1,17 +1,15 @@
import fetch from "node-fetch";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import assert from "assert"; import assert from "assert";
import { Category } from "../../src/types/segments.model"; import { Category } from "../../src/types/segments.model";
import { client } from "../utils/httpClient";
describe("shadowBanUser", () => { describe("shadowBanUser", () => {
const endpoint = `${getbaseURL()}/api/shadowBanUser`;
const getShadowBan = (userID: string) => db.prepare("get", `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]); const getShadowBan = (userID: string) => db.prepare("get", `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, [userID]);
const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]); const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]); const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
const endpoint = "/api/shadowBanUser";
const VIPuserID = "shadow-ban-vip"; const VIPuserID = "shadow-ban-vip";
before(async () => { before(async () => {
@@ -36,10 +34,15 @@ describe("shadowBanUser", () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]); await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]);
}); });
it("Should be able to ban user and hide submissions", (done: Done) => { it("Should be able to ban user and hide submissions", (done) => {
const userID = "shadowBanned"; const userID = "shadowBanned";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -52,10 +55,17 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to unban user without unhiding submissions", (done: Done) => { it("Should be able to unban user without unhiding submissions", (done) => {
const userID = "shadowBanned"; const userID = "shadowBanned";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false&unHideOldSubmissions=false`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
unHideOldSubmissions: false,
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -68,10 +78,16 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to ban user and hide submissions from only some categories", (done: Done) => { it("Should be able to ban user and hide submissions from only some categories", (done) => {
const userID = "shadowBanned2"; const userID = "shadowBanned2";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&categories=["sponsor"]`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
categories: `["sponsor"]`
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -85,10 +101,16 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to unban user and unhide submissions", (done: Done) => { it("Should be able to unban user and unhide submissions", (done) => {
const userID = "shadowBanned2"; const userID = "shadowBanned2";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -101,10 +123,17 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to unban user and unhide some submissions", (done: Done) => { it("Should be able to unban user and unhide some submissions", (done) => {
const userID = "shadowBanned3"; const userID = "shadowBanned3";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=false&categories=["sponsor"]`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: false,
categories: `["sponsor"]`
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
@@ -118,10 +147,18 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should get 409 when re-shadowbanning user", (done: Done) => { it("Should get 409 when re-shadowbanning user", (done) => {
const userID = "shadowBanned4"; const userID = "shadowBanned4";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=true&categories=["sponsor"]&unHideOldSubmissions=false`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: true,
categories: `["sponsor"]`,
unHideOldSubmissions: false
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 409); assert.strictEqual(res.status, 409);
@@ -135,10 +172,18 @@ describe("shadowBanUser", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should be able to re-shadowban user to hide old submissions", (done: Done) => { it("Should be able to re-shadowban user to hide old submissions", (done) => {
const userID = "shadowBanned4"; const userID = "shadowBanned4";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuserID}&enabled=true&categories=["sponsor"]&unHideOldSubmissions=true`, { client({
method: "POST" method: "POST",
url: endpoint,
params: {
userID,
adminUserID: VIPuserID,
enabled: true,
categories: `["sponsor"]`,
unHideOldSubmissions: true
}
}) })
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);

View File

@@ -1,13 +1,20 @@
import fetch from "node-fetch";
import { postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { client } from "../utils/httpClient";
import assert from "assert"; import assert from "assert";
describe("unBan", () => { describe("unBan", () => {
const endpoint = `${getbaseURL()}/api/shadowBanUser`; const endpoint = "/api/shadowBanUser";
const VIPuser = "VIPUser-unBan"; const VIPuser = "VIPUser-unBan";
const postUnBan = (userID: string, adminUserID: string, enabled: boolean) => client({
url: endpoint,
method: "POST",
params: {
userID,
adminUserID,
enabled
}
});
const videoIDUnBanCheck = (videoID: string, userID: string, status: number) => db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', [videoID, userID, status]); const videoIDUnBanCheck = (videoID: string, userID: string, status: number) => db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "videoID" = ? AND "userID" = ? AND "shadowHidden" = ?', [videoID, userID, status]);
before(async () => { before(async () => {
const insertShadowBannedUserQuery = 'INSERT INTO "shadowBannedUsers" VALUES(?)'; const insertShadowBannedUserQuery = 'INSERT INTO "shadowBannedUsers" VALUES(?)';
@@ -30,9 +37,7 @@ describe("unBan", () => {
it("Should be able to unban a user and re-enable shadow banned segments", (done) => { it("Should be able to unban a user and re-enable shadow banned segments", (done) => {
const userID = "testMan-unBan"; const userID = "testMan-unBan";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { postUnBan(userID, VIPuser, false)
...postJSON
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await videoIDUnBanCheck("unBan-videoID-0", userID, 1); const result = await videoIDUnBanCheck("unBan-videoID-0", userID, 1);
@@ -44,9 +49,7 @@ describe("unBan", () => {
it("Should be able to unban a user and re-enable shadow banned segments without lockCategories entrys", (done) => { it("Should be able to unban a user and re-enable shadow banned segments without lockCategories entrys", (done) => {
const userID = "testWoman-unBan"; const userID = "testWoman-unBan";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { postUnBan(userID, VIPuser, false)
...postJSON
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await videoIDUnBanCheck("unBan-videoID-1", userID, 1); const result = await videoIDUnBanCheck("unBan-videoID-1", userID, 1);
@@ -58,9 +61,7 @@ describe("unBan", () => {
it("Should be able to unban a user and re-enable shadow banned segments with a mix of lockCategories entrys", (done) => { it("Should be able to unban a user and re-enable shadow banned segments with a mix of lockCategories entrys", (done) => {
const userID = "testEntity-unBan"; const userID = "testEntity-unBan";
fetch(`${endpoint}?userID=${userID}&adminUserID=${VIPuser}&enabled=false`, { postUnBan(userID, VIPuser, false)
...postJSON
})
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const result = await db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?', [userID, 1]); const result = await db.prepare("all", 'SELECT * FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?', [userID, 1]);

View File

@@ -1,17 +1,17 @@
import fetch from "node-fetch";
import { config } from "../../src/config"; import { config } from "../../src/config";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { getHash } from "../../src/utils/getHash"; import { getHash } from "../../src/utils/getHash";
import { ImportMock } from "ts-mock-imports"; import { ImportMock } from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi"; import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
import { YouTubeApiMock } from "../youtubeMock"; import { YouTubeApiMock } from "../youtubeMock";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient";
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI"); const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI");
const sinonStub = mockManager.mock("listVideos"); const sinonStub = mockManager.mock("listVideos");
sinonStub.callsFake(YouTubeApiMock.listVideos); sinonStub.callsFake(YouTubeApiMock.listVideos);
const vipUser = "VIPUser";
const randomID2 = "randomID2";
describe("voteOnSponsorTime", () => { describe("voteOnSponsorTime", () => {
before(async () => { 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", 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, ["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-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-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, ["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]); 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", 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 "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ["no-sponsor-segments-video", "someUser", "sponsor"]); await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ["no-sponsor-segments-video", "someUser", "sponsor"]);
}); });
// constants // 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 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]); 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"; const UUID = "vote-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`) postVote("randomID", UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -81,9 +100,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`) postVote(randomID2, UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -93,9 +112,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&type=0`) postVote("randomID3", UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -105,75 +124,81 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should not be able to downvote a segment if the user is shadow banned", (done: Done) => { it("Should not be able to downvote a segment if the user is shadow banned", (done) => {
fetch(`${endpoint}?userID=randomID4&UUID=vote-uuid-1.6&type=0`) const UUID = "vote-uuid-1.6";
postVote("randomID4", UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1.6"); const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 10); assert.strictEqual(row.votes, 10);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should not be able to upvote a segment if the user hasn't submitted yet", (done: Done) => { it("Should not be able to upvote a segment if the user hasn't submitted yet", (done) => {
fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1&type=1`) const UUID = "vote-uuid-1";
postVote("hasNotSubmittedID", UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1"); const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 2); assert.strictEqual(row.votes, 2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("Should not be able to downvote a segment if the user hasn't submitted yet", (done: Done) => { it("Should not be able to downvote a segment if the user hasn't submitted yet", (done) => {
fetch(`${endpoint}?userID=hasNotSubmittedID&UUID=vote-uuid-1.5&type=0`) const UUID = "vote-uuid-1.5";
postVote("hasNotSubmittedID", UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-1.5"); const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 10); assert.strictEqual(row.votes, 10);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("VIP should be able to completely downvote a segment", (done: Done) => { it("VIP should be able to completely downvote a segment", (done) => {
fetch(`${endpoint}?userID=VIPUser&UUID=vote-uuid-3&type=0`) const UUID = "vote-uuid-3";
postVote(vipUser, UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("vote-uuid-3"); const row = await getSegmentVotes(UUID);
assert.ok(row.votes <= -2); assert.ok(row.votes <= -2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("should be able to completely downvote your own segment", (done: Done) => { it("should be able to completely downvote your own segment", (done) => {
fetch(`${endpoint}?userID=own-submission-id&UUID=own-submission-uuid&type=0`) const UUID = "own-submission-uuid";
postVote("own-submission-id", UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("own-submission-uuid"); const row = await getSegmentVotes(UUID);
assert.ok(row.votes <= -2); assert.ok(row.votes <= -2);
done(); done();
}) })
.catch(err => done(err)); .catch(err => done(err));
}); });
it("should not be able to completely downvote somebody elses segment", (done: Done) => { it("should not be able to completely downvote somebody elses segment", (done) => {
fetch(`${endpoint}?userID=randomID2&UUID=not-own-submission-uuid&type=0`) const UUID = "not-own-submission-uuid";
postVote(randomID2, UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes("not-own-submission-uuid"); const row = await getSegmentVotes(UUID);
assert.strictEqual(row.votes, 499); assert.strictEqual(row.votes, 499);
done(); done();
}) })
.catch(err => done(err)); .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"; const UUID = "vote-uuid-4";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=intro`) postVoteCategory(randomID2, UUID, "intro")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
@@ -189,9 +214,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "incorrect-category";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=fakecategory`) postVoteCategory(randomID2, UUID, "fakecategory")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
@@ -201,9 +226,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "incorrect-category";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=highlight`) postVoteCategory(randomID2, UUID, "highlight")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
@@ -213,39 +238,36 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-4";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=outro`) postVoteCategory(randomID2, UUID, "outro")
.then(async res => { .then(async res => {
if (res.status === 200) { assert.strictEqual(res.status, 200, "Status code should be 200");
const submissionRow = await getSegmentCategory(UUID); const submissionRow = await getSegmentCategory(UUID);
const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]); const categoryRows = await db.prepare("all", `SELECT votes, category FROM "categoryVotes" WHERE "UUID" = ?`, [UUID]);
let introVotes = 0; let introVotes = 0;
let outroVotes = 0; let outroVotes = 0;
let sponsorVotes = 0; let sponsorVotes = 0;
for (const row of categoryRows) { for (const row of categoryRows) {
if (row?.category === "intro") introVotes += row?.votes; if (row?.category === "intro") introVotes += row?.votes;
if (row?.category === "outro") outroVotes += row?.votes; if (row?.category === "outro") outroVotes += row?.votes;
if (row?.category === "sponsor") sponsorVotes += 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(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)); .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 UUID = "incorrect-category-change";
const vote = (inputCat: string, assertCat: string, callback: Done) => { const vote = (inputCat: string, assertCat: string, callback: Mocha.Done) => {
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&category=${inputCat}`) postVoteCategory(randomID2, UUID, inputCat)
.then(async () => { .then(async () => {
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
assert.strictEqual(row.category, assertCat); 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"; const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&category=outro`) postVoteCategory(vipUser, UUID, "outro")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
@@ -273,9 +295,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-5_1";
fetch(`${endpoint}?userID=testman&UUID=${UUID}&category=outro`) postVoteCategory("testman", UUID, "outro")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentCategory("vote-uuid-5"); const row = await getSegmentCategory("vote-uuid-5");
@@ -285,9 +307,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "invalid-uuid";
fetch(`${endpoint}?userID=randomID3&UUID=${UUID}&category=intro`) postVoteCategory("randomID3", UUID, "intro")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
@@ -295,9 +317,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=1`) postVote(randomID2, UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -307,9 +329,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=0`) postVote(randomID2, UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -319,9 +341,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-5";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) postVote(vipUser, UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -331,9 +353,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "warnvote-uuid-0";
fetch(`${endpoint}?userID=warn-voteuser01&UUID=${UUID}&type=1`) postVote("warn-voteuser01", UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 403); assert.strictEqual(res.status, 403);
done(); done();
@@ -341,9 +363,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=0`) postVote("randomID", UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -353,9 +375,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&type=1`) postVote("randomID", UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -365,9 +387,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "no-sponsor-segments-uuid-0";
fetch(`${endpoint}?userID=randomID&UUID=${UUID}&category=outro`) postVoteCategory("randomID", UUID, "outro")
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentCategory(UUID); const row = await getSegmentCategory(UUID);
@@ -377,9 +399,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) postVote(vipUser, UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -389,9 +411,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=0`) postVote(vipUser, UUID, 0)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const row = await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -401,9 +423,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "segment-hidden-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=1`) postVote(vipUser, UUID, 1)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]); const row = await db.prepare("get", `SELECT "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
@@ -413,9 +435,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "vote-uuid-2";
fetch(`${endpoint}?userID=randomID2&UUID=${UUID}&type=20`) postVote(randomID2, UUID, 20)
.then(async res => { .then(async res => {
assert.strictEqual(res.status, 200); assert.strictEqual(res.status, 200);
const row = await getSegmentVotes(UUID); const row = await getSegmentVotes(UUID);
@@ -425,9 +447,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=10`) postVote(vipUser, UUID, 10)
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();
@@ -435,9 +457,9 @@ describe("voteOnSponsorTime", () => {
.catch(err => done(err)); .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"; const UUID = "segment-locking-uuid-1";
fetch(`${endpoint}?userID=VIPUser&UUID=${UUID}&type=11`) postVote(vipUser, UUID, 11)
.then(res => { .then(res => {
assert.strictEqual(res.status, 400); assert.strictEqual(res.status, 400);
done(); done();