Skip to content

Commit 7364499

Browse files
committed
lockCategoriesHTTP
- highLoad - compact getUserID - add genRandomValue method
1 parent 5e3ec89 commit 7364499

File tree

6 files changed

+87
-229
lines changed

6 files changed

+87
-229
lines changed

test/cases/getUserID.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,17 @@ describe("getUserID", () => {
104104
});
105105

106106
describe("getUserID 400/ 404", () => {
107-
it("Should be able to get a 400 (No username parameter)", () =>
108-
client.get(endpoint)
109-
.then(res => assert.strictEqual(res.status, 400))
110-
);
107+
const validateStatus = (query: string, status: number) =>
108+
getUserName(query)
109+
.then(res => assert.strictEqual(res.status, status));
111110

112-
it("Should return 400 if no username parameter specified", () =>
111+
it("Should be able to get a 400 (No username parameter)", () =>
113112
client.get(endpoint)
114113
.then(res => assert.strictEqual(res.status, 400))
115114
);
116115

117-
it("Should not allow usernames more than 64 characters", () =>
118-
getUserName("0".repeat(65))
119-
.then(res => assert.strictEqual(res.status, 400))
120-
);
121-
122-
it("Should not allow usernames less than 3 characters", () =>
123-
getUserName("aa")
124-
.then(res => assert.strictEqual(res.status, 400))
125-
);
126-
127-
it("Should return 400 (username longer than 64 chars)", () =>
128-
getUserName(users["public_1"].username+0)
129-
.then(res => assert.strictEqual(res.status, 400))
130-
);
131-
132-
it("Should return 404 if escaped backslashes present", () =>
133-
getUserName("%redos\\\\_")
134-
.then(res => assert.strictEqual(res.status, 404))
135-
);
136-
137-
it("Should return 404 if backslashes present", () =>
138-
getUserName(`\\%redos\\_`)
139-
.then(res => assert.strictEqual(res.status, 404))
140-
);
116+
it("Should not allow usernames more than 64 characters", () => validateStatus("0".repeat(65), 400));
117+
it("Should not allow usernames less than 3 characters", () => validateStatus("aa", 400));
118+
it("Should return 404 if escaped backslashes present", () => validateStatus("%redos\\\\_", 404));
119+
it("Should return 404 if backslashes present", () => validateStatus(`\\%redos\\_`, 404));
141120
});

test/cases/highLoad.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,18 @@ describe("High load test", () => {
1313
sinon.restore();
1414
});
1515

16-
it("Should return 503 on getTopUsers", async () => {
17-
await client.get("/api/getTopUsers?sortType=0")
18-
.then(res => {
19-
assert.strictEqual(res.status, 503);
20-
});
21-
});
16+
it("Should return 503 on getTopUsers", () =>
17+
client.get("/api/getTopUsers?sortType=0")
18+
.then(res => assert.strictEqual(res.status, 503))
19+
);
2220

23-
it("Should return 503 on getTopCategoryUsers", async () => {
24-
await client.get("/api/getTopCategoryUsers?sortType=0&category=sponsor")
25-
.then(res => {
26-
assert.strictEqual(res.status, 503);
27-
});
28-
});
21+
it("Should return 503 on getTopCategoryUsers", () =>
22+
client.get("/api/getTopCategoryUsers?sortType=0&category=sponsor")
23+
.then(res => assert.strictEqual(res.status, 503))
24+
);
2925

30-
it("Should return 0 on getTotalStats", async () => {
31-
await client.get("/api/getTotalStats")
32-
.then(res => {
33-
assert.strictEqual(res.status, 200);
34-
});
35-
});
26+
it("Should return 0 on getTotalStats", () =>
27+
client.get("/api/getTotalStats")
28+
.then(res => assert.strictEqual(res.status, 200))
29+
);
3630
});

test/cases/lockCategoriesHttp.ts

Lines changed: 55 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,252 +1,132 @@
11
import assert from "assert";
22
import { client } from "../utils/httpClient";
3-
import { getHash } from "../../src/utils/getHash";
43
import { db } from "../../src/databases/databases";
54
import { UserID } from "../../src/types/user.model";
65
import { Category, VideoID } from "../../src/types/segments.model";
6+
import { insertVipUser } from "../utils/queryGen";
7+
import { genUser } from "../utils/genUser";
8+
import { genRandomValue } from "../utils/getRandom";
79

810
interface LockCategory {
911
category: Category,
1012
reason: string,
1113
videoID: VideoID,
1214
userID: UserID
1315
}
14-
const lockVIPUser = "lockCategoriesHttpVIPUser";
15-
const lockVIPUserHash = getHash(lockVIPUser);
1616
const endpoint = "/api/lockCategories";
17+
const lockUser = genUser("lockCategoriesHttp", "vip");
1718
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
1819

