getLockCategories

- add insertLock
This commit is contained in:
Michael C 2023-09-28 23:44:14 -04:00
parent 53e5dcb2f0
commit f72b1abf41
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
2 changed files with 139 additions and 194 deletions

View file

@ -1,26 +1,57 @@
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases"; import { db } from "../../src/databases/databases";
import assert from "assert"; import assert from "assert";
import { client } from "../utils/httpClient"; import { client } from "../utils/httpClient";
import { mixedDeepEquals } from "../utils/partialDeepEquals"; import { insertLock } from "../utils/queryGen";
import { multiGenRandomValue } from "../utils/getRandom";
const endpoint = "/api/lockCategories"; const endpoint = "/api/lockCategories";
const defaultActionTypes = ["skip", "mute"]; const defaultActionTypes = ["skip", "mute"];
const getLockCategories = (videoID: string, actionType = defaultActionTypes, service = "YouTube") => client.get(endpoint, { params: { videoID, actionType, service } }); const getLockCategories = (videoID: string, actionType = defaultActionTypes, service = "YouTube") => client.get(endpoint, { params: { videoID, actionType, service } });
const queryStatusCheck = (status: number, queryString: string) =>
client.get(`${endpoint}${queryString}`)
.then(res => assert.strictEqual(res.status, status));
type lockResponse = {
categories?: string[],
reason?: string,
actionTypes?: string[]
}
type lockOverrides = {
actionTypes?: string[],
service?: string
}
const validateResponse = (videoID: string, overrides: lockOverrides = {}, expected: lockResponse): Promise<void> => {
const actionTypes = overrides.actionTypes ?? defaultActionTypes;
const service = overrides.service ?? "YouTube";
const defaultExpected = { categories: [ "sponsor" ], reason: "", actionTypes: defaultActionTypes };
const expectedResponse = { ...defaultExpected, ...expected };
return getLockCategories(videoID, actionTypes, service)
.then(res => {
assert.strictEqual(res.status, 200);
assert.deepStrictEqual(res.data, expectedResponse);
});
};
const validate404 = (videoID: string, overrides: lockOverrides = {}): Promise<void> => {
const actionTypes = overrides.actionTypes || defaultActionTypes;
const service = overrides.service || "YouTube";
return getLockCategories(videoID, actionTypes, service)
.then(res => assert.strictEqual(res.status, 404));
};
const videoIDs = multiGenRandomValue("video", "getLockCategories", 3);
describe("getLockCategories", () => { describe("getLockCategories", () => {
before(async () => { before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)'; await insertLock(db, { videoID: videoIDs[0], reason: "1-short" });
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]); await insertLock(db, { videoID: videoIDs[0], reason: "1-longer-reason", actionType: "mute", category: "interaction" });
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType","category", "reason", "service") VALUES (?, ?, ?, ?, ?, ?)'; await insertLock(db, { videoID: videoIDs[1], reason: "2-reason", category: "preview" });
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "skip", "sponsor", "1-short", "YouTube"]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory1", "mute", "interaction", "1-longer-reason", "YouTube"]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory2", "skip", "preview", "2-reason", "YouTube"]); await insertLock(db, { videoID: videoIDs[2], reason: "3-reason", category: "nonmusic", actionType: "mute", service: "PeerTube" });
await insertLock(db, { videoID: videoIDs[2], reason: "3-reason" });
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "mute", "nonmusic", "3-reason", "PeerTube"]); await insertLock(db, { videoID: videoIDs[2], reason: "3-longer-reason", category: "selfpromo", actionType: "full" });
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "skip", "sponsor", "3-reason", "YouTube"]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesVIP"), "getLockCategory3", "full", "outro", "3-longer-reason", "YouTube"]);
}); });
it("Should update the database version when starting the application", async () => { it("Should update the database version when starting the application", async () => {
@ -28,198 +59,87 @@ describe("getLockCategories", () => {
assert.ok(version >= 29, `Version isn't greater than 29. Version is ${version}`); assert.ok(version >= 29, `Version isn't greater than 29. Version is ${version}`);
}); });
it("Should be able to get multiple locks", (done) => { // 200 tests
getLockCategories("getLockCategory1") it("should return 200 by single actionType", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionType=mute`));
.then(res => { it("should return 200 by single actionTypes JSON", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionTypes=["mute"]`));
assert.strictEqual(res.status, 200); it("should return 200 by repeating actionTypes", () => queryStatusCheck(200, `?videoID=${videoIDs[0]}&actionType=mute&actionType=skip`) );
const expected = {
categories: [
"sponsor",
"interaction"
],
reason: "1-longer-reason",
actionTypes: defaultActionTypes
};
assert.ok(mixedDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to get single locks", (done) => { // 404 tests
getLockCategories("getLockCategory2") it("should return 404 if no lock exists", () => validate404("getLockCategoryNull"));
.then(res => { it("should return 404 if invalid actionTypes specified", () => validate404(videoIDs[0], { actionTypes: ["ban"] }));
assert.strictEqual(res.status, 200);
const expected = {
categories: [
"preview"
],
reason: "2-reason",
actionTypes: defaultActionTypes
};
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 404 if no lock exists", (done) => { // 400 tests
getLockCategories("getLockCategoryNull") it("should return 400 if no videoID specified", () => queryStatusCheck(400, ""));
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
});
it("should return 400 if no videoID specified", (done) => { // complicated response tests
client.get(endpoint) it("Should be able to get multiple locks", () =>
.then(res => { validateResponse(videoIDs[0], {}, {
assert.strictEqual(res.status, 400); categories: [
done(); "sponsor",
}) "interaction"
.catch(err => done(err)); ],
}); reason: "1-longer-reason"
})
);
it("Should be able to get multiple locks with service", (done) => { it("Should be able to get single locks", () =>
getLockCategories("getLockCategory1", defaultActionTypes, "YouTube") validateResponse(videoIDs[1], {}, {
.then(res => { categories: [
assert.strictEqual(res.status, 200); "preview"
const expected = { ],
categories: [ reason: "2-reason",
"sponsor", })
"interaction" );
],
reason: "1-longer-reason",
actionTypes: defaultActionTypes
};
assert.ok(mixedDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to get single locks with service", (done) => { it("Should be able to get multiple locks with service", () =>
getLockCategories("getLockCategory3", defaultActionTypes, "PeerTube") validateResponse(videoIDs[0], {}, {
.then(res => { categories: [
assert.strictEqual(res.status, 200); "sponsor",
const expected = { "interaction"
categories: [ ],
"nonmusic" reason: "1-longer-reason",
], })
reason: "3-reason", );
actionTypes: defaultActionTypes
};
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get single locks with service", (done) => { it("Should be able to get single locks with service", () =>
getLockCategories("getLockCategory3", defaultActionTypes, "Youtube") validateResponse(videoIDs[2], { service: "PeerTube" }, {
.then(res => { categories: [ "nonmusic" ],
assert.strictEqual(res.status, 200); reason: "3-reason",
const expected = { })
categories: [ );
"sponsor"
],
reason: "3-reason",
actionTypes: defaultActionTypes
};
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return result from Youtube service if service not match", (done) => { it("Should be able to get single locks with service", () =>
getLockCategories("getLockCategory3", defaultActionTypes, "Dailymotion") validateResponse(videoIDs[2], { service: "Youtube" }, {
.then(res => { reason: "3-reason",
assert.strictEqual(res.status, 200); })
const expected = { );
categories: [
"sponsor"
],
reason: "3-reason",
actionTypes: defaultActionTypes
};
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 404 if invalid actionTypes specified", (done) => { it("should return result from Youtube service if service not match", () =>
getLockCategories("getLockCategory1", ["ban"]) validateResponse(videoIDs[2], { service: "Dailymotion" }, {
.then(res => { reason: "3-reason",
assert.strictEqual(res.status, 404); })
done(); );
})
.catch(err => done(err));
});
it("should be able to get with specific actionType", (done) => { it("should be able to get with specific actionType", () =>
getLockCategories("getLockCategory1", ["mute"]) validateResponse(videoIDs[0], { actionTypes: ["mute"] }, {
.then(res => { categories: [
assert.strictEqual(res.status, 200); "interaction"
const expected = { ],
categories: [ reason: "1-longer-reason",
"interaction" actionTypes: ["mute"]
], })
reason: "1-longer-reason", );
actionTypes: ["mute"]
};
mixedDeepEquals(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get skip, mute, full", (done) => { it("Should be able to get skip, mute, full", () => {
const actionTypes = [...defaultActionTypes, "full"]; const actionTypes = [...defaultActionTypes, "full"];
getLockCategories("getLockCategory3", actionTypes) return validateResponse(videoIDs[2], { actionTypes }, {
.then(res => { categories: [
assert.strictEqual(res.status, 200); "sponsor",
const expected = { // "nonmusic", // no nonmusic since it's on other service
categories: [ "selfpromo"
"sponsor", ],
"nonmusic", reason: "3-longer-reason",
"outro" actionTypes
], });
reason: "3-longer-reason",
actionTypes
};
mixedDeepEquals(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 200 by single actionType", (done) => {
client.get(`${endpoint}?videoID=getLockCategory1&actionType=mute`)
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("should return 200 by single actionTypes JSON", (done) => {
client.get(`${endpoint}?videoID=getLockCategory1&actionTypes=["mute"]`)
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("should return 200 by repeating actionTypes", (done) => {
client.get(`${endpoint}?videoID=getLockCategory1&actionType=mute&actionType=skip`)
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
}); });
}); });

View file

@ -2,6 +2,9 @@ import { IDatabase } from "../../src/databases/IDatabase";
import { HashedUserID } from "../../src/types/user.model"; import { HashedUserID } from "../../src/types/user.model";
import { User, userArray, usernameUserArray } from "./genUser"; import { User, userArray, usernameUserArray } from "./genUser";
import { Feature } from "../../src/types/user.model"; import { Feature } from "../../src/types/user.model";
import { ActionType, Category, Service, VideoIDHash } from "../../src/types/segments.model";
import { genRandomValue } from "./getRandom";
import { getHash } from "../../src/utils/getHash";
// segments // segments
export { insertSegment } from "./segmentQueryGen"; export { insertSegment } from "./segmentQueryGen";
@ -46,6 +49,28 @@ export const insertVideoInfo = async (db: IDatabase, videoID: string, channelID:
await db.prepare("run", query, [videoID, channelID, title, published]); await db.prepare("run", query, [videoID, channelID, title, published]);
}; };
interface lockParams {
videoID?: string,
userID?: HashedUserID | "",
actionType?: ActionType | string,
category?: Category | string,
hashedVideoID?: VideoIDHash | "",
reason?: string,
service?: Service | string
}
export const insertLock = async(db: IDatabase, overrides: lockParams = {}) => {
const query = 'INSERT INTO "lockCategories" ("videoID", "userID", "actionType", "category", "hashedVideoID", "reason", "service") VALUES (?, ?, ?, ?, ?, ?, ?)';
const identifier = "lock";
const defaults = {
videoID: genRandomValue("video", identifier), userID: genRandomValue("user", identifier),
actionType: "skip", category: "sponsor", hashedVideoID: "", reason: "", service: Service.YouTube
};
const params = { ...defaults, ...overrides };
params.hashedVideoID = getHash(params.videoID);
await db.prepare("run", query, Object.values(params));
};
// warning // warning
type warningParams = { type warningParams = {
userID?: HashedUserID, userID?: HashedUserID,