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";
|
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) => client.get(`${endpoint}/${hash}`);
|
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
|
|
|
|
2021-07-07 22:44:05 +02:00
|
|
|
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category", "reason", "hashedVideoID") VALUES (?, ?, ?, ?, ?)';
|
2021-09-05 01:23:43 +02:00
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "sponsor", "1-reason-short", getHash("getLockHash1", 1)]);
|
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash1", "interaction", "1-reason-longer", getHash("getLockHash1", 1)]);
|
2021-07-12 08:43:46 +02:00
|
|
|
|
2021-09-05 01:23:43 +02:00
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash2", "preview", "2-reason", getHash("getLockHash2", 1)]);
|
2021-07-05 05:33:12 +02:00
|
|
|
|
2021-09-05 01:23:43 +02:00
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "getLockHash3", "nonmusic", "3-reason", getHash("getLockHash3", 1)]);
|
2021-07-12 08:43:46 +02:00
|
|
|
|
2021-09-05 01:23:43 +02:00
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-1", "outro", "fake1-reason", fakeHash]);
|
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "intro", "fake2-longer-reason", fakeHash]);
|
|
|
|
await db.prepare("run", insertLockCategoryQuery, [getHash("getLockCategoriesHashVIP"), "fakehash-2", "preview", "fake2-short", fakeHash]);
|
2021-07-05 05:33:12 +02:00
|
|
|
});
|
|
|
|
|
2021-07-13 21:55:03 +02:00
|
|
|
it("Database should be greater or equal to version 20", 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(
|
|
|
|
version >= 20,
|
|
|
|
`Version isn't greater than 20. 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"
|
2021-07-13 21:55:03 +02:00
|
|
|
],
|
|
|
|
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"
|
2021-07-13 21:55:03 +02:00
|
|
|
],
|
|
|
|
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"
|
2021-07-13 21:55:03 +02:00
|
|
|
],
|
|
|
|
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"
|
2021-07-13 21:55:03 +02:00
|
|
|
],
|
|
|
|
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"
|
2021-07-13 21:55:03 +02:00
|
|
|
],
|
|
|
|
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")
|
2021-07-13 21:55:03 +02: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("")
|
2021-07-13 21:55:03 +02:00
|
|
|
.then(res => {
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(err => done(err));
|
2021-07-05 05:33:12 +02:00
|
|
|
});
|
|
|
|
});
|