]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/mock-servers/mock-plugin-blocklist.ts
Merge branch 'release/3.4.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-plugin-blocklist.ts
CommitLineData
41fb13c3 1import express, { Request, Response } from 'express'
7820a54e 2import { Server } from 'http'
63da15eb 3import { randomInt } from '@shared/core-utils'
70430c27 4import { terminateServer } from './utils'
8bff1fe0
C
5
6type BlocklistResponse = {
7 data: {
8 value: string
9 action?: 'add' | 'remove'
91b8e675 10 updatedAt?: string
8bff1fe0
C
11 }[]
12}
13
14export class MockBlocklist {
15 private body: BlocklistResponse
7820a54e 16 private server: Server
8bff1fe0
C
17
18 initialize () {
63da15eb 19 return new Promise<number>(res => {
8bff1fe0
C
20 const app = express()
21
41fb13c3 22 app.get('/blocklist', (req: Request, res: Response) => {
8bff1fe0
C
23 return res.json(this.body)
24 })
25
3c25d37a 26 const port = 45000 + randomInt(1, 1000)
63da15eb 27 this.server = app.listen(port, () => res(port))
8bff1fe0
C
28 })
29 }
30
31 replace (body: BlocklistResponse) {
32 this.body = body
33 }
7820a54e
C
34
35 terminate () {
70430c27 36 return terminateServer(this.server)
7820a54e 37 }
8bff1fe0 38}