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