everything to postClearCache

This commit is contained in:
Michael C 2021-09-22 17:50:06 -04:00
parent 94e1e8c377
commit c779c2c19e
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
21 changed files with 750 additions and 860 deletions

41
package-lock.json generated
View file

@ -11,6 +11,7 @@
"dependencies": {
"@ajayyy/lru-diskcache": "^1.1.9",
"@types/request": "^2.48.7",
"axios": "^0.21.4",
"better-sqlite3": "^7.4.3",
"cron": "^1.8.2",
"express": "^4.17.1",
@ -1111,6 +1112,14 @@
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"node_modules/axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"dependencies": {
"follow-redirects": "^1.14.0"
}
},
"node_modules/babel-runtime": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz",
@ -2570,6 +2579,25 @@
"integrity": "sha512-XprP7lDrVT+kE2c2YlfiV+IfS9zxukiIOvNamPNsImNhXadSsQEbosItdL9bUQlCZXR13SvPk20BjWSWLA7m4A==",
"dev": true
},
"node_modules/follow-redirects": {
"version": "1.14.4",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz",
"integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
@ -6660,6 +6688,14 @@
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"requires": {
"follow-redirects": "^1.14.0"
}
},
"babel-runtime": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz",
@ -7785,6 +7821,11 @@
"integrity": "sha512-XprP7lDrVT+kE2c2YlfiV+IfS9zxukiIOvNamPNsImNhXadSsQEbosItdL9bUQlCZXR13SvPk20BjWSWLA7m4A==",
"dev": true
},
"follow-redirects": {
"version": "1.14.4",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz",
"integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g=="
},
"form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",

View file

