Skip to content

Commit f72b1ab

Browse files
committed
getLockCategories
- add insertLock
1 parent 53e5dcb commit f72b1ab

File tree

2 files changed

+149
-204
lines changed

2 files changed

+149
-204
lines changed

test/cases/getLockCategories.ts

Lines changed: 124 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -1,225 +1,145 @@
1-
import { getHash } from "../../src/utils/getHash";
21
import { db } from "../../src/databases/databases";
32
import assert from "assert";
43
import { client } from "../utils/httpClient";
5-
import { mixedDeepEquals } from "../utils/partialDeepEquals";
4+
import { insertLock } from "../utils/queryGen";
5+
import { multiGenRandomValue } from "../utils/getRandom";
6+
67
const endpoint = "/api/lockCategories";
78
const defaultActionTypes = ["skip", "mute"];
9+
810
const getLockCategories = (videoID: string, actionType = defaultActionTypes, service = "YouTube") => client.get(endpoint, { params: { videoID, actionType, service } });
11+
const queryStatusCheck = (status: number, queryString: string) =>
12+
client.get(`${endpoint}${queryString}`)
13+
.then(res => assert.strictEqual(res.status, status));
14+
15+
type lockResponse = {
16+
categories?: string[],
17+
reason?: string,
18+
actionTypes?: string[]
19+
}
20+
type lockOverrides = {
21+
actionTypes?: string[],
22+
service?: string
23+
}
24+
const validateResponse = (videoID: string, overrides: lockOverrides = {}, expected: lockResponse): Promise<void> => {
25+
const actionTypes = overrides.actionTypes ?? defaultActionTypes;
26+
const service = overrides.service ?? "YouTube";
27+
const defaultExpected = { categories: [ "sponsor" ], reason: "", actionTypes: defaultActionTypes };
28+
const expectedResponse = { ...defaultExpected, ...expected };
29+
return getLockCategories(videoID, actionTypes, service)
30+
.then(res => {
31+
assert.strictEqual(res.status, 200);
32+
assert.deepStrictEqual(res.data, expectedResponse);
33+
});
34+
};
35+
36+
const validate404 = (videoID: string, overrides: lockOverrides = {}): Promise<void> => {
37+
const actionTypes = overrides.actionTypes || defaultActionTypes;
38+
const service = overrides.service || "YouTube";
39+
return getLockCategories(videoID, actionTypes, service)
40+
.then(res => assert.strictEqual(res.status, 404));
41+
};
42+
43+
const videoIDs = multiGenRandomValue("video", "getLockCategories", 3);
944

1045
describe("getLockCategories", () => {
1146
before(async () => {
12-
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
13-
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]);
47+
await insertLock(db, { videoID: videoIDs[0], reason: "1-short" });
48+
await insertLock(db, { videoID: videoIDs[0], reason: "1-longer-reason", actionType: "mute", category: "interaction" });
1449

15-
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType","category", "reason", "service") VALUES (?, ?, ?, ?, ?, ?)';
16-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "skip", "sponsor", "1-short", "YouTube"]);
17-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "mute", "interaction", "1-longer-reason", "YouTube"]);
50+
await insertLock(db, { videoID: videoIDs[1], reason: "2-reason", category: "preview" });
1851

19-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory2", "skip", "preview", "2-reason", "YouTube"]);
20-
21-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "mute", "nonmusic", "3-reason", "PeerTube"]);
22-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "skip", "sponsor", "3-reason", "YouTube"]);
23-
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "full", "outro", "3-longer-reason", "YouTube"]);
52+
await insertLock(db, { videoID: videoIDs[2], reason: "3-reason", category: "nonmusic", actionType: "mute", service: "PeerTube" });
53+
await insertLock(db, { videoID: videoIDs[2], reason: "3-reason" });
54+
await insertLock(db, { videoID: videoIDs[2], reason: "3-longer-reason", category: "selfpromo", actionType: "full" });
2455
});
2556

2657
it("Should update the database version when starting the application", async () => {
2758
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
2859
assert.ok(version >= 29, `Version isn't greater than 29. Version is ${version}`);
2960
});
3061

