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