Skip to content

Commit 53e5dcb

Browse files
committed
addUserAsVIP
- add genAnonUser
1 parent 73e5ade commit 53e5dcb

File tree

2 files changed

+75
-115
lines changed

2 files changed

+75
-115
lines changed

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
});

test/utils/genUser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { genRandom } from "./getRandom";
22
import { UserID, HashedUserID } from "../../src/types/user.model";
33
import { getHash } from "../../src/utils/getHash";
44

5+
type info = Record<string, any>
6+
57
export interface User {
68
privID: UserID,
79
pubID: HashedUserID
8-
info: Record<string, any>
10+
info: info
911
}
1012
export type userArray = Record<string, User>
1113

@@ -14,10 +16,16 @@ export interface UsernameUser extends User {
1416
}
1517
export type usernameUserArray = Record<string, UsernameUser>
1618

17-
export const genUser = (fnname: string, testcase: string): User => {
19+
export const genUser = (fnname: string, testcase: string, info: info = {}): User => {
1820
const privID = `${fnname}-${testcase}-${genRandom(2)}` as UserID;
1921
const pubID = getHash(privID);
20-
return { privID, pubID, info: {} };
22+
return { privID, pubID, info };
23+
};
24+
25+
export const genAnonUser = (info: info = {}): User => {
26+
const privID = `user-${genRandom()}` as UserID;
27+
const pubID = getHash(privID);
28+
return { privID, pubID, info };
2129
};
2230

2331
export const genUsers = (fnname: string, testcase: string[]): userArray => {

0 commit comments

Comments
 (0)