Add tests for npm run scripts

This commit is contained in:
Chocobozzz 2017-09-07 15:27:35 +02:00
parent 04de542abd
commit fdbda9e3d6
25 changed files with 223 additions and 39 deletions

View File

@ -16,7 +16,7 @@ db.init(true)
const storageDir = STORAGE[storage] const storageDir = STORAGE[storage]
return new Promise((res, rej) => { return new Promise((res, rej) => {
rimraf(storageDir, function (err) { rimraf(storageDir, err => {
if (err) return rej(err) if (err) return rej(err)
console.info('%s deleted.', storageDir) console.info('%s deleted.', storageDir)

View File

@ -18,7 +18,7 @@ db.init(true)
.then(user => { .then(user => {
if (!user) { if (!user) {
console.error('User unknown.') console.error('User unknown.')
return process.exit(-1)
} }
const readline = require('readline') const readline = require('readline')

View File

@ -1,8 +1,5 @@
import { readFileSync, writeFileSync } from 'fs' import * as Promise from 'bluebird'
import { join } from 'path'
import * as parseTorrent from 'parse-torrent'
import { CONFIG, STATIC_PATHS } from '../server/initializers/constants'
import { database as db } from '../server/initializers/database' import { database as db } from '../server/initializers/database'
import { hasFriends } from '../server/lib/friends' import { hasFriends } from '../server/lib/friends'
@ -20,11 +17,18 @@ db.init(true)
return db.Video.list() return db.Video.list()
}) })
.then(videos => { .then(videos => {
const tasks: Promise<any>[] = []
videos.forEach(video => { videos.forEach(video => {
console.log('Updating video ' + video.uuid)
video.VideoFiles.forEach(file => { video.VideoFiles.forEach(file => {
video.createTorrentAndSetInfoHash(file) tasks.push(video.createTorrentAndSetInfoHash(file))
}) })
}) })
return Promise.all(tasks)
})
.then(() => {
process.exit(0) process.exit(0)
}) })

View File

@ -25,8 +25,15 @@ function isTestInstance () {
} }
function root () { function root () {
// We are in /dist/helpers/utils.js // We are in /helpers/utils.js
return join(__dirname, '..', '..', '..') const paths = [ __dirname, '..', '..' ]
// We are under /dist directory
if (process.mainModule.filename.endsWith('.ts') === false) {
paths.push('..')
}
return join.apply(null, paths)
} }
function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {

View File

@ -1,3 +1,5 @@
import 'express-validator'
function exists (value: any) { function exists (value: any) {
return value !== undefined && value !== null return value !== undefined && value !== null
} }

View File

@ -1,4 +1,5 @@
import * as validator from 'validator' import * as validator from 'validator'
import 'express-validator'
import { isArray, exists } from './misc' import { isArray, exists } from './misc'
import { isTestInstance } from '../core-utils' import { isTestInstance } from '../core-utils'

View File

@ -1,3 +1,4 @@
import 'express-validator'
import { has, values } from 'lodash' import { has, values } from 'lodash'
import { import {

View File

@ -1,5 +1,6 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as validator from 'validator' import * as validator from 'validator'
import 'express-validator'
import { exists } from './misc' import { exists } from './misc'
import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'

View File

@ -1,5 +1,6 @@
import { values } from 'lodash' import { values } from 'lodash'
import * as validator from 'validator' import * as validator from 'validator'
import 'express-validator'
import 'multer' import 'multer'
import { import {

View File

@ -8,26 +8,25 @@ import { CONFIG } from './constants'
// Do not use barrel, we need to load database first // Do not use barrel, we need to load database first
import { logger } from '../helpers/logger' import { logger } from '../helpers/logger'
import { isTestInstance, readdirPromise } from '../helpers/core-utils' import { isTestInstance, readdirPromise } from '../helpers/core-utils'
import {
ApplicationModel, import { VideoModel } from './../models/video/video-interface'
AuthorModel, import { VideoTagModel } from './../models/video/video-tag-interface'
JobModel, import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
OAuthClientModel, import { VideoFileModel } from './../models/video/video-file-interface'
OAuthTokenModel, import { VideoAbuseModel } from './../models/video/video-abuse-interface'
PodModel, import { UserModel } from './../models/user/user-interface'
RequestModel, import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
RequestToPodModel, import { TagModel } from './../models/video/tag-interface'
RequestVideoEventModel, import { RequestModel } from './../models/request/request-interface'
RequestVideoQaduModel, import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
TagModel, import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
UserModel, import { RequestToPodModel } from './../models/request/request-to-pod-interface'
UserVideoRateModel, import { PodModel } from './../models/pod/pod-interface'
VideoAbuseModel, import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
BlacklistedVideoModel, import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
VideoFileModel, import { JobModel } from './../models/job/job-interface'
VideoTagModel, import { AuthorModel } from './../models/video/author-interface'
VideoModel import { ApplicationModel } from './../models/application/application-interface'
} from '../models'
const dbname = CONFIG.DATABASE.DBNAME const dbname = CONFIG.DATABASE.DBNAME
const username = CONFIG.DATABASE.USERNAME const username = CONFIG.DATABASE.USERNAME

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { UserModel } from '../user' import { UserModel } from '../user/user-interface'
export type OAuthTokenInfo = { export type OAuthTokenInfo = {
refreshToken: string refreshToken: string

View File

@ -2,7 +2,7 @@ import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { AbstractRequestClass } from './abstract-request-interface' import { AbstractRequestClass } from './abstract-request-interface'
import { PodInstance, PodAttributes } from '../pod' import { PodInstance, PodAttributes } from '../pod/pod-interface'
import { RequestEndpoint } from '../../../shared/models/request-scheduler.model' import { RequestEndpoint } from '../../../shared/models/request-scheduler.model'
export type RequestsGrouped = { export type RequestsGrouped = {

View File

@ -2,8 +2,8 @@ import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface' import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
import { VideoInstance } from '../video' import { VideoInstance } from '../video/video-interface'
import { PodInstance } from '../pod' import { PodInstance } from '../pod/pod-interface'
import { RequestVideoEventType } from '../../../shared/models/request-scheduler.model' import { RequestVideoEventType } from '../../../shared/models/request-scheduler.model'

View File

@ -2,8 +2,8 @@ import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface' import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
import { VideoInstance } from '../video' import { VideoInstance } from '../video/video-interface'
import { PodInstance } from '../pod' import { PodInstance } from '../pod/pod-interface'
import { RequestVideoQaduType } from '../../../shared/models/request-scheduler.model' import { RequestVideoQaduType } from '../../../shared/models/request-scheduler.model'

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { PodInstance } from '../pod' import { PodInstance } from '../pod/pod-interface'
export namespace AuthorMethods { export namespace AuthorMethods {
export type FindOrCreateAuthor = ( export type FindOrCreateAuthor = (

View File

@ -1,7 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import * as Promise from 'bluebird' import * as Promise from 'bluebird'
import { PodInstance } from '../pod' import { PodInstance } from '../pod/pod-interface'
import { ResultList } from '../../../shared' import { ResultList } from '../../../shared'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need

View File

@ -378,6 +378,8 @@ createTorrentAndSetInfoHash = function (this: VideoInstance, videoFile: VideoFil
return createTorrentPromise(this.getVideoFilePath(videoFile), options) return createTorrentPromise(this.getVideoFilePath(videoFile), options)
.then(torrent => { .then(torrent => {
const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
logger.info('Creating torrent %s.', filePath)
return writeFilePromise(filePath, torrent).then(() => torrent) return writeFilePromise(filePath, torrent).then(() => torrent)
}) })
.then(torrent => { .then(torrent => {

View File

@ -0,0 +1,2 @@
// Order of the tests we want to execute
import './reset-password'

View File

@ -0,0 +1,45 @@
import 'mocha'
import {
createUser,
execCLI,
flushTests,
getEnvCli,
killallServers,
login,
runServer,
ServerInfo,
setAccessTokensToServers
} from '../utils'
describe('Test reset password scripts', function () {
let server: ServerInfo
before(async function () {
this.timeout(30000)
await flushTests()
server = await runServer(1)
await setAccessTokensToServers([ server ])
await createUser(server.url, server.accessToken, 'user_1', 'super password')
})
it('Should change the user password from CLI', async function () {
this.timeout(20000)
const env = getEnvCli(server)
await execCLI(`echo coucou | ${env} npm run reset-password -- -u user_1`)
await login(server.url, server.client, { username: 'user_1', password: 'coucou' }, 200)
})
after(async function () {
killallServers([ server ])
// Keep the logs if the test failed
if (this['ok']) {
await flushTests()
}
})
})

View File

@ -0,0 +1,71 @@
import 'mocha'
import * as chai from 'chai'
const expect = chai.expect
import {
execCLI,
flushTests,
getEnvCli,
getVideosList,
killallServers,
parseTorrentVideo,
runServer,
ServerInfo,
setAccessTokensToServers,
uploadVideo
} from '../utils'
describe('Test update host scripts', function () {
let server: ServerInfo
before(async function () {
this.timeout(30000)
await flushTests()
const overrideConfig = {
webserver: {
port: 9256
}
}
server = await runServer(1, overrideConfig)
await setAccessTokensToServers([ server ])
// Upload two videos for our needs
const videoAttributes = {}
await uploadVideo(server.url, server.accessToken, videoAttributes)
await uploadVideo(server.url, server.accessToken, videoAttributes)
})
it('Should update torrent hosts', async function () {
this.timeout(20000)
killallServers([ server ])
server = await runServer(1)
const env = getEnvCli(server)
await execCLI(`${env} npm run update-host`)
const res = await getVideosList(server.url)
const videos = res.body.data
expect(videos[0].files[0].magnetUri).to.contain('localhost%3A9001%2Ftracker%2Fsocket')
expect(videos[0].files[0].magnetUri).to.contain('localhost%3A9001%2Fstatic%2Fwebseed%2F')
expect(videos[1].files[0].magnetUri).to.contain('localhost%3A9001%2Ftracker%2Fsocket')
expect(videos[1].files[0].magnetUri).to.contain('localhost%3A9001%2Fstatic%2Fwebseed%2F')
const torrent = await parseTorrentVideo(server, videos[0].uuid)
expect(torrent.announce[0]).to.equal('ws://localhost:9001/tracker/socket')
expect(torrent.urlList[0]).to.contain('http://localhost:9001/static/webseed')
})
after(async function () {
killallServers([ server ])
// Keep the logs if the test failed
if (this['ok']) {
await flushTests()
}
})
})

View File

@ -1,3 +1,4 @@
// Order of the tests we want to execute // Order of the tests we want to execute
import './client' import './client'
import './api/' import './api/'
import './cli/'

24
server/tests/utils/cli.ts Normal file
View File

@ -0,0 +1,24 @@
import { exec } from 'child_process'
import { ServerInfo } from './servers'
function getEnvCli (server?: ServerInfo) {
return `NODE_ENV=test NODE_APP_INSTANCE=${server.serverNumber}`
}
async function execCLI (command: string) {
return new Promise((res, rej) => {
exec(command, (err, stdout, stderr) => {
if (err) return rej(err)
return res(stdout)
})
})
}
// ---------------------------------------------------------------------------
export {
execCLI,
getEnvCli
}

View File

@ -1,3 +1,4 @@
export * from './cli'
export * from './clients' export * from './clients'
export * from './config' export * from './config'
export * from './login' export * from './login'

View File

@ -5,6 +5,7 @@ interface ServerInfo {
app: ChildProcess, app: ChildProcess,
url: string url: string
host: string host: string
serverNumber: number
client: { client: {
id: string, id: string,
@ -65,9 +66,10 @@ function flushTests () {
}) })
} }
function runServer (serverNumber: number) { function runServer (serverNumber: number, configOverride?: Object) {
const server: ServerInfo = { const server: ServerInfo = {
app: null, app: null,
serverNumber: serverNumber,
url: `http://localhost:${9000 + serverNumber}`, url: `http://localhost:${9000 + serverNumber}`,
host: `localhost:${9000 + serverNumber}`, host: `localhost:${9000 + serverNumber}`,
client: { client: {
@ -98,6 +100,11 @@ function runServer (serverNumber: number) {
const env = Object.create(process.env) const env = Object.create(process.env)
env['NODE_ENV'] = 'test' env['NODE_ENV'] = 'test'
env['NODE_APP_INSTANCE'] = serverNumber.toString() env['NODE_APP_INSTANCE'] = serverNumber.toString()
if (configOverride !== undefined) {
env['NODE_CONFIG'] = JSON.stringify(configOverride)
}
const options = { const options = {
silent: true, silent: true,
env: env, env: env,

View File

@ -1,8 +1,11 @@
import { readFile } from 'fs'
import * as request from 'supertest' import * as request from 'supertest'
import { join, isAbsolute } from 'path' import { join, isAbsolute } from 'path'
import * as parseTorrent from 'parse-torrent'
import { makeGetRequest } from './requests' import { makeGetRequest } from './requests'
import { readFilePromise } from './miscs' import { readFilePromise } from './miscs'
import { ServerInfo } from './servers'
type VideoAttributes = { type VideoAttributes = {
name?: string name?: string
@ -232,6 +235,17 @@ function rateVideo (url: string, accessToken: string, id: number, rating: string
.expect(specialStatus) .expect(specialStatus)
} }
function parseTorrentVideo (server: ServerInfo, videoUUID: string) {
return new Promise<any>((res, rej) => {
const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', videoUUID + '.torrent')
readFile(torrentPath, (err, data) => {
if (err) return rej(err)
return res(parseTorrent(data))
})
})
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
@ -250,5 +264,6 @@ export {
testVideoImage, testVideoImage,
uploadVideo, uploadVideo,
updateVideo, updateVideo,
rateVideo rateVideo,
parseTorrentVideo
} }