19-
2020
const goodResponse = (): any => ({
2121
videoID: "test-videoid",
2222
userID: "not-vip-test-userid",
2323
categories: ["sponsor"],
2424
actionTypes: ["skip"]
2525
});
2626

27+
const errTest = (method = "POST", override: any): Promise<void> => {
28+
const data = { ...goodResponse(), ...override };
29+
return client(endpoint, { method, data })
30+
.then(res => assert.strictEqual(res.status, 400));
31+
};
32+
33+
const statusTest = (method = "POST", data: any, status: number): Promise<void> =>
34+
client(endpoint, { method, data })
35+
.then(res => assert.strictEqual(res.status, status));
36+
2737
describe("POST lockCategories HTTP submitting", () => {
2838
before(async () => {
29-
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
30-
await db.prepare("run", insertVipUserQuery, [lockVIPUserHash]);
31-
});
32-
33-
it("Should update the database version when starting the application", async () => {
34-
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
35-
assert.ok(version > 1);
39+
await insertVipUser(db, lockUser);
3640
});
3741

38-
it("should be able to add poi type category by type skip", (done) => {
39-
const videoID = "add-record-poi";
40-
client.post(endpoint, {
42+
it("should be able to add poi type category by type skip", () => {
43+
const videoID = genRandomValue("video","lockCategoriesHttp");
44+
const json = {
4145
videoID,
42-
userID: lockVIPUser,
46+
userID: lockUser.privID,
4347
categories: ["poi_highlight"],
4448
actionTypes: ["skip"]
45-
})
46-
.then(res => {
47-
assert.strictEqual(res.status, 200);
48-
checkLockCategories(videoID)
49-
.then(result => {
50-
assert.strictEqual(result.length, 1);
51-
assert.strictEqual(result[0], "poi_highlight");
52-
});
53-
done();
54-
})
55-
.catch(err => done(err));
49+
};
50+
return statusTest("POST", json, 200)
51+
.then(() => checkLockCategories(videoID))
52+
.then(result => {
53+
assert.strictEqual(result.length, 1);
54+
assert.strictEqual(result[0].category, "poi_highlight");
55+
});
5656
});
5757

58-
it("Should not add lock of invalid type", (done) => {
59-
const videoID = "add_invalid_type";
60-
client.post(endpoint, {
58+
it("Should not add lock of invalid type", () => {
59+
const videoID = genRandomValue("video","lockCategoriesHttp");
60+
const json = {
6161
videoID,
62-
userID: lockVIPUser,
62+
userID: lockUser.privID,
6363
categories: ["future_unused_invalid_type"],
6464
actionTypes: ["skip"]
65-
})
66-
.then(res => {
67-
assert.strictEqual(res.status, 200);
68-
checkLockCategories(videoID)
69-
.then(result => {
70-
assert.strictEqual(result.length, 0);
71-
});
72-
done();
73-
})
74-
.catch(err => done(err));
65+
};
66+
return statusTest("POST", json, 200)
67+
.then(() => checkLockCategories(videoID))
68+
.then(result => assert.strictEqual(result.length, 0));
7569
});
7670
});
7771

7872
describe("DELETE lockCategories 403/400 tests", () => {
79-
it(" Should return 400 for no data", (done) => {
80-
client.delete(endpoint, {})
81-
.then(res => {
82-
assert.strictEqual(res.status, 400);
83-
done();
84-
})
85-
.catch(err => done(err));
86-
});
73+
it("Should return 400 for no data", () => statusTest("DELETE", {}, 400));
74+
it("Should return 400 for no data", () => statusTest("POST", {}, 400));
8775

88-
it("Should return 400 for no categories", (done) => {
76+
it("Should return 400 for no categories", () => {
8977
const json: any = {
9078
videoID: "test",
9179
userID: "test",
9280
categories: [],
9381
};
94-
client.delete(endpoint, json)
95-
.then(res => {
96-
assert.strictEqual(res.status, 400);
97-
done();
98-
})
99-
.catch(err => done(err));
82+
return statusTest("DELETE", json, 400);
10083
});
10184

102-
it("Should return 400 for no userID", (done) => {
85+
it("Should return 400 for no userID", () => {
10386
const json: any = {
10487
videoID: "test",
10588
userID: null,
10689
categories: ["sponsor"],
10790
};
108-
109-
client.post(endpoint, json)
110-
.then(res => {
111-
assert.strictEqual(res.status, 400);
112-
done();
113-
})
114-
.catch(err => done(err));
91+
return statusTest("POST", json, 400);
11592
});
11693

117-
it("Should return 400 for no videoID", (done) => {
94+
it("Should return 400 for no videoID", () => {
11895
const json: any = {
11996
videoID: null,
12097
userID: "test",
12198
categories: ["sponsor"],
12299
};
123-
124-
client.post(endpoint, json)
125-
.then(res => {
126-
assert.strictEqual(res.status, 400);
127-
done();
128-
})
129-
.catch(err => done(err));
100+
return statusTest("POST", json, 400);
130101
});
131102

132-
it("Should return 400 for invalid category array", (done) => {
103+
it("Should return 400 for invalid category array", () => {
133104
const json = {
134105
videoID: "test",
135106
userID: "test",
136107
categories: {},
137108
};
138-
139-
client.post(endpoint, json)
140-
.then(res => {
141-
assert.strictEqual(res.status, 400);
142-
done();
143-
})
144-
.catch(err => done(err));
145-
});
146-
147-
it("Should return 400 for bad format categories", (done) => {
148-
const json = {
149-
videoID: "test",
150-
userID: "test",
151-
categories: "sponsor",
152-
};
153-
154-
client.post(endpoint, json)
155-
.then(res => {
156-
assert.strictEqual(res.status, 400);
157-
done();
158-
})
159-
.catch(err => done(err));
160-
});
161-
162-
it("Should return 403 if user is not VIP", (done) => {
163-
const json = {
164-
videoID: "test",
165-
userID: "test",
166-
categories: [
167-
"sponsor",
168-
],
169-
};
170-
171-
client.post(endpoint, json)
172-
.then(res => {
173-
assert.strictEqual(res.status, 403);
174-
done();
175-
})
176-
.catch(err => done(err));
109+
return statusTest("POST", json, 400);
177110
});
178111
});
179112

180113
describe("manual DELETE/POST lockCategories 400 tests", () => {
181-
it("DELETE Should return 400 for no data", (done) => {
182-
client.delete(endpoint, { data: {} })
183-
.then(res => {
184-
assert.strictEqual(res.status, 400);
185-
done();
186-
})
187-
.catch(err => done(err));
188-
});
189-
it("POST Should return 400 for no data", (done) => {
190-
client.post(endpoint, {})
191-
.then(res => {
192-
assert.strictEqual(res.status, 400);
193-
done();
194-
})
195-
.catch(err => done(err));
196-
});
197-
it("DELETE Should return 400 for bad format categories", (done) => {
198-
const data = goodResponse();
199-
data.categories = "sponsor";
200-
client.delete(endpoint, { data })
201-
.then(res => {
202-
assert.strictEqual(res.status, 400);
203-
done();
204-
})
205-
.catch(err => done(err));
206-
});
207-
it("POST Should return 400 for bad format categories", (done) => {
208-
const data = goodResponse();
209-
data.categories = "sponsor";
210-
client.post(endpoint, data)
211-
.then(res => {
212-
assert.strictEqual(res.status, 400);
213-
done();
214-
})
215-
.catch(err => done(err));
216-
});
217-
it("DELETE Should return 403 if user is not VIP", (done) => {
218-
const data = goodResponse();
219-
client.delete(endpoint, { data })
220-
.then(res => {
221-
assert.strictEqual(res.status, 403);
222-
done();
223-
})
224-
.catch(err => done(err));
225-
});
226-
it("POST Should return 403 if user is not VIP", (done) => {
227-
const data = goodResponse();
228-
client.post(endpoint, data)
229-
.then(res => {
230-
assert.strictEqual(res.status, 403);
231-
done();
232-
})
233-
.catch(err => done(err));
234-
});
114+
it("DELETE Should return 400 for no data", () => statusTest("DELETE", {}, 400));
115+
it("POST Should return 400 for no data", () => statusTest("POST", {}, 400));
116+
117+
it("DELETE Should return 400 for bad format categories", () => errTest("DELETE", { categories: "sponsor" }));
118+
it("POST Should return 400 for bad format categories", () => errTest("POST", { categories: "sponsor" }));
119+
120+
it("DELETE Should return 403 if user is not VIP", () => statusTest("DELETE", goodResponse(), 403));
121+
it("POST Should return 403 if user is not VIP", () => statusTest("POST", goodResponse(), 403));
235122
});
236123

237124
describe("array of DELETE/POST lockCategories 400 tests", () => {
238125
for (const key of [ "videoID", "userID", "categories" ]) {
239126
for (const method of ["DELETE", "POST"]) {
240-
it(`${method} - Should return 400 for invalid ${key}`, (done) => {
241-
const data = goodResponse();
242-
data[key] = null;
243-
client(endpoint, { data, method })
244-
.then(res => {
245-
assert.strictEqual(res.status, 400);
246-
done();
247-
})
248-
.catch(err => done(err));
249-
});
127+
it(`${method} - Should return 400 for invalid ${key}`, () =>
128+
errTest(method, { [key]: null })
129+
);
250130
}
251131
}
252132
});

0 commit comments

Comments
 (0)