SponsorBlockServer/test/cases/tempVip.ts

258 lines
10 KiB
TypeScript
Raw Normal View History

2021-12-31 10:26:37 +01:00
import { config } from "../../src/config";
import { getHash } from "../../src/utils/getHash";
import { tempVIPKey } from "../../src/utils/redisKeys";
import { HashedUserID } from "../../src/types/user.model";
import { client } from "../utils/httpClient";
import { db, privateDB } from "../../src/databases/databases";
import redis from "../../src/utils/redis";
import assert from "assert";
// helpers
const getSegment = (UUID: string) => db.prepare("get", `SELECT "votes", "locked", "category" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
const permVIP1 = "tempVip_permaVIPOne";
const publicPermVIP1 = getHash(permVIP1) as HashedUserID;
const permVIP2 = "tempVip_permaVIPTwo";
const publicPermVIP2 = getHash(permVIP2) as HashedUserID;
2021-12-31 10:26:37 +01:00
const tempVIPOne = "tempVipTempOne";
const publicTempVIPOne = getHash(tempVIPOne) as HashedUserID;
const UUID0 = "tempvip-uuid0";
const UUID1 = "tempvip-uuid1";
const tempVIPEndpoint = "/api/addUserAsTempVIP";
const addTempVIP = (enabled: string, adminUserID: string, userID: HashedUserID, channelVideoID = "channelid-convert") => client({
2021-12-31 10:26:37 +01:00
url: tempVIPEndpoint,
method: "POST",
params: {
userID,
adminUserID,
channelVideoID,
enabled
2021-12-31 10:26:37 +01:00
}
});
const voteEndpoint = "/api/voteOnSponsorTime";
const postVote = (userID: string, UUID: string, type: number) => client({
method: "POST",
url: voteEndpoint,
params: {
userID,
UUID,
type
}
});
const postVoteCategory = (userID: string, UUID: string, category: string) => client({
method: "POST",
url: voteEndpoint,
params: {
userID,
UUID,
category
}
});
const checkUserVIP = async (publicID: HashedUserID): Promise<string> =>
await redis.get(tempVIPKey(publicID));
2021-12-31 10:26:37 +01:00
describe("tempVIP test", function() {
before(async function() {
if (!config.redis?.enabled) this.skip();
2021-12-31 10:26:37 +01:00
const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
await db.prepare("run", insertSponsorTimeQuery, ["channelid-convert", 0, 1, 0, 0, UUID0, "testman", 0, 50, "sponsor", 0]);
await db.prepare("run", insertSponsorTimeQuery, ["channelid-convert", 1, 9, 0, 1, "tempvip-submit", publicTempVIPOne, 0, 50, "sponsor", 0]);
await db.prepare("run", insertSponsorTimeQuery, ["otherchannel", 1, 9, 0, 1, UUID1, "testman", 0, 50, "sponsor", 0]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP1]);
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [publicPermVIP2]);
2021-12-31 10:26:37 +01:00
// clear redis if running consecutive tests
await redis.del(tempVIPKey(publicTempVIPOne));
2021-12-31 10:26:37 +01:00
});
it("Should update db version when starting the application", () => {
privateDB.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])
.then(row => {
assert.ok(row.value >= 5, `Versions are not at least 5. private is ${row.value}`);
});
});
it("User should not already be temp VIP", (done) => {
checkUserVIP(publicTempVIPOne)
2021-12-31 10:26:37 +01:00
.then(result => {
assert.ok(!result);
})
.then(async () => {
2021-12-31 23:12:46 +01:00
const row = await privateDB.prepare("get", `SELECT * FROM "tempVipLog" WHERE "targetUserID" = ?`, [publicTempVIPOne]);
assert.ok(!row?.enabled);
done();
2021-12-31 10:26:37 +01:00
})
.catch(err => done(err));
});
it("Should be able to normal upvote as a user", (done) => {
postVote(tempVIPOne, UUID0, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0);
assert.strictEqual(row.votes, 1);
done();
})
.catch(err => done(err));
});
it("Should be able to add tempVIP", (done) => {
addTempVIP("true", permVIP1, publicTempVIPOne)
2021-12-31 10:26:37 +01:00
.then(async res => {
assert.strictEqual(res.status, 200);
// check redis
const vip = await checkUserVIP(publicTempVIPOne);
assert.strictEqual(vip, "ChannelID");
assert.strictEqual(res.data, "Temp VIP added on channel ChannelAuthor");
// check privateDB
2021-12-31 23:12:46 +01:00
const row = await privateDB.prepare("get", `SELECT * FROM "tempVipLog" WHERE "targetUserID" = ?`, [publicTempVIPOne]);
assert.ok(row.enabled);
2021-12-31 10:26:37 +01:00
done();
})
.catch(err => done(err));
});
it("Should be able to VIP downvote", (done) => {
postVote(tempVIPOne, UUID0, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0);
assert.strictEqual(row.votes, -2);
done();
})
.catch(err => done(err));
});
2022-02-03 19:49:13 +01:00
it("Should not be able to lock segment", (done) => {
2021-12-31 10:26:37 +01:00
postVote(tempVIPOne, UUID0, 1)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0);
2022-02-03 19:51:15 +01:00
assert.strictEqual(row.votes, 1);
2022-02-03 19:49:13 +01:00
assert.strictEqual(row.locked, 0);
2021-12-31 10:26:37 +01:00
done();
})
.catch(err => done(err));
});
2022-02-03 19:49:13 +01:00
it("Should be able to change category but not lock", (done) => {
2021-12-31 10:26:37 +01:00
postVoteCategory(tempVIPOne, UUID0, "filler")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID0);
assert.strictEqual(row.category, "filler");
2022-02-03 19:49:13 +01:00
assert.strictEqual(row.locked, 0);
2021-12-31 10:26:37 +01:00
done();
})
.catch(err => done(err));
});
it("Should be able to remove tempVIP prematurely", (done) => {
addTempVIP("false", permVIP1, publicTempVIPOne)
2021-12-31 10:26:37 +01:00
.then(async res => {
assert.strictEqual(res.status, 200);
const vip = await checkUserVIP(publicTempVIPOne);
assert.strictEqual(res.data, "Temp VIP removed");
assert.ok(!vip, "Should be no listed channelID");
done();
2021-12-31 10:26:37 +01:00
})
.catch(err => done(err));
});
it("Should not be able to VIP downvote", (done) => {
postVote(tempVIPOne, UUID1, 0)
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID1);
assert.strictEqual(row.votes, 0);
done();
})
.catch(err => done(err));
});
it("Should not be able to VIP change category", (done) => {
postVoteCategory(tempVIPOne, UUID1, "filler")
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await getSegment(UUID1);
assert.strictEqual(row.category, "sponsor");
done();
})
.catch(err => done(err));
});
// error code testing
it("Should be able to add tempVIP after removal", (done) => {
addTempVIP("true", permVIP1, publicTempVIPOne)
.then(async res => {
assert.strictEqual(res.status, 200);
const vip = await checkUserVIP(publicTempVIPOne);
assert.strictEqual(vip, "ChannelID");
done();
})
.catch(err => done(err));
});
it("Should not be able to add VIP without existing VIP (403)", (done) => {
const privateID = "non-vip-privateid";
addTempVIP("true", privateID, publicTempVIPOne)
.then(async res => {
assert.strictEqual(res.status, 403);
const vip = await checkUserVIP(getHash(privateID) as HashedUserID);
assert.ok(!vip, "Should be no listed channelID");
done();
})
.catch(err => done(err));
});
it("Should not be able to add permanant VIP as temporary VIP (409)", (done) => {
addTempVIP("true", permVIP1, publicPermVIP2)
.then(async res => {
assert.strictEqual(res.status, 409);
const vip = await checkUserVIP(publicPermVIP2);
assert.ok(!vip, "Should be no listed channelID");
done();
})
.catch(err => done(err));
});
it("Temp VIP should not be able to add another Temp VIP (403)", (done) => {
const privateID = "non-vip-privateid";
const publicID = getHash(privateID) as HashedUserID;
addTempVIP("true", tempVIPOne, publicID)
.then(async res => {
assert.strictEqual(res.status, 403);
const vip = await checkUserVIP(publicID);
assert.ok(!vip, "Should be no listed channelID");
done();
})
.catch(err => done(err));
});
// error 40X testing
it("Should return 404 with invalid videoID", (done) => {
const privateID = "non-vip-privateid";
const publicID = getHash(privateID) as HashedUserID;
addTempVIP("true", permVIP1, publicID, "knownWrongID")
.then(async res => {
assert.strictEqual(res.status, 404);
const vip = await checkUserVIP(publicID);
assert.ok(!vip, "Should be no listed channelID");
done();
})
.catch(err => done(err));
});
it("Should return 400 with invalid userID", (done) => {
addTempVIP("true", permVIP1, "" as HashedUserID, "knownWrongID")
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should return 400 with invalid adminUserID", (done) => {
addTempVIP("true", "", publicTempVIPOne)
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should return 400 with invalid channelID", (done) => {
addTempVIP("true", permVIP1, publicTempVIPOne, "")
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
2021-12-31 10:26:37 +01:00
});