Skip to content

Commit a9ef381

Browse files
committed
add segment generator
- getIsUserVIP - postClearCache - update boilerplate
1 parent 964634d commit a9ef381

File tree

5 files changed

+141
-99
lines changed

5 files changed

+141
-99
lines changed

test/case_boilerplate.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import { db } from "../../src/databases/databases";
2+
import assert from "assert";
3+
import { client } from "../utils/httpClient";
4+
import { genUsers, User } from "../utils/genUser";
5+
import { insertSegment, insertVip } from "../utils/queryGen";
6+
17
const endpoint = "/api/endpoint";
28

39
const postTestEndpoint = () => client({

test/cases/getIsUserVIP.ts

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,42 @@
11
import { db } from "../../src/databases/databases";
2-
import { getHash } from "../../src/utils/getHash";
2+
import { genUsers, User } from "../utils/genUser";
33
import { client } from "../utils/httpClient";
44
import assert from "assert";
5+
import { insertVip } from "../utils/queryGen";
56

6-
const VIPUser = "isUserVIPVIP";
7-
const normalUser = "isUserVIPNormal";
87
const endpoint = "/api/isUserVIP";
98
const vipUserRequest = (userID: string) => client.get(endpoint, { params: { userID } });
9+
const checkVipStatus = (user: User, expected: boolean) =>
10+
vipUserRequest(user.privID)
11+
.then(res => {
12+
assert.strictEqual(res.status, 200);
13+
assert.strictEqual(res.data.vip, expected);
14+
});
15+
16+
const cases = [
17+
"vip",
18+
"normal",
19+
];
20+
const users = genUsers("endpoint", cases);
1021

1122
describe("getIsUserVIP", () => {
12-
before(() => {
13-
db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(VIPUser)]);
23+
before(async () => {
24+
await insertVip(db, users["vip"].pubID);
1425
});
1526

16-
it("Should be able to get a 200", (done) => {
17-
vipUserRequest(VIPUser)
18-
.then(res => {
19-
assert.strictEqual(res.status, 200, "response should be 200");
20-
done();
21-
})
22-
.catch(err => done(err));
23-
});
27+
// status checks
28+
it("Should be able to get a 200", () =>
29+
vipUserRequest(users["vip"].privID)
30+
.then(res => assert.strictEqual(res.status, 200))
31+
);
2432

2533

26-
it("Should get a 400 if no userID", (done) => {
34+
it("Should get a 400 if no userID", () =>
2735
client.get(endpoint)
28-
.then(res => {
29-
assert.strictEqual(res.status, 400, "response should be 400");
30-
done();
31-
})
32-
.catch(err => done(err));
33-
});
36+
.then(res => assert.strictEqual(res.status, 400, "response should be 400"))
37+
);
3438

35-
it("Should say a VIP is a VIP", (done) => {
36-
vipUserRequest(VIPUser)
37-
.then(res => {
38-
assert.strictEqual(res.status, 200);
39-
assert.strictEqual(res.data.vip, true);
40-
done();
41-
})
42-
.catch(err => done(err));
43-
});
44-
45-
it("Should say a normal user is not a VIP", (done) => {
46-
vipUserRequest(normalUser)
47-
.then(res => {
48-
assert.strictEqual(res.status, 200);
49-
assert.strictEqual(res.data.vip, false);
50-
done();
51-
})
52-
.catch(err => done(err));
53-
});
39+
// user checks
40+
it("Should say a VIP is a VIP", () => checkVipStatus(users["vip"], true));
41+
it("Should say a normal user is not a VIP", () => checkVipStatus(users["normal"], false));
5442
});

test/cases/postClearCache.ts

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,46 @@
11
import { db } from "../../src/databases/databases";
2-
import { getHash } from "../../src/utils/getHash";
32
import assert from "assert";
43
import { client } from "../utils/httpClient";
4+
import { genUsers, User } from "../utils/genUser";
5+
import { insertSegment, insertVip } from "../utils/queryGen";
56

6-
const VIPUser = "clearCacheVIP";
7-
const regularUser = "regular-user";
87
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);
1015

1116
describe("postClearCache", () => {
1217
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");
1620
});
1721

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+
);
2626

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+
);
3531

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+
);
4436

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+
);
5341

54-
it("Should give 400 with missing userID", (done) => {
42+
it("Should give 400 with missing userID", () =>
5543
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+
);
6246
});

test/utils/queryGen.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@ import { HashedUserID } from "../../src/types/user.model";
33
import { User, userArray, usernameUserArray } from "./genUser";
44
import { Feature } from "../../src/types/user.model";
55

