]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add tests for npm run scripts
authorChocobozzz <florian.bigard@gmail.com>
Thu, 7 Sep 2017 13:27:35 +0000 (15:27 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Thu, 7 Sep 2017 13:54:34 +0000 (15:54 +0200)
25 files changed:
scripts/danger/clean/cleaner.ts
scripts/reset-password.ts
scripts/update-host.ts
server/helpers/core-utils.ts
server/helpers/custom-validators/misc.ts
server/helpers/custom-validators/pods.ts
server/helpers/custom-validators/remote/videos.ts
server/helpers/custom-validators/users.ts
server/helpers/custom-validators/videos.ts
server/initializers/database.ts
server/models/oauth/oauth-token-interface.ts
server/models/request/request-interface.ts
server/models/request/request-video-event-interface.ts
server/models/request/request-video-qadu-interface.ts
server/models/video/author-interface.ts
server/models/video/video-abuse-interface.ts
server/models/video/video.ts
server/tests/cli/index.ts [new file with mode: 0644]
server/tests/cli/reset-password.ts [new file with mode: 0644]
server/tests/cli/update-host.ts [new file with mode: 0644]
server/tests/index.ts
server/tests/utils/cli.ts [new file with mode: 0644]
server/tests/utils/index.ts
server/tests/utils/servers.ts
server/tests/utils/videos.ts

index d80b8d3237ce8fc11fac916f56876a8c247359ba..4283335287c068cd98fc7af7077cc221e70c18ea 100644 (file)
@@ -16,7 +16,7 @@ db.init(true)
       const storageDir = STORAGE[storage]
 
       return new Promise((res, rej) => {
-        rimraf(storageDir, function (err) {
+        rimraf(storageDir, err => {
           if (err) return rej(err)
 
           console.info('%s deleted.', storageDir)
index 5ab7d01e5d38ba54bd456f44f3850f255412486f..f0c06a7bfddcd9b66ef53fb54807ee5bceb99c8a 100755 (executable)
@@ -18,7 +18,7 @@ db.init(true)
   .then(user => {
     if (!user) {
       console.error('User unknown.')
-      return
+      process.exit(-1)
     }
 
     const readline = require('readline')
index 5e69e4172bd9629b537605abce809b8f602ee026..06d84a658ca416107372efec502ac8424b62525c 100755 (executable)
@@ -1,8 +1,5 @@
-import { readFileSync, writeFileSync } from 'fs'
-import { join } from 'path'
-import * as parseTorrent from 'parse-torrent'
+import * as Promise from 'bluebird'
 
-import { CONFIG, STATIC_PATHS } from '../server/initializers/constants'
 import { database as db } from '../server/initializers/database'
 import { hasFriends } from '../server/lib/friends'
 
@@ -20,11 +17,18 @@ db.init(true)
     return db.Video.list()
   })
   .then(videos => {
+    const tasks: Promise<any>[] = []
+
     videos.forEach(video => {
+      console.log('Updating video ' + video.uuid)
+
       video.VideoFiles.forEach(file => {
-        video.createTorrentAndSetInfoHash(file)
+        tasks.push(video.createTorrentAndSetInfoHash(file))
       })
     })
 
+    return Promise.all(tasks)
+  })
+  .then(() => {
     process.exit(0)
   })
index f8dd45533f352a05c6f85e2c0de3948e3dd9d3a3..2ec7e6515fc82982e573d29e08438436b709e7e1 100644 (file)
@@ -25,8 +25,15 @@ function isTestInstance () {
 }
 
 function root () {
-  // We are in /dist/helpers/utils.js
-  return join(__dirname, '..', '..', '..')
+  // We are in /helpers/utils.js
+  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> {
index f6bb02c8e9fc03865456b76395e00b3b15259033..8d215a416bc899c6cbf122b7fad5f603f2dbc512 100644 (file)
@@ -1,3 +1,5 @@
+import 'express-validator'
+
 function exists (value: any) {
   return value !== undefined && value !== null
 }
index f2ca520c089891c536f95acffdeb6200b311a440..844bfdf7833b4a56758e83baeb3c123eaaa00583 100644 (file)
@@ -1,4 +1,5 @@
 import * as validator from 'validator'
+import 'express-validator'
 
 import { isArray, exists } from './misc'
 import { isTestInstance } from '../core-utils'
index 49f76e90921c904bf5245418e2aa7ca6d236fea3..e5c76f3cacf98206b243a015eca716705b765eae 100644 (file)
@@ -1,3 +1,4 @@
+import 'express-validator'
 import { has, values } from 'lodash'
 
 import {
index 00061f9df82d4a52a45d134d6b98180ff42f9958..805437efa2896b0c44f24d5933b208ba4fa816df 100644 (file)
@@ -1,5 +1,6 @@
 import { values } from 'lodash'
 import * as validator from 'validator'
+import 'express-validator'
 
 import { exists } from './misc'
 import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
index 8b3a2664447442010eafbc897dfa775ca496478c..1d27e47fc392a57c4a04057c16ac8569cfc43cba 100644 (file)
@@ -1,5 +1,6 @@
 import { values } from 'lodash'
 import * as validator from 'validator'
+import 'express-validator'
 import 'multer'
 
 import {
index d04c8db1b40beca01481bccf1d814ec26d42fb3b..d59fd365573513243de7dc86e12c063d747db537 100644 (file)
@@ -8,26 +8,25 @@ import { CONFIG } from './constants'
 // Do not use barrel, we need to load database first
 import { logger } from '../helpers/logger'
 import { isTestInstance, readdirPromise } from '../helpers/core-utils'
-import {
-  ApplicationModel,
-  AuthorModel,
-  JobModel,
-  OAuthClientModel,
-  OAuthTokenModel,
-  PodModel,
-  RequestModel,
-  RequestToPodModel,
-  RequestVideoEventModel,
-  RequestVideoQaduModel,
-  TagModel,
-  UserModel,
-  UserVideoRateModel,
-  VideoAbuseModel,
-  BlacklistedVideoModel,
-  VideoFileModel,
-  VideoTagModel,
-  VideoModel
-} from '../models'
+
+import { VideoModel } from './../models/video/video-interface'
+import { VideoTagModel } from './../models/video/video-tag-interface'
+import { BlacklistedVideoModel } from './../models/video/video-blacklist-interface'
+import { VideoFileModel } from './../models/video/video-file-interface'
+import { VideoAbuseModel } from './../models/video/video-abuse-interface'
+import { UserModel } from './../models/user/user-interface'
+import { UserVideoRateModel } from './../models/user/user-video-rate-interface'
+import { TagModel } from './../models/video/tag-interface'
+import { RequestModel } from './../models/request/request-interface'
+import { RequestVideoQaduModel } from './../models/request/request-video-qadu-interface'
+import { RequestVideoEventModel } from './../models/request/request-video-event-interface'
+import { RequestToPodModel } from './../models/request/request-to-pod-interface'
+import { PodModel } from './../models/pod/pod-interface'
+import { OAuthTokenModel } from './../models/oauth/oauth-token-interface'
+import { OAuthClientModel } from './../models/oauth/oauth-client-interface'
+import { JobModel } from './../models/job/job-interface'
+import { AuthorModel } from './../models/video/author-interface'
+import { ApplicationModel } from './../models/application/application-interface'
 
 const dbname = CONFIG.DATABASE.DBNAME
 const username = CONFIG.DATABASE.USERNAME
index 97af3c81520e26bfbd03a184f6c74b34321a1377..0c947bde874f50f42b0f36a7071446afc347998c 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
-import { UserModel } from '../user'
+import { UserModel } from '../user/user-interface'
 
 export type OAuthTokenInfo = {
   refreshToken: string
index 7b0ee4df9af0092430902c97a189558a596f2fe0..dae35651bf9ebfd3ad85d1ec08d9ed3b4b7129f0 100644 (file)
@@ -2,7 +2,7 @@ import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
 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'
 
 export type RequestsGrouped = {
index a5032e1b13222679b7e82adb247f656c8949a1a9..26cabe3ba86baadf57b66e25f4965f2bf4e50b35 100644 (file)
@@ -2,8 +2,8 @@ import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
 import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
-import { VideoInstance } from '../video'
-import { PodInstance } from '../pod'
+import { VideoInstance } from '../video/video-interface'
+import { PodInstance } from '../pod/pod-interface'
 
 import { RequestVideoEventType } from '../../../shared/models/request-scheduler.model'
 
index 9a172a4d4cd07b8b351b11bda386d6b510ac5d24..b7b7b1ecc1bf30d77993c6c95d320cbf11de62ae 100644 (file)
@@ -2,8 +2,8 @@ import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
 import { AbstractRequestClass, AbstractRequestToPodClass } from './abstract-request-interface'
-import { VideoInstance } from '../video'
-import { PodInstance } from '../pod'
+import { VideoInstance } from '../video/video-interface'
+import { PodInstance } from '../pod/pod-interface'
 
 import { RequestVideoQaduType } from '../../../shared/models/request-scheduler.model'
 
index dbcb85b17d2ce45e78833f3621a2e580c9a8f8bc..52a00a1d33c2e8d6b5c5aef94c958756a5c6cb58 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
-import { PodInstance } from '../pod'
+import { PodInstance } from '../pod/pod-interface'
 
 export namespace AuthorMethods {
   export type FindOrCreateAuthor = (
index abc82f7ac074f84d719bdb7743e79af3dd0816f3..978268926cbb9ad44f8c406fd2ab5edb67f13f93 100644 (file)
@@ -1,7 +1,7 @@
 import * as Sequelize from 'sequelize'
 import * as Promise from 'bluebird'
 
-import { PodInstance } from '../pod'
+import { PodInstance } from '../pod/pod-interface'
 import { ResultList } from '../../../shared'
 
 // Don't use barrel, import just what we need
index 8c69fe189e98e6992e4a008010e4978aec8e4b0c..1134525f02b4798bdb77f40d4ae4b2d148b31b66 100644 (file)
@@ -378,6 +378,8 @@ createTorrentAndSetInfoHash = function (this: VideoInstance, videoFile: VideoFil
   return createTorrentPromise(this.getVideoFilePath(videoFile), options)
     .then(torrent => {
       const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
+      logger.info('Creating torrent %s.', filePath)
+
       return writeFilePromise(filePath, torrent).then(() => torrent)
     })
     .then(torrent => {
diff --git a/server/tests/cli/index.ts b/server/tests/cli/index.ts
new file mode 100644 (file)
index 0000000..e5a19ff
--- /dev/null
@@ -0,0 +1,2 @@
+// Order of the tests we want to execute
+import './reset-password'
diff --git a/server/tests/cli/reset-password.ts b/server/tests/cli/reset-password.ts
new file mode 100644 (file)
index 0000000..bbf0b3d
--- /dev/null
@@ -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()
+    }
+  })
+})
diff --git a/server/tests/cli/update-host.ts b/server/tests/cli/update-host.ts
new file mode 100644 (file)
index 0000000..af9703b
--- /dev/null
@@ -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()
+    }
+  })
+})
index 26f0816b761ad2500c5848c3f6ee226f64843fbc..a7dd0d824fc154daff26f0fb8d333abdd56caf89 100644 (file)
@@ -1,3 +1,4 @@
 // Order of the tests we want to execute
 import './client'
 import './api/'
+import './cli/'
diff --git a/server/tests/utils/cli.ts b/server/tests/utils/cli.ts
new file mode 100644 (file)
index 0000000..5f07a83
--- /dev/null
@@ -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
+}
index 9077b056865a955f223997f5edaee45a59a87d5a..0fa28f2afdbebd3445728de9c2d38dfaca4871a3 100644 (file)
@@ -1,3 +1,4 @@
+export * from './cli'
 export * from './clients'
 export * from './config'
 export * from './login'
index 272a8935eef3e53fcbc9cfee64d2ed153ba66dd0..88027f74e557dc71eb0551bcd4277499cd7d7472 100644 (file)
@@ -5,6 +5,7 @@ interface ServerInfo {
   app: ChildProcess,
   url: string
   host: string
+  serverNumber: number
 
   client: {
     id: string,
@@ -65,9 +66,10 @@ function flushTests () {
   })
 }
 
-function runServer (serverNumber: number) {
+function runServer (serverNumber: number, configOverride?: Object) {
   const server: ServerInfo = {
     app: null,
+    serverNumber: serverNumber,
     url: `http://localhost:${9000 + serverNumber}`,
     host: `localhost:${9000 + serverNumber}`,
     client: {
@@ -98,6 +100,11 @@ function runServer (serverNumber: number) {
   const env = Object.create(process.env)
   env['NODE_ENV'] = 'test'
   env['NODE_APP_INSTANCE'] = serverNumber.toString()
+
+  if (configOverride !== undefined) {
+    env['NODE_CONFIG'] = JSON.stringify(configOverride)
+  }
+
   const options = {
     silent: true,
     env: env,
index 42b7dd05afb556ad3143b6c8c831c05b6a56d576..509a2430a282cb82f96792ff1317a38d908e21a6 100644 (file)
@@ -1,8 +1,11 @@
+import { readFile } from 'fs'
 import * as request from 'supertest'
 import { join, isAbsolute } from 'path'
+import * as parseTorrent from 'parse-torrent'
 
 import { makeGetRequest } from './requests'
 import { readFilePromise } from './miscs'
+import { ServerInfo } from './servers'
 
 type VideoAttributes = {
   name?: string
@@ -232,6 +235,17 @@ function rateVideo (url: string, accessToken: string, id: number, rating: string
           .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 {
@@ -250,5 +264,6 @@ export {
   testVideoImage,
   uploadVideo,
   updateVideo,
-  rateVideo
+  rateVideo,
+  parseTorrentVideo
 }