mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-05-11 10:26:25 +02:00
add segment generator
- getIsUserVIP - postClearCache - update boilerplate
This commit is contained in:
parent
964634dc51
commit
a9ef3815e2
5 changed files with 141 additions and 99 deletions
|
@ -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 endpoint = "/api/endpoint";
|
||||||
|
|
||||||
const postTestEndpoint = () => client({
|
const postTestEndpoint = () => client({
|
||||||
|
|
|
@ -1,54 +1,42 @@
|
||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import { getHash } from "../../src/utils/getHash";
|
import { genUsers, User } from "../utils/genUser";
|
||||||
import { client } from "../utils/httpClient";
|
import { client } from "../utils/httpClient";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
import { insertVip } from "../utils/queryGen";
|
||||||
|
|
||||||
const VIPUser = "isUserVIPVIP";
|
|
||||||
const normalUser = "isUserVIPNormal";
|
|
||||||
const endpoint = "/api/isUserVIP";
|
const endpoint = "/api/isUserVIP";
|
||||||
const vipUserRequest = (userID: string) => client.get(endpoint, { params: { userID } });
|
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", () => {
|
describe("getIsUserVIP", () => {
|
||||||
before(() => {
|
before(async () => {
|
||||||
db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(VIPUser)]);
|
await insertVip(db, users["vip"].pubID);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to get a 200", (done) => {
|
// status checks
|
||||||
vipUserRequest(VIPUser)
|
it("Should be able to get a 200", () =>
|
||||||
.then(res => {
|
vipUserRequest(users["vip"].privID)
|
||||||
assert.strictEqual(res.status, 200, "response should be 200");
|
.then(res => assert.strictEqual(res.status, 200))
|
||||||
done();
|
);
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
it("Should get a 400 if no userID", (done) => {
|
it("Should get a 400 if no userID", () =>
|
||||||
client.get(endpoint)
|
client.get(endpoint)
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 400, "response should be 400"))
|
||||||
assert.strictEqual(res.status, 400, "response should be 400");
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should say a VIP is a VIP", (done) => {
|
// user checks
|
||||||
vipUserRequest(VIPUser)
|
it("Should say a VIP is a VIP", () => checkVipStatus(users["vip"], true));
|
||||||
.then(res => {
|
it("Should say a normal user is not a VIP", () => checkVipStatus(users["normal"], false));
|
||||||
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));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,62 +1,46 @@
|
||||||
import { db } from "../../src/databases/databases";
|
import { db } from "../../src/databases/databases";
|
||||||
import { getHash } from "../../src/utils/getHash";
|
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { client } from "../utils/httpClient";
|
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 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", () => {
|
describe("postClearCache", () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('${getHash(VIPUser)}')`);
|
await insertVip(db, users["vip"].pubID);
|
||||||
const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "category", "shadowHidden") VALUES';
|
await insertSegment(db, "clearSegments", "clear-test");
|
||||||
await db.prepare("run", `${startOfQuery}('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0)`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to clear cache for existing video", (done) => {
|
it("Should be able to clear cache for existing video", () =>
|
||||||
postClearCache(VIPUser, "clear-test")
|
postClearCache(users["vip"], "clear-test")
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 200))
|
||||||
assert.strictEqual(res.status, 200);
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should be able to clear cache for nonexistent video", (done) => {
|
it("Should be able to clear cache for nonexistent video", () =>
|
||||||
postClearCache(VIPUser, "dne-video")
|
postClearCache(users["vip"], "dne-video")
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 200))
|
||||||
assert.strictEqual(res.status, 200);
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should get 403 as non-vip", (done) => {
|
it("Should get 403 as non-vip", () =>
|
||||||
postClearCache(regularUser, "clear-test")
|
postClearCache(users["normal"], "clear-test")
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 403))
|
||||||
assert.strictEqual(res.status, 403);
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should give 400 with missing videoID", (done) => {
|
it("Should give 400 with missing videoID", () =>
|
||||||
client.post(endpoint, { params: { userID: VIPUser } })
|
client.post(endpoint, { params: { userID: users["vip"].privID } })
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 400))
|
||||||
assert.strictEqual(res.status, 400);
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should give 400 with missing userID", (done) => {
|
it("Should give 400 with missing userID", () =>
|
||||||
client.post(endpoint, { params: { videoID: "clear-test" } })
|
client.post(endpoint, { params: { videoID: "clear-test" } })
|
||||||
.then(res => {
|
.then(res => assert.strictEqual(res.status, 400))
|
||||||
assert.strictEqual(res.status, 400);
|
);
|
||||||
done();
|
|
||||||
})
|
|
||||||
.catch(err => done(err));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,24 +3,14 @@ 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";
|
||||||
|
|
||||||
// usernames
|
// segments
|
||||||
export const insertUsername = async (db: IDatabase, userID: HashedUserID, userName: string, locked = false) => {
|
export { insertSegment } from "./segmentQueryGen";
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
// vip
|
// vip
|
||||||
export const insertVip = async (db: IDatabase, userID: HashedUserID) => {
|
export const insertVip = async (db: IDatabase, userID: HashedUserID) => {
|
||||||
const query = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
const query = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
|
||||||
await db.prepare("run", query, [userID]);
|
await db.prepare("run", query, [userID]);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const insertVipBulk = async (db: IDatabase, users: userArray) => {
|
export const insertVipBulk = async (db: IDatabase, users: userArray) => {
|
||||||
for (const user of Object.values(users))
|
for (const user of Object.values(users))
|
||||||
await insertVip(db, user.pubID);
|
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(?, ?, ?, ?)';
|
const query = 'INSERT INTO "userFeatures" ("userID", "feature", "issuerUserID", "timeSubmitted") VALUES(?, ?, ?, ?)';
|
||||||
await db.prepare("run", query, [target, feature, issuer, time]);
|
await db.prepare("run", query, [target, feature, issuer, time]);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const bulkGrantFeature = async (db: IDatabase, users: userArray, feature: Feature, issuer: User, time = 0) => {
|
export const bulkGrantFeature = async (db: IDatabase, users: userArray, feature: Feature, issuer: User, time = 0) => {
|
||||||
for (const user of Object.values(users))
|
for (const user of Object.values(users))
|
||||||
await grantFeature(db, user.pubID, feature, issuer.pubID, time);
|
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);
|
||||||
|
};
|
||||||
|
|
64
test/utils/segmentQueryGen.ts
Normal file
64
test/utils/segmentQueryGen.ts
Normal 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));
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue