SponsorBlockServer/test/cases/postClearCache.ts

63 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-09-23 00:52:35 +02:00
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
2021-07-12 08:43:46 +02:00
import assert from "assert";
2021-09-23 05:18:31 +02:00
import { client } from "../utils/httpClient";
2021-06-18 23:46:18 +02:00
2021-09-05 01:23:43 +02:00
const VIPUser = "clearCacheVIP";
const regularUser = "regular-user";
2021-09-23 05:18:31 +02:00
const endpoint = "/api/clearCache";
const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } });
2021-09-05 01:23:43 +02:00
2021-07-12 08:43:46 +02:00
describe("postClearCache", () => {
2021-06-18 23:46:18 +02:00
before(async () => {
2021-09-05 01:23:43 +02:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('${getHash(VIPUser)}')`);
const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden") VALUES';
await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`);
2021-06-18 23:46:18 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to clear cache for existing video", (done) => {
postClearCache(VIPUser, "clear-test")
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
2021-06-18 23:46:18 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should be able to clear cache for nonexistent video", (done) => {
postClearCache(VIPUser, "dne-video")
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
2021-06-18 23:46:18 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should get 403 as non-vip", (done) => {
postClearCache(regularUser, "clear-test")
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
2021-06-18 23:46:18 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should give 400 with missing videoID", (done) => {
client.post(endpoint, { params: { userID: VIPUser } })
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-18 23:46:18 +02:00
});
2021-09-23 05:18:31 +02:00
it("Should give 400 with missing userID", (done) => {
client.post(endpoint, { params: { videoID: "clear-test" } })
2021-07-12 08:43:46 +02:00
.then(async res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-06-18 23:46:18 +02:00
});
});