everything to postClearCache

This commit is contained in:
Michael C
2021-09-22 17:50:06 -04:00
parent 94e1e8c377
commit c779c2c19e
21 changed files with 750 additions and 860 deletions

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({
userID: "test-qwertyuiopasdfghjklzxcvbnm",
videoID: testID,
segments: [{
segment: [13, 17],
category: "sponsor",
}],
}),
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 = [{