31-
it("Should be able to get multiple locks", (done) => {
32-
getLockCategories("getLockCategory1")
33-
.then(res => {
34-
assert.strictEqual(res.status, 200);
35-
const expected = {
36-
categories: [
37-
"sponsor",
38-
"interaction"
39-
],
40-
reason: "1-longer-reason",
41-
actionTypes: defaultActionTypes
42-
};
43-
assert.ok(mixedDeepEquals(res.data, expected));
44-
done();
45-
})
46-
.catch(err => done(err));
47-
});
48-
49-
it("Should be able to get single locks", (done) => {
50-
getLockCategories("getLockCategory2")
51-
.then(res => {
52-
assert.strictEqual(res.status, 200);
53-
const expected = {
54-
categories: [
55-
"preview"
56-
],
57-
reason: "2-reason",
58-
actionTypes: defaultActionTypes
59-
};
60-
assert.deepStrictEqual(res.data, expected);
61-
done();
62-
})
63-
.catch(err => done(err));
64-
});
65-
66-
it("should return 404 if no lock exists", (done) => {
67-
getLockCategories("getLockCategoryNull")
68-
.then(res => {
69-
assert.strictEqual(res.status, 404);
70-
done();
71-
})
72-
.catch(err => done(err));
73-
});
74-
75-
it("should return 400 if no videoID specified", (done) => {
76-
client.get(endpoint)
77-
.then(res => {
78-
assert.strictEqual(res.status, 400);
79-
done();
80-
})
81-
.catch(err => done(err));
82-
});
83-
84-
it("Should be able to get multiple locks with service", (done) => {
85-
getLockCategories("getLockCategory1", defaultActionTypes, "YouTube")
86-
.then(res => {
87-
assert.strictEqual(res.status, 200);
88-
const expected = {
89-
categories: [
90-
"sponsor",
91-
"interaction"
92-
],
93-
reason: "1-longer-reason",
94-
actionTypes: defaultActionTypes
95-
};
96-
assert.ok(mixedDeepEquals(res.data, expected));
97-
done();
98-
})
99-
.catch(err => done(err));
100-
});
101-
102-
it("Should be able to get single locks with service", (done) => {
103-
getLockCategories("getLockCategory3", defaultActionTypes, "PeerTube")
104-
.then(res => {
105-
assert.strictEqual(res.status, 200);
106-
const expected = {
107-
categories: [
108-
"nonmusic"
109-
],
110-
reason: "3-reason",
111-
actionTypes: defaultActionTypes
112-
};
113-
assert.deepStrictEqual(res.data, expected);
114-
done();
115-
})
116-
.catch(err => done(err));
117-
});
118-
119-
it("Should be able to get single locks with service", (done) => {
120-
getLockCategories("getLockCategory3", defaultActionTypes, "Youtube")
121-
.then(res => {
122-
assert.strictEqual(res.status, 200);
123-
const expected = {
124-
categories: [
125-
"sponsor"
126-
],
127-
reason: "3-reason",
128-
actionTypes: defaultActionTypes
129-
};
130-
assert.deepStrictEqual(res.data, expected);
131-
done();
132-
})
133-
.catch(err => done(err));
134-
});
135-
136-
it("should return result from Youtube service if service not match", (done) => {
137-
getLockCategories("getLockCategory3", defaultActionTypes, "Dailymotion")
138-
.then(res => {
139-
assert.strictEqual(res.status, 200);
140-
const expected = {
141-
categories: [
142-
"sponsor"
143-
],
144-
reason: "3-reason",
145-
actionTypes: defaultActionTypes
146-
};
147-
assert.deepStrictEqual(res.data, expected);
148-
done();
149-
})
150-
.catch(err => done(err));
151-
});
152-
153-
it("should return 404 if invalid actionTypes specified", (done) => {
154-
getLockCategories("getLockCategory1", ["ban"])
155-
.then(res => {
156-
assert.strictEqual(res.status, 404);
157-
done();
158-
})
159-
.catch(err => done(err));
160-
});
161-
162-
it("should be able to get with specific actionType", (done) => {
163-
getLockCategories("getLockCategory1", ["mute"])
164-
.then(res => {
165-
assert.strictEqual(res.status, 200);
166-
const expected = {
167-
categories: [
168-
"interaction"
169-
],
170-
reason: "1-longer-reason",
171-
actionTypes: ["mute"]
172-
};
173-
mixedDeepEquals(res.data, expected);
174-
done();
175-
})
176-
.catch(err => done(err));
177-
});
178-
179-
it("Should be able to get skip, mute, full", (done) => {
62+
// 200 tests
63+
it("should return 200 by single actionType", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionType=mute`));
64+
it("should return 200 by single actionTypes JSON", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionTypes=["mute"]`));
65+
it("should return 200 by repeating actionTypes", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionType=mute&actionType=skip`) );
66+
67+
// 404 tests
68+
it("should return 404 if no lock exists", () => validate404("getLockCategoryNull"));
69+
it("should return 404 if invalid actionTypes specified", () => validate404(videoIDs[0], { actionTypes: ["ban"] }));
70+
71+
// 400 tests
72+
it("should return 400 if no videoID specified", () => queryStatusCheck(400, ""));
73+
74+
// complicated response tests
75+
it("Should be able to get multiple locks", () =>
76+
validateResponse(videoIDs[0], {}, {
77+
categories: [
78+
"sponsor",
79+
"interaction"
80+
],
81+
reason: "1-longer-reason"
82+
})
83+
);
84+
85+
it("Should be able to get single locks", () =>
86+
validateResponse(videoIDs[1], {}, {
87+
categories: [
88+
"preview"
89+
],
90+
reason: "2-reason",
91+
})
92+
);
93+
94+
it("Should be able to get multiple locks with service", () =>
95+
validateResponse(videoIDs[0], {}, {
96+
categories: [
97+
"sponsor",
98+
"interaction"
99+
],
100+
reason: "1-longer-reason",
101+
})
102+
);
103+
104+
it("Should be able to get single locks with service", () =>
105+
validateResponse(videoIDs[2], { service: "PeerTube" }, {
106+
categories: [ "nonmusic" ],
107+
reason: "3-reason",
108+
})
109+
);
110+
111+
it("Should be able to get single locks with service", () =>
112+
validateResponse(videoIDs[2], { service: "Youtube" }, {
113+
reason: "3-reason",
114+
})
115+
);
116+
117+
it("should return result from Youtube service if service not match", () =>
118+
validateResponse(videoIDs[2], { service: "Dailymotion" }, {
119+
reason: "3-reason",
120+
})
121+
);
122+
123+
it("should be able to get with specific actionType", () =>
124+
validateResponse(videoIDs[0], { actionTypes: ["mute"] }, {
125+
categories: [
126+
"interaction"
127+
],
128+
reason: "1-longer-reason",
129+
actionTypes: ["mute"]
130+
})
131+
);
132+
133+
it("Should be able to get skip, mute, full", () => {
180134
const actionTypes = [...defaultActionTypes, "full"];
181-
getLockCategories("getLockCategory3", actionTypes)
182-
.then(res => {
183-
assert.strictEqual(res.status, 200);
184-
const expected = {
185-
categories: [
186-
"sponsor",
187-
"nonmusic",
188-
"outro"
189-
],
190-
reason: "3-longer-reason",
191-
actionTypes
192-
};
193-
mixedDeepEquals(res.data, expected);
194-
done();
195-
})
196-
.catch(err => done(err));
197-
});
198-
199-
it("should return 200 by single actionType", (done) => {
200-
client.get(`${endpoint}?videoID=getLockCategory1&actionType=mute`)
201-
.then(res => {
202-
assert.strictEqual(res.status, 200);
203-
done();
204-
})
205-
.catch(err => done(err));
206-
});
207-
208-
it("should return 200 by single actionTypes JSON", (done) => {
209-
client.get(`${endpoint}?videoID=getLockCategory1&actionTypes=["mute"]`)
210-
.then(res => {
211-
assert.strictEqual(res.status, 200);
212-
done();
213-
})
214-
.catch(err => done(err));
215-
});
216-
217-
it("should return 200 by repeating actionTypes", (done) => {
218-
client.get(`${endpoint}?videoID=getLockCategory1&actionType=mute&actionType=skip`)
219-
.then(res => {
220-
assert.strictEqual(res.status, 200);
221-
done();
222-
})
223-
.catch(err => done(err));
135+
return validateResponse(videoIDs[2], { actionTypes }, {
136+
categories: [
137+
"sponsor",
138+
// "nonmusic", // no nonmusic since it's on other service
139+
"selfpromo"
140+
],
141+
reason: "3-longer-reason",
142+
actionTypes
143+
});
224144
});
225145
});

test/utils/queryGen.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { IDatabase } from "../../src/databases/IDatabase";
22
import { HashedUserID } from "../../src/types/user.model";
33
import { User, userArray, usernameUserArray } from "./genUser";
44
import { Feature } from "../../src/types/user.model";
5+
import { ActionType, Category, Service, VideoIDHash } from "../../src/types/segments.model";
6+
import { genRandomValue } from "./getRandom";
7+
import { getHash } from "../../src/utils/getHash";
58

69
// segments
710
export { insertSegment } from "./segmentQueryGen";
@@ -46,6 +49,28 @@ export const insertVideoInfo = async (db: IDatabase, videoID: string, channelID:
4649
await db.prepare("run", query, [videoID, channelID, title, published]);
4750
};
4851

52+
interface lockParams {
53+
videoID?: string,
54+
userID?: HashedUserID | "",
55+
actionType?: ActionType | string,
56+
category?: Category | string,
57+
hashedVideoID?: VideoIDHash | "",
58+
reason?: string,
59+
service?: Service | string
60+
}
61+
62+
export const insertLock = async(db: IDatabase, overrides: lockParams = {}) => {
63+
const query = 'INSERT INTO "lockCategories" ("videoID", "userID", "actionType", "category", "hashedVideoID", "reason", "service") VALUES (?, ?, ?, ?, ?, ?, ?)';
64+
const identifier = "lock";
65+
const defaults = {
66+
videoID: genRandomValue("video", identifier), userID: genRandomValue("user", identifier),
67+
actionType: "skip", category: "sponsor", hashedVideoID: "", reason: "", service: Service.YouTube
68+
};
69+
const params = { ...defaults, ...overrides };
70+
params.hashedVideoID = getHash(params.videoID);
71+
await db.prepare("run", query, Object.values(params));
72+
};
73+
4974
// warning
5075
type warningParams = {
5176
userID?: HashedUserID,

0 commit comments

Comments
 (0)