From d3a2251d4a31041d805bbe39d998de90906b05da Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Sun, 15 Oct 2017 22:41:18 +0200 Subject: [PATCH] Allow anonymous user in SMTP notifier SMTP notifier should be able to send emails with anonymous user, i.e. without providing username and password in configuration file. --- .../src/lib/configuration/Configuration.d.ts | 4 +- server/src/lib/notifiers/MailSenderBuilder.ts | 10 ++-- .../test/notifiers/MailSenderBuilder.test.ts | 51 ++++++++++++------- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/server/src/lib/configuration/Configuration.d.ts b/server/src/lib/configuration/Configuration.d.ts index 3b6b0c2a..199a84bc 100644 --- a/server/src/lib/configuration/Configuration.d.ts +++ b/server/src/lib/configuration/Configuration.d.ts @@ -73,8 +73,8 @@ export interface GmailNotifierConfiguration { } export interface SmtpNotifierConfiguration { - username: string; - password: string; + username?: string; + password?: string; host: string; port: number; secure: boolean; diff --git a/server/src/lib/notifiers/MailSenderBuilder.ts b/server/src/lib/notifiers/MailSenderBuilder.ts index 3b2a1ba9..1b44f7b4 100644 --- a/server/src/lib/notifiers/MailSenderBuilder.ts +++ b/server/src/lib/notifiers/MailSenderBuilder.ts @@ -28,11 +28,15 @@ export class MailSenderBuilder implements IMailSenderBuilder { host: options.host, port: options.port, secure: options.secure, // upgrade later with STARTTLS - auth: { + }; + + if (options.username && options.password) { + smtpOptions.auth = { user: options.username, pass: options.password - } - }; + }; + } + return new MailSender(smtpOptions, this.nodemailer); } } \ No newline at end of file diff --git a/server/test/notifiers/MailSenderBuilder.test.ts b/server/test/notifiers/MailSenderBuilder.test.ts index f0d76911..6bdc0f5f 100644 --- a/server/test/notifiers/MailSenderBuilder.test.ts +++ b/server/test/notifiers/MailSenderBuilder.test.ts @@ -25,24 +25,41 @@ describe("test MailSenderBuilder", function() { Assert.equal(createTransportStub.getCall(0).args[0].auth.pass, "pass_gmail"); }); - it("should create a smtp mail sender", function() { - const mailSenderBuilder = new MailSenderBuilder(Nodemailer); - mailSenderBuilder.buildSmtp({ - host: "mail.example.com", - password: "password", - port: 25, - secure: true, - username: "user", - sender: "admin@example.com" + describe("build smtp mail sender", function() { + it("should create a smtp mail sender with authenticated user", function() { + const mailSenderBuilder = new MailSenderBuilder(Nodemailer); + mailSenderBuilder.buildSmtp({ + host: "mail.example.com", + password: "password", + port: 25, + secure: true, + username: "user", + sender: "admin@example.com" + }); + Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], { + host: "mail.example.com", + auth: { + pass: "password", + user: "user" + }, + port: 25, + secure: true, + }); }); - Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], { - host: "mail.example.com", - auth: { - pass: "password", - user: "user" - }, - port: 25, - secure: true, + + it("should create a smtp mail sender with anonymous user", function() { + const mailSenderBuilder = new MailSenderBuilder(Nodemailer); + mailSenderBuilder.buildSmtp({ + host: "mail.example.com", + port: 25, + secure: true, + sender: "admin@example.com" + }); + Assert.deepStrictEqual(createTransportStub.getCall(0).args[0], { + host: "mail.example.com", + port: 25, + secure: true, + }); }); }); }); \ No newline at end of file