aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-email.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-06 10:36:54 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:17 +0200
commit8ef9457fdee7812b1a8cc3b3bdeff94130819003 (patch)
treef95a5565332a095e17014f211386853a86c9dee1 /shared/extra-utils/mock-servers/mock-email.ts
parentc8fc9b47188b33cfc33184b1ee7e680a093244f1 (diff)
downloadPeerTube-8ef9457fdee7812b1a8cc3b3bdeff94130819003.tar.gz
PeerTube-8ef9457fdee7812b1a8cc3b3bdeff94130819003.tar.zst
PeerTube-8ef9457fdee7812b1a8cc3b3bdeff94130819003.zip
Correctly export misc files
Diffstat (limited to 'shared/extra-utils/mock-servers/mock-email.ts')
-rw-r--r--shared/extra-utils/mock-servers/mock-email.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/shared/extra-utils/mock-servers/mock-email.ts b/shared/extra-utils/mock-servers/mock-email.ts
new file mode 100644
index 000000000..9fc9a5ad0
--- /dev/null
+++ b/shared/extra-utils/mock-servers/mock-email.ts
@@ -0,0 +1,64 @@
1import { ChildProcess } from 'child_process'
2import { randomInt } from '../../core-utils/miscs/miscs'
3import { parallelTests } from '../server/servers'
4
5const MailDev = require('maildev')
6
7class MockSmtpServer {
8
9 private static instance: MockSmtpServer
10 private started = false
11 private emailChildProcess: ChildProcess
12 private emails: object[]
13
14 private constructor () { }
15
16 collectEmails (emailsCollection: object[]) {
17 return new Promise<number>((res, rej) => {
18 const port = parallelTests() ? randomInt(1000, 2000) : 1025
19 this.emails = emailsCollection
20
21 if (this.started) {
22 return res(undefined)
23 }
24
25 const maildev = new MailDev({
26 ip: '127.0.0.1',
27 smtp: port,
28 disableWeb: true,
29 silent: true
30 })
31
32 maildev.on('new', email => {
33 this.emails.push(email)
34 })
35
36 maildev.listen(err => {
37 if (err) return rej(err)
38
39 this.started = true
40
41 return res(port)
42 })
43 })
44 }
45
46 kill () {
47 if (!this.emailChildProcess) return
48
49 process.kill(this.emailChildProcess.pid)
50
51 this.emailChildProcess = null
52 MockSmtpServer.instance = null
53 }
54
55 static get Instance () {
56 return this.instance || (this.instance = new this())
57 }
58}
59
60// ---------------------------------------------------------------------------
61
62export {
63 MockSmtpServer
64}