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