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