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