]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/mock-servers/mock-email.ts
Optimize torrent URL update
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-email.ts
CommitLineData
3d470a53 1import { ChildProcess } from 'child_process'
6c5065a0
C
2import { randomInt } from '@shared/core-utils'
3import { parallelTests } from '../miscs'
af37210c 4
3d470a53
C
5const MailDev = require('maildev')
6
af37210c
JM
7class MockSmtpServer {
8
9 private static instance: MockSmtpServer
10 private started = false
89ada4e2 11 private emailChildProcess: ChildProcess
af37210c
JM
12 private emails: object[]
13
3d470a53 14 private constructor () { }
af37210c
JM
15
16 collectEmails (emailsCollection: object[]) {
7c3b7976
C
17 return new Promise<number>((res, rej) => {
18 const port = parallelTests() ? randomInt(1000, 2000) : 1025
3d470a53 19 this.emails = emailsCollection
7c3b7976 20
af37210c 21 if (this.started) {
ba5a8d89 22 return res(undefined)
af37210c
JM
23 }
24
3d470a53
C
25 const maildev = new MailDev({
26 ip: '127.0.0.1',
27 smtp: port,
28 disableWeb: true,
29 silent: true
30 })
31
32 maildev.on('new', email => {
33 this.emails.push(email)
af37210c 34 })
3d470a53
C
35
36 maildev.listen(err => {
37 if (err) return rej(err)
faa9d434 38
af37210c 39 this.started = true
faa9d434 40
7c3b7976 41 return res(port)
af37210c
JM
42 })
43 })
44 }
45
89ada4e2 46 kill () {
a4101923
C
47 if (!this.emailChildProcess) return
48
89ada4e2
C
49 process.kill(this.emailChildProcess.pid)
50
51 this.emailChildProcess = null
52 MockSmtpServer.instance = null
53 }
54
af37210c
JM
55 static get Instance () {
56 return this.instance || (this.instance = new this())
57 }
f076daa7
C
58}
59
60// ---------------------------------------------------------------------------
61
62export {
af37210c 63 MockSmtpServer
f076daa7 64}