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