1
0
Fork 0
peertube/server/tests/shared/mock-servers/mock-email.ts

63 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-14 14:52:22 +00:00
import { ChildProcess } from 'child_process'
2021-10-26 14:03:53 +00:00
import MailDev from '@peertube/maildev'
import { parallelTests, randomInt } from '@shared/core-utils'
class MockSmtpServer {
private static instance: MockSmtpServer
private started = false
2019-01-08 14:51:52 +00:00
private emailChildProcess: ChildProcess
private emails: object[]
2021-06-14 14:52:22 +00:00
private constructor () { }
collectEmails (emailsCollection: object[]) {
2019-04-24 13:10:37 +00:00
return new Promise<number>((res, rej) => {
const port = parallelTests() ? randomInt(1000, 2000) : 1025
2021-06-14 14:52:22 +00:00
this.emails = emailsCollection
2019-04-24 13:10:37 +00:00
if (this.started) {
2021-02-03 08:33:05 +00:00
return res(undefined)
}
2021-06-14 14:52:22 +00:00
const maildev = new MailDev({
ip: '127.0.0.1',
smtp: port,
disableWeb: true,
silent: true
})
maildev.on('new', email => {
this.emails.push(email)
})
2021-06-14 14:52:22 +00:00
maildev.listen(err => {
if (err) return rej(err)
2020-06-17 08:55:40 +00:00
this.started = true
2020-06-17 08:55:40 +00:00
2019-04-24 13:10:37 +00:00
return res(port)
})
})
}
2019-01-08 14:51:52 +00:00
kill () {
2019-01-09 14:14:29 +00:00
if (!this.emailChildProcess) return
2019-01-08 14:51:52 +00:00
process.kill(this.emailChildProcess.pid)
this.emailChildProcess = null
MockSmtpServer.instance = null
}
static get Instance () {
return this.instance || (this.instance = new this())
}
2018-01-30 14:16:24 +00:00
}
// ---------------------------------------------------------------------------
export {
MockSmtpServer
2018-01-30 14:16:24 +00:00
}