From f6500729850549e632399155d0f3b69822e28b16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Apr 2021 13:52:27 +0200 Subject: [PATCH] More robust tests --- .../api/notifications/admin-notifications.ts | 10 ++--- .../notifications/moderation-notifications.ts | 39 ++++++++++--------- server/tests/api/server/auto-follows.ts | 7 ++-- .../mock-servers/joinpeertube-versions.ts | 6 ++- .../mock-servers/mock-instances-index.ts | 6 ++- 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/server/tests/api/notifications/admin-notifications.ts b/server/tests/api/notifications/admin-notifications.ts index e07327d74..cfe0bd2bb 100644 --- a/server/tests/api/notifications/admin-notifications.ts +++ b/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 () { diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts index 4c00d97f8..4ce6675b6 100644 --- a/server/tests/api/notifications/moderation-notifications.ts +++ b/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 () { diff --git a/server/tests/api/server/auto-follows.ts b/server/tests/api/server/auto-follows.ts index e04d70af4..1519b263f 100644 --- a/server/tests/api/server/auto-follows.ts +++ b/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 } } diff --git a/shared/extra-utils/mock-servers/joinpeertube-versions.ts b/shared/extra-utils/mock-servers/joinpeertube-versions.ts index d7d5b2c49..5ea432ecf 100644 --- a/shared/extra-utils/mock-servers/joinpeertube-versions.ts +++ b/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(res => { + return new Promise(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)) }) } diff --git a/shared/extra-utils/mock-servers/mock-instances-index.ts b/shared/extra-utils/mock-servers/mock-instances-index.ts index 2604eda03..c9e33087d 100644 --- a/shared/extra-utils/mock-servers/mock-instances-index.ts +++ b/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(res => { + return new Promise(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)) }) } -- 2.41.0