add segment generator

- getIsUserVIP
- postClearCache
- update boilerplate
This commit is contained in:
Michael C 2023-09-27 22:21:42 -04:00
parent 964634dc51
commit a9ef3815e2
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
5 changed files with 141 additions and 99 deletions

View file

@ -1,3 +1,9 @@
import { db } from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
import { genUsers, User } from "../utils/genUser";
import { insertSegment, insertVip } from "../utils/queryGen";
const endpoint = "/api/endpoint";
const postTestEndpoint = () => client({

View file

@ -1,54 +1,42 @@
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import { genUsers, User } from "../utils/genUser";
import { client } from "../utils/httpClient";
import assert from "assert";
import { insertVip } from "../utils/queryGen";
const VIPUser = "isUserVIPVIP";
const normalUser = "isUserVIPNormal";
const endpoint = "/api/isUserVIP";
const vipUserRequest = (userID: string) => client.get(endpoint, { params: { userID } });
const checkVipStatus = (user: User, expected: boolean) =>
vipUserRequest(user.privID)
.then(res => {
assert.strictEqual(res.status, 200);
assert.strictEqual(res.data.vip, expected);
});
const cases = [
"vip",
"normal",
];
const users = genUsers("endpoint", cases);
describe("getIsUserVIP", () => {
before(() => {
db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(VIPUser)]);
before(async () => {
await insertVip(db, users["vip"].pubID);
});
it("Should be able to get a 200", (done) => {
vipUserRequest(VIPUser)
.then(res => {
assert.strictEqual(res.status, 200, "response should be 200");
done();
})
.catch(err => done(err));
});
// status checks
it("Should be able to get a 200", () =>
vipUserRequest(users["vip"].privID)
.then(res => assert.strictEqual(res.status, 200))
);
it("Should get a 400 if no userID", (done) => {
it("Should get a 400 if no userID", () =>
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400, "response should be 400");
done();
})
.catch(err => done(err));
});
.then(res => assert.strictEqual(res.status, 400, "response should be 400"))
);
it("Should say a VIP is a VIP", (done) => {
vipUserRequest(VIPUser)
.then(res => {
assert.strictEqual(res.status, 200);
assert.strictEqual(res.data.vip, true);
done();
})
.catch(err => done(err));
});
it("Should say a normal user is not a VIP", (done) => {
vipUserRequest(normalUser)
.then(res => {
assert.strictEqual(res.status, 200);
assert.strictEqual(res.data.vip, false);
done();
})
.catch(err => done(err));
});
// user checks
it("Should say a VIP is a VIP", () => checkVipStatus(users["vip"], true));
it("Should say a normal user is not a VIP", () => checkVipStatus(users["normal"], false));
});

View file

