Skip to content

Commit 5714f51

Browse files
authored
Merge pull request #561 from mchangrh/test-helpers
long overdue test helpers (partial)
2 parents 9dd8b28 + 68bb39c commit 5714f51

25 files changed

+1298
-1622
lines changed

src/utils/getSubmissionUUID.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getHash } from "./getHash";
22
import { HashedValue } from "../types/hash.model";
33
import { ActionType, VideoID, Service, Category } from "../types/segments.model";
4-
import { UserID } from "../types/user.model";
4+
import { HashedUserID } from "../types/user.model";
55

66
export function getSubmissionUUID(
77
videoID: VideoID,
88
category: Category,
99
actionType: ActionType,
1010
description: string,
11-
userID: UserID,
11+
userID: HashedUserID,
1212
startTime: number,
1313
endTime: number,
1414
service: Service

test/case_boilerplate.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
7+
const endpoint = "/api/endpoint";
8+
9+
const postTestEndpoint = () => client({
10+
method: "POST",
11+
url: endpoint,
12+
data: {
13+
}
14+
});
15+
16+
const cases = [
17+
"firstCase",
18+
"secondCase",
19+
"thirdCase"
20+
];
21+
const users = genUsers("endpoint", cases);
22+
const vipUser = genUser("endpoint", "vip");

test/cases/addFeatures.ts

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,57 @@
11
import assert from "assert";
22
import { db } from "../../src/databases/databases";
3-
import { Feature, HashedUserID } from "../../src/types/user.model";
3+
import { Feature } from "../../src/types/user.model";
44
import { hasFeature } from "../../src/utils/features";
5-
import { getHash } from "../../src/utils/getHash";
65
import { client } from "../utils/httpClient";
6+
import { grantFeature, insertVip } from "../utils/queryGen";
7+
import { User, genUser, genUsers } from "../utils/genUser";
78

89
const endpoint = "/api/feature";
910

10-
const postAddFeatures = (userID: string, adminUserID: string, feature: Feature, enabled: string) => client({
11+
const postAddFeatures = (userID: string, adminUserID: string, feature: Feature, enabled: boolean) => client({
1112
method: "POST",
1213
url: endpoint,
1314
data: {
1415
userID,
1516
feature,
16-
enabled,
17+
enabled: String(enabled),
1718
adminUserID
1819
}
1920
});
2021

21-
const privateVipUserID = "VIPUser-addFeatures";
22-
const vipUserID = getHash(privateVipUserID);
22+
const cases = [
23+
"grant",
24+
"remove",
25+
"update"
26+
];
27+
const users = genUsers("addFeatures", cases);
28+
const vipUser = genUser("addFeatures", "vip");
2329

24-
const hashedUserID1 = "user1-addFeatures" as HashedUserID;
25-
const hashedUserID2 = "user2-addFeatures" as HashedUserID;
26-
const hashedUserID3 = "user3-addFeatures" as HashedUserID;
30+
const testedFeature = Feature.ChapterSubmitter;
31+
const validFeatures = [testedFeature];
2732

28-
const validFeatures = [Feature.ChapterSubmitter];
33+
const updateValidateFeature = (user: User, feature: Feature, grant: boolean, issuer: User): Promise<void> =>
34+
postAddFeatures(user.pubID, issuer.privID, feature, grant)
35+
.then(res => assert.strictEqual(res.status, 200)) // ensure request was successful
36+
.then(() => hasFeature(user.pubID, feature))
37+
.then(result => assert.strictEqual(result, grant)); // ensure user has new feature
2938

3039
describe("addFeatures", () => {
31-
before(() => {
32-
const userFeatureQuery = `INSERT INTO "userFeatures" ("userID", "feature", "issuerUserID", "timeSubmitted") VALUES(?, ?, ?, ?)`;
33-
34-
return Promise.all([
35-
db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]),
36-
37-
db.prepare("run", userFeatureQuery, [hashedUserID2, Feature.ChapterSubmitter, "some-user", 0]),
38-
db.prepare("run", userFeatureQuery, [hashedUserID3, Feature.ChapterSubmitter, "some-user", 0])
39-
]);
40+
before(async () => {
41+
await insertVip(db, vipUser.pubID);
42+
await grantFeature(db, users["remove"].pubID, testedFeature, vipUser.pubID);
43+
await grantFeature(db, users["update"].pubID, testedFeature, vipUser.pubID);
4044
});
4145

42-
it("can add features", async () => {
46+
it("can add features", (done) => {
4347
for (const feature of validFeatures) {
44-
const result = await postAddFeatures(hashedUserID1, privateVipUserID, feature, "true");
45-
assert.strictEqual(result.status, 200);
46-
47-
assert.strictEqual(await hasFeature(hashedUserID1, feature), true);
48+
updateValidateFeature(users["grant"], feature, true, vipUser)
49+
.catch(err => done(err));
4850
}
51+
done();
4952
});
5053

51-
it("can remove features", async () => {
52-
const feature = Feature.ChapterSubmitter;
54+
it("can remove features", () => updateValidateFeature(users["remove"], testedFeature, false, vipUser));
5355

54-
const result = await postAddFeatures(hashedUserID2, privateVipUserID, feature, "false");
55-
assert.strictEqual(result.status, 200);
56-
57-
assert.strictEqual(await hasFeature(hashedUserID2, feature), false);
58-
});
59-
60-
it("can update features", async () => {
61-
const feature = Feature.ChapterSubmitter;
62-
63-
const result = await postAddFeatures(hashedUserID3, privateVipUserID, feature, "true");
64-
assert.strictEqual(result.status, 200);
65-
66-
assert.strictEqual(await hasFeature(hashedUserID3, feature), true);
67-
});
56+
it("can update features", () => updateValidateFeature(users["update"], testedFeature, true, vipUser));
6857
});

test/cases/addUserAsVIP.ts

Lines changed: 64 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { getHash } from "../../src/utils/getHash";
21
import { HashedUserID } from "../../src/types/user.model";
32
import { client } from "../utils/httpClient";
43
import { db } from "../../src/databases/databases";
54
import assert from "assert";
5+
import { genAnonUser, genUsers } from "../utils/genUser";
66

77
// helpers
88
const checkUserVIP = (publicID: string) => db.prepare("get", `SELECT "userID" FROM "vipUsers" WHERE "userID" = ?`, [publicID]);
99

10+
const cases = [
11+
"vip-1",
12+
];
13+
const users = genUsers("endpoint", cases);
14+
15+
// hardcoded into test code
1016
const adminPrivateUserID = "testUserId";
11-
const permVIP1 = "addVIP_permaVIPOne";
12-
const publicPermVIP1 = getHash(permVIP1) as HashedUserID;
13-
const permVIP2 = "addVIP_permaVIPTwo";
14-
const publicPermVIP2 = getHash(permVIP2) as HashedUserID;
15-
const permVIP3 = "addVIP_permaVIPThree";
16-
const publicPermVIP3 = getHash(permVIP3) as HashedUserID;
1717

1818
const endpoint = "/api/addUserAsVIP";
1919
const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPrivateUserID) => client({
@@ -26,116 +26,68 @@ const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPriva
2626
}
2727
});
2828

29+
const testVIPUpdate = (target: HashedUserID, enabled: boolean, adminID: string = adminPrivateUserID) =>
30+
addUserAsVIP(target, enabled, adminID)
31+
.then(res => assert.strictEqual(res.status, 200))
32+
.then(() => checkUserVIP(target))
33+
.then(row => assert.ok(Boolean(row) == enabled));
34+
35+
const statusTest = (status: number, data: Record<string, any>) =>
36+
client({
37+
method: "POST",
38+
url: endpoint,
39+
params: data
40+
}).then(res => assert.strictEqual(res.status, status));
41+
2942
describe("addVIP test", function() {
30-
it("User should not already be VIP", (done) => {
31-
checkUserVIP(publicPermVIP1)
32-
.then(result => {
33-
assert.ok(!result);
34-
done();
35-
})
36-
.catch(err => done(err));
37-
});
38-
it("Should be able to add user as VIP", (done) => {
39-
addUserAsVIP(publicPermVIP1, true)
40-
.then(async res => {
41-
assert.strictEqual(res.status, 200);
42-
const row = await checkUserVIP(publicPermVIP1);
43-
assert.ok(row);
44-
done();
45-
})
46-
.catch(err => done(err));
47-
});
48-
it("Should be able to add second user as VIP", (done) => {
49-
addUserAsVIP(publicPermVIP2, true)
50-
.then(async res => {
51-
assert.strictEqual(res.status, 200);
52-
const row = await checkUserVIP(publicPermVIP2);
53-
assert.ok(row);
54-
done();
55-
})
56-
.catch(err => done(err));
57-
});
58-
it("Should return 403 with invalid adminID", (done) => {
59-
addUserAsVIP(publicPermVIP1, true, "Invalid_Admin_User_ID")
60-
.then(res => {
61-
assert.strictEqual(res.status, 403);
62-
done();
63-
})
64-
.catch(err => done(err));
65-
});
66-
it("Should return 400 with missing adminID", (done) => {
67-
client({
68-
method: "POST",
69-
url: endpoint,
70-
params: {
71-
userID: publicPermVIP1,
72-
enabled: String(true)
73-
}
43+
it("User should not already be VIP", () =>
44+
checkUserVIP(users["vip-1"].pubID)
45+
.then(result => assert.ok(!result))
46+
);
47+
it("Should be able to add user as VIP", () =>
48+
testVIPUpdate(users["vip-1"].pubID, true)
49+
);
50+
it("Should be able to remove VIP", () =>
51+
testVIPUpdate(users["vip-1"].pubID, false)
52+
);
53+
it("Should be able to add second user as VIP", () =>
54+
testVIPUpdate(genAnonUser().pubID, true)
55+
);
56+
it("Should return 403 with invalid adminID", () =>
57+
addUserAsVIP(genAnonUser().pubID, true, genAnonUser().privID)
58+
.then(res => assert.strictEqual(res.status, 403))
59+
);
60+
it("Should return 400 with missing adminID", () =>
61+
statusTest(400, {
62+
userID: genAnonUser().pubID,
63+
enabled: String(true)
7464
})
75-
.then(res => {
76-
assert.strictEqual(res.status, 400);
77-
done();
78-
})
79-
.catch(err => done(err));
80-
});
81-
it("Should return 400 with missing userID", (done) => {
82-
client({
83-
method: "POST",
84-
url: endpoint,
85-
params: {
86-
enabled: String(true),
87-
adminUserID: adminPrivateUserID
88-
}
65+
);
66+
it("Should return 400 with missing userID", () =>
67+
statusTest(400, {
68+
enabled: String(true),
69+
adminUserID: adminPrivateUserID
8970
})
90-
.then(res => {
91-
assert.strictEqual(res.status, 400);
92-
done();
93-
})
94-
.catch(err => done(err));
95-
});
96-
it("Should be able to remove VIP", (done) => {
97-
addUserAsVIP(publicPermVIP1, false)
98-
.then(async res => {
99-
assert.strictEqual(res.status, 200);
100-
const row = await checkUserVIP(publicPermVIP1);
101-
assert.ok(!row);
102-
done();
103-
})
104-
.catch(err => done(err));
105-
});
106-
it("Should remove VIP if enabled is false", (done) => {
107-
client({
108-
method: "POST",
109-
url: endpoint,
110-
params: {
111-
userID: publicPermVIP2,
71+
);
72+
it("Should remove VIP if enabled is not true", () => {
73+
const user = genAnonUser();
74+
return testVIPUpdate(user.pubID, true)
75+
.then(() => statusTest(200, {
76+
userID: user.pubID,
11277
adminUserID: adminPrivateUserID,
11378
enabled: "invalid-text"
114-
}
115-
})
116-
.then(async res => {
117-
assert.strictEqual(res.status, 200);
118-
const row = await checkUserVIP(publicPermVIP2);
119-
assert.ok(!row);
120-
done();
121-
})
122-
.catch(err => done(err));
79+
}))
80+
.then(() => checkUserVIP(user.pubID))
81+
.then(row => assert.ok(!row));
12382
});
124-
it("Should remove VIP if enabled is missing", (done) => {
125-
client({
126-
method: "POST",
127-
url: endpoint,
128-
params: {
129-
userID: publicPermVIP3,
130-
adminUserID: adminPrivateUserID
131-
}
132-
})
133-
.then(async res => {
134-
assert.strictEqual(res.status, 200);
135-
const row = await checkUserVIP(publicPermVIP3);
136-
assert.ok(!row);
137-
done();
138-
})
139-
.catch(err => done(err));
83+
it("Should remove VIP if enabled is missing", () => {
84+
const user = genAnonUser();
85+
return testVIPUpdate(user.pubID, true)
86+
.then(() => statusTest(200, {
87+
userID: user.pubID,
88+
adminUserID: adminPrivateUserID,
89+
}))
90+
.then(() => checkUserVIP(user.pubID))
91+
.then(row => assert.ok(!row));
14092
});
14193
});

0 commit comments

Comments
 (0)