diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 11:58:15 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 12:24:03 +0100 |
commit | c55e3d7227fe1453869e309025996b9d75256d5d (patch) | |
tree | 08e9b0ca210d75c82c8606fef0852eca020e8e0e /server/tests/shared/mock-servers/mock-email.ts | |
parent | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (diff) | |
download | PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.gz PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.zst PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.zip |
Move test functions outside extra-utils
Diffstat (limited to 'server/tests/shared/mock-servers/mock-email.ts')
-rw-r--r-- | server/tests/shared/mock-servers/mock-email.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/server/tests/shared/mock-servers/mock-email.ts b/server/tests/shared/mock-servers/mock-email.ts new file mode 100644 index 000000000..c518679c9 --- /dev/null +++ b/server/tests/shared/mock-servers/mock-email.ts | |||
@@ -0,0 +1,62 @@ | |||
1 | import { ChildProcess } from 'child_process' | ||
2 | import MailDev from '@peertube/maildev' | ||
3 | import { parallelTests, randomInt } from '@shared/core-utils' | ||
4 | |||
5 | class MockSmtpServer { | ||
6 | |||
7 | private static instance: MockSmtpServer | ||
8 | private started = false | ||
9 | private emailChildProcess: ChildProcess | ||
10 | private emails: object[] | ||
11 | |||
12 | private constructor () { } | ||
13 | |||
14 | collectEmails (emailsCollection: object[]) { | ||
15 | return new Promise<number>((res, rej) => { | ||
16 | const port = parallelTests() ? randomInt(1000, 2000) : 1025 | ||
17 | this.emails = emailsCollection | ||
18 | |||
19 | if (this.started) { | ||
20 | return res(undefined) | ||
21 | } | ||
22 | |||
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) | ||
32 | }) | ||
33 | |||
34 | maildev.listen(err => { | ||
35 | if (err) return rej(err) | ||
36 | |||
37 | this.started = true | ||
38 | |||
39 | return res(port) | ||
40 | }) | ||
41 | }) | ||
42 | } | ||
43 | |||
44 | kill () { | ||
45 | if (!this.emailChildProcess) return | ||
46 | |||
47 | process.kill(this.emailChildProcess.pid) | ||
48 | |||
49 | this.emailChildProcess = null | ||
50 | MockSmtpServer.instance = null | ||
51 | } | ||
52 | |||
53 | static get Instance () { | ||
54 | return this.instance || (this.instance = new this()) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | // --------------------------------------------------------------------------- | ||
59 | |||
60 | export { | ||
61 | MockSmtpServer | ||
62 | } | ||