]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/e2e/src/utils/mock-smtp.ts
Add E2E client tests for signup approval
[github/Chocobozzz/PeerTube.git] / client / e2e / src / utils / mock-smtp.ts
diff --git a/client/e2e/src/utils/mock-smtp.ts b/client/e2e/src/utils/mock-smtp.ts
new file mode 100644 (file)
index 0000000..614477d
--- /dev/null
@@ -0,0 +1,58 @@
+import { ChildProcess } from 'child_process'
+import MailDev from '@peertube/maildev'
+
+class MockSMTPServer {
+
+  private static instance: MockSMTPServer
+  private started = false
+  private emailChildProcess: ChildProcess
+  private emails: object[]
+
+  collectEmails (port: number, emailsCollection: object[]) {
+    return new Promise<number>((res, rej) => {
+      this.emails = emailsCollection
+
+      if (this.started) {
+        return res(undefined)
+      }
+
+      const maildev = new MailDev({
+        ip: '127.0.0.1',
+        smtp: port,
+        disableWeb: true,
+        silent: true
+      })
+
+      maildev.on('new', email => {
+        this.emails.push(email)
+      })
+
+      maildev.listen(err => {
+        if (err) return rej(err)
+
+        this.started = true
+
+        return res(port)
+      })
+    })
+  }
+
+  kill () {
+    if (!this.emailChildProcess) return
+
+    process.kill(this.emailChildProcess.pid)
+
+    this.emailChildProcess = null
+    MockSMTPServer.instance = null
+  }
+
+  static get Instance () {
+    return this.instance || (this.instance = new this())
+  }
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  MockSMTPServer
+}