SponsorBlockServer/test/cases/getLockCategoriesByHash.ts

187 lines
7.3 KiB
TypeScript
Raw Normal View History

2021-09-23 00:52:35 +02:00
import { getHash } from "../../src/utils/getHash";
import { db } from "../../src/databases/databases";
2021-07-12 08:43:46 +02:00
import assert from "assert";
2021-09-22 23:50:06 +02:00
import { client } from "../utils/httpClient";
2022-01-08 00:08:27 +01:00
import { ActionType } from "../../src/types/segments.model";
2021-07-05 05:33:12 +02:00
2021-09-05 01:23:43 +02:00
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
2021-09-22 23:50:06 +02:00
const endpoint = "/api/lockCategories";
const getLockCategories = (hash: string, actionType = [ActionType.Mute, ActionType.Skip]) => client.get(`${endpoint}/${hash}`, { params: { actionType } });
2021-09-05 01:23:43 +02:00
2021-07-12 08:43:46 +02:00
describe("getLockCategoriesByHash", () => {
2021-07-05 05:33:12 +02:00
before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
2021-09-05 01:23:43 +02:00
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
2021-07-12 08:43:46 +02:00
2022-01-08 00:08:27 +01:00
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "actionType", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?, ?)';
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "skip", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
2021-07-12 08:43:46 +02:00
2022-01-08 00:08:27 +01:00
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "skip", "preview", "2-reason", getHash("getLockHash2", 1)]);
2021-07-05 05:33:12 +02:00
2022-01-08 00:08:27 +01:00
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "skip", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
2021-07-12 08:43:46 +02:00
2022-01-08 00:08:27 +01:00
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "mute", "outro", "fake1-reason", fakeHash]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "intro", "fake2-longer-reason", fakeHash]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "mute", "preview", "fake2-short", fakeHash]);
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "full", "sponsor", "fake2-notshown", fakeHash]);
2021-07-05 05:33:12 +02:00
});
2022-01-08 00:08:27 +01:00
it("Database should be greater or equal to version 29", async () => {
2021-07-12 08:43:46 +02:00
const version = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
2021-08-01 09:59:25 +02:00
assert(
2022-01-08 00:08:27 +01:00
version >= 29,
`Version isn't greater than 29. Version is ${version}`);
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("Should be able to get multiple locks in one object", (done) => {
2021-09-05 01:23:43 +02:00
const videoID = "getLockHash1";
const hash = getHash(videoID, 1);
2021-09-22 23:50:06 +02:00
getLockCategories(hash.substring(0,4))
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 200);
const expected = [{
2021-09-05 01:23:43 +02:00
videoID,
hash,
2021-07-12 08:43:46 +02:00
categories: [
"sponsor",
"interaction"
],
reason: "1-reason-longer"
2021-07-12 08:43:46 +02:00
}];
2021-09-22 23:50:06 +02:00
assert.deepStrictEqual(res.data, expected);
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("Should be able to get single lock", (done) => {
2021-09-05 01:23:43 +02:00
const videoID = "getLockHash2";
const hash = getHash(videoID, 1);
2021-09-22 23:50:06 +02:00
getLockCategories(hash.substring(0,6))
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 200);
const expected = [{
2021-09-05 01:23:43 +02:00
videoID,
hash,
2021-07-12 08:43:46 +02:00
categories: [
"preview"
],
reason: "2-reason"
2021-07-12 08:43:46 +02:00
}];
2021-09-22 23:50:06 +02:00
assert.deepStrictEqual(res.data, expected);
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-07-12 08:43:46 +02:00
2021-09-22 23:50:06 +02:00
it("Should be able to get by half full hash", (done) => {
2021-09-05 01:23:43 +02:00
const videoID = "getLockHash3";
const hash = getHash(videoID, 1);
2021-09-22 23:50:06 +02:00
getLockCategories(hash.substring(0,32))
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 200);
const expected = [{
2021-09-05 01:23:43 +02:00
videoID,
hash,
2021-07-12 08:43:46 +02:00
categories: [
"nonmusic"
],
reason: "3-reason"
2021-07-12 08:43:46 +02:00
}];
2021-09-22 23:50:06 +02:00
assert.deepStrictEqual(res.data, expected);
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("Should be able to get multiple by similar hash with multiple categories", (done) => {
getLockCategories(fakeHash.substring(0,5))
.then(res => {
2021-07-12 08:43:46 +02:00
assert.strictEqual(res.status, 200);
const expected = [{
videoID: "fakehash-1",
2021-09-05 01:23:43 +02:00
hash: fakeHash,
2021-07-12 08:43:46 +02:00
categories: [
"outro"
],
reason: "fake1-reason"
2021-07-12 08:43:46 +02:00
}, {
videoID: "fakehash-2",
2021-09-05 01:23:43 +02:00
hash: fakeHash,
2021-07-12 08:43:46 +02:00
categories: [
"intro",
"preview"
],
reason: "fake2-longer-reason"
2021-07-12 08:43:46 +02:00
}];
2021-09-22 23:50:06 +02:00
assert.deepStrictEqual(res.data, expected);
2021-07-12 08:43:46 +02:00
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("should return 404 once hash prefix varies", (done) => {
getLockCategories("b05aa")
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("should return 404 if no lock exists", (done) => {
getLockCategories("aaaaaa")
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 404);
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-09-22 23:50:06 +02:00
it("should return 400 if full hash sent", (done) => {
getLockCategories(fakeHash)
2021-07-12 08:43:46 +02:00
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2021-07-07 23:37:40 +02:00
2021-09-22 23:50:06 +02:00
it("should return 400 if hash too short", (done) => {
getLockCategories("00")
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-07-07 23:37:40 +02:00
});
2021-09-22 23:50:06 +02:00
it("should return 400 if no hash specified", (done) => {
getLockCategories("")
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
2021-07-05 05:33:12 +02:00
});
2022-01-08 00:08:27 +01:00
it("Should be able to get by actionType", (done) => {
getLockCategories(fakeHash.substring(0,5), [ActionType.Full])
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
videoID: "fakehash-2",
hash: fakeHash,
categories: [
"sponsor"
],
reason: "fake2-notshown"
}];
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
2021-07-05 05:33:12 +02:00
});