Browse Source

More robust tests

Chocobozzz 3 years ago
parent
commit
f650072985

+ 5 - 5
server/tests/api/notifications/admin-notifications.ts

@@ -3,6 +3,7 @@
 import 'mocha'
 import { expect } from 'chai'
 import { MockJoinPeerTubeVersions } from '@shared/extra-utils/mock-servers/joinpeertube-versions'
+import { PluginType } from '@shared/models'
 import { cleanupTests, installPlugin, setPluginLatestVersion, setPluginVersion, wait } from '../../../../shared/extra-utils'
 import { ServerInfo } from '../../../../shared/extra-utils/index'
 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
@@ -13,7 +14,6 @@ import {
   prepareNotificationsTest
 } from '../../../../shared/extra-utils/users/user-notifications'
 import { UserNotification, UserNotificationType } from '../../../../shared/models/users'
-import { PluginType } from '@shared/models'
 
 describe('Test admin notifications', function () {
   let server: ServerInfo
@@ -26,11 +26,14 @@ describe('Test admin notifications', function () {
   before(async function () {
     this.timeout(120000)
 
+    joinPeerTubeServer = new MockJoinPeerTubeVersions()
+    const port = await joinPeerTubeServer.initialize()
+
     const config = {
       peertube: {
         check_latest_version: {
           enabled: true,
-          url: 'http://localhost:42102/versions.json'
+          url: `http://localhost:${port}/versions.json`
         }
       },
       plugins: {
@@ -66,9 +69,6 @@ describe('Test admin notifications', function () {
       accessToken: server.accessToken,
       npmName: 'peertube-theme-background-red'
     })
-
-    joinPeerTubeServer = new MockJoinPeerTubeVersions()
-    await joinPeerTubeServer.initialize()
   })
 
   describe('Latest PeerTube version notification', function () {

+ 20 - 19
server/tests/api/notifications/moderation-notifications.ts

@@ -2,8 +2,9 @@
 
 import 'mocha'
 import { v4 as uuidv4 } from 'uuid'
-
+import { AbuseState } from '@shared/models'
 import {
+  addAbuseMessage,
   addVideoCommentThread,
   addVideoToBlacklist,
   cleanupTests,
@@ -20,18 +21,19 @@ import {
   removeVideoFromBlacklist,
   reportAbuse,
   unfollow,
+  updateAbuse,
   updateCustomConfig,
   updateCustomSubConfig,
-  wait,
-  updateAbuse,
-  addAbuseMessage
+  wait
 } from '../../../../shared/extra-utils'
 import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
 import {
+  checkAbuseStateChange,
   checkAutoInstanceFollowing,
   CheckerBaseParams,
+  checkNewAbuseMessage,
   checkNewAccountAbuseForModerators,
   checkNewBlacklistOnMyVideo,
   checkNewCommentAbuseForModerators,
@@ -41,15 +43,12 @@ import {
   checkUserRegistered,
   checkVideoAutoBlacklistForModerators,
   checkVideoIsPublished,
-  prepareNotificationsTest,
-  checkAbuseStateChange,
-  checkNewAbuseMessage
+  prepareNotificationsTest
 } from '../../../../shared/extra-utils/users/user-notifications'
 import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions'
 import { CustomConfig } from '../../../../shared/models/server'
 import { UserNotification } from '../../../../shared/models/users'
 import { VideoPrivacy } from '../../../../shared/models/videos'
-import { AbuseState } from '@shared/models'
 
 describe('Test moderation notifications', function () {
   let servers: ServerInfo[] = []
@@ -364,16 +363,7 @@ describe('Test moderation notifications', function () {
 
   describe('New instance follows', function () {
     const instanceIndexServer = new MockInstancesIndex()
-    const config = {
-      followings: {
-        instance: {
-          autoFollowIndex: {
-            indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
-            enabled: true
-          }
-        }
-      }
-    }
+    let config: any
     let baseParams: CheckerBaseParams
 
     before(async () => {
@@ -384,8 +374,19 @@ describe('Test moderation notifications', function () {
         token: servers[0].accessToken
       }
 
-      await instanceIndexServer.initialize()
+      const port = await instanceIndexServer.initialize()
       instanceIndexServer.addInstance(servers[1].host)
+
+      config = {
+        followings: {
+          instance: {
+            autoFollowIndex: {
+              indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
+              enabled: true
+            }
+          }
+        }
+      }
     })
 
     it('Should send a notification only to admin when there is a new instance follower', async function () {

+ 4 - 3
server/tests/api/server/auto-follows.ts

@@ -1,7 +1,7 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import * as chai from 'chai'
 import 'mocha'
+import * as chai from 'chai'
 import {
   acceptFollower,
   cleanupTests,
@@ -153,9 +153,10 @@ describe('Test auto follows', function () {
 
   describe('Auto follow index', function () {
     const instanceIndexServer = new MockInstancesIndex()
+    let port: number
 
     before(async () => {
-      await instanceIndexServer.initialize()
+      port = await instanceIndexServer.initialize()
     })
 
     it('Should not auto follow index if the option is not enabled', async function () {
@@ -177,7 +178,7 @@ describe('Test auto follows', function () {
         followings: {
           instance: {
             autoFollowIndex: {
-              indexUrl: 'http://localhost:42101/api/v1/instances/hosts',
+              indexUrl: `http://localhost:${port}/api/v1/instances/hosts`,
               enabled: true
             }
           }

+ 4 - 2
shared/extra-utils/mock-servers/joinpeertube-versions.ts

@@ -1,10 +1,11 @@
 import * as express from 'express'
+import { randomInt } from '@shared/core-utils'
 
 export class MockJoinPeerTubeVersions {
   private latestVersion: string
 
   initialize () {
-    return new Promise<void>(res => {
+    return new Promise<number>(res => {
       const app = express()
 
       app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
@@ -21,7 +22,8 @@ export class MockJoinPeerTubeVersions {
         })
       })
 
-      app.listen(42102, () => res())
+      const port = 42201 + randomInt(1, 100)
+      app.listen(port, () => res(port))
     })
   }
 

+ 4 - 2
shared/extra-utils/mock-servers/mock-instances-index.ts

@@ -1,10 +1,11 @@
 import * as express from 'express'
+import { randomInt } from '@shared/core-utils'
 
 export class MockInstancesIndex {
   private readonly indexInstances: { host: string, createdAt: string }[] = []
 
   initialize () {
-    return new Promise<void>(res => {
+    return new Promise<number>(res => {
       const app = express()
 
       app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
@@ -28,7 +29,8 @@ export class MockInstancesIndex {
         })
       })
 
-      app.listen(42101, () => res())
+      const port = 42101 + randomInt(1, 100)
+      app.listen(port, () => res(port))
     })
   }