Merge remote-tracking branch 'upstream/master' into fix/prepare-statements

This commit is contained in:
Nanobyte
2021-06-21 18:19:50 +02:00
53 changed files with 2764 additions and 2133 deletions

View File

@@ -0,0 +1,312 @@
import fetch from 'node-fetch';
import {db} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
const ENOENTID = "0000000000000000000000000000000000000000000000000000000000000000"
const upvotedID = "a000000000000000000000000000000000000000000000000000000000000000"
const downvotedID = "b000000000000000000000000000000000000000000000000000000000000000"
const lockedupID = "c000000000000000000000000000000000000000000000000000000000000000"
const infvotesID = "d000000000000000000000000000000000000000000000000000000000000000"
const shadowhiddenID = "e000000000000000000000000000000000000000000000000000000000000000"
const lockeddownID = "f000000000000000000000000000000000000000000000000000000000000000"
const hiddenID = "1000000000000000000000000000000000000000000000000000000000000000"
const fillerID1 = "1100000000000000000000000000000000000000000000000000000000000000"
const fillerID2 = "1200000000000000000000000000000000000000000000000000000000000000"
const fillerID3 = "1300000000000000000000000000000000000000000000000000000000000000"
const fillerID4 = "1400000000000000000000000000000000000000000000000000000000000000"
const fillerID5 = "1500000000000000000000000000000000000000000000000000000000000000"
const oldID = "a0000000-0000-0000-0000-000000000000"
describe('getSegmentInfo', () => {
before(async () => {
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES';
await db.prepare("run", startOfQuery + "('upvoted', 1, 10, 2, 0, '" + upvotedID+ "', 'testman', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '" + getHash('upvoted', 1) + "')");
await db.prepare("run", startOfQuery + "('downvoted', 1, 10, -2, 0, '" + downvotedID+ "', 'testman', 0, 50, 'sponsor', 'YouTube', 120, 0, 0, '" + getHash('downvoted', 1) + "')");
await db.prepare("run", startOfQuery + "('locked-up', 1, 10, 2, 1, '"+ lockedupID +"', 'testman', 0, 50, 'sponsor', 'YouTube', 101, 0, 0, '" + getHash('locked-up', 1) + "')");
await db.prepare("run", startOfQuery + "('infvotes', 1, 10, 100000, 0, '"+infvotesID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 101, 0, 0, '" + getHash('infvotes', 1) + "')");
await db.prepare("run", startOfQuery + "('hidden', 1, 10, 2, 0, '"+hiddenID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 140, 1, 0, '" + getHash('hidden', 1) + "')");
await db.prepare("run", startOfQuery + "('shadowhidden', 1, 10, 2, 0, '"+shadowhiddenID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 140, 0, 1, '" + getHash('shadowhidden', 1) + "')");
await db.prepare("run", startOfQuery + "('locked-down', 1, 10, -2, 1, '"+lockeddownID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 200, 0, 0, '" + getHash('locked-down', 1) + "')");
await db.prepare("run", startOfQuery + "('oldID', 1, 10, 1, 0, '"+oldID+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('oldID', 1) + "')");
await db.prepare("run", startOfQuery + "('filler', 1, 2, 1, 0, '"+fillerID1+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')");
await db.prepare("run", startOfQuery + "('filler', 2, 3, 1, 0, '"+fillerID2+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')");
await db.prepare("run", startOfQuery + "('filler', 3, 4, 1, 0, '"+fillerID3+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')");
await db.prepare("run", startOfQuery + "('filler', 4, 5, 1, 0, '"+fillerID4+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')");
await db.prepare("run", startOfQuery + "('filler', 5, 6, 1, 0, '"+fillerID5+"', 'testman', 0, 50, 'sponsor', 'YouTube', 300, 0, 0, '" + getHash('filler', 1) + "')");
});
it('Should be able to retreive upvoted segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${upvotedID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "upvoted" && data[0].votes === 2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive downvoted segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${downvotedID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "downvoted" && data[0].votes === -2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive locked up segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${lockedupID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "locked-up" && data[0].locked === 1 && data[0].votes === 2) {
done();
} else {
done ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive infinite vote segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${infvotesID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "infvotes" && data[0].votes === 100000) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive shadowhidden segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${shadowhiddenID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "shadowhidden" && data[0].shadowHidden === 1) {
done();
} else {
done ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive locked down segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${lockeddownID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "locked-down" && data[0].votes === -2 && data[0].locked === 1) {
done();
} else {
done ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive hidden segment', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${hiddenID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "hidden" && data[0].hidden === 1) {
done();
} else {
done ("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive segment with old ID', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${oldID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "oldID" && data[0].votes === 1) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive single segment in array', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}"]`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].videoID === "upvoted" && data[0].votes === 2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to retreive multiple segments in array', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "${downvotedID}"]`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 2 &&
(data[0].videoID === "upvoted" && data[0].votes === 2) &&
(data[1].videoID === "downvoted" && data[1].votes === -2)) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be possible to send unexpected query parameters', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${upvotedID}&fakeparam=hello&category=sponsor`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data[0].videoID === "upvoted" && data[0].votes === 2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should return 400 if array passed to UUID', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=["${upvotedID}", "${downvotedID}"]`)
.then(res => {
if (res.status !== 400) done("non 400 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return 400 if bad array passed to UUIDs', (done: Done) => {
fetch(getbaseURL() + "/api/segmentInfo?UUIDs=[not-quoted,not-quoted]")
.then(res => {
if (res.status !== 400) done("non 404 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return 400 if bad UUID passed', (done: Done) => {
fetch(getbaseURL() + "/api/segmentInfo?UUID=notarealuuid")
.then(res => {
if (res.status !== 400) done("non 400 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return 400 if bad UUIDs passed in array', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["notarealuuid", "anotherfakeuuid"]`)
.then(res => {
if (res.status !== 400) done("non 400 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return good UUID when mixed with bad UUIDs', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "anotherfakeuuid"]`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].videoID === "upvoted" && data[0].votes === 2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("couldn't call endpoint"));
});
it('Should cut off array at 10', (done: Done) => {
const filledIDArray = `["${upvotedID}", "${downvotedID}", "${lockedupID}", "${shadowhiddenID}", "${lockeddownID}", "${hiddenID}", "${fillerID1}", "${fillerID2}", "${fillerID3}", "${fillerID4}", "${fillerID5}"]`
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=${filledIDArray}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
// last segment should be fillerID4
if (data.length === 10 && data[0].videoID === "upvoted" && data[0].votes === 2 && data[9].videoID === "filler" && data[9].UUID === fillerID4) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("couldn't call endpoint"));
});
it('Should not duplicate reponses', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${upvotedID}", "${downvotedID}"]`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 2 && data[0].videoID === "upvoted" && data[0].votes === 2 && data[1].videoID === "downvoted" && data[1].votes === -2) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return 400 if UUID not found', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${ENOENTID}`)
.then(res => {
if (res.status !== 400) done("non 400 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
});

View File

@@ -21,111 +21,104 @@ describe('getSkipSegments', () => {
});
it('Should be able to get a time by category 1', () => {
it('Should be able to get a time by category 1', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=sponsor")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0" && data[0].videoDuration === 100) {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to get a time by category for a different service 1', () => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=sponsor&service=PeerTube")
it('Should be able to get a time by category for a different service 1', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest2&category=sponsor&service=PeerTube")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0-1" && data[0].videoDuration === 120) {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
it('Should be able to get a time by category 2', () => {
it('Should be able to get a time by category 2', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=intro")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-2") {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get a time by categories array', () => {
it('Should be able to get a time by categories array', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\"]")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0" && data[0].videoDuration === 100) {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get a time by categories array 2', () => {
it('Should be able to get a time by categories array 2', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"intro\"]")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-2" && data[0].videoDuration === 101) {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should be empty if all submissions are hidden', () => {
it('Should return 404 if all submissions are hidden', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=onlyHiddenSegments")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 0) {
return;
} else {
return ("Received incorrect body: " + (await res.text()));
}
}
.then(res => {
if (res.status !== 404) done("non 404 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should be able to get multiple times by category', () => {
it('Should be able to get multiple times by category', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=multiple&categories=[\"intro\"]")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200)done("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
@@ -133,28 +126,28 @@ describe('getSkipSegments', () => {
let success = true;
for (const segment of data) {
if ((segment.segment[0] !== 20 || segment.segment[1] !== 33
|| segment.category !== "intro" || segment.UUID !== "1-uuid-7" || segment.videoDuration === 500) &&
|| segment.category !== "intro" || segment.UUID !== "1-uuid-7") &&
(segment.segment[0] !== 1 || segment.segment[1] !== 11
|| segment.category !== "intro" || segment.UUID !== "1-uuid-6" || segment.videoDuration === 400)) {
|| segment.category !== "intro" || segment.UUID !== "1-uuid-6")) {
success = false;
break;
}
}
if (success) return;
else return ("Received incorrect body: " + body);
if (success) done();
else done("Received incorrect body: " + body);
} else {
return ("Received incorrect body: " + body);
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint\n\n" + err));
});
it('Should be able to get multiple times by multiple categories', () => {
it('Should be able to get multiple times by multiple categories', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[\"sponsor\", \"intro\"]")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
@@ -171,91 +164,99 @@ describe('getSkipSegments', () => {
}
}
if (success) return;
else return ("Received incorrect body: " + body);
if (success) done();
else done("Received incorrect body: " + body);
} else {
return ("Received incorrect body: " + body);
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should be possible to send unexpected query parameters', () => {
it('Should be possible to send unexpected query parameters', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&fakeparam=hello&category=sponsor")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-0") {
return;
done();
} else {
return ("Received incorrect body: " + body);
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint"));
.catch(err => done("Couldn't call endpoint"));
});
it('Low voted submissions should be hidden', () => {
it('Low voted submissions should be hidden', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=test3&category=sponsor")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-4") {
return;
done();
} else {
return ("Received incorrect body: " + body);
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should return 404 if no segment found', () => {
it('Should return 404 if no segment found', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=notarealvideo")
.then(res => {
if (res.status !== 404) return ("non 404 respone code: " + res.status);
else return; // pass
if (res.status !== 404) done("non 404 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should return 400 if bad categories argument', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&categories=[not-quoted,not-quoted]")
.then(res => {
if (res.status !== 400) done("non 400 respone code: " + res.status);
else done(); // pass
})
.catch(err => ("couldn't call endpoint"));
});
it('Should be able send a comma in a query param', () => {
it('Should be able send a comma in a query param', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest,test&category=sponsor")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done ("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 1 && data[0].segment[0] === 1 && data[0].segment[1] === 11
&& data[0].category === "sponsor" && data[0].UUID === "1-uuid-1") {
return;
done();
} else {
return ("Received incorrect body: " + body);
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
it('Should always get locked segment', () => {
it('Should always get locked segment', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=locked&category=intro")
.then(async res => {
if (res.status !== 200) return ("Status code was: " + res.status);
if (res.status !== 200) done ("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 1 && data[0].segment[0] === 20 && data[0].segment[1] === 33
&& data[0].category === "intro" && data[0].UUID === "1-uuid-locked-8") {
return;
done();
} else {
return ("Received incorrect body: " + (await res.text()));
done("Received incorrect body: " + (await res.text()));
}
}
})

View File

@@ -19,6 +19,8 @@ describe('getSegmentsByHash', () => {
await db.prepare("run", query, ['getSegmentsByHash-noMatchHash', 40, 50, 2, 'getSegmentsByHash-noMatchHash', 'testman', 0, 50, 'sponsor', 'YouTube', 0, 0, 'fdaffnoMatchHash']);
await db.prepare("run", query, ['getSegmentsByHash-1', 60, 70, 2, 'getSegmentsByHash-1', 'testman', 0, 50, 'sponsor', 'YouTube', 0, 0, '3272fa85ee0927f6073ef6f07ad5f3146047c1abba794cfa364d65ab9921692b']);
await db.prepare("run", query, ['onlyHidden', 60, 70, 2, 'onlyHidden', 'testman', 0, 50, 'sponsor', 'YouTube', 1, 0, 'f3a199e1af001d716cdc6599360e2b062c2d2b3fa2885f6d9d2fd741166cbbd3']);
await db.prepare("run", query, ['highlightVid', 60, 60, 2, 'highlightVid-1', 'testman', 0, 50, 'highlight', 'YouTube', 0, 0, getHash('highlightVid', 1)]);
await db.prepare("run", query, ['highlightVid', 70, 70, 2, 'highlightVid-2', 'testman', 0, 50, 'highlight', 'YouTube', 0, 0, getHash('highlightVid', 1)]);
});
it('Should be able to get a 200', (done: Done) => {
@@ -158,7 +160,7 @@ describe('getSegmentsByHash', () => {
if (res.status !== 200) done("non 200 status code, was " + res.status);
else {
const body = await res.json();
if (body.length !== 1) done("expected 2 videos, got " + body.length);
if (body.length !== 1) done("expected 1 video, got " + body.length);
else if (body[0].segments.length !== 1) done("expected 1 segments for first video, got " + body[0].segments.length);
else if (body[0].segments[0].UUID !== 'getSegmentsByHash-0-0-1') done("both segments are not sponsor");
else done();
@@ -167,6 +169,20 @@ describe('getSegmentsByHash', () => {
.catch(err => done("Couldn't call endpoint"));
});
it('Should only return one segment when fetching highlight segments', (done: Done) => {
fetch(getbaseURL() + '/api/skipSegments/c962?category=highlight')
.then(async res => {
if (res.status !== 200) done("non 200 status code, was " + res.status);
else {
const body = await res.json();
if (body.length !== 1) done("expected 1 video, got " + body.length);
else if (body[0].segments.length !== 1) done("expected 1 segment, got " + body[0].segments.length);
else done();
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to post a segment and get it using endpoint', (done: Done) => {
let testID = 'abc123goodVideo';
fetch(getbaseURL() + "/api/postVideoSponsorTimes", {

View File

@@ -9,14 +9,14 @@ describe('getUserInfo', () => {
await db.prepare("run", insertUserNameQuery, [getHash("getuserinfo_user_01"), 'Username user 01']);
const sponsorTimesQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000001', getHash("getuserinfo_user_01"), 0, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000002', getHash("getuserinfo_user_01"), 0, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['yyyxxxzzz', 1, 11, -1, 'uuid000003', getHash("getuserinfo_user_01"), 0, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['yyyxxxzzz', 1, 11, -2, 'uuid000004', getHash("getuserinfo_user_01"), 0, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['xzzzxxyyy', 1, 11, -5, 'uuid000005', getHash("getuserinfo_user_01"), 0, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['zzzxxxyyy', 1, 11, 2, 'uuid000006', getHash("getuserinfo_user_02"), 0, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000007', getHash("getuserinfo_user_02"), 0, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000008', getHash("getuserinfo_user_02"), 0, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000001', getHash("getuserinfo_user_01"), 1, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000002', getHash("getuserinfo_user_01"), 2, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['yyyxxxzzz', 1, 11, -1, 'uuid000003', getHash("getuserinfo_user_01"), 3, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['yyyxxxzzz', 1, 11, -2, 'uuid000004', getHash("getuserinfo_user_01"), 4, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['xzzzxxyyy', 1, 11, -5, 'uuid000005', getHash("getuserinfo_user_01"), 5, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['zzzxxxyyy', 1, 11, 2, 'uuid000006', getHash("getuserinfo_user_02"), 6, 10, 'sponsor', 0]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000007', getHash("getuserinfo_user_02"), 7, 10, 'sponsor', 1]);
await db.prepare("run", sponsorTimesQuery, ['xxxyyyzzz', 1, 11, 2, 'uuid000008', getHash("getuserinfo_user_02"), 8, 10, 'sponsor', 1]);
const insertWarningQuery = 'INSERT INTO warnings ("userID", "issueTime", "issuerUserID", "enabled") VALUES (?, ?, ?, ?)';
await db.prepare("run", insertWarningQuery, [getHash('getuserinfo_warning_0'), 10, 'getuserinfo_vip', 1]);
@@ -25,7 +25,7 @@ describe('getUserInfo', () => {
});
it('Should be able to get a 200', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01')
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_user_01')
.then(res => {
if (res.status !== 200) done('non 200 (' + res.status + ')');
else done(); // pass
@@ -34,7 +34,7 @@ describe('getUserInfo', () => {
});
it('Should be able to get a 400 (No userID parameter)', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo')
fetch(getbaseURL() + '/api/userInfo')
.then(res => {
if (res.status !== 400) done('non 400 (' + res.status + ')');
else done(); // pass
@@ -42,8 +42,8 @@ describe('getUserInfo', () => {
.catch(err => done('couldn\'t call endpoint'));
});
it('Should done(info', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01')
it('Should be able to get user info', (done: Done) => {
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_user_01')
.then(async res => {
if (res.status !== 200) {
done("non 200");
@@ -55,8 +55,16 @@ describe('getUserInfo', () => {
done('Returned incorrect minutesSaved "' + data.minutesSaved + '"');
} else if (data.viewCount !== 30) {
done('Returned incorrect viewCount "' + data.viewCount + '"');
} else if (data.ignoredViewCount !== 20) {
done('Returned incorrect ignoredViewCount "' + data.ignoredViewCount + '"');
} else if (data.segmentCount !== 3) {
done('Returned incorrect segmentCount "' + data.segmentCount + '"');
} else if (data.ignoredSegmentCount !== 2) {
done('Returned incorrect ignoredSegmentCount "' + data.ignoredSegmentCount + '"');
} else if (data.reputation !== -2) {
done('Returned incorrect reputation "' + data.reputation + '"');
} else if (data.lastSegmentID !== "uuid000005") {
done('Returned incorrect last segment "' + data.lastSegmentID + '"');
} else {
done(); // pass
}
@@ -66,7 +74,7 @@ describe('getUserInfo', () => {
});
it('Should get warning data', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_0')
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_warning_0')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
@@ -79,8 +87,22 @@ describe('getUserInfo', () => {
.catch(err => ("couldn't call endpoint"));
});
it('Should get warning data with public ID', (done: Done) => {
fetch(getbaseURL() + '/api/userInfo?publicUserID=' + getHash("getuserinfo_warning_0"))
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();
if (data.warnings !== 1) done('wrong number of warnings: ' + data.warnings + ', not ' + 1);
else done();
}
})
.catch(err => ("couldn't call endpoint"));
});
it('Should get multiple warnings', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_1')
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_warning_1')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
@@ -93,8 +115,8 @@ describe('getUserInfo', () => {
.catch(err => ("couldn't call endpoint"));
});
it('Should not get warnings if noe', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_2')
it('Should not get warnings if none', (done: Done) => {
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_warning_2')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
@@ -108,7 +130,7 @@ describe('getUserInfo', () => {
});
it('Should done(userID for userName (No userName set)', (done: Done) => {
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_02')
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_user_02')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
@@ -122,4 +144,18 @@ describe('getUserInfo', () => {
})
.catch(err => ('couldn\'t call endpoint'));
});
it('Should return null segment if none', (done: Done) => {
fetch(getbaseURL() + '/api/userInfo?userID=getuserinfo_null')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();
if (data.lastSegmentID !== null) done('returned segment ' + data.warnings + ', not ' + null);
else done(); // pass
}
})
.catch(err => ("couldn't call endpoint"));
});
});

View File

@@ -0,0 +1,72 @@
import fetch from 'node-fetch';
import {Done, getbaseURL} from '../utils';
import {db} from '../../src/databases/databases';
import {getHash} from '../../src/utils/getHash';
describe('postClearCache', () => {
before(async () => {
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("clearing-vip") + "')");
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES';
await db.prepare("run", startOfQuery + "('clear-test', 0, 1, 2, 'clear-uuid', 'testman', 0, 50, 'sponsor', 0, '" + getHash('clear-test', 1) + "')");
});
it('Should be able to clear cache for existing video', (done: Done) => {
fetch(getbaseURL()
+ "/api/clearCache?userID=clearing-vip&videoID=clear-test", {
method: 'POST'
})
.then(res => {
if (res.status === 200) done();
else done("Status code was " + res.status);
})
.catch(err => done(err));
});
it('Should be able to clear cache for nonexistent video', (done: Done) => {
fetch(getbaseURL()
+ "/api/clearCache?userID=clearing-vip&videoID=dne-video", {
method: 'POST'
})
.then(res => {
if (res.status === 200) done();
else done("Status code was " + res.status);
})
.catch(err => done(err));
});
it('Should get 403 as non-vip', (done: Done) => {
fetch(getbaseURL()
+ "/api/clearCache?userID=regular-user&videoID=clear-tes", {
method: 'POST'
})
.then(async res => {
if (res.status !== 403) done('non 403 (' + res.status + ')');
else done(); // pass
})
.catch(err => done(err));
});
it('Should give 400 with missing videoID', (done: Done) => {
fetch(getbaseURL()
+ "/api/clearCache?userID=clearing-vip", {
method: 'POST'
})
.then(async res => {
if (res.status !== 400) done('non 400 (' + res.status + ')');
else done(); // pass
})
.catch(err => done(err));
});
it('Should give 400 with missing userID', (done: Done) => {
fetch(getbaseURL()
+ "/api/clearCache?userID=clearing-vip", {
method: 'POST'
})
.then(async res => {
if (res.status !== 400) done('non 400 (' + res.status + ')');
else done(); // pass
})
.catch(err => done(err));
});
});

View File

@@ -118,7 +118,7 @@ describe('postSkipSegments', () => {
.then(async res => {
if (res.status === 200) {
const row = await db.prepare('get', `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZX"]);
if (row.startTime === 0 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor" && row.videoDuration === 5010) {
if (row.startTime === 0 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor" && row.videoDuration === 4980) {
done();
} else {
done("Submitted times were not saved. Actual submission: " + JSON.stringify(row));
@@ -140,7 +140,7 @@ describe('postSkipSegments', () => {
body: JSON.stringify({
userID: "test",
videoID: "dQw4w9WgXZH",
videoDuration: 5010.20,
videoDuration: 4980.20,
segments: [{
segment: [1, 10],
category: "sponsor",
@@ -150,7 +150,7 @@ describe('postSkipSegments', () => {
.then(async res => {
if (res.status === 200) {
const row = await db.prepare('get', `SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ?`, ["dQw4w9WgXZH"]);
if (row.startTime === 1 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor" && row.videoDuration === 5010.20) {
if (row.startTime === 1 && row.endTime === 10 && row.locked === 0 && row.category === "sponsor" && row.videoDuration === 4980.20) {
done();
} else {
done("Submitted times were not saved. Actual submission: " + JSON.stringify(row));
@@ -237,6 +237,21 @@ describe('postSkipSegments', () => {
}
});
it('Should still not be allowed if youtube thinks duration is 0', (done: Done) => {
fetch(getbaseURL()
+ "/api/postVideoSponsorTimes?videoID=noDuration&startTime=30&endTime=10000&userID=testing", {
method: 'POST',
})
.then(async res => {
if (res.status === 403) done(); // pass
else {
const body = await res.text();
done("non 403 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to submit a single time under a different service (JSON method)', (done: Done) => {
fetch(getbaseURL()
+ "/api/postVideoSponsorTimes", {
@@ -500,6 +515,51 @@ describe('postSkipSegments', () => {
.catch(err => done("Couldn't call endpoint"));
});
it('Should be rejected if segment starts and ends at the same time', (done: Done) => {
fetch(getbaseURL()
+ "/api/skipSegments?videoID=qqwerty&startTime=90&endTime=90&userID=testing&category=intro", {
method: 'POST',
})
.then(async res => {
if (res.status === 400) done(); // pass
else {
const body = await res.text();
done("non 400 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be accepted if highlight segment starts and ends at the same time', (done: Done) => {
fetch(getbaseURL()
+ "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30&userID=testing&category=highlight", {
method: 'POST',
})
.then(async res => {
if (res.status === 200) done(); // pass
else {
const body = await res.text();
done("non 200 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be rejected if highlight segment doesn\'t start and end at the same time', (done: Done) => {
fetch(getbaseURL()
+ "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing&category=highlight", {
method: 'POST',
})
.then(async res => {
if (res.status === 400) done(); // pass
else {
const body = await res.text();
done("non 400 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be rejected if a sponsor is less than 1 second', (done: Done) => {
fetch(getbaseURL()
+ "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing", {
@@ -621,34 +681,6 @@ describe('postSkipSegments', () => {
.catch(err => done(err));
});
it('Should be allowed if youtube thinks duration is 0', (done: Done) => {
fetch(getbaseURL()
+ "/api/postVideoSponsorTimes?videoID=noDuration&startTime=30&endTime=10000&userID=testing", {
method: 'POST',
})
.then(async res => {
if (res.status === 200) done(); // pass
else {
const body = await res.text();
done("non 200 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be rejected if not a valid videoID', (done: Done) => {
fetch(getbaseURL()
+ "/api/postVideoSponsorTimes?videoID=knownWrongID&startTime=30&endTime=1000000&userID=testing")
.then(async res => {
if (res.status === 403) done(); // pass
else {
const body = await res.text();
done("non 403 status code: " + res.status + " (" + body + ")");
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should return 400 for missing params (Params method)', (done: Done) => {
fetch(getbaseURL()
+ "/api/postVideoSponsorTimes?startTime=9&endTime=10&userID=test", {

View File

@@ -101,7 +101,7 @@ describe('postWarning', () => {
.catch(err => done(err));
});
it('Should not be able to create warning if vip (exp 403)', (done: Done) => {
it('Should not be able to create warning if not vip (exp 403)', (done: Done) => {
let json = {
issuerUserID: 'warning-not-vip',
userID: 'warning-1',

123
test/cases/reputation.ts Normal file
View File

@@ -0,0 +1,123 @@
import assert from 'assert';
import { db } from '../../src/databases/databases';
import { UserID } from '../../src/types/user.model';
import { getHash } from '../../src/utils/getHash';
import { getReputation } from '../../src/utils/reputation';
const userIDLowSubmissions = "reputation-lowsubmissions" as UserID;
const userIDHighDownvotes = "reputation-highdownvotes" as UserID;
const userIDHighNonSelfDownvotes = "reputation-highnonselfdownvotes" as UserID;
const userIDNewSubmissions = "reputation-newsubmissions" as UserID;
const userIDLowSum = "reputation-lowsum" as UserID;
const userIDHighRepBeforeManualVote = "reputation-oldhighrep" as UserID;
const userIDHighRep = "reputation-highrep" as UserID;
const userIDHighRepAndLocked = "reputation-highlockedrep" as UserID;
describe('reputation', () => {
before(async () => {
const videoID = "reputation-videoID";
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES';
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-0-uuid-0', '${getHash(userIDLowSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-0-uuid-1', '${getHash(userIDLowSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 100, 0, 'reputation-0-uuid-2', '${getHash(userIDLowSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-1-uuid-0', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -2, 0, 'reputation-1-uuid-1', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -2, 0, 'reputation-1-uuid-2', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -2, 0, 'reputation-1-uuid-3', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -2, 0, 'reputation-1-uuid-4', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-1-uuid-5', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-uuid-6', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-uuid-7', '${getHash(userIDHighDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
// Each downvote is on a different video (ie. they didn't resubmit to fix their downvote)
await db.prepare("run", startOfQuery + `('${videoID}A', 1, 11, 2, 0, 'reputation-1-1-uuid-0', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
// Different category, same video
await db.prepare("run", startOfQuery + `('${videoID}A', 1, 11, -2, 0, 'reputation-1-1-uuid-1', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'intro', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-1-uuid-2', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-1-uuid-3', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-1-uuid-4', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-1-1-uuid-5', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-1-uuid-6', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-1-1-uuid-7', '${getHash(userIDHighNonSelfDownvotes)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-2-uuid-0', '${getHash(userIDNewSubmissions)}', ${Date.now()}, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-2-uuid-1', '${getHash(userIDNewSubmissions)}', ${Date.now()}, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-2-uuid-2', '${getHash(userIDNewSubmissions)}', ${Date.now()}, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-2-uuid-3', '${getHash(userIDNewSubmissions)}', ${Date.now()}, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-2-uuid-4', '${getHash(userIDNewSubmissions)}', ${Date.now()}, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-2-uuid-5', '${getHash(userIDNewSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-2-uuid-6', '${getHash(userIDNewSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-2-uuid-7', '${getHash(userIDNewSubmissions)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-3-uuid-0', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 1, 0, 'reputation-3-uuid-1', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-3-uuid-2', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-3-uuid-3', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 1, 0, 'reputation-3-uuid-4', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-3-uuid-5', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-3-uuid-6', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-3-uuid-7', '${getHash(userIDLowSum)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-4-uuid-0', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-4-uuid-1', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-4-uuid-2', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-4-uuid-3', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-4-uuid-4', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-4-uuid-5', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-4-uuid-6', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-4-uuid-7', '${getHash(userIDHighRepBeforeManualVote)}', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-5-uuid-0', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-5-uuid-1', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-5-uuid-2', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-5-uuid-3', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-5-uuid-4', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-5-uuid-5', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-5-uuid-6', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-5-uuid-7', '${getHash(userIDHighRep)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 1, 'reputation-6-uuid-0', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 1, 'reputation-6-uuid-1', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 1, 'reputation-6-uuid-2', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 1, 'reputation-6-uuid-3', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 2, 0, 'reputation-6-uuid-4', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, -1, 0, 'reputation-6-uuid-5', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-6-uuid-6', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
await db.prepare("run", startOfQuery + `('${videoID}', 1, 11, 0, 0, 'reputation-6-uuid-7', '${getHash(userIDHighRepAndLocked)}', 1606240000000, 50, 'sponsor', 'YouTube', 100, 0, 0, '${getHash(videoID, 1)}')`);
});
it("user in grace period", async () => {
assert.strictEqual(await getReputation(getHash(userIDLowSubmissions)), 0);
});
it("user with high downvote ratio", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighDownvotes)), -2.125);
});
it("user with high non self downvote ratio", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighNonSelfDownvotes)), -1.6428571428571428);
});
it("user with mostly new submissions", async () => {
assert.strictEqual(await getReputation(getHash(userIDNewSubmissions)), 0);
});
it("user with not enough vote sum", async () => {
assert.strictEqual(await getReputation(getHash(userIDLowSum)), 0);
});
it("user with lots of old votes (before autovote was disabled) ", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighRepBeforeManualVote)), 0);
});
it("user with high reputation", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighRep)), 0.24137931034482757);
});
it("user with high reputation and locked segments", async () => {
assert.strictEqual(await getReputation(getHash(userIDHighRepAndLocked)), 1.8413793103448277);
});
});

184
test/cases/setUsername.ts Normal file
View File

@@ -0,0 +1,184 @@
import fetch from 'node-fetch';
import { Done, getbaseURL } from '../utils';
import { db } from '../../src/databases/databases';
import { getHash } from '../../src/utils/getHash';
const adminPrivateUserID = 'testUserId';
const user01PrivateUserID = 'setUsername_01';
const username01 = 'Username 01';
const user02PrivateUserID = 'setUsername_02';
const username02 = 'Username 02';
const user03PrivateUserID = 'setUsername_03';
const username03 = 'Username 03';
const user04PrivateUserID = 'setUsername_04';
const username04 = 'Username 04';
const user05PrivateUserID = 'setUsername_05';
const username05 = 'Username 05';
const user06PrivateUserID = 'setUsername_06';
const username06 = 'Username 06';
const user07PrivateUserID = 'setUsername_07';
const username07 = 'Username 07';
async function addUsername(userID: string, userName: string, locked = 0) {
await db.prepare('run', 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
}
async function getUsername(userID: string) {
const row = await db.prepare('get', 'SELECT "userName" FROM "userNames" WHERE userID = ?', [userID]);
if (!row) {
return null;
}
return row.userName;
}
describe('setUsername', () => {
before(async () => {
await addUsername(getHash(user01PrivateUserID), username01, 0);
await addUsername(getHash(user02PrivateUserID), username02, 0);
await addUsername(getHash(user03PrivateUserID), username03, 0);
await addUsername(getHash(user04PrivateUserID), username04, 1);
await addUsername(getHash(user05PrivateUserID), username05, 0);
await addUsername(getHash(user06PrivateUserID), username06, 0);
await addUsername(getHash(user07PrivateUserID), username07, 1);
});
it('Should return 200', (done: Done) => {
fetch(`${getbaseURL()}/api/setUsername?userID=${user01PrivateUserID}&username=Changed%20Username`, {
method: 'POST',
})
.then(res => {
if (res.status !== 200) done(`Status code was ${res.status}`);
else done(); // pass
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should return 400 for missing param "userID"', (done: Done) => {
fetch(`${getbaseURL()}/api/setUsername?username=MyUsername`, {
method: 'POST',
})
.then(res => {
if (res.status !== 400) done(`Status code was ${res.status}`);
else done(); // pass
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should return 400 for missing param "username"', (done: Done) => {
fetch(`${getbaseURL()}/api/setUsername?userID=test`, {
method: 'POST',
})
.then(res => {
if (res.status !== 400) done(`Status code was ${res.status}`);
else done(); // pass
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should return 400 for "username" longer then 64 characters', (done: Done) => {
const username65 = '0000000000000000000000000000000000000000000000000000000000000000X';
fetch(`${getbaseURL()}/api/setUsername?userID=test&username=${encodeURIComponent(username65)}`, {
method: 'POST',
})
.then(res => {
if (res.status !== 400) done(`Status code was ${res.status}`);
else done(); // pass
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should not change username if it contains "discord"', (done: Done) => {
const newUsername = 'discord.me';
fetch(`${getbaseURL()}/api/setUsername?userID=${user02PrivateUserID}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
if (res.status !== 200) done(`Status code was ${res.status}`);
else {
const userName = await getUsername(getHash(user02PrivateUserID));
if (userName === newUsername) {
done(`Username '${username02}' got changed to '${newUsername}'`);
}
else done();
}
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should be able to change username', (done: Done) => {
const newUsername = 'newUsername';
fetch(`${getbaseURL()}/api/setUsername?userID=${user03PrivateUserID}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
const username = await getUsername(getHash(user03PrivateUserID));
if (username !== newUsername) done(`Username did not change`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should not be able to change locked username', (done: Done) => {
const newUsername = 'newUsername';
fetch(`${getbaseURL()}/api/setUsername?userID=${user04PrivateUserID}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
const username = await getUsername(getHash(user04PrivateUserID));
if (username === newUsername) done(`Username '${username04}' got changed to '${username}'`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
it('Should filter out unicode control characters', (done: Done) => {
const newUsername = 'This\nUsername+has\tInvalid+Characters';
fetch(`${getbaseURL()}/api/setUsername?userID=${user05PrivateUserID}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
const username = await getUsername(getHash(user05PrivateUserID));
if (username === newUsername) done(`Username contains unicode control characters`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
it('Incorrect adminUserID should return 403', (done: Done) => {
const newUsername = 'New Username';
fetch(`${getbaseURL()}/api/setUsername?adminUserID=invalidAdminID&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
if (res.status !== 403) done(`Status code was ${res.status}`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
it('Admin should be able to change username', (done: Done) => {
const newUsername = 'New Username';
fetch(`${getbaseURL()}/api/setUsername?adminUserID=${adminPrivateUserID}&userID=${getHash(user06PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
const username = await getUsername(getHash(user06PrivateUserID));
if (username !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
it('Admin should be able to change locked username', (done: Done) => {
const newUsername = 'New Username';
fetch(`${getbaseURL()}/api/setUsername?adminUserID=${adminPrivateUserID}&userID=${getHash(user07PrivateUserID)}&username=${encodeURIComponent(newUsername)}`, {
method: 'POST',
})
.then(async res => {
const username = await getUsername(getHash(user06PrivateUserID));
if (username !== newUsername) done(`Failed to change username from '${username06}' to '${newUsername}'`);
else done();
})
.catch(err => done(`couldn't call endpoint`));
});
});

126
test/cases/shadowBanUser.ts Normal file
View File

@@ -0,0 +1,126 @@
import fetch from 'node-fetch';
import {db, privateDB} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
describe('shadowBanUser', () => {
before(() => {
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", views, category, "service", "videoDuration", "hidden", "shadowHidden", "hashedVideoID") VALUES';
db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, 'shadow-1-uuid-0', 'shadowBanned', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest2', 1, 11, 2, 0, 'shadow-1-uuid-0-1', 'shadowBanned', 0, 50, 'sponsor', 'PeerTube', 120, 0, 0, '" + getHash('testtesttest2', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, 'shadow-1-uuid-2', 'shadowBanned', 0, 50, 'intro', 'YouTube', 101, 0, 0, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, 'shadow-2-uuid-0', 'shadowBanned2', 0, 50, 'sponsor', 'YouTube', 100, 0, 0, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest2', 1, 11, 2, 0, 'shadow-2-uuid-0-1', 'shadowBanned2', 0, 50, 'sponsor', 'PeerTube', 120, 0, 0, '" + getHash('testtesttest2', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, 'shadow-2-uuid-2', 'shadowBanned2', 0, 50, 'intro', 'YouTube', 101, 0, 0, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest', 1, 11, 2, 0, 'shadow-3-uuid-0', 'shadowBanned3', 0, 50, 'sponsor', 'YouTube', 100, 0, 1, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest2', 1, 11, 2, 0, 'shadow-3-uuid-0-1', 'shadowBanned3', 0, 50, 'sponsor', 'PeerTube', 120, 0, 1, '" + getHash('testtesttest2', 1) + "')");
db.prepare("run", startOfQuery + "('testtesttest', 20, 33, 2, 0, 'shadow-3-uuid-2', 'shadowBanned3', 0, 50, 'intro', 'YouTube', 101, 0, 1, '" + getHash('testtesttest', 1) + "')");
db.prepare("run", `INSERT INTO "shadowBannedUsers" VALUES('shadowBanned3')`);
db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES ('` + getHash("shadow-ban-vip") + "')");
});
it('Should be able to ban user and hide submissions', (done: Done) => {
fetch(getbaseURL() + "/api/shadowBanUser?userID=shadowBanned&adminUserID=shadow-ban-vip", {
method: 'POST'
})
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned", 1]);
const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]);
if (shadowRow && videoRow?.length === 3) {
done();
} else {
done("Ban failed " + JSON.stringify(videoRow) + " " + JSON.stringify(shadowRow));
}
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to unban user without unhiding submissions', (done: Done) => {
fetch(getbaseURL() + "/api/shadowBanUser?userID=shadowBanned&adminUserID=shadow-ban-vip&enabled=false&unHideOldSubmissions=false", {
method: 'POST'
})
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned", 1]);
const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned"]);
if (!shadowRow && videoRow?.length === 3) {
done();
} else {
done("Unban failed " + JSON.stringify(videoRow) + " " + JSON.stringify(shadowRow));
}
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to ban user and hide submissions from only some categories', (done: Done) => {
fetch(getbaseURL() + '/api/shadowBanUser?userID=shadowBanned2&adminUserID=shadow-ban-vip&categories=["sponsor"]', {
method: 'POST'
})
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const videoRow: {category: string, shadowHidden: number}[] = (await db.prepare('all', `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned2", 1]));
const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]);
if (shadowRow && 2 == videoRow?.length && 2 === videoRow?.filter((elem) => elem?.category === "sponsor")?.length) {
done();
} else {
done("Ban failed " + JSON.stringify(videoRow) + " " + JSON.stringify(shadowRow));
}
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to unban user and unhide submissions', (done: Done) => {
fetch(getbaseURL() + "/api/shadowBanUser?userID=shadowBanned2&adminUserID=shadow-ban-vip&enabled=false", {
method: 'POST'
})
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const videoRow = await db.prepare('all', `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned2", 1]);
const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned2"]);
if (!shadowRow && videoRow?.length === 0) {
done();
} else {
done("Unban failed " + JSON.stringify(videoRow) + " " + JSON.stringify(shadowRow));
}
}
})
.catch(err => done("Couldn't call endpoint"));
});
it('Should be able to unban user and unhide some submissions', (done: Done) => {
fetch(getbaseURL() + `/api/shadowBanUser?userID=shadowBanned3&adminUserID=shadow-ban-vip&enabled=false&categories=["sponsor"]`, {
method: 'POST'
})
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const videoRow = await db.prepare('all', `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, ["shadowBanned3", 1]);
const shadowRow = await db.prepare('get', `SELECT * FROM "shadowBannedUsers" WHERE "userID" = ?`, ["shadowBanned3"]);
if (!shadowRow && videoRow?.length === 1 && videoRow[0]?.category === "intro") {
done();
} else {
done("Unban failed " + JSON.stringify(videoRow) + " " + JSON.stringify(shadowRow));
}
}
})
.catch(err => done("Couldn't call endpoint"));
});
});

View File

@@ -1,14 +1,14 @@
import fetch from 'node-fetch';
import * as utils from '../utils';
import { getHash } from '../../src/utils/getHash';
import { db, privateDB } from '../../src/databases/databases';
import { db } from '../../src/databases/databases';
describe('unBan', () => {
before(async () => {
const insertShadowBannedUserQuery = 'INSERT INTO "shadowBannedUsers" VALUES(?)';
await privateDB.prepare("run", insertShadowBannedUserQuery, ['testMan-unBan']);
await privateDB.prepare("run", insertShadowBannedUserQuery, ['testWoman-unBan']);
await privateDB.prepare("run", insertShadowBannedUserQuery, ['testEntity-unBan']);
await db.prepare("run", insertShadowBannedUserQuery, ['testMan-unBan']);
await db.prepare("run", insertShadowBannedUserQuery, ['testWoman-unBan']);
await db.prepare("run", insertShadowBannedUserQuery, ['testEntity-unBan']);
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", insertVipUserQuery, [getHash("VIPUser-unBan")]);

View File

@@ -1,6 +1,6 @@
import fetch from 'node-fetch';
import {config} from '../../src/config';
import {db, privateDB} from '../../src/databases/databases';
import {db} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
import {ImportMock} from 'ts-mock-imports';
@@ -57,7 +57,7 @@ describe('voteOnSponsorTime', () => {
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash("VIPUser")]);
await privateDB.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
await db.prepare("run", 'INSERT INTO "lockCategories" ("videoID", "userID", "category") VALUES (?, ?, ?)', ['no-sponsor-segments-video', 'someUser', 'sponsor']);
});
@@ -263,6 +263,24 @@ describe('voteOnSponsorTime', () => {
.catch(err => done(err));
});
it('Should not able to change to highlight category', (done: Done) => {
fetch(getbaseURL()
+ "/api/voteOnSponsorTime?userID=randomID2&UUID=incorrect-category&category=highlight")
.then(async res => {
if (res.status === 400) {
let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["incorrect-category"]);
if (row.category === "sponsor") {
done();
} else {
done("Vote did not succeed. Submission went from sponsor to " + row.category);
}
} else {
done("Status code was " + res.status);
}
})
.catch(err => done(err));
});
it('Should be able to change your vote for a category and it should add your vote to the database', (done: Done) => {
fetch(getbaseURL()
+ "/api/voteOnSponsorTime?userID=randomID2&UUID=vote-uuid-4&category=outro")
@@ -427,10 +445,10 @@ describe('voteOnSponsorTime', () => {
+ "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&type=0")
.then(async res => {
let row = await db.prepare('get', `SELECT "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]);
if (res.status === 403 && row.votes === 2) {
if (res.status === 200 && row.votes === 2) {
done();
} else {
done("Status code was " + res.status + " instead of 403, row was " + JSON.stringify(row));
done("Status code was " + res.status + " instead of 200, row was " + JSON.stringify(row));
}
})
.catch(err => done(err));
@@ -455,10 +473,10 @@ describe('voteOnSponsorTime', () => {
+ "/api/voteOnSponsorTime?userID=randomID&UUID=no-sponsor-segments-uuid-0&category=outro")
.then(async res => {
let row = await db.prepare('get', `SELECT "category" FROM "sponsorTimes" WHERE "UUID" = ?`, ["no-sponsor-segments-uuid-0"]);
if (res.status === 403 && row.category === "sponsor") {
if (res.status === 200 && row.category === "sponsor") {
done();
} else {
done("Status code was " + res.status + " instead of 403, row was " + JSON.stringify(row));
done("Status code was " + res.status + " instead of 200, row was " + JSON.stringify(row));
}
})
.catch(err => done(err));