6-
// usernames
7-
export const insertUsername = async (db: IDatabase, userID: HashedUserID, userName: string, locked = false) => {
8-
const query = 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)';
9-
const lockedValue = Number(locked);
10-
await db.prepare("run", query, [userID, userName, lockedValue]);
11-
};
12-
13-
export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray) => {
14-
for (const user of Object.values(users))
15-
await insertUsername(db, user.pubID, user.username, false);
16-
};
6+
// segments
7+
export { insertSegment } from "./segmentQueryGen";
178

189
// vip
1910
export const insertVip = async (db: IDatabase, userID: HashedUserID) => {
2011
const query = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
2112
await db.prepare("run", query, [userID]);
2213
};
23-
2414
export const insertVipBulk = async (db: IDatabase, users: userArray) => {
2515
for (const user of Object.values(users))
2616
await insertVip(db, user.pubID);
@@ -31,8 +21,18 @@ export const grantFeature = async (db: IDatabase, target: HashedUserID, feature:
3121
const query = 'INSERT INTO "userFeatures" ("userID", "feature", "issuerUserID", "timeSubmitted") VALUES(?, ?, ?, ?)';
3222
await db.prepare("run", query, [target, feature, issuer, time]);
3323
};
34-
3524
export const bulkGrantFeature = async (db: IDatabase, users: userArray, feature: Feature, issuer: User, time = 0) => {
3625
for (const user of Object.values(users))
3726
await grantFeature(db, user.pubID, feature, issuer.pubID, time);
38-
};
27+
};
28+
29+
// usernames
30+
export const insertUsername = async (db: IDatabase, userID: HashedUserID, userName: string, locked = false) => {
31+
const query = 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)';
32+
const lockedValue = Number(locked);
33+
await db.prepare("run", query, [userID, userName, lockedValue]);
34+
};
35+
export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray) => {
36+
for (const user of Object.values(users))
37+
await insertUsername(db, user.pubID, user.username, false);
38+
};

test/utils/segmentQueryGen.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { IDatabase } from "../../src/databases/IDatabase";
2+
import { Service, VideoIDHash, VideoID } from "../../src/types/segments.model";
3+
import { HashedUserID, UserID } from "../../src/types/user.model";
4+
import { genRandom } from "./getRandom";
5+
import { getHash } from "../../src/utils/getHash";
6+
7+
type insertSegmentParams = {
8+
videoID?: string,
9+
startTime?: number,
10+
endTime?: number,
11+
votes?: number,
12+
locked?: boolean | number,
13+
UUID?: string,
14+
userID?: HashedUserID | "",
15+
timeSubmitted?: number,
16+
views?: number,
17+
category?: string,
18+
actionType?: string,
19+
service?: Service,
20+
videoDuration?: number,
21+
hidden?: boolean | number,
22+
shadowHidden?: boolean | number,
23+
hashedVideoID?: VideoIDHash | "",
24+
description?: string
25+
};
26+
const defaultSegmentParams: insertSegmentParams = {
27+
videoID: "",
28+
startTime: 0,
29+
endTime: 0,
30+
votes: 0,
31+
locked: false,
32+
UUID: "",
33+
userID: "",
34+
timeSubmitted: 0,
35+
views: 0,
36+
category: "sponsor",
37+
actionType: "skip",
38+
service: Service.YouTube,
39+
videoDuration: 0,
40+
hidden: false,
41+
shadowHidden: false,
42+
hashedVideoID: "",
43+
description: ""
44+
};
45+
46+
// sponsorTimes
47+
export const insertSegment = async(db: IDatabase, fnname: string, testcase: string, params: insertSegmentParams = defaultSegmentParams) => {
48+
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID", "description") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
49+
// corrections for parameters
50+
const identifier = `${fnname}-${testcase}`;
51+
const correctedParams = params;
52+
// generate defaults
53+
const videoID = (params.videoID || `vid-${identifier}`) as VideoID;
54+
const userID = (params.userID || `user-${identifier}`) as UserID;
55+
if (!params.videoID) correctedParams.videoID = videoID;
56+
if (!params.UUID) correctedParams.UUID = `uuid-${identifier}-${genRandom(2)}`;
57+
if (!params.userID) correctedParams.userID = getHash(userID);
58+
if (!params.hashedVideoID) correctedParams.hashedVideoID = getHash(videoID);
59+
// convert bool to 0 | 1
60+
correctedParams.locked = Number(params.locked);
61+
correctedParams.hidden = Number(params.hidden);
62+
correctedParams.shadowHidden = Number(params.shadowHidden);
63+
await db.prepare("run", query, Object.values(correctedParams));
64+
};

0 commit comments

Comments
 (0)