|
1 | 1 | import { db } from "../../src/databases/databases";
|
2 |
| -import { getHash } from "../../src/utils/getHash"; |
3 | 2 | import assert from "assert";
|
4 | 3 | import { client } from "../utils/httpClient";
|
| 4 | +import { genUsers, User } from "../utils/genUser"; |
| 5 | +import { insertSegment, insertVip } from "../utils/queryGen"; |
5 | 6 |
|
6 |
| -const VIPUser = "clearCacheVIP"; |
7 |
| -const regularUser = "regular-user"; |
8 | 7 | const endpoint = "/api/clearCache";
|
9 |
| -const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } }); |
| 8 | +const postClearCache = (user: User, videoID: string) => client({ method: "post", url: endpoint, params: { userID: user.privID, videoID } }); |
| 9 | + |
| 10 | +const cases = [ |
| 11 | + "vip", |
| 12 | + "normal", |
| 13 | +]; |
| 14 | +const users = genUsers("postClearCache", cases); |
10 | 15 |
|
11 | 16 | describe("postClearCache", () => {
|
12 | 17 | before(async () => {
|
13 |
| - await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('${getHash(VIPUser)}')`); |
14 |
| - const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden") VALUES'; |
15 |
| - await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`); |
| 18 | + await insertVip(db, users["vip"].pubID); |
| 19 | + await insertSegment(db, "clearSegments", "clear-test"); |
16 | 20 | });
|
17 | 21 |
|
18 |
| - it("Should be able to clear cache for existing video", (done) => { |
19 |
| - postClearCache(VIPUser, "clear-test") |
20 |
| - .then(res => { |
21 |
| - assert.strictEqual(res.status, 200); |
22 |
| - done(); |
23 |
| - }) |
24 |
| - .catch(err => done(err)); |
25 |
| - }); |
| 22 | + it("Should be able to clear cache for existing video", () => |
| 23 | + postClearCache(users["vip"], "clear-test") |
| 24 | + .then(res => assert.strictEqual(res.status, 200)) |
| 25 | + ); |
26 | 26 |
|
27 |
| - it("Should be able to clear cache for nonexistent video", (done) => { |
28 |
| - postClearCache(VIPUser, "dne-video") |
29 |
| - .then(res => { |
30 |
| - assert.strictEqual(res.status, 200); |
31 |
| - done(); |
32 |
| - }) |
33 |
| - .catch(err => done(err)); |
34 |
| - }); |
| 27 | + it("Should be able to clear cache for nonexistent video", () => |
| 28 | + postClearCache(users["vip"], "dne-video") |
| 29 | + .then(res => assert.strictEqual(res.status, 200)) |
| 30 | + ); |
35 | 31 |
|
36 |
| - it("Should get 403 as non-vip", (done) => { |
37 |
| - postClearCache(regularUser, "clear-test") |
38 |
| - .then(res => { |
39 |
| - assert.strictEqual(res.status, 403); |
40 |
| - done(); |
41 |
| - }) |
42 |
| - .catch(err => done(err)); |
43 |
| - }); |
| 32 | + it("Should get 403 as non-vip", () => |
| 33 | + postClearCache(users["normal"], "clear-test") |
| 34 | + .then(res => assert.strictEqual(res.status, 403)) |
| 35 | + ); |
44 | 36 |
|
45 |
| - it("Should give 400 with missing videoID", (done) => { |
46 |
| - client.post(endpoint, { params: { userID: VIPUser } }) |
47 |
| - .then(res => { |
48 |
| - assert.strictEqual(res.status, 400); |
49 |
| - done(); |
50 |
| - }) |
51 |
| - .catch(err => done(err)); |
52 |
| - }); |
| 37 | + it("Should give 400 with missing videoID", () => |
| 38 | + client.post(endpoint, { params: { userID: users["vip"].privID } }) |
| 39 | + .then(res => assert.strictEqual(res.status, 400)) |
| 40 | + ); |
53 | 41 |
|
54 |
| - it("Should give 400 with missing userID", (done) => { |
| 42 | + it("Should give 400 with missing userID", () => |
55 | 43 | client.post(endpoint, { params: { videoID: "clear-test" } })
|
56 |
| - .then(res => { |
57 |
| - assert.strictEqual(res.status, 400); |
58 |
| - done(); |
59 |
| - }) |
60 |
| - .catch(err => done(err)); |
61 |
| - }); |
| 44 | + .then(res => assert.strictEqual(res.status, 400)) |
| 45 | + ); |
62 | 46 | });
|
0 commit comments