Browse Source

test(server): Create TestingTransport option for testing email sending

Michael Bromley 7 years ago
parent
commit
634325d0c0

+ 13 - 2
server/src/config/email/email-transport-options.ts

@@ -1,6 +1,8 @@
 /**
  * A subset of the SMTP transport options of Nodemailer (https://nodemailer.com/smtp/)
  */
+import { GeneratedEmailContext } from '../../email/email-context';
+
 export interface SMTPTransportOptions {
     type: 'smtp';
     /** the hostname or IP address to connect to (defaults to ‘localhost’) */
@@ -51,14 +53,23 @@ export interface FileTransportOptions {
 }
 
 /**
- * Does nothing with the generated email. Mainly intended for use in testing.
+ * Does nothing with the generated email. Mainly intended for use in testing where we don't care about the email transport.
  */
 export interface NoopTransportOptions {
     type: 'none';
 }
 
+/**
+ * Forwards the raw GeneratedEmailContext object to a provided callback, for use in testing.
+ */
+export interface TestingTransportOptions {
+    type: 'testing';
+    onSend: (context: GeneratedEmailContext) => void;
+}
+
 export type EmailTransportOptions =
     | SMTPTransportOptions
     | SendmailTransportOptions
     | FileTransportOptions
-    | NoopTransportOptions;
+    | NoopTransportOptions
+    | TestingTransportOptions;

+ 4 - 1
server/src/email/email-sender.ts

@@ -57,6 +57,9 @@ export class EmailSender {
                 } as SMTPTransport.Options);
                 await this.sendMail(email, transporter);
                 break;
+            case 'testing':
+                options.onSend(email);
+                break;
             default:
                 return assertNever(options);
         }
@@ -71,7 +74,7 @@ export class EmailSender {
     }
 
     private async sendFileHtml(email: GeneratedEmailContext, pathWithoutExt: string) {
-        const content = `<html>
+        const content = `<html lang="en">
             <head>
                 <title>${email.subject}</title>
                 <style>