@ -18,6 +18,7 @@
"dependencies": {
"@ajayyy/lru-diskcache": "^1.1.9",
"@types/request": "^2.48.7",
"axios": "^0.21.4",
"better-sqlite3": "^7.4.3",
"cron": "^1.8.2",
"express": "^4.17.1",

View file

@ -1,4 +1,4 @@
import fetch from "node-fetch";
import axios from "axios";
import {Logger} from "../utils/logger";
import {config} from "../config";
import {getIP} from "../utils/getIP";
@ -6,7 +6,7 @@ import {getHash} from "../utils/getHash";
import {NextFunction, Request, Response} from "express";
export function userCounter(req: Request, res: Response, next: NextFunction): void {
fetch(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`, {method: "POST"})
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getHash(getIP(req), 1)}`)
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
next();

View file

@ -2,6 +2,7 @@ import {db} from "../databases/databases";
import {config} from "../config";
import {Request, Response} from "express";
import fetch from "node-fetch";
import axios from "axios";
import {Logger} from "../utils/logger";
// A cache of the number of chrome web store users
@ -44,10 +45,9 @@ export async function getTotalStats(req: Request, res: Response): Promise<void>
function updateExtensionUsers() {
if (config.userCounterURL) {
fetch(`${config.userCounterURL}/api/v1/userCount`)
.then(res => res.json())
.then(data => {
apiUsersCache = Math.max(apiUsersCache, data.userCount);
axios.get(`${config.userCounterURL}/api/v1/userCount`)
.then(res => {
apiUsersCache = Math.max(apiUsersCache, res.data.userCount);
})
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
}

View file

@ -1,21 +1,20 @@
import fetch from "node-fetch";
import { Done} from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import { client } from "../utils/httpClient";
import assert from "assert";
const endpoint = `${getbaseURL()}/api/isUserVIP`;
const VIPUser = "isUserVIPVIP";
const normalUser = "isUserVIPNormal";
const endpoint = "/api/isUserVIP";
const vipUserRequest = (userID: string) => client.get(endpoint, { params: { userID }});
describe("getIsUserVIP", () => {
before(() => {
db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(VIPUser)]);
});
it("Should be able to get a 200", (done: Done) => {
fetch(`${endpoint}?userID=${VIPUser}`)
it("Should be able to get a 200", (done) => {
vipUserRequest(VIPUser)
.then(res => {
assert.strictEqual(res.status, 200, "response should be 200");
done();
@ -24,8 +23,8 @@ describe("getIsUserVIP", () => {
});
it("Should get a 400 if no userID", (done: Done) => {
fetch(endpoint)
it("Should get a 400 if no userID", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400, "response should be 400");
done();
@ -33,23 +32,21 @@ describe("getIsUserVIP", () => {
.catch(err => done(err));
});
it("Should say a VIP is a VIP", (done: Done) => {
fetch(`${endpoint}?userID=${VIPUser}`)
.then(async res => {
it("Should say a VIP is a VIP", (done) => {
vipUserRequest(VIPUser)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.vip, true);
assert.strictEqual(res.data.vip, true);
done();
})
.catch(err => done(err));
});
it("Should say a normal user is not a VIP", (done: Done) => {
fetch(`${endpoint}?userID=${normalUser}`)
.then(async res => {
it("Should say a normal user is not a VIP", (done) => {
vipUserRequest(normalUser)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.vip, false);
assert.strictEqual(res.data.vip, false);
done();
})
.catch(err => done(err));

View file

@ -1,13 +1,11 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {getHash} from "../../src/utils/getHash";
import {db} from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
const endpoint = "/api/lockCategories";
const getLockCategories = (videoID: string) => client.get(endpoint, { params: { videoID } });
describe("getLockCategories", () => {
const endpoint = `${getbaseURL()}/api/lockCategories`;
before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesVIP")]);
@ -26,11 +24,10 @@ describe("getLockCategories", () => {
assert.ok(version > 20, `Version isn't greater than 20. Version is ${version}`);
});
it("Should be able to get multiple locks", (done: Done) => {
fetch(`${endpoint}?videoID=getLock1`)
.then(async res => {
it("Should be able to get multiple locks", (done) => {
getLockCategories("getLock1")
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
categories: [
"sponsor",
@ -38,31 +35,30 @@ describe("getLockCategories", () => {
],
reason: "1-longer-reason"
};
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get single locks", (done: Done) => {
fetch(`${endpoint}?videoID=getLock2`)
.then(async res => {
it("Should be able to get single locks", (done) => {
getLockCategories("getLock2")
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
categories: [
"preview"
],
reason: "2-reason"
};
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 404 if no lock exists", (done: Done) => {
fetch(`${endpoint}?videoID=getLockNull`)
it("should return 404 if no lock exists", (done) => {
getLockCategories("getLockNull")
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -70,8 +66,8 @@ describe("getLockCategories", () => {
.catch(err => done(err));
});
it("should return 400 if no videoID specified", (done: Done) => {
fetch(endpoint)
it("should return 400 if no videoID specified", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();

View file

@ -1,14 +1,13 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {getHash} from "../../src/utils/getHash";
import {db} from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
const fakeHash = "b05a20424f24a53dac1b059fb78d861ba9723645026be2174c93a94f9106bb35";
const endpoint = "/api/lockCategories";
const getLockCategories = (hash: string) => client.get(`${endpoint}/${hash}`);
describe("getLockCategoriesByHash", () => {
const endpoint = `${getbaseURL()}/api/lockCategories`;
before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", insertVipUserQuery, [getHash("getLockCategoriesHashVIP")]);
@ -33,13 +32,12 @@ describe("getLockCategoriesByHash", () => {
`Version isn't greater than 20. Version is ${version}`);
});
it("Should be able to get multiple locks in one object", (done: Done) => {
it("Should be able to get multiple locks in one object", (done) => {
const videoID = "getLockHash1";
const hash = getHash(videoID, 1);
fetch(`${endpoint}/${hash.substring(0,4)}`)
.then(async res => {
getLockCategories(hash.substring(0,4))
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID,
hash,
@ -49,19 +47,18 @@ describe("getLockCategoriesByHash", () => {
],
reason: "1-reason-longer"
}];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get single lock", (done: Done) => {
it("Should be able to get single lock", (done) => {
const videoID = "getLockHash2";
const hash = getHash(videoID, 1);
fetch(`${endpoint}/${hash.substring(0,6)}`)
.then(async res => {
getLockCategories(hash.substring(0,6))
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID,
hash,
@ -70,19 +67,18 @@ describe("getLockCategoriesByHash", () => {
],
reason: "2-reason"
}];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get by half full hash", (done: Done) => {
it("Should be able to get by half full hash", (done) => {
const videoID = "getLockHash3";
const hash = getHash(videoID, 1);
fetch(`${endpoint}/${hash.substring(0,32)}`)
.then(async res => {
getLockCategories(hash.substring(0,32))
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID,
hash,
@ -91,17 +87,16 @@ describe("getLockCategoriesByHash", () => {
],
reason: "3-reason"
}];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get multiple by similar hash with multiple categories", (done: Done) => {
fetch(`${endpoint}/${fakeHash.substring(0,5)}`)
.then(async res => {
it("Should be able to get multiple by similar hash with multiple categories", (done) => {
getLockCategories(fakeHash.substring(0,5))
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "fakehash-1",
hash: fakeHash,
@ -118,14 +113,14 @@ describe("getLockCategoriesByHash", () => {
],
reason: "fake2-longer-reason"
}];
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 404 once hash prefix varies", (done: Done) => {
fetch(`${endpoint}/b05aa`)
it("should return 404 once hash prefix varies", (done) => {
getLockCategories("b05aa")
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -133,8 +128,8 @@ describe("getLockCategoriesByHash", () => {
.catch(err => done(err));
});
it("should return 404 if no lock exists", (done: Done) => {
fetch(`${endpoint}/aaaaaa`)
it("should return 404 if no lock exists", (done) => {
getLockCategories("aaaaaa")
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -142,8 +137,8 @@ describe("getLockCategoriesByHash", () => {
.catch(err => done(err));
});
it("should return 400 if no videoID specified", (done: Done) => {
fetch(`${endpoint}/`)
it("should return 400 if full hash sent", (done) => {
getLockCategories(fakeHash)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -151,8 +146,8 @@ describe("getLockCategoriesByHash", () => {
.catch(err => done(err));
});
it("should return 400 if full hash sent", (done: Done) => {
fetch(`${endpoint}/${fakeHash}`)
it("should return 400 if hash too short", (done) => {
getLockCategories("00")
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -160,17 +155,8 @@ describe("getLockCategoriesByHash", () => {
.catch(err => done(err));
});
it("should return 400 if hash too short", (done: Done) => {
fetch(`${endpoint}/00`)
.then(res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
it("should return 400 if no hash specified", (done: Done) => {
fetch(endpoint)
it("should return 400 if no hash specified", (done) => {
getLockCategories("")
.then(res => {
assert.strictEqual(res.status, 400);
done();

View file

@ -1,29 +1,27 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import {deepStrictEqual} from "assert";
import { client } from "../utils/httpClient";
const endpoint = "/api/getSavedTimeForUser";
describe("getSavedTimeForUser", () => {
const endpoint = `${getbaseURL()}/api/getSavedTimeForUser`;
const user1 = "getSavedTimeForUserUser";
before(async () => {
const startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", "views", "shadowHidden") VALUES';
await db.prepare("run", `${startOfQuery}(?, ?, ?, ?, ?, ?, ?, ?, ?)`,
["getSavedTimeForUser", 1, 11, 2, "gstfu0", getHash("getSavedTimeForUserUser"), 0, 50, 0]);
["getSavedTimeForUser", 1, 11, 2, "gstfu0", getHash(user1), 0, 50, 0]);
return;
});
it("Should be able to get a 200", (done: Done) => {
fetch(`${endpoint}?userID=getSavedTimeForUserUser`)
.then(async res => {
const data = await res.json();
it("Should be able to get a 200", (done) => {
client.get(endpoint, { params: { userID: user1 }})
.then(res => {
// (end-start)*minute * views
const savedMinutes = ((11-1)/60) * 50;
const expected = {
timeSaved: savedMinutes
};
deepStrictEqual(data, expected);
deepStrictEqual(res.data, expected);
done();
})
.catch((err) => done(err));

View file

@ -1,11 +1,9 @@
import fetch from "node-fetch";
import {db} from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { client } from "../utils/httpClient";
import assert from "assert";
describe("getSearchSegments", () => {
const endpoint = `${getbaseURL()}/api/searchSegments`;
const endpoint = "/api/searchSegments";
before(async () => {
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "views", "locked", "hidden", "shadowHidden", "timeSubmitted", "UUID", "userID", "category", "actionType") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
await db.prepare("run", query, ["searchTest0", 0, 1, 2, 0, 0, 0, 0, 1, "search-normal", "searchTestUser", "sponsor", "skip"]);
@ -34,11 +32,11 @@ describe("getSearchSegments", () => {
return;
});
it("Should be able to show all segments under searchTest0", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0`)
.then(async res => {
it("Should be able to show all segments under searchTest0", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 5);
assert.strictEqual(data.page, 0);
@ -52,11 +50,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter by category", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0&category=selfpromo`)
.then(async res => {
it("Should be able to filter by category", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0", category: "selfpromo" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -66,11 +64,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter by category", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0&category=selfpromo`)
.then(async res => {
it("Should be able to filter by category", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0", category: "selfpromo" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -80,11 +78,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter by lock status", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0&locked=false`)
.then(async res => {
it("Should be able to filter by lock status", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0", locked: false }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 4);
assert.strictEqual(data.page, 0);
@ -97,11 +95,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter by hide status", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0&hidden=false`)
.then(async res => {
it("Should be able to filter by hide status", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0", hidden: false }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 4);
assert.strictEqual(data.page, 0);
@ -114,11 +112,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter by ignored status", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest0&ignored=false`)
.then(async res => {
it("Should be able to filter by ignored status", (done) => {
client.get(endpoint, { params: { videoID: "searchTest0", ignored: false }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 3);
assert.strictEqual(data.page, 0);
@ -130,11 +128,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by min views", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest1&minViews=6`)
.then(async res => {
it("Should be able to filter segments by min views", (done) => {
client.get(endpoint, { params: { videoID: "searchTest1", minViews: 6 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -144,11 +142,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by max views", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest1&maxViews=10`)
.then(async res => {
it("Should be able to filter segments by max views", (done) => {
client.get(endpoint, { params: { videoID: "searchTest1", maxViews: 10 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -158,11 +156,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by min and max views", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest1&maxViews=10&minViews=1`)
.then(async res => {
it("Should be able to filter segments by min and max views", (done) => {
client.get(endpoint, { params: { videoID: "searchTest1", maxViews: 10, minViews: 1 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -172,11 +170,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by min votes", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest2&minVotes=0`)
.then(async res => {
it("Should be able to filter segments by min votes", (done) => {
client.get(endpoint, { params: { videoID: "searchTest2", minVotes: 0 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 2);
assert.strictEqual(data.page, 0);
@ -187,11 +185,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by max votes", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest2&maxVotes=10`)
.then(async res => {
it("Should be able to filter segments by max votes", (done) => {
client.get(endpoint, { params: { videoID: "searchTest2", maxVotes: 10 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 2);
assert.strictEqual(data.page, 0);
@ -202,11 +200,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to filter segments by both min and max votes", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest2&maxVotes=10&minVotes=0`)
.then(async res => {
it("Should be able to filter segments by both min and max votes", (done) => {
client.get(endpoint, { params: { videoID: "searchTest2", maxVotes: 10, minVotes: 0 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 1);
assert.strictEqual(data.page, 0);
@ -216,11 +214,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to get first page of results", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest4`)
.then(async res => {
it("Should be able to get first page of results", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 0);
@ -239,11 +237,11 @@ describe("getSearchSegments", () => {
.catch(err => done(err));
});
it("Should be able to get second page of results", (done: Done) => {
fetch(`${endpoint}?videoID=searchTest4&page=1`)
.then(async res => {
it("Should be able to get second page of results", (done) => {
client.get(endpoint, { params: { videoID: "searchTest4", page: 1 }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const segments = data.segments;
assert.strictEqual(data.segmentCount, 12);
assert.strictEqual(data.page, 1);

View file

@ -1,9 +1,7 @@
import fetch from "node-fetch";
import {db} from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import assert from "assert";
import { client } from "../utils/httpClient";
const ENOENTID = "0".repeat(64);
const upvotedID = `a${"0".repeat(63)}`;
@ -27,8 +25,10 @@ const userAgents = {
blank: ""
};
const endpoint = "/api/segmentInfo";
const singleUUIDLookup = (UUID: string) => client.get(endpoint, { params: { UUID } });
describe("getSegmentInfo", () => {
const endpoint = `${getbaseURL()}/api/segmentInfo`;
before(async () => {
const insertQuery = `INSERT INTO
"sponsorTimes"("videoID", "startTime", "endTime", "votes", "locked",
@ -49,154 +49,146 @@ describe("getSegmentInfo", () => {
await db.prepare("run", insertQuery, ["segmentInfoFiller", 5, 6, 1, 0, fillerID5, "segmentInfoUser", 0, 50, 0, 0, userAgents.blank]);
});
it("Should be able to retreive upvoted segment", (done: Done) => {
fetch(`${endpoint}?UUID=${upvotedID}`)
.then(async res => {
it("Should be able to retreive upvoted segment", (done) => {
singleUUIDLookup(upvotedID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
userAgent: userAgents.vanced,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive downvoted segment", (done: Done) => {
fetch(`${endpoint}?UUID=${downvotedID}`)
.then(async res => {
it("Should be able to retreive downvoted segment", (done) => {
singleUUIDLookup(downvotedID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoDownvoted",
votes: -2,
userAgent: userAgents.meabot,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive locked up segment", (done: Done) => {
fetch(`${endpoint}?UUID=${lockedupID}`)
.then(async res => {
it("Should be able to retreive locked up segment", (done) => {
singleUUIDLookup(lockedupID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoLockedup",
locked: 1,
votes: 2,
userAgent: userAgents.mpv,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive infinite vote segment", (done: Done) => {
fetch(`${endpoint}?UUID=${infvotesID}`)
.then(async res => {
it("Should be able to retreive infinite vote segment", (done) => {
singleUUIDLookup(infvotesID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoInfvotes",
votes: 100000,
userAgent: userAgents.nodesb,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive shadowhidden segment", (done: Done) => {
fetch(`${endpoint}?UUID=${shadowhiddenID}`)
.then(async res => {
it("Should be able to retreive shadowhidden segment", (done) => {
singleUUIDLookup(shadowhiddenID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoShadowhidden",
shadowHidden: 1,
userAgent: userAgents.blank,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive locked down segment", (done: Done) => {
fetch(`${endpoint}?UUID=${lockeddownID}`)
.then(async res => {
it("Should be able to retreive locked down segment", (done) => {
singleUUIDLookup(lockeddownID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoLockedown",
locked: 1,
votes: -2,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive hidden segment", (done: Done) => {
fetch(`${endpoint}?UUID=${hiddenID}`)
.then(async res => {
it("Should be able to retreive hidden segment", (done) => {
singleUUIDLookup(hiddenID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoHidden",
hidden: 1,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive segment with old ID", (done: Done) => {
fetch(`${endpoint}?UUID=${oldID}`)
.then(async res => {
it("Should be able to retreive segment with old ID", (done) => {
singleUUIDLookup(oldID)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoOldID",
votes: 1,
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive single segment in array", (done: Done) => {
fetch(`${endpoint}?UUIDs=["${upvotedID}"]`)
.then(async res => {
it("Should be able to retreive single segment in array", (done) => {
client.get(endpoint, { params: { UUIDs: `["${upvotedID}"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
}];
assert.strictEqual(data.length, 1);
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to retreive multiple segments in array", (done: Done) => {
fetch(`${endpoint}?UUIDs=["${upvotedID}", "${downvotedID}"]`)
.then(async res => {
it("Should be able to retreive multiple segments in array", (done) => {
client.get(endpoint, { params: { UUIDs: `["${upvotedID}", "${downvotedID}"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
@ -211,11 +203,11 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should be possible to send unexpected query parameters", (done: Done) => {
fetch(`${endpoint}?UUID=${upvotedID}&fakeparam=hello&category=sponsor`)
.then(async res => {
it("Should be possible to send unexpected query parameters", (done) => {
client.get(endpoint, { params: { UUID: upvotedID, fakeparam: "hello", category: "sponsor" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
@ -226,8 +218,8 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should return 400 if array passed to UUID", (done: Done) => {
fetch(`${endpoint}?UUID=["${upvotedID}", "${downvotedID}"]`)
it("Should return 400 if array passed to UUID", (done) => {
client.get(`${endpoint}?UUID=["${upvotedID}", "${downvotedID}"]`)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -235,8 +227,8 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should return 400 if bad array passed to UUIDs", (done: Done) => {
fetch(`${endpoint}?UUIDs=[not-quoted,not-quoted]`)
it("Should return 400 if bad array passed to UUIDs", (done) => {
client.get(`${endpoint}?UUIDs=[not-quoted,not-quoted]`)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -244,8 +236,8 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should return 400 if bad UUID passed", (done: Done) => {
fetch(`${endpoint}?UUID=notarealuuid`)
it("Should return 400 if bad UUID passed", (done) => {
client.get(`${endpoint}?UUID=notarealuuid`)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -253,8 +245,8 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should return 400 if bad UUIDs passed in array", (done: Done) => {
fetch(`${endpoint}?UUIDs=["notarealuuid", "anotherfakeuuid"]`)
it("Should return 400 if bad UUIDs passed in array", (done) => {
client.get(`${endpoint}?UUIDs=["notarealuuid", "anotherfakeuuid"]`)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -262,11 +254,11 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should return good UUID when mixed with bad UUIDs", (done: Done) => {
fetch(`${endpoint}?UUIDs=["${upvotedID}", "anotherfakeuuid"]`)
.then(async res => {
it("Should return good UUID when mixed with bad UUIDs", (done) => {
client.get(`${endpoint}?UUIDs=["${upvotedID}", "anotherfakeuuid"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
@ -278,13 +270,13 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should cut off array at 10", function(done: Done) {
it("Should cut off array at 10", function(done) {
this.timeout(10000);
const filledIDArray = `["${upvotedID}", "${downvotedID}", "${lockedupID}", "${shadowhiddenID}", "${lockeddownID}", "${hiddenID}", "${fillerID1}", "${fillerID2}", "${fillerID3}", "${fillerID4}", "${fillerID5}"]`;
fetch(`${endpoint}?UUIDs=${filledIDArray}`)
.then(async res => {
client.get(endpoint, { params: { UUIDs: filledIDArray }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 10);
assert.strictEqual(data[0].videoID, "segmentInfoUpvoted");
assert.strictEqual(data[0].votes, 2);
@ -295,19 +287,18 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should not duplicate reponses", (done: Done) => {
fetch(`${endpoint}?UUIDs=["${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${downvotedID}"]`)
.then(async res => {
it("Should not duplicate reponses", (done) => {
client.get(`${endpoint}?UUIDs=["${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${downvotedID}"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.length, 2);
assert.strictEqual(res.data.length, 2);
done();
})
.catch(err => done(err));
});
it("Should return 400 if UUID not found", (done: Done) => {
fetch(`${endpoint}?UUID=${ENOENTID}`)
it("Should return 400 if UUID not found", (done) => {
client.get(endpoint, { params: { UUID: ENOENTID }})
.then(res => {
if (res.status !== 400) done(`non 400 response code: ${res.status}`);
else done(); // pass
@ -315,11 +306,11 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should be able to retreive multiple segments with multiple parameters", (done: Done) => {
fetch(`${endpoint}?UUID=${upvotedID}&UUID=${downvotedID}`)
.then(async res => {
it("Should be able to retreive multiple segments with multiple parameters", (done) => {
client.get(`${endpoint}?UUID=${upvotedID}&UUID=${downvotedID}`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2,
@ -334,16 +325,15 @@ describe("getSegmentInfo", () => {
.catch(err => done(err));
});
it("Should not parse repeated UUID if UUIDs present", (done: Done) => {
fetch(`${endpoint}?UUID=${downvotedID}&UUID=${lockedupID}&UUIDs=["${upvotedID}"]`)
.then(async res => {
it("Should not parse repeated UUID if UUIDs present", (done) => {
client.get(`${endpoint}?UUID=${downvotedID}&UUID=${lockedupID}&UUIDs=["${upvotedID}"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = [{
videoID: "segmentInfoUpvoted",
votes: 2
}];
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));

View file

@ -1,6 +1,5 @@
import { getService } from "../../src/utils/getService";
import { Service } from "../../src/types/segments.model";
import assert from "assert";
describe("getService", () => {

View file

@ -1,12 +1,10 @@
import fetch from "node-fetch";
import {db} from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import assert from "assert";
import { client } from "../utils/httpClient";
describe("getSkipSegments", () => {
const endpoint = `${getbaseURL()}/api/skipSegments`;
const endpoint = "/api/skipSegments";
before(async () => {
const query = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "hidden", "shadowHidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
await db.prepare("run", query, ["getSkipSegmentID0", 1, 11, 2, 0, "uuid01", "testman", 0, 50, "sponsor", "skip", "YouTube", 100, 0, 0]);
@ -28,12 +26,11 @@ describe("getSkipSegments", () => {
return;
});
it("Should be able to get a time by category 1", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor`)
.then(async res => {
it("Should be able to get a time by category 1", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", category: "sponsor" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segment[0], 1);
assert.strictEqual(data[0].segment[1], 11);
@ -45,11 +42,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get a time by category and action type", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor&actionType=mute`)
.then(async res => {
it("Should be able to get a time by category and action type", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", category: "sponsor", actionType: "mute" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
segment: [12, 14],
category: "sponsor",
@ -63,11 +60,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get a time by category and getSkipSegmentMultiple action types", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor&actionType=mute&actionType=skip`)
.then(async res => {
it("Should be able to get a time by category and getSkipSegmentMultiple action types", (done) => {
client.get(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor&actionType=mute&actionType=skip`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
segment: [1, 11],
category: "sponsor",
@ -83,11 +80,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get a time by category and getSkipSegmentMultiple action types (JSON array)", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor&actionTypes=["mute","skip"]`)
.then(async res => {
it("Should be able to get a time by category and getSkipSegmentMultiple action types (JSON array)", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", category: "sponsor", actionTypes: `["mute","skip"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
segment: [1, 11],
category: "sponsor",
@ -102,11 +99,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get a time by category for a different service 1", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID1&category=sponsor&service=PeerTube`)
.then(async res => {
it("Should be able to get a time by category for a different service 1", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID1", category: "sponsor", service: "PeerTube" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
const expected = [{
segment: [1, 11],
category: "sponsor",
@ -120,28 +117,29 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get a time by category 2", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=intro`)
.then(async res => {
it("Should be able to get a time by category 2", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", category: "intro" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [20, 33],
category: "intro",
UUID: "uuid03"
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1);
done();
})
.catch(err => done(err));
});
it("Should be able to get a time by categories array", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&categories=["sponsor"]`)
.then(async res => {
it("Should be able to get a time by categories array", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", categories: `["sponsor"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [1, 11],
category: "sponsor",
@ -149,17 +147,17 @@ describe("getSkipSegments", () => {
videoDuration: 100
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1);
done();
})
.catch(err => done(err));
});
it("Should be able to get a time by categories array 2", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&categories=["intro"]`)
.then(async res => {
it("Should be able to get a time by categories array 2", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", categories: `["intro"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [20, 33],
category: "intro",
@ -167,14 +165,13 @@ describe("getSkipSegments", () => {
videoDuration: 101
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 1);
done();
})
.catch(err => done(err));
});
it("Should return 404 if all submissions are hidden", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID6`)
it("Should return 404 if all submissions are hidden", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID6" }})
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -182,11 +179,12 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get getSkipSegmentMultiple times by category", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentMultiple&categories=["intro"]`)
.then(async res => {
it("Should be able to get getSkipSegmentMultiple times by category", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentMultiple", categories: `["intro"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
segment: [1, 11],
category: "intro",
@ -197,17 +195,17 @@ describe("getSkipSegments", () => {
UUID: "uuid41",
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 2);
done();
})
.catch(err => done(err));
});
it("Should be able to get getSkipSegmentMultiple times by getSkipSegmentMultiple categories", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&categories=["sponsor", "intro"]`)
.then(async res => {
it("Should be able to get getSkipSegmentMultiple times by getSkipSegmentMultiple categories", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", categories: `["sponsor", "intro"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
segment: [1, 11],
category: "sponsor",
@ -218,17 +216,16 @@ describe("getSkipSegments", () => {
UUID: "uuid03",
}];
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data.length, 2);
done();
})
.catch(err => done(err));
});
it("Should be possible to send unexpected query parameters", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&fakeparam=hello&category=sponsor`)
.then(async res => {
it("Should be possible to send unexpected query parameters", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", fakeparam: "hello", category: "sponsor" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [1, 11],
@ -241,11 +238,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Low voted submissions should be hidden", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID3&category=sponsor`)
.then(async res => {
it("Low voted submissions should be hidden", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID3", category: "sponsor" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [1, 11],
@ -258,8 +255,8 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should return 404 if no segment found", (done: Done) => {
fetch(`${endpoint}?videoID=notarealvideo`)
it("Should return 404 if no segment found", (done) => {
client.get(endpoint, { params: { videoID: "notarealvideo" }})
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -267,8 +264,8 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should return 400 if bad categories argument", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&categories=[not-quoted,not-quoted]`)
it("Should return 400 if bad categories argument", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID0", categories: `[not-quoted,not-quoted]` }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -276,11 +273,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able send a comma in a query param", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID2&category=sponsor`)
.then(async res => {
it("Should be able send a comma in a query param", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentID2", category: "sponsor" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [1, 11],
@ -293,11 +290,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should always get getSkipSegmentLocked segment", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentLocked&category=intro`)
.then(async res => {
it("Should always get getSkipSegmentLocked segment", (done) => {
client.get(endpoint, { params: { videoID: "getSkipSegmentLocked", category: "intro" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [20, 33],
@ -310,11 +307,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get getSkipSegmentMultiple categories with repeating parameters", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&category=sponsor&category=intro`)
.then(async res => {
it("Should be able to get getSkipSegmentMultiple categories with repeating parameters", (done) => {
client.get(`${endpoint}?category=sponsor&category=intro`, { params: { videoID: "getSkipSegmentID0" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
segment: [1, 11],
@ -331,11 +328,11 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get, categories param overriding repeating category", (done: Done) => {
fetch(`${endpoint}?videoID=getSkipSegmentID0&categories=["sponsor"]&category=intro`)
.then(async res => {
it("Should be able to get, categories param overriding repeating category", (done) => {
client.get(`${endpoint}?videoID=getSkipSegmentID0&categories=["sponsor"]&category=intro`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segment: [1, 11],
@ -348,13 +345,13 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get specific segments with requiredSegments", (done: Done) => {
it("Should be able to get specific segments with requiredSegments", (done) => {
const required2 = "requiredSegmentVid2";
const required3 = "requiredSegmentVid3";
fetch(`${endpoint}?videoID=requiredSegmentVid&requiredSegments=["${required2}","${required3}"]`)
.then(async res => {
client.get(endpoint, { params: { videoID: "requiredSegmentVid", requiredSegments: `["${required2}","${required3}"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
UUID: required2,
@ -367,13 +364,13 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should be able to get specific segments with repeating requiredSegment", (done: Done) => {
it("Should be able to get specific segments with repeating requiredSegment", (done) => {
const required2 = "requiredSegmentVid2";
const required3 = "requiredSegmentVid3";
fetch(`${endpoint}?videoID=requiredSegmentVid&requiredSegment=${required2}&requiredSegment=${required3}`)
.then(async res => {
client.get(`${endpoint}?videoID=requiredSegmentVid&requiredSegment=${required2}&requiredSegment=${required3}`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
UUID: required2,
@ -386,9 +383,9 @@ describe("getSkipSegments", () => {
.catch(err => done(err));
});
it("Should get 400 if no videoID passed in", (done: Done) => {
fetch(`${endpoint}`)
.then(async res => {
it("Should get 400 if no videoID passed in", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();
})

View file

@ -1,20 +1,18 @@
import fetch from "node-fetch";
import {db} from "../../src/databases/databases";
import { Done, postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import {getHash} from "../../src/utils/getHash";
import {ImportMock,} from "ts-mock-imports";
import * as YouTubeAPIModule from "../../src/utils/youtubeApi";
import {YouTubeApiMock} from "../youtubeMock";
import assert from "assert";
import { client } from "../utils/httpClient";
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, "YouTubeAPI");
const sinonStub = mockManager.mock("listVideos");
sinonStub.callsFake(YouTubeApiMock.listVideos);
describe("getSkipSegmentsByHash", () => {
const endpoint = `${getbaseURL()}/api/skipSegments`;
const endpoint = "/api/skipSegments";
const getSegmentsByHash0Hash = "fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910";
const requiredSegmentVidHash = "d51822c3f681e07aef15a8855f52ad12db9eb9cf059e65b16b64c43359557f61";
before(async () => {
@ -34,8 +32,8 @@ describe("getSkipSegmentsByHash", () => {
await db.prepare("run", query, ["requiredSegmentVid", 80, 90, 2, "requiredSegmentVid-4", "testman", 0, 50, "sponsor", "skip", "YouTube", 0, 0, requiredSegmentVidHash]);
});
it("Should be able to get a 200", (done: Done) => {
fetch(`${endpoint}/3272f?categories=["sponsor", "intro"]`)
it("Should be able to get a 200", (done) => {
client.get(`${endpoint}/3272f`, { params: { categories: `["sponsor", "intro"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
done();
@ -43,46 +41,42 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should return 404 if no segments are found even if a video for the given hash is known", (done: Done) => {
fetch(`${endpoint}/3272f?categories=["shilling"]`)
.then(async res => {
it("Should return 404 if no segments are found even if a video for the given hash is known", (done) => {
client.get(`${endpoint}/3272f`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 404);
const expected = "[]";
const body = await res.text();
assert.strictEqual(body, expected);
assert.equal(res.data.length, 0);
done();
})
.catch(err => done(err));
});
it("Should be able to get an empty array if no videos", (done: Done) => {
fetch(`${endpoint}/11111?categories=["shilling"]`)
.then(async res => {
it("Should be able to get an empty array if no videos", (done) => {
client.get(`${endpoint}/11111`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 404);
const body = await res.text();
const expected = "[]";
assert.strictEqual(JSON.parse(body).length, 0);
assert.strictEqual(body, expected);
const body = res.data;
assert.strictEqual(body.length, 0);
done();
})
.catch(err => done(err));
});
it("Should be able to get an empty array if only hidden videos", (done: Done) => {
fetch(`${endpoint}/f3a1?categories=["sponsor"]`)
.then(async res => {
it("Should be able to get an empty array if only hidden videos", (done) => {
client.get(`${endpoint}/f3a1`, { params: { categories:`["sponsor"]` }})
.then(res => {
if (res.status !== 404) done(`non 404 status code, was ${res.status}`);
else {
const body = await res.text();
if (JSON.parse(body).length === 0 && body === "[]") done(); // pass
const body = res.data;
if (body.length === 0) done(); // pass
else done("non empty array returned");
}
})
.catch(err => done(err));
});
it("Should return 400 prefix too short", (done: Done) => {
fetch(`${endpoint}/11?categories=["shilling"]`)
it("Should return 400 prefix too short", (done) => {
client.get(`${endpoint}/11`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -90,10 +84,10 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should return 400 prefix too long", (done: Done) => {
it("Should return 400 prefix too long", (done) => {
const prefix = "1".repeat(50);
assert.ok(prefix.length > 33, "failed to generate long enough string");
fetch(`${endpoint}/${prefix}?categories=["shilling"]`)
client.get(`${endpoint}/${prefix}`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -101,9 +95,9 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should return 404 prefix in range", (done: Done) => {
it("Should return 404 prefix in range", (done) => {
const prefix = "1".repeat(5);
fetch(`${endpoint}/${prefix}?categories=["shilling"]`)
client.get(`${endpoint}/${prefix}`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -111,8 +105,8 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should return 400 for no hash", (done: Done) => {
fetch(`${endpoint}/?categories=["shilling"]`)
it("Should return 400 for no hash", (done) => {
client.get(`${endpoint}`, { params: { categories: `["shilling"]` }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -120,8 +114,8 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should return 400 for bad format categories", (done: Done) => {
fetch(`${endpoint}/fdaf?categories=shilling`)
it("Should return 400 for bad format categories", (done) => {
client.get(`${endpoint}/fdaf`, { params: { categories: "shilling" }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -129,11 +123,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get multiple videos", (done: Done) => {
fetch(`${endpoint}/fdaf?categories=["sponsor","intro"]`)
.then(async res => {
it("Should be able to get multiple videos", (done) => {
client.get(`${endpoint}/fdaf`, { params: { categories: `["sponsor","intro"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 2);
assert.strictEqual(data[1].segments.length, 1);
@ -142,12 +136,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get 200 for no categories (default sponsor)", (done: Done) => {
fetch(`${endpoint}/fdaf`)
.then(async res => {
it("Should be able to get 200 for no categories (default sponsor)", (done) => {
client.get(`${endpoint}/fdaf`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.length, 2);
const data = res.data;
const expected = [{
segments: [{
category: "sponsor",
@ -158,6 +151,7 @@ describe("getSkipSegmentsByHash", () => {
category: "sponsor",
}]
}];
assert.strictEqual(data.length, 2);
assert.ok(partialDeepEquals(data, expected));
assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[1].segments.length, 1);
@ -166,11 +160,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get 200 for no categories (default sponsor) with action type", (done: Done) => {
fetch(`${endpoint}/fdaf?actionType=skip`)
.then(async res => {
it("Should be able to get 200 for no categories (default sponsor) with action type", (done) => {
client.get(`${endpoint}/fdaf`, { params: { actionType: "skip" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 1);
assert.strictEqual(data[1].segments.length, 1);
@ -190,11 +184,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get 200 for no categories (default sponsor) with multiple action types", (done: Done) => {
fetch(`${endpoint}/fdaf?actionType=skip&actionType=mute`)
.then(async res => {
it("Should be able to get 200 for no categories (default sponsor) with multiple action types", (done) => {
client.get(`${endpoint}/fdaf?actionType=skip&actionType=mute`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
assert.strictEqual(data[0].segments.length, 2);
assert.strictEqual(data[1].segments.length, 1);
@ -216,11 +210,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get 200 for no categories (default sponsor) with multiple action types (JSON array)", (done: Done) => {
fetch(`${endpoint}/fdaf?actionTypes=["skip","mute"]`)
.then(async res => {
it("Should be able to get 200 for no categories (default sponsor) with multiple action types (JSON array)", (done) => {
client.get(`${endpoint}/fdaf?actionTypes=["skip","mute"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 2);
const expected = [{
segments: [{
@ -240,11 +234,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get 200 for no categories (default sponsor) for a non YouTube service", (done: Done) => {
fetch(`${endpoint}/fdaf?service=PeerTube`)
.then(async res => {
it("Should be able to get 200 for no categories (default sponsor) for a non YouTube service", (done) => {
client.get(`${endpoint}/fdaf`, { params: { service: "PeerTube" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
@ -258,11 +252,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should only return one segment when fetching highlight segments", (done: Done) => {
fetch(`${endpoint}/c962?category=poi_highlight`)
.then(async res => {
it("Should only return one segment when fetching highlight segments", (done) => {
client.get(`${endpoint}/c962`, { params: { category: "poi_highlight" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments.length, 1);
done();
@ -270,24 +264,21 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to post a segment and get it using endpoint", (done: Done) => {
it("Should be able to post a segment and get it using endpoint", (done) => {
const testID = "abc123goodVideo";
fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, {
...postJSON,
body: JSON.stringify({
client.post("/api/skipSegments", {
userID: "test-qwertyuiopasdfghjklzxcvbnm",
videoID: testID,
segments: [{
segment: [13, 17],
category: "sponsor",
}],
}),
})
.then(async () => {
fetch(`${endpoint}/${getHash(testID, 1).substring(0, 3)}`)
.then(async res => {
.then(() => {
client.get(`${endpoint}/${getHash(testID, 1).substring(0, 3)}`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
@ -303,11 +294,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(`(post) ${err}`));
});
it("Should be able to get multiple categories with repeating parameters", (done: Done) => {
fetch(`${endpoint}/fdaff4?&category=sponsor&category=intro`)
.then(async res => {
it("Should be able to get multiple categories with repeating parameters", (done) => {
client.get(`${endpoint}/fdaff4?&category=sponsor&category=intro`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
@ -326,11 +317,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get specific segments with requiredSegments", (done: Done) => {
fetch(`${endpoint}/d518?requiredSegments=["requiredSegmentVid-2","requiredSegmentVid-3"]`)
.then(async res => {
it("Should be able to get specific segments with requiredSegments", (done) => {
client.get(`${endpoint}/d518?requiredSegments=["requiredSegmentVid-2","requiredSegmentVid-3"]`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
const expected = [{
segments: [{
@ -346,11 +337,11 @@ describe("getSkipSegmentsByHash", () => {
.catch(err => done(err));
});
it("Should be able to get specific segments with repeating requiredSegment", (done: Done) => {
fetch(`${endpoint}/d518?requiredSegment=requiredSegmentVid-2&requiredSegment=requiredSegmentVid-3`)
.then(async res => {
it("Should be able to get specific segments with repeating requiredSegment", (done) => {
client.get(`${endpoint}/d518?requiredSegment=requiredSegmentVid-2&requiredSegment=requiredSegmentVid-3`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.strictEqual(data.length, 1);
assert.strictEqual(data[0].segments.length, 2);
const expected = [{

View file

@ -1,21 +1,19 @@
import assert from "assert";
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {db} from "../../src/databases/databases";
import { client } from "../utils/httpClient";
let dbVersion: number;
describe("getStatus", () => {
const endpoint = `${getbaseURL()}/api/status`;
const endpoint = "/api/status";
before(async () => {
dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
});
it("Should be able to get status", (done: Done) => {
fetch(endpoint)
.then(async res => {
it("Should be able to get status", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.ok(data.uptime); // uptime should be greater than 1s
assert.strictEqual(data.commit, "test");
assert.strictEqual(data.db, Number(dbVersion));
@ -26,57 +24,52 @@ describe("getStatus", () => {
.catch(err => done(err));
});
it("Should be able to get uptime only", (done: Done) => {
fetch(`${endpoint}/uptime`)
.then(async res => {
it("Should be able to get uptime only", (done) => {
client.get(`${endpoint}/uptime`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
assert.ok(Number(data) >= 1); // uptime should be greater than 1s
assert.ok(Number(res.data) >= 1); // uptime should be greater than 1s
done();
})
.catch(err => done(err));
});
it("Should be able to get commit only", (done: Done) => {
fetch(`${endpoint}/commit`)
.then(async res => {
it("Should be able to get commit only", (done) => {
client.get(`${endpoint}/commit`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
assert.strictEqual(data, "test"); // commit should be test
assert.strictEqual(res.data, "test"); // commit should be test
done();
})
.catch(err => done(err));
});
it("Should be able to get db only", (done: Done) => {
fetch(`${endpoint}/db`)
.then(async res => {
it("Should be able to get db only", (done) => {
client.get(`${endpoint}/db`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
assert.strictEqual(Number(data), Number(dbVersion)); // commit should be test
assert.strictEqual(Number(res.data), Number(dbVersion)); // commit should be test
done();
})
.catch(err => done(err));
});
it("Should be able to get startTime only", (done: Done) => {
fetch(`${endpoint}/startTime`)
.then(async res => {
it("Should be able to get startTime only", (done) => {
client.get(`${endpoint}/startTime`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
const now = Date.now();
assert.ok(Number(data) <= now); // startTime should be more than now
assert.ok(Number(res.data) <= now); // startTime should be more than now
done();
})
.catch(err => done(err));
});
it("Should be able to get processTime only", (done: Done) => {
fetch(`${endpoint}/processTime`)
.then(async res => {
it("Should be able to get processTime only", (done) => {
client.get(`${endpoint}/processTime`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
assert.ok(Number(data) >= 0);
assert.ok(Number(res.data) >= 0);
done();
})
.catch(err => done(err));

View file

@ -1,12 +1,13 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import assert from "assert";
import { client } from "../utils/httpClient";
import { AxiosResponse } from "axios";
const endpoint = "/api/userID";
const getUserName = (username: string): Promise<AxiosResponse> => client.get(endpoint, { params: { username }});
describe("getUserID", () => {
const endpoint = `${getbaseURL()}/api/userID`;
before(async () => {
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_01"), "fuzzy user 01"]);
@ -23,17 +24,17 @@ describe("getUserID", () => {
await db.prepare("run", insertUserNameQuery, [getHash("getuserid_user_12"), "a"]);
});
it("Should be able to get a 200", (done: Done) => {
fetch(`${endpoint}?username=fuzzy+user+01`)
.then(async res => {
it("Should be able to get a 200", (done) => {
getUserName("fuzzy user 01")
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to get a 400 (No username parameter)", (done: Done) => {
fetch(endpoint)
it("Should be able to get a 400 (No username parameter)", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -41,17 +42,17 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("Should be able to get a 200 (username is public id)", (done: Done) => {
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}`)
.then(async res => {
it("Should be able to get a 200 (username is public id)", (done) => {
client.get(endpoint, { params: { username: getHash("getuserid_user_06") }})
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to get a 400 (username longer than 64 chars)", (done: Done) => {
fetch(`${endpoint}?username=${getHash("getuserid_user_06")}0`)
it("Should be able to get a 400 (username longer than 64 chars)", (done) => {
client.get(endpoint, { params: { username: `${getHash("getuserid_user_06")}0` }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -59,24 +60,23 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("Should be able to get single username", (done: Done) => {
fetch(`${endpoint}?username=fuzzy+user+01`)
.then(async res => {
it("Should be able to get single username", (done) => {
client.get(endpoint, { params: { username: "fuzzy user 01" }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
userID: getHash("getuserid_user_01")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get multiple fuzzy user info from start", (done: Done) => {
fetch(`${endpoint}?username=fuzzy+user`)
.then(async res => {
it("Should be able to get multiple fuzzy user info from start", (done) => {
getUserName("fuzzy user")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
@ -85,16 +85,15 @@ describe("getUserID", () => {
userName: "fuzzy user 02",
userID: getHash("getuserid_user_02")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get multiple fuzzy user info from middle", (done: Done) => {
fetch(`${endpoint}?username=user`)
.then(async res => {
it("Should be able to get multiple fuzzy user info from middle", (done) => {
getUserName("user")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
@ -106,48 +105,45 @@ describe("getUserID", () => {
userName: "specific user 03",
userID: getHash("getuserid_user_03")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get with public ID", (done: Done) => {
it("Should be able to get with public ID", (done) => {
const userID = getHash("getuserid_user_06");
fetch(`${endpoint}?username=${userID}`)
.then(async res => {
getUserName(userID)
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: userID,
userID
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get with fuzzy public ID", (done: Done) => {
it("Should be able to get with fuzzy public ID", (done) => {
const userID = getHash("getuserid_user_06");
fetch(`${endpoint}?username=${userID.substr(10,60)}`)
.then(async res => {
getUserName(userID.substr(10,60))
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: userID,
userID
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get repeating username", (done: Done) => {
fetch(`${endpoint}?username=repeating`)
.then(async res => {
it("Should be able to get repeating username", (done) => {
getUserName("repeating")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
@ -156,16 +152,15 @@ describe("getUserID", () => {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get repeating fuzzy username", (done: Done) => {
fetch(`${endpoint}?username=peat`)
.then(async res => {
it("Should be able to get repeating fuzzy username", (done) => {
getUserName("peat")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
@ -174,45 +169,42 @@ describe("getUserID", () => {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should avoid ReDOS with _", (done: Done) => {
fetch(`${endpoint}?username=_redos_`)
.then(async res => {
it("should avoid ReDOS with _", (done) => {
getUserName("_redos_")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "_redos_",
userID: getHash("getuserid_user_09")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should avoid ReDOS with %", (done: Done) => {
fetch(`${endpoint}?username=%redos%`)
.then(async res => {
it("should avoid ReDOS with %", (done) => {
getUserName("%redos%")
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "%redos%",
userID: getHash("getuserid_user_08")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 404 if escaped backslashes present", (done: Done) => {
fetch(`${endpoint}?username=%redos\\\\_`)
it("should return 404 if escaped backslashes present", (done) => {
getUserName("%redos\\\\_")
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -220,8 +212,8 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("should return 404 if backslashes present", (done: Done) => {
fetch(`${endpoint}?username=\\%redos\\_`)
it("should return 404 if backslashes present", (done) => {
getUserName(`\\%redos\\_`)
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -229,23 +221,22 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("should return user if just backslashes", (done: Done) => {
fetch(`${endpoint}?username=\\\\\\`)
.then(async res => {
it("should return user if just backslashes", (done) => {
getUserName(`\\\\\\`)
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "\\\\\\",
userID: getHash("getuserid_user_11")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should not allow usernames more than 64 characters", (done: Done) => {
fetch(`${endpoint}?username=${"0".repeat(65)}`)
it("should not allow usernames more than 64 characters", (done) => {
getUserName("0".repeat(65))
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -253,8 +244,8 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("should not allow usernames less than 3 characters", (done: Done) => {
fetch(`${endpoint}?username=aa`)
it("should not allow usernames less than 3 characters", (done) => {
getUserName("aa")
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -262,24 +253,23 @@ describe("getUserID", () => {
.catch(err => done(err));
});
it("should allow exact match", (done: Done) => {
fetch(`${endpoint}?username=a&exact=true`)
.then(async res => {
it("should allow exact match", (done) => {
client.get(endpoint, { params: { username: "a", exact: true }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "a",
userID: getHash("getuserid_user_12")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to get repeating username with exact username", (done: Done) => {
fetch(`${endpoint}?username=repeating&exact=true`)
.then(async res => {
it("Should be able to get repeating username with exact username", (done) => {
client.get(endpoint, { params: { username: "repeating", exact: true }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "repeating",
@ -288,16 +278,15 @@ describe("getUserID", () => {
userName: "repeating",
userID: getHash("getuserid_user_05")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should not get exact unless explicitly set to true", (done: Done) => {
fetch(`${endpoint}?username=user&exact=1`)
.then(async res => {
it("Should not get exact unless explicitly set to true", (done) => {
client.get(endpoint, { params: { username: "user", exact: 1 }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = [{
userName: "fuzzy user 01",
@ -309,15 +298,14 @@ describe("getUserID", () => {
userName: "specific user 03",
userID: getHash("getuserid_user_03")
}];
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("should return 400 if no username parameter specified", (done: Done) => {
fetch(endpoint)
it("should return 400 if no username parameter specified", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();

View file

@ -1,13 +1,11 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import {db} from "../../src/databases/databases";
import {getHash} from "../../src/utils/getHash";
import assert from "assert";
import { client } from "../utils/httpClient";
describe("getUserInfo", () => {
const endpoint = `${getbaseURL()}/api/userInfo`;
const endpoint = "/api/userInfo";
before(async () => {
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_01"), "Username user 01"]);
@ -36,8 +34,8 @@ describe("getUserInfo", () => {
await db.prepare("run", insertBanQuery, [getHash("getuserinfo_ban_01")]);
});
it("Should be able to get a 200", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_user_01`)
it("Should be able to get a 200", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_user_01" }})
.then(res => {
assert.strictEqual(res.status, 200);
done();
@ -45,8 +43,8 @@ describe("getUserInfo", () => {
.catch(err => done(err));
});
it("Should be able to get a 400 (No userID parameter)", (done: Done) => {
fetch(endpoint)
it("Should be able to get a 400 (No userID parameter)", (done) => {
client.get(endpoint, { params: { userID: "" }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -54,9 +52,9 @@ describe("getUserInfo", () => {
.catch(err => done(err));
});
it("Should be able to get user info", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_user_01`)
.then(async res => {
it("Should be able to get user info", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_user_01" }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = {
userName: "Username user 01",
@ -72,98 +70,91 @@ describe("getUserInfo", () => {
warnings: 0,
warningReason: ""
};
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should get warning data", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_0&value=warnings`)
.then(async res => {
it("Should get warning data", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_0", value: "warnings" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 1
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should get warning data with public ID", (done: Done) => {
fetch(`${endpoint}?publicUserID=${getHash("getuserinfo_warning_0")}&values=["warnings"]`)
.then(async res => {
it("Should get warning data with public ID", (done) => {
client.get(endpoint, { params: { publicUserID: getHash("getuserinfo_warning_0"), values: `["warnings"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 1
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should get multiple warnings", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_1&value=warnings`)
.then(async res => {
it("Should get multiple warnings", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_1", value: "warnings" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 2
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should not get warnings if none", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_2&value=warnings`)
.then(async res => {
it("Should not get warnings if none", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_2", value: "warnings" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 0,
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should done(userID for userName (No userName set)", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_user_02&value=userName`)
.then(async res => {
it("Should done(userID for userName (No userName set)", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_user_02", value: "userName" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
userName: "c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f"
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should return null segment if none", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_null&value=lastSegmentID`)
.then(async res => {
it("Should return null segment if none", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_null", value: "lastSegmentID" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.strictEqual(data.lastSegmentID, null);
assert.strictEqual(res.data.lastSegmentID, null);
done();
})
.catch(err => done(err));
});
it("Should return zeroes if userid does not exist", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_null&value=lastSegmentID`)
.then(async res => {
const data = await res.json();
it("Should return zeroes if userid does not exist", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_null", value: "lastSegmentID" }})
.then(res => {
const data = res.data;
for (const value in data) {
if (data[value] === null && value !== "lastSegmentID") {
done(`returned null for ${value}`);
@ -174,115 +165,108 @@ describe("getUserInfo", () => {
.catch(err => done(err));
});
it("Should get warning reason from from single enabled warning", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_0&values=["warningReason"]`)
.then(async res => {
it("Should get warning reason from from single enabled warning", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_0", values: `["warningReason"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warningReason: "warning0-0",
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should get most recent warning from two enabled warnings", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_1&value=warningReason`)
.then(async res => {
it("Should get most recent warning from two enabled warnings", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_1", value: "warningReason" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warningReason: "warning1-1"
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should not get disabled warning", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_2&values=["warnings","warningReason"]`)
.then(async res => {
it("Should not get disabled warning", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_2", values: `["warnings","warningReason"]` }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 0,
warningReason: ""
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should not get newer disabled warning", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_3&value=warnings&value=warningReason`)
.then(async res => {
it("Should not get newer disabled warning", (done) => {
client.get(`${endpoint}?userID=getuserinfo_warning_3&value=warnings&value=warningReason`)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
warnings: 1,
warningReason: "warning3-0"
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should get 400 if bad values specified", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_warning_3&value=invalid-value`)
.then(async res => {
it("Should get 400 if bad values specified", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_warning_3", value: "invalid-value" }})
.then(res => {
assert.strictEqual(res.status, 400);
done(); // pass
})
.catch(err => done(err));
});
it("Should get ban data for banned user (only appears when specifically requested)", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_ban_01&value=banned`)
.then(async res => {
it("Should get ban data for banned user (only appears when specifically requested)", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_ban_01", value: "banned" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
banned: true
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should get ban data for unbanned user (only appears when specifically requested)", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_notban_01&value=banned`)
.then(async res => {
it("Should get ban data for unbanned user (only appears when specifically requested)", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_notban_01", value: "banned" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
banned: false
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should throw 400 on bad json in values", (done: Done) => {
fetch(`${endpoint}?userID=x&values=[userID]`)
.then(async res => {
it("Should throw 400 on bad json in values", (done) => {
client.get(endpoint, { params: { userID: "x", values: `[userID]` }})
.then(res => {
assert.strictEqual(res.status, 400);
done(); // pass
})
.catch(err => done(err));
});
it("Should return 200 on userID not found", (done: Done) => {
fetch(`${endpoint}?userID=notused-userid`)
.then(async res => {
it("Should return 200 on userID not found", (done) => {
client.get(endpoint, { params: { userID: "notused-userid" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
minutesSaved: 0,
segmentCount: 0,
@ -294,15 +278,15 @@ describe("getUserInfo", () => {
reputation: 0,
vip: false,
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done(); // pass
})
.catch(err => done(err));
});
it("Should only count long segments as 10 minutes", (done: Done) => {
fetch(`${endpoint}?userID=getuserinfo_user_03`)
.then(async res => {
it("Should only count long segments as 10 minutes", (done) => {
client.get(endpoint, { params: { userID: "getuserinfo_user_03" }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = {
userName: "807e0a5d0a62c9c4365fae3d403e4618a3226f231314a898fa1555a0e55eab9e",
@ -318,8 +302,7 @@ describe("getUserInfo", () => {
warnings: 0,
warningReason: ""
};
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));

View file

@ -1,12 +1,11 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import { db } from "../../src/databases/databases";
import { getHash } from "../../src/utils/getHash";
import assert from "assert";
import { client } from "../utils/httpClient";
describe("getUserStats", () => {
const endpoint = "/api/userStats";
before(async () => {
const insertUserNameQuery = 'INSERT INTO "userNames" ("userID", "userName") VALUES(?, ?)';
await db.prepare("run", insertUserNameQuery, [getHash("getuserstats_user_01"), "Username user 01"]);
@ -24,8 +23,8 @@ describe("getUserStats", () => {
});
it("Should be able to get a 400 (No userID parameter)", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats`)
it("Should be able to get a 400 (No userID parameter)", (done) => {
client.get(endpoint)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -33,9 +32,9 @@ describe("getUserStats", () => {
.catch(err => done(err));
});
it("Should be able to get all user info", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01&fetchCategoryStats=true&fetchActionTypeStats=true`)
.then(async res => {
it("Should be able to get all user info", (done) => {
client.get(endpoint, { params: { userID: "getuserstats_user_01", fetchCategoryStats: true, fetchActionTypeStats: true }})
.then(res => {
assert.strictEqual(res.status, 200);
const expected = {
userName: "Username user 01",
@ -59,18 +58,17 @@ describe("getUserStats", () => {
segmentCount: 8
}
};
const data = await res.json();
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to get all zeroes for invalid userid", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_invalid`)
.then(async res => {
it("Should be able to get all zeroes for invalid userid", (done) => {
client.get(endpoint, { params: { userID: "getuserstats_user_invalid" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
for (const value in data.overallStats) {
if (data[value]) {
done(`returned non-zero for ${value}`);
@ -81,11 +79,11 @@ describe("getUserStats", () => {
.catch(err => done(err));
});
it("Should be able to get all zeroes for only ignored segments", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_02`)
.then(async res => {
it("Should be able to get all zeroes for only ignored segments", (done) => {
client.get(endpoint, { params: { userID: "getuserstats_user_02" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
for (const value in data.overallStats) {
if (data[value]) {
done(`returned non-zero for ${value}`);
@ -96,11 +94,11 @@ describe("getUserStats", () => {
.catch(err => done(err));
});
it("Should not get extra stats if not requested", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01`)
.then(async res => {
it("Should not get extra stats if not requested", (done) => {
client.get(endpoint, { params: { userID: "getuserstats_user_01" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
// check for categoryCount
if (data.categoryCount || data.actionTypeCount) {
done("returned extra stats");
@ -110,11 +108,11 @@ describe("getUserStats", () => {
.catch(err => done(err));
});
it("Should get parts of extra stats if not requested", (done: Done) => {
fetch(`${getbaseURL()}/api/userStats?userID=getuserstats_user_01&fetchActionTypeStats=true`)
.then(async res => {
it("Should get parts of extra stats if not requested", (done) => {
client.get(endpoint, { params: { userID: "getuserstats_user_01", fetchActionTypeStats: true }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
// check for categoryCount
if (data.categoryCount && !data.actionTypeCount) {
done("returned extra stats");

View file

@ -1,10 +1,8 @@
import fetch from "node-fetch";
import { Done, postJSON } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import {getHash} from "../../src/utils/getHash";
import {db} from "../../src/databases/databases";
import assert from "assert";
import {LockCategory } from "../../src/types/segments.model";
import { client } from "../utils/httpClient";
const stringDeepEquals = (a: string[] ,b: string[]): boolean => {
let result = true;
@ -14,8 +12,8 @@ const stringDeepEquals = (a: string[] ,b: string[]): boolean => {
return result;
};
const endpoint = `${getbaseURL()}/api/lockCategories`;
const submitEndpoint = `${getbaseURL()}/api/skipSegments`;
const endpoint = "/api/lockCategories";
const submitEndpoint = "/api/skipSegments";
const checkLockCategories = (videoID: string): Promise<LockCategory[]> => db.prepare("all", 'SELECT * FROM "lockCategories" WHERE "videoID" = ?', [videoID]);
const lockVIPUser = "lockCategoriesRecordsVIPUser";
const lockVIPUserHash = getHash(lockVIPUser);
@ -44,7 +42,7 @@ describe("lockCategoriesRecords", () => {
assert.ok(version > 1);
});
it("Should be able to submit categories not in video (http response)", (done: Done) => {
it("Should be able to submit categories not in video (http response)", (done) => {
const json = {
videoID: "no-segments-video-id",
userID: "lockCategoriesRecordsVIPUser",
@ -63,20 +61,16 @@ describe("lockCategoriesRecords", () => {
"shilling",
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json)
})
.then(async res => {
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.deepStrictEqual(data, expected);
assert.deepStrictEqual(res.data, expected);
done();
})
.catch(err => done(err));
});
it("Should be able to submit categories not in video (sql check)", (done: Done) => {
it("Should be able to submit categories not in video (sql check)", (done) => {
const videoID = "no-segments-video-id-1";
const json = {
videoID,
@ -90,10 +84,7 @@ describe("lockCategoriesRecords", () => {
"intro",
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json)
})
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID);
@ -111,7 +102,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should be able to submit categories not in video with reason (http response)", (done: Done) => {
it("Should be able to submit categories not in video with reason (http response)", (done) => {
const videoID = "no-segments-video-id";
const json = {
videoID,
@ -126,7 +117,6 @@ describe("lockCategoriesRecords", () => {
],
reason: "new reason"
};
const expected = {
submitted: [
"outro",
@ -135,20 +125,16 @@ describe("lockCategoriesRecords", () => {
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json)
})
.then(async res => {
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
assert.deepStrictEqual(data.submitted, expected.submitted);
assert.deepStrictEqual(res.data.submitted, expected.submitted);
done();
})
.catch(err => done(err));
});
it("Should be able to submit categories not in video with reason (sql check)", (done: Done) => {
it("Should be able to submit categories not in video with reason (sql check)", (done) => {
const videoID = "no-segments-video-id-1";
const json = {
videoID,
@ -170,10 +156,7 @@ describe("lockCategoriesRecords", () => {
"intro"
];
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json)
})
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID);
@ -192,7 +175,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should be able to submit categories with _ in the category", (done: Done) => {
it("Should be able to submit categories with _ in the category", (done) => {
const json = {
videoID: "underscore",
userID: lockVIPUser,
@ -200,10 +183,7 @@ describe("lockCategoriesRecords", () => {
"word_word",
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories("underscore");
@ -213,7 +193,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should be able to submit categories with upper and lower case in the category", (done: Done) => {
it("Should be able to submit categories with upper and lower case in the category", (done) => {
const json = {
videoID: "bothCases",
userID: lockVIPUser,
@ -221,10 +201,7 @@ describe("lockCategoriesRecords", () => {
"wordWord",
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories("bothCases");
@ -234,7 +211,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should not be able to submit categories with $ in the category", (done: Done) => {
it("Should not be able to submit categories with $ in the category", (done) => {
const videoID = "specialChar";
const json = {
videoID,
@ -244,10 +221,7 @@ describe("lockCategoriesRecords", () => {
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID);
@ -257,11 +231,8 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 for missing params", (done: Done) => {
fetch(endpoint, {
...postJSON,
body: JSON.stringify({}),
})
it("Should return 400 for missing params", (done) => {
client.post(endpoint, {})
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -269,16 +240,13 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 for no categories", (done: Done) => {
it("Should return 400 for no categories", (done) => {
const json: any = {
videoID: "test",
userID: "test",
categories: [],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -286,17 +254,14 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 for no userID", (done: Done) => {
const json: any = {
it("Should return 400 for no userID", (done) => {
const json = {
videoID: "test",
userID: null,
categories: ["sponsor"],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -304,17 +269,14 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 for no videoID", (done: Done) => {
const json: any = {
it("Should return 400 for no videoID", (done) => {
const json = {
videoID: null,
userID: "test",
categories: ["sponsor"],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -322,17 +284,14 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 object categories", (done: Done) => {
it("Should return 400 object categories", (done) => {
const json = {
videoID: "test",
userID: "test",
categories: {},
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -340,17 +299,14 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 400 bad format categories", (done: Done) => {
it("Should return 400 bad format categories", (done) => {
const json = {
videoID: "test",
userID: "test",
categories: "sponsor",
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 400);
done();
@ -358,7 +314,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should return 403 if user is not VIP", (done: Done) => {
it("Should return 403 if user is not VIP", (done) => {
const json = {
videoID: "test",
userID: "test",
@ -367,10 +323,7 @@ describe("lockCategoriesRecords", () => {
],
};
fetch(endpoint, {
...postJSON,
body: JSON.stringify(json),
})
client.post(endpoint, json)
.then(res => {
assert.strictEqual(res.status, 403);
done();
@ -378,7 +331,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should be able to delete a lockCategories record", (done: Done) => {
it("Should be able to delete a lockCategories record", (done) => {
const videoID = "delete-record";
const json = {
videoID,
@ -388,13 +341,7 @@ describe("lockCategoriesRecords", () => {
],
};
fetch(endpoint, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(json),
})
client.delete(endpoint, { data: json })
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID);
@ -404,7 +351,7 @@ describe("lockCategoriesRecords", () => {
.catch(err => done(err));
});
it("Should be able to delete one lockCategories record without removing another", (done: Done) => {
it("Should be able to delete one lockCategories record without removing another", (done) => {
const videoID = "delete-record-1";
const json = {
videoID,
@ -414,13 +361,7 @@ describe("lockCategoriesRecords", () => {
],
};
fetch(endpoint, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(json),
})
client.delete(endpoint, { data: json })
.then(async res => {
assert.strictEqual(res.status, 200);
const result = await checkLockCategories(videoID);
@ -437,29 +378,24 @@ describe("lockCategoriesRecords", () => {
*/
const lockedVideoID = "lockCategoryVideo";
const testSubmitUser = "testman42-qwertyuiopasdfghjklzxcvbnm";
it("Should not be able to submit a segment to a video with a lock-category record (single submission)", (done: Done) => {
fetch(submitEndpoint, {
...postJSON,
body: JSON.stringify({
it("Should not be able to submit a segment to a video with a lock-category record (single submission)", (done) => {
client.post(submitEndpoint, {
userID: testSubmitUser,
videoID: lockedVideoID,
segments: [{
segment: [20, 40],
category: "sponsor",
}],
}),
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
.catch(err => done(err));
});
it("Should not be able to submit segments to a video where any of the submissions with a no-segment record", (done: Done) => {
fetch(submitEndpoint, {
...postJSON,
body: JSON.stringify({
it("Should not be able to submit segments to a video where any of the submissions with a no-segment record", (done) => {
client.post(submitEndpoint, {
userID: testSubmitUser,
videoID: lockedVideoID,
segments: [{
@ -468,10 +404,9 @@ describe("lockCategoriesRecords", () => {
}, {
segment: [50, 60],
category: "intro",
}],
},),
}]
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 403);
done();
})
@ -479,45 +414,39 @@ describe("lockCategoriesRecords", () => {
});
it("Should be able to submit a segment to a video with a different no-segment record", (done: Done) => {
fetch(submitEndpoint, {
...postJSON,
body: JSON.stringify({
it("Should be able to submit a segment to a video with a different no-segment record", (done) => {
client.post(submitEndpoint, {
userID: testSubmitUser,
videoID: lockedVideoID,
segments: [{
segment: [20, 40],
category: "intro",
}],
}),
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("Should be able to submit a segment to a video with no no-segment records", (done: Done) => {
fetch(submitEndpoint, {
...postJSON,
body: JSON.stringify({
it("Should be able to submit a segment to a video with no no-segment records", (done) => {
client.post(submitEndpoint, {
userID: testSubmitUser,
videoID: "normalVideo",
segments: [{
segment: [20, 40],
category: "intro",
}],
}),
})
.then(async res => {
.then(res => {
assert.strictEqual(res.status, 200);
done();
})
.catch(err => done(err));
});
it("should be able to get existing category lock", (done: Done) => {
it("should be able to get existing category lock", (done) => {
const expected = {
categories: [
"sponsor",
@ -526,10 +455,10 @@ describe("lockCategoriesRecords", () => {
"shilling"
],
};
fetch(`${endpoint}?videoID=no-segments-video-id`)
.then(async res => {
client.get(endpoint, { params: {videoID: "no-segments-video-id" }})
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const data = res.data;
assert.ok(stringDeepEquals(data.categories, expected.categories));
done();
})

View file

@ -1,11 +1,10 @@
import fetch from "node-fetch";
import {db} from "../../src/databases/databases";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";
import assert from "assert";
import { client } from "../utils/httpClient";
const endpoint = `${getbaseURL()}/api/getVideoSponsorTimes`;
const endpoint = "/api/getVideoSponsorTimes";
const getOldSponsorTime = (videoID: string) => client.get(endpoint, { params: { videoID } });
describe("getVideoSponsorTime (Old get method)", () => {
before(async () => {
@ -14,8 +13,8 @@ describe("getVideoSponsorTime (Old get method)", () => {
await db.prepare("run", insertSponsorTimes, ["oldGetSponsorTime1,test", 1, 11, 2, "oldGetSponsorTime01", "oldGetSponsorTimeUser", 0, 50, "sponsor", 0]);
});
it("Should be able to get a time", (done: Done) => {
fetch(`${endpoint}?videoID=oldGetSponsorTime0`)
it("Should be able to get a time", (done) => {
getOldSponsorTime("oldGetSponsorTime0")
.then(res => {
assert.strictEqual(res.status, 200);
done();
@ -23,8 +22,8 @@ describe("getVideoSponsorTime (Old get method)", () => {
.catch(err => done(err));
});
it("Should return 404 if no segment found", (done: Done) => {
fetch(`${endpoint}?videoID=notarealvideo`)
it("Should return 404 if no segment found", (done) => {
getOldSponsorTime("notarealvideo")
.then(res => {
assert.strictEqual(res.status, 404);
done();
@ -33,8 +32,8 @@ describe("getVideoSponsorTime (Old get method)", () => {
});
it("Should be possible to send unexpected query parameters", (done: Done) => {
fetch(`${getbaseURL()}/api/getVideoSponsorTimes?videoID=oldGetSponsorTime0&fakeparam=hello`)
it("Should be possible to send unexpected query parameters", (done) => {
client.get(endpoint, { params: { videoID: "oldGetSponsorTime0", fakeparam: "hello" }})
.then(res => {
assert.strictEqual(res.status, 200);
done();
@ -42,30 +41,28 @@ describe("getVideoSponsorTime (Old get method)", () => {
.catch(() => done("couldn't callendpoint"));
});
it("Should be able send a comma in a query param", (done: Done) => {
fetch(`${getbaseURL()}/api/getVideoSponsorTimes?videoID=oldGetSponsorTime1,test`)
.then(async res => {
it("Should be able send a comma in a query param", (done) => {
getOldSponsorTime("oldGetSponsorTime1,test")
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
UUIDs: ["oldGetSponsorTime01"],
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));
});
it("Should be able to get the correct time", (done: Done) => {
fetch(`${getbaseURL()}/api/getVideoSponsorTimes?videoID=oldGetSponsorTime0`)
.then(async res => {
it("Should be able to get the correct time", (done) => {
getOldSponsorTime("oldGetSponsorTime0")
.then(res => {
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
sponsorTimes: [[1, 11]],
UUIDs: ["oldGetSponsorTime00"]
};
assert.ok(partialDeepEquals(data, expected));
assert.ok(partialDeepEquals(res.data, expected));
done();
})
.catch(err => done(err));

View file

@ -1,17 +1,15 @@
import fetch from "node-fetch";
import { Done } from "../utils/utils";
import { getbaseURL } from "../utils/getBaseURL";
import { partialDeepEquals } from "../utils/partialDeepEquals";import {db} from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
const videoID1 = "dQw4w9WgXcQ";
const videoID2 = "dQw4w9WgXcE";
const userID = "testtesttesttesttesttesttesttesttest";
const endpoint = "/api/postVideoSponsorTimes";
describe("postVideoSponsorTime (Old submission method)", () => {
it("Should be able to submit a time (GET)", (done: Done) => {
fetch(`${getbaseURL()
}/api/postVideoSponsorTimes?videoID=${videoID1}&startTime=1&endTime=10&userID=${userID}`)
it("Should be able to submit a time (GET)", (done) => {
client.get(endpoint, { params: { videoID: videoID1, startTime: 1, endTime: 10, userID }})
.then(async res => {
assert.strictEqual(res.status, 200);
const row = await db.prepare("get", `SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ?`, [videoID1]);
@ -26,13 +24,11 @@ describe("postVideoSponsorTime (Old submission method)", () => {
.catch(err => done(err));
});
it("Should be able to submit a time (POST)", (done: Done) => {
fetch(`${getbaseURL()
}/api/postVideoSponsorTimes?videoID=${videoID2}&startTime=1&endTime=11&userID=${userID}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
it("Should be able to submit a time (POST)", (done) => {
client({
url: endpoint,
params: { videoID: videoID2, startTime: 1, endTime: 11, userID },
method: "post",
})
.then(async res => {
assert.strictEqual(res.status, 200);
@ -48,10 +44,9 @@ describe("postVideoSponsorTime (Old submission method)", () => {
.catch(err => done(err));
});
it("Should return 400 for missing params", (done: Done) => {
fetch(`${getbaseURL()
}/api/postVideoSponsorTimes?startTime=1&endTime=10&userID=${userID}`)
.then(async res => {
it("Should return 400 for missing params", (done) => {
client.post(endpoint, { params: { startTime: 1, endTime: 10, userID }})
.then(res => {
assert.strictEqual(res.status, 400);
done();
})

13
test/utils/httpClient.ts Normal file
View file

@ -0,0 +1,13 @@
import { config } from "../../src/config";
import axios, { AxiosRequestConfig } from "axios";
export function getbaseURL(): string {
return `http://localhost:${config.port}`;
}
export const defaultConfig: AxiosRequestConfig = {
baseURL: getbaseURL(),
validateStatus: (status) => status < 500
};
export const client = axios.create(defaultConfig);