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