fix docker build, add proper mocks to tests, remove YouTubeAPI dependency on youtube test mock, move index.ts and test.ts to /src ant /test folders

This commit is contained in:
Dainius Dauksevicius
2020-10-20 22:56:07 +03:00
parent 62b008e693
commit 12729b36fb
17 changed files with 365 additions and 207 deletions

View File

@@ -2,8 +2,13 @@ import request from 'request';
import {db} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
import {ImportMock,} from 'ts-mock-imports';
import * as YouTubeAPIModule from '../../src/utils/youtubeApi';
import {YouTubeApiMock} from '../youtubeMock';
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, 'YouTubeAPI');
const sinonStub = mockManager.mock('listVideos');
sinonStub.callsFake(YouTubeApiMock.listVideos);
describe('getSegmentsByHash', () => {
before(() => {
@@ -12,6 +17,7 @@ describe('getSegmentsByHash', () => {
db.exec(startOfQuery + "('getSegmentsByHash-0', 20, 30, 2, 'getSegmentsByHash-0-1', 'testman', 100, 150, 'intro', 0, '" + getHash('getSegmentsByHash-0', 1) + "')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910
db.exec(startOfQuery + "('getSegmentsByHash-noMatchHash', 40, 50, 2, 'getSegmentsByHash-noMatchHash', 'testman', 0, 50, 'sponsor', 0, 'fdaffnoMatchHash')"); // hash = fdaff4dee1043451faa7398324fb63d8618ebcd11bddfe0491c488db12c6c910
db.exec(startOfQuery + "('getSegmentsByHash-1', 60, 70, 2, 'getSegmentsByHash-1', 'testman', 0, 50, 'sponsor', 0, '" + getHash('getSegmentsByHash-1', 1) + "')"); // hash = 3272fa85ee0927f6073ef6f07ad5f3146047c1abba794cfa364d65ab9921692b
});
it('Should be able to get a 200', (done: Done) => {

View File

@@ -3,6 +3,13 @@ import {config} from '../../src/config';
import {getHash} from '../../src/utils/getHash';
import {Done, getbaseURL} from '../utils';
import {db} from '../../src/databases/databases';
import {ImportMock} from 'ts-mock-imports';
import * as YouTubeAPIModule from '../../src/utils/youtubeApi';
import {YouTubeApiMock} from '../youtubeMock';
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, 'YouTubeAPI');
const sinonStub = mockManager.mock('listVideos');
sinonStub.callsFake(YouTubeApiMock.listVideos);
describe('postSkipSegments', () => {
before(() => {
@@ -227,7 +234,7 @@ describe('postSkipSegments', () => {
if (err) done(err);
else if (res.statusCode === 400) {
let rows = db.prepare('all', "SELECT startTime, endTime, category FROM sponsorTimes WHERE videoID = ? and votes > -1", ["80percent_video"]);
let success = true && rows.length == 2;
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") ||

View File

@@ -3,6 +3,13 @@ import {config} from '../../src/config';
import {db, privateDB} from '../../src/databases/databases';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
import {ImportMock} from 'ts-mock-imports';
import * as YouTubeAPIModule from '../../src/utils/youtubeApi';
import {YouTubeApiMock} from '../youtubeMock';
const mockManager = ImportMock.mockStaticClass(YouTubeAPIModule, 'YouTubeAPI');
const sinonStub = mockManager.mock('listVideos');
sinonStub.callsFake(YouTubeApiMock.listVideos);
describe('voteOnSponsorTime', () => {
before(() => {

53
test/test.ts Normal file
View File

@@ -0,0 +1,53 @@
import Mocha from 'mocha';
import fs from 'fs';
import path from 'path';
import {config} from '../src/config';
import {createServer} from '../src/app';
import {createMockServer} from './mocks';
import {Logger} from '../src/utils/logger';
import {initDb} from '../src/databases/databases';
import {ImportMock} from 'ts-mock-imports';
import * as rateLimitMiddlewareModule from '../src/middleware/requestRateLimit';
import rateLimit from 'express-rate-limit';
ImportMock.mockFunction(rateLimitMiddlewareModule, 'rateLimitMiddleware', rateLimit({
skip: () => {
return true;
}
}));
// delete old test database
if (fs.existsSync(config.db)) fs.unlinkSync(config.db)
if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB);
initDb();
// Instantiate a Mocha instance.
const mocha = new Mocha();
const testDir = './test/cases';
// Add each .ts file to the mocha instance
fs.readdirSync(testDir)
.filter(function(file) {
// Only keep the .ts files
return file.substr(-3) === '.ts';
})
.forEach(function(file) {
mocha.addFile(
path.join(testDir, file)
);
});
const mockServer = createMockServer(() => {
Logger.info("Started mock HTTP Server");
const server = createServer(() => {
Logger.info("Started main HTTP server");
// Run the tests.
mocha.run((failures) => {
mockServer.close();
server.close();
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});
});
});

View File

@@ -7,65 +7,63 @@ YouTubeAPI.videos.list({
// https://developers.google.com/youtube/v3/docs/videos
export const YouTubeAPI = {
listVideos: (id: string, callback: (ytErr: any, data: any) => void) => {
YouTubeAPI.videos.list({
id: id,
}, callback);
},
videos: {
list: (obj: { part: string; id: any } | { id: string }, callback: (ytErr: any, data: any) => void) => {
if (obj.id === "knownWrongID") {
callback(undefined, {
pageInfo: {
totalResults: 0,
},
items: [],
});
}
if (obj.id === "noDuration") {
callback(undefined, {
pageInfo: {
totalResults: 1,
},
items: [
{
contentDetails: {
duration: "PT0S",
},
snippet: {
title: "Example Title",
thumbnails: {
maxres: {
url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
},
export class YouTubeApiMock {
static listVideos(videoID: string, callback: (ytErr: any, data: any) => void) {
const obj = {
id: videoID
};
if (obj.id === "knownWrongID") {
callback(undefined, {
pageInfo: {
totalResults: 0,
},
items: [],
});
}
if (obj.id === "noDuration") {
callback(undefined, {
pageInfo: {
totalResults: 1,
},
items: [
{
contentDetails: {
duration: "PT0S",
},
snippet: {
title: "Example Title",
thumbnails: {
maxres: {
url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
},
},
},
],
});
} else {
callback(undefined, {
pageInfo: {
totalResults: 1,
},
items: [
{
contentDetails: {
duration: "PT1H23M30S",
},
snippet: {
title: "Example Title",
thumbnails: {
maxres: {
url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
},
],
});
} else {
callback(undefined, {
pageInfo: {
totalResults: 1,
},
items: [
{
contentDetails: {
duration: "PT1H23M30S",
},
snippet: {
title: "Example Title",
thumbnails: {
maxres: {
url: "https://sponsor.ajay.app/LogoSponsorBlockSimple256px.png",
},
},
},
],
});
}
},
},
};
},
],
});
}
}
}