2021-01-06 01:43:28 +01:00
import fetch from 'node-fetch' ;
2020-10-17 20:56:54 +02:00
import { config } from '../../src/config' ;
import { getHash } from '../../src/utils/getHash' ;
import { Done , getbaseURL } from '../utils' ;
import { db } from '../../src/databases/databases' ;
2020-10-20 21:56:07 +02:00
import { ImportMock } from 'ts-mock-imports' ;
import * as YouTubeAPIModule from '../../src/utils/youtubeApi' ;
import { YouTubeApiMock } from '../youtubeMock' ;
2021-03-30 01:16:18 +02:00
import e from 'express' ;
2020-10-20 21:56:07 +02:00
const mockManager = ImportMock . mockStaticClass ( YouTubeAPIModule , 'YouTubeAPI' ) ;
const sinonStub = mockManager . mock ( 'listVideos' ) ;
sinonStub . callsFake ( YouTubeApiMock . listVideos ) ;
2020-10-17 20:56:54 +02:00
describe ( 'postSkipSegments' , ( ) = > {
before ( ( ) = > {
2021-03-07 06:21:56 +01:00
let startOfQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden", "hashedVideoID") VALUES' ;
2020-10-17 20:56:54 +02:00
2021-03-02 03:37:35 +01:00
db . prepare ( "run" , startOfQuery + "('80percent_video', 0, 1000, 0, '80percent-uuid-0', '" + getHash ( "test" ) + "', 0, 0, 'interaction', 0, '80percent_video')" ) ;
db . prepare ( "run" , startOfQuery + "('80percent_video', 1001, 1005, 0, '80percent-uuid-1', '" + getHash ( "test" ) + "', 0, 0, 'interaction', 0, '80percent_video')" ) ;
db . prepare ( "run" , startOfQuery + "('80percent_video', 0, 5000, -2, '80percent-uuid-2', '" + getHash ( "test" ) + "', 0, 0, 'interaction', 0, '80percent_video')" ) ;
2020-10-17 20:56:54 +02:00
const now = Date . now ( ) ;
const warnVip01Hash = getHash ( "warn-vip01" ) ;
const warnUser01Hash = getHash ( "warn-user01" ) ;
const warnUser02Hash = getHash ( "warn-user02" ) ;
2021-01-24 18:45:13 +01:00
const warnUser03Hash = getHash ( "warn-user03" ) ;
2020-10-17 20:56:54 +02:00
const MILLISECONDS_IN_HOUR = 3600000 ;
const warningExpireTime = MILLISECONDS_IN_HOUR * config . hoursAfterWarningExpires ;
2021-03-07 06:21:56 +01:00
const startOfWarningQuery = 'INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES' ;
2021-03-02 03:37:35 +01:00
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser01Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser01Hash + "', '" + ( now - 1000 ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser01Hash + "', '" + ( now - 2000 ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser01Hash + "', '" + ( now - 3601000 ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser02Hash + "', '" + now + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser02Hash + "', '" + ( now - ( warningExpireTime + 1000 ) ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser02Hash + "', '" + ( now - ( warningExpireTime + 2000 ) ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser03Hash + "', '" + now + "', '" + warnVip01Hash + "', 0)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser03Hash + "', '" + ( now - 1000 ) + "', '" + warnVip01Hash + "', 0)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser03Hash + "', '" + ( now - 2000 ) + "', '" + warnVip01Hash + "', 1)" ) ;
db . prepare ( "run" , startOfWarningQuery + "('" + warnUser03Hash + "', '" + ( now - 3601000 ) + "', '" + warnVip01Hash + "', 1)" ) ;
2021-02-19 02:24:04 +01:00
2021-03-07 06:21:56 +01:00
db . prepare ( "run" , ` INSERT INTO "vipUsers" ("userID") VALUES (' ` + getHash ( "VIPUserSubmission" ) + "')" ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be able to submit a single time (Params method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes?videoID=dQw4w9WgXcR&startTime=2&endTime=10&userID=test&category=sponsor" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 200 ) {
2021-03-07 06:21:56 +01:00
const row = await db . prepare ( 'get' , ` SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "dQw4w9WgXcR" ] ) ;
2021-01-06 01:43:28 +01:00
if ( row . startTime === 2 && row . endTime === 10 && row . category === "sponsor" ) {
done ( ) ;
2020-10-17 20:56:54 +02:00
} else {
2021-01-06 01:43:28 +01:00
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be able to submit a single time (JSON method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcF" ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 200 ) {
2021-03-07 06:21:56 +01:00
const row = await db . prepare ( 'get' , ` SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "dQw4w9WgXcF" ] ) ;
2021-02-19 02:24:04 +01:00
if ( row . startTime === 0 && row . endTime === 10 && row . locked === 0 && row . category === "sponsor" ) {
2021-01-06 01:43:28 +01:00
done ( ) ;
2020-10-17 20:56:54 +02:00
} else {
2021-01-06 01:43:28 +01:00
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
2021-03-27 00:13:52 +01:00
it ( 'Should be able to submit a single time with a duration from the YouTube API (JSON method)' , ( done : Done ) = > {
2021-03-20 03:45:30 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXZX" ,
videoDuration : 100 ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
. 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 ) {
done ( ) ;
} else {
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
}
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
2021-03-27 00:13:52 +01:00
it ( 'Should be able to submit a single time with a precise duration close to the one from the YouTube API (JSON method)' , ( done : Done ) = > {
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXZH" ,
videoDuration : 5010.20 ,
segments : [ {
segment : [ 1 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
. 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 ) {
done ( ) ;
} else {
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
}
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
it ( 'Should be able to submit a single time with a duration in the body (JSON method)' , ( done : Done ) = > {
2021-03-20 03:45:30 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "test" ,
videoID : "noDuration" ,
videoDuration : 100 ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
. then ( async res = > {
if ( res . status === 200 ) {
const row = await db . prepare ( 'get' , ` SELECT "startTime", "endTime", "locked", "category", "videoDuration" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "noDuration" ] ) ;
if ( row . startTime === 0 && row . endTime === 10 && row . locked === 0 && row . category === "sponsor" && row . videoDuration === 100 ) {
done ( ) ;
} else {
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
}
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
2021-03-30 01:16:18 +02:00
it ( 'Should be able to submit with a new duration, and hide old submissions and remove segment locks' , async ( ) = > {
2021-04-24 01:54:42 +02:00
await db . prepare ( "run" , ` INSERT INTO "lockCategories" ("userID", "videoID", "category")
VALUES ( '` + getHash("VIPUser-lockCategories") + "' , 'noDuration' , 'sponsor' ) " ) ;
2021-03-30 01:16:18 +02:00
try {
const res = await fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "test" ,
videoID : "noDuration" ,
videoDuration : 100 ,
segments : [ {
segment : [ 1 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} ) ;
if ( res . status === 200 ) {
2021-04-24 01:54:42 +02:00
const lockCategoriesRow = await db . prepare ( 'get' , ` SELECT * from "lockCategories" WHERE videoID = ? ` , [ "noDuration" ] ) ;
2021-03-30 01:16:18 +02:00
const videoRows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "locked", "category", "videoDuration"
FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 0 ` , ["noDuration"]);
const videoRow = videoRows [ 0 ] ;
const hiddenVideoRows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "locked", "category", "videoDuration"
FROM "sponsorTimes" WHERE "videoID" = ? AND hidden = 1 ` , ["noDuration"]);
2021-04-24 01:54:42 +02:00
if ( lockCategoriesRow === undefined && videoRows . length === 1 && hiddenVideoRows . length === 1 && videoRow . startTime === 1 && videoRow . endTime === 10
2021-03-30 01:16:18 +02:00
&& videoRow . locked === 0 && videoRow . category === "sponsor" && videoRow . videoDuration === 100 ) {
return ;
} else {
return "Submitted times were not saved. Actual submission: " + JSON . stringify ( videoRow ) ;
}
} else {
return "Status code was " + res . status ;
}
} catch ( e ) {
return e ;
}
} ) ;
2021-03-20 02:31:16 +01:00
it ( 'Should be able to submit a single time under a different service (JSON method)' , ( done : Done ) = > {
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcG" ,
service : "PeerTube" ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
. then ( async res = > {
if ( res . status === 200 ) {
const row = await db . prepare ( 'get' , ` SELECT "startTime", "endTime", "locked", "category", "service" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "dQw4w9WgXcG" ] ) ;
if ( row . startTime === 0 && row . endTime === 10 && row . locked === 0 && row . category === "sponsor" && row . service === "PeerTube" ) {
done ( ) ;
} else {
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
}
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
2021-02-19 02:24:04 +01:00
it ( 'VIP submission should start locked' , ( done : Done ) = > {
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "VIPUserSubmission" ,
videoID : "vipuserIDSubmission" ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-02-19 02:24:04 +01:00
if ( res . status === 200 ) {
2021-03-07 06:21:56 +01:00
const row = await db . prepare ( 'get' , ` SELECT "startTime", "endTime", "locked", "category" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "vipuserIDSubmission" ] ) ;
2021-02-19 02:24:04 +01:00
if ( row . startTime === 0 && row . endTime === 10 && row . locked === 1 && row . category === "sponsor" ) {
done ( ) ;
} else {
done ( "Submitted times were not saved. Actual submission: " + JSON . stringify ( row ) ) ;
}
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
2020-10-17 20:56:54 +02:00
it ( 'Should be able to submit multiple times (JSON method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
2020-10-17 20:56:54 +02:00
userID : "test" ,
videoID : "dQw4w9WgXcQ" ,
segments : [ {
segment : [ 3 , 10 ] ,
category : "sponsor" ,
} , {
segment : [ 30 , 60 ] ,
category : "intro" ,
} ] ,
2021-01-06 01:43:28 +01:00
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 200 ) {
2021-03-07 06:21:56 +01:00
const rows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? ` , [ "dQw4w9WgXcR" ] ) ;
2021-01-06 01:43:28 +01:00
let success = true ;
if ( rows . length === 2 ) {
for ( const row of rows ) {
if ( ( row . startTime !== 3 || row . endTime !== 10 || row . category !== "sponsor" ) &&
( row . startTime !== 30 || row . endTime !== 60 || row . category !== "intro" ) ) {
success = false ;
break ;
2020-10-17 20:56:54 +02:00
}
}
}
2021-01-06 01:43:28 +01:00
if ( success ) done ( ) ;
else done ( "Submitted times were not saved. Actual submissions: " + JSON . stringify ( rows ) ) ;
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) . timeout ( 5000 ) ;
it ( 'Should allow multiple times if total is under 80% of video(JSON method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
2020-10-17 20:56:54 +02:00
userID : "test" ,
videoID : "L_jWHffIx5E" ,
segments : [ {
segment : [ 3 , 3000 ] ,
category : "sponsor" ,
} , {
segment : [ 3002 , 3050 ] ,
category : "intro" ,
} , {
segment : [ 45 , 100 ] ,
category : "interaction" ,
} , {
segment : [ 99 , 170 ] ,
category : "sponsor" ,
} ] ,
2021-01-06 01:43:28 +01:00
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 200 ) {
2021-03-07 06:21:56 +01:00
const rows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1 ` , [ "L_jWHffIx5E" ] ) ;
2021-01-06 01:43:28 +01:00
let success = true ;
if ( rows . length === 4 ) {
for ( const row of rows ) {
if ( ( row . startTime !== 3 || row . endTime !== 3000 || row . category !== "sponsor" ) &&
( row . startTime !== 3002 || row . endTime !== 3050 || row . category !== "intro" ) &&
( row . startTime !== 45 || row . endTime !== 100 || row . category !== "interaction" ) &&
( row . startTime !== 99 || row . endTime !== 170 || row . category !== "sponsor" ) ) {
success = false ;
break ;
2020-10-17 20:56:54 +02:00
}
}
}
2021-01-06 01:43:28 +01:00
if ( success ) done ( ) ;
else done ( "Submitted times were not saved. Actual submissions: " + JSON . stringify ( rows ) ) ;
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) . timeout ( 5000 ) ;
it ( 'Should reject multiple times if total is over 80% of video (JSON method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
2020-10-17 20:56:54 +02:00
userID : "test" ,
videoID : "n9rIGdXnSJc" ,
segments : [ {
segment : [ 0 , 2000 ] ,
category : "interaction" ,
} , {
segment : [ 3000 , 4000 ] ,
category : "sponsor" ,
} , {
segment : [ 1500 , 2750 ] ,
category : "sponsor" ,
} , {
segment : [ 4050 , 4750 ] ,
category : "intro" ,
} ] ,
2021-01-06 01:43:28 +01:00
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-03-20 03:45:30 +01:00
if ( res . status === 403 ) {
2021-03-07 06:21:56 +01:00
const rows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1 ` , [ "n9rIGdXnSJc" ] ) ;
2021-01-06 01:43:28 +01:00
let success = true ;
if ( rows . length === 4 ) {
for ( const row of rows ) {
if ( ( row . startTime === 0 || row . endTime === 2000 || row . category === "interaction" ) ||
( row . startTime === 3000 || row . endTime === 4000 || row . category === "sponsor" ) ||
( row . startTime === 1500 || row . endTime === 2750 || row . category === "sponsor" ) ||
( row . startTime === 4050 || row . endTime === 4750 || row . category === "intro" ) ) {
success = false ;
break ;
2020-10-17 20:56:54 +02:00
}
}
}
2021-01-06 01:43:28 +01:00
if ( success ) done ( ) ;
else
done ( "Submitted times were not saved. Actual submissions: " + JSON . stringify ( rows ) ) ;
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) . timeout ( 5000 ) ;
it ( 'Should reject multiple times if total is over 80% of video including previosuly submitted times(JSON method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "80percent_video" ,
segments : [ {
segment : [ 2000 , 4000 ] ,
category : "sponsor" ,
} , {
segment : [ 1500 , 2750 ] ,
category : "sponsor" ,
} , {
segment : [ 4050 , 4750 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-03-20 03:45:30 +01:00
if ( res . status === 403 ) {
2021-03-07 06:21:56 +01:00
const rows = await db . prepare ( 'all' , ` SELECT "startTime", "endTime", "category" FROM "sponsorTimes" WHERE "videoID" = ? and "votes" > -1 ` , [ "80percent_video" ] ) ;
2021-01-06 01:43:28 +01:00
let success = rows . length == 2 ;
for ( const row of rows ) {
if ( ( row . startTime === 2000 || row . endTime === 4000 || row . category === "sponsor" ) ||
( row . startTime === 1500 || row . endTime === 2750 || row . category === "sponsor" ) ||
( row . startTime === 4050 || row . endTime === 4750 || row . category === "sponsor" ) ) {
success = false ;
break ;
2020-10-17 20:56:54 +02:00
}
}
2021-01-06 01:43:28 +01:00
if ( success ) done ( ) ;
else
done ( "Submitted times were not saved. Actual submissions: " + JSON . stringify ( rows ) ) ;
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) . timeout ( 5000 ) ;
it ( 'Should be accepted if a non-sponsor is less than 1 second' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing&category=intro" , {
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" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
2021-04-05 05:12:26 +02:00
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" ) ) ;
} ) ;
2020-10-17 20:56:54 +02:00
it ( 'Should be rejected if a sponsor is less than 1 second' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/skipSegments?videoID=qqwerty&startTime=30&endTime=30.5&userID=testing" , {
method : 'POST' ,
} )
. then ( async res = > {
if ( res . status === 400 ) done ( ) ; // pass
else {
const body = await res . text ( ) ;
done ( "non 403 status code: " + res . status + " (" + body + ")" ) ;
}
} )
. catch ( err = > done ( "Couldn't call endpoint" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be rejected if over 80% of the video' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes?videoID=qqwerty&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" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( "Should be rejected if NB's predicted probability is <70%." , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes?videoID=LevkAjUE6d4&startTime=40&endTime=60&userID=testing" )
. 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" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be rejected if user has to many active warnings' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "warn-user01" ,
videoID : "dQw4w9WgXcF" ,
segments : [ {
segment : [ 0 , 10 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 403 ) {
done ( ) ; // success
} else {
done ( "Status code was " + res . status ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be accepted if user has some active warnings' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
2020-10-17 20:56:54 +02:00
userID : "warn-user02" ,
videoID : "dQw4w9WgXcF" ,
segments : [ {
segment : [ 50 , 60 ] ,
category : "sponsor" ,
} ] ,
2021-01-06 01:43:28 +01:00
} ) ,
} )
. then ( async res = > {
if ( res . status === 200 ) {
2021-01-24 18:45:13 +01:00
done ( ) ; // success
} else {
const body = await res . text ( ) ;
done ( "Status code was " + res . status + " " + body ) ;
}
} )
. catch ( err = > done ( err ) ) ;
} ) ;
it ( 'Should be accepted if user has some warnings removed' , ( done : Done ) = > {
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes" , {
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
} ,
body : JSON.stringify ( {
userID : "warn-user03" ,
videoID : "dQw4w9WgXcF" ,
segments : [ {
segment : [ 53 , 60 ] ,
category : "sponsor" ,
} ] ,
} ) ,
} )
. then ( async res = > {
if ( res . status === 200 ) {
2021-01-06 01:43:28 +01:00
done ( ) ; // success
} else {
const body = await res . text ( ) ;
done ( "Status code was " + res . status + " " + body ) ;
}
} )
. catch ( err = > done ( err ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be allowed if youtube thinks duration is 0' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
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" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should be rejected if not a valid videoID' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
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" ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (Params method)' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
+ "/api/postVideoSponsorTimes?startTime=9&endTime=10&userID=test" , {
method : 'POST' ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (JSON method) 1' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
segments : [ {
segment : [ 9 , 10 ] ,
category : "sponsor" ,
} , {
segment : [ 31 , 60 ] ,
category : "intro" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (JSON method) 2' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcQ" ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (JSON method) 3' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcQ" ,
segments : [ {
segment : [ 0 ] ,
category : "sponsor" ,
} , {
segment : [ 31 , 60 ] ,
category : "intro" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (JSON method) 4' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcQ" ,
segments : [ {
segment : [ 9 , 10 ] ,
} , {
segment : [ 31 , 60 ] ,
category : "intro" ,
} ] ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
it ( 'Should return 400 for missing params (JSON method) 5' , ( done : Done ) = > {
2021-01-06 01:43:28 +01:00
fetch ( getbaseURL ( )
2020-10-17 20:56:54 +02:00
+ "/api/postVideoSponsorTimes" , {
2021-01-06 01:43:28 +01:00
method : 'POST' ,
headers : {
'Content-Type' : 'application/json' ,
2020-10-17 20:56:54 +02:00
} ,
2021-01-06 01:43:28 +01:00
body : JSON.stringify ( {
userID : "test" ,
videoID : "dQw4w9WgXcQ" ,
} ) ,
} )
2021-03-02 03:37:35 +01:00
. then ( async res = > {
2021-01-06 01:43:28 +01:00
if ( res . status === 400 ) done ( ) ;
else done ( true ) ;
} )
. catch ( err = > done ( true ) ) ;
2020-10-17 20:56:54 +02:00
} ) ;
} ) ;