add segment generator

- getIsUserVIP
- postClearCache
- update boilerplate
This commit is contained in:
Michael C
2023-09-27 22:21:42 -04:00
parent 964634dc51
commit a9ef3815e2
5 changed files with 141 additions and 99 deletions

View File

@@ -1,62 +1,46 @@
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import assert from "assert";
import { client } from "../utils/httpClient";
import { genUsers, User } from "../utils/genUser";
import { insertSegment, insertVip } from "../utils/queryGen";
const VIPUser = "clearCacheVIP";
const regularUser = "regular-user";
const endpoint = "/api/clearCache";
const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } });
const postClearCache = (user: User, videoID: string) => client({ method: "post", url: endpoint, params: { userID: user.privID, videoID } });
const cases = [
"vip",
"normal",
];
const users = genUsers("postClearCache", cases);
describe("postClearCache", () => {
before(async () => {
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)`);
await insertVip(db, users["vip"].pubID);
await insertSegment(db, "clearSegments", "clear-test");
});
it("Should be able to clear cache for existing video", (done) => {
postClearCache(VIPUser, "clear-test")
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to clear cache for existing video", () =>
postClearCache(users["vip"], "clear-test")
.then(res => assert.strictEqual(res.status, 200))
);
it("Should be able to clear cache for nonexistent video", (done) => {
postClearCache(VIPUser, "dne-video")
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to clear cache for nonexistent video", () =>
postClearCache(users["vip"], "dne-video")
.then(res => assert.strictEqual(res.status, 200))
);
it("Should get 403 as non-vip", (done) => {
postClearCache(regularUser, "clear-test")
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
});
it("Should get 403 as non-vip", () =>
postClearCache(users["normal"], "clear-test")
.then(res => assert.strictEqual(res.status, 403))
);
it("Should give 400 with missing videoID", (done) => {
client.post(endpoint, { params: { userID: VIPUser } })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should give 400 with missing videoID", () =>
client.post(endpoint, { params: { userID: users["vip"].privID } })
.then(res => assert.strictEqual(res.status, 400))
);
it("Should give 400 with missing userID", (done) => {
it("Should give 400 with missing userID", () =>
client.post(endpoint, { params: { videoID: "clear-test" } })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
.then(res => assert.strictEqual(res.status, 400))
);
});