@ -1,62 +1,46 @@
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import assert from "assert";
import { client } from "../utils/httpClient";
import { genUsers, User } from "../utils/genUser";
import { insertSegment, insertVip } from "../utils/queryGen";
const VIPUser = "clearCacheVIP";
const regularUser = "regular-user";
const endpoint = "/api/clearCache";
const postClearCache = (userID: string, videoID: string) => client({ method: "post", url: endpoint, params: { userID, videoID } });
const postClearCache = (user: User, videoID: string) => client({ method: "post", url: endpoint, params: { userID: user.privID, videoID } });
const cases = [
"vip",
"normal",
];
const users = genUsers("postClearCache", cases);
describe("postClearCache", () => {
before(async () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('${getHash(VIPUser)}')`);
const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden") VALUES';
await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`);
await insertVip(db, users["vip"].pubID);
await insertSegment(db, "clearSegments", "clear-test");
});
it("Should be able to clear cache for existing video", (done) => {
postClearCache(VIPUser, "clear-test")
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to clear cache for existing video", () =>
postClearCache(users["vip"], "clear-test")
.then(res => assert.strictEqual(res.status, 200))
);
it("Should be able to clear cache for nonexistent video", (done) => {
postClearCache(VIPUser, "dne-video")
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to clear cache for nonexistent video", () =>
postClearCache(users["vip"], "dne-video")
.then(res => assert.strictEqual(res.status, 200))
);
it("Should get 403 as non-vip", (done) => {
postClearCache(regularUser, "clear-test")
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
});
it("Should get 403 as non-vip", () =>
postClearCache(users["normal"], "clear-test")
.then(res => assert.strictEqual(res.status, 403))
);
it("Should give 400 with missing videoID", (done) => {
client.post(endpoint, { params: { userID: VIPUser } })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("Should give 400 with missing videoID", () =>
client.post(endpoint, { params: { userID: users["vip"].privID } })
.then(res => assert.strictEqual(res.status, 400))
);
it("Should give 400 with missing userID", (done) => {
it("Should give 400 with missing userID", () =>
client.post(endpoint, { params: { videoID: "clear-test" } })
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
.then(res => assert.strictEqual(res.status, 400))
);
});

View file

@ -3,24 +3,14 @@ import { HashedUserID } from "../../src/types/user.model";
import { User, userArray, usernameUserArray } from "./genUser";
import { Feature } from "../../src/types/user.model";
// usernames
export const insertUsername = async (db: IDatabase, userID: HashedUserID, userName: string, locked = false) => {
const query = 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)';
const lockedValue = Number(locked);
await db.prepare("run", query, [userID, userName, lockedValue]);
};
export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray) => {
for (const user of Object.values(users))
await insertUsername(db, user.pubID, user.username, false);
};
// segments
export { insertSegment } from "./segmentQueryGen";
// vip
export const insertVip = async (db: IDatabase, userID: HashedUserID) => {
const query = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", query, [userID]);
};
export const insertVipBulk = async (db: IDatabase, users: userArray) => {
for (const user of Object.values(users))
await insertVip(db, user.pubID);
@ -31,8 +21,18 @@ export const grantFeature = async (db: IDatabase, target: HashedUserID, feature:
const query = 'INSERT INTO "userFeatures" ("userID", "feature", "issuerUserID", "timeSubmitted") VALUES(?, ?, ?, ?)';
await db.prepare("run", query, [target, feature, issuer, time]);
};
export const bulkGrantFeature = async (db: IDatabase, users: userArray, feature: Feature, issuer: User, time = 0) => {
for (const user of Object.values(users))
await grantFeature(db, user.pubID, feature, issuer.pubID, time);
};
};
// usernames
export const insertUsername = async (db: IDatabase, userID: HashedUserID, userName: string, locked = false) => {
const query = 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)';
const lockedValue = Number(locked);
await db.prepare("run", query, [userID, userName, lockedValue]);
};
export const insertUsernameBulk = async (db: IDatabase, users: usernameUserArray) => {
for (const user of Object.values(users))
await insertUsername(db, user.pubID, user.username, false);
};

View file

@ -0,0 +1,64 @@
import { IDatabase } from "../../src/databases/IDatabase";
import { Service, VideoIDHash, VideoID } from "../../src/types/segments.model";
import { HashedUserID, UserID } from "../../src/types/user.model";
import { genRandom } from "./getRandom";
import { getHash } from "../../src/utils/getHash";
type insertSegmentParams = {
videoID?: string,
startTime?: number,
endTime?: number,
votes?: number,
locked?: boolean | number,
UUID?: string,
userID?: HashedUserID | "",
timeSubmitted?: number,
views?: number,
category?: string,
actionType?: string,
service?: Service,
videoDuration?: number,
hidden?: boolean | number,
shadowHidden?: boolean | number,
hashedVideoID?: VideoIDHash | "",
description?: string
};
const defaultSegmentParams: insertSegmentParams = {
videoID: "",
startTime: 0,
endTime: 0,
votes: 0,
locked: false,
UUID: "",
userID: "",
timeSubmitted: 0,
views: 0,
category: "sponsor",
actionType: "skip",
service: Service.YouTube,
videoDuration: 0,
hidden: false,
shadowHidden: false,
hashedVideoID: "",
description: ""
};
// sponsorTimes
export const insertSegment = async(db: IDatabase, fnname: string, testcase: string, params: insertSegmentParams = defaultSegmentParams) => {
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID", "description") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
// corrections for parameters
const identifier = `${fnname}-${testcase}`;
const correctedParams = params;
// generate defaults
const videoID = (params.videoID || `vid-${identifier}`) as VideoID;
const userID = (params.userID || `user-${identifier}`) as UserID;
if (!params.videoID) correctedParams.videoID = videoID;
if (!params.UUID) correctedParams.UUID = `uuid-${identifier}-${genRandom(2)}`;
if (!params.userID) correctedParams.userID = getHash(userID);
if (!params.hashedVideoID) correctedParams.hashedVideoID = getHash(videoID);
// convert bool to 0 | 1
correctedParams.locked = Number(params.locked);
correctedParams.hidden = Number(params.hidden);
correctedParams.shadowHidden = Number(params.shadowHidden);
await db.prepare("run", query, Object.values(correctedParams));
};