Skip to content

Commit

Permalink
fix: add support for SendGrid categories and streamline SendGrid dete…
Browse files Browse the repository at this point in the history
…ction (#2053)
  • Loading branch information
Gustolandia committed Dec 23, 2024
1 parent ed34bd6 commit fa49236
Show file tree
Hide file tree
Showing 9 changed files with 1,434 additions and 1,516 deletions.
776 changes: 603 additions & 173 deletions firestore-bigquery-export/functions/package-lock.json

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 31 additions & 69 deletions firestore-send-email/functions/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Mail = require("nodemailer/lib/mailer");
const { logger } = require("firebase-functions");

import { setSmtpCredentials } from "../src/helpers";
import { setSmtpCredentials, isSendGrid } from "../src/helpers";
import { Config } from "../src/types";

const consoleLogSpy = jest.spyOn(logger, "warn").mockImplementation();
Expand All @@ -11,7 +11,7 @@ const regex = new RegExp(
"^(smtp[s]*://(.*?(:[^:@]*)?@)?[^:@]+:[0-9]+(\\?[^ ]*)?)$"
);

describe("set server credentials helper function", () => {
describe("setSmtpCredentials function", () => {
test("return smtpServerDomain credentials with new password", () => {
const config: Config = {
smtpConnectionUri:
Expand All @@ -32,26 +32,7 @@ describe("set server credentials helper function", () => {
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpServerDomain credentials with new password (old deleted)", () => {
const config: Config = {
smtpConnectionUri: "smtps://[email protected]@smtp.gmail.com:465",
smtpPassword: "sec#:@ret-password",
location: "",
mailCollection: "",
defaultFrom: "",
};
const credentials = setSmtpCredentials(config);
expect(credentials).toBeInstanceOf(Mail);
expect(credentials.options.port).toBe(465);
expect(credentials.options.host).toBe("smtp.gmail.com");
expect(credentials.options.auth.pass).toBe(config.smtpPassword);
expect(credentials.options.secure).toBe(true);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpConnectionUri credentials with old password", () => {
test("return smtpServerDomain credentials with old password", () => {
const config: Config = {
smtpConnectionUri:
"smtps://[email protected]:[email protected]:465",
Expand Down Expand Up @@ -130,7 +111,7 @@ describe("set server credentials helper function", () => {
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return valid smtpConnectionUri credentials with special chars in password config", () => {
test("return valid smtpConnectionUri credentials with valid special chars in password", () => {
const config: Config = {
smtpConnectionUri:
"smtp://[email protected]@smtp.gmail.com:465?pool=true&service=gmail",
Expand All @@ -153,30 +134,7 @@ describe("set server credentials helper function", () => {
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return valid smtpConnectionUri credentials with valid special chars in connectionUri password", () => {
const config: Config = {
smtpConnectionUri:
"smtp://[email protected]:4,hdhuNTbv9zMrP4&7&7%*[email protected]:465?pool=true&service=gmail",
location: "",
mailCollection: "",
defaultFrom: "",
};
const credentials = setSmtpCredentials(config);

expect(credentials).toBeInstanceOf(Mail);
expect(credentials.options.port).toBe(465);
expect(credentials.options.host).toBe("smtp.gmail.com");
expect(credentials.options.auth.user).toBe("[email protected]");
expect(credentials.options.auth.pass).toBe("4,hdhuNTbv9zMrP4&7&7%*3");
expect(credentials.options.secure).toBe(false);
expect(credentials.options.pool).toBe(true);
expect(credentials.options.service).toBe("gmail");

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return invalid smtpConnectionUri credentials with invalid special chars in connectionUri password", () => {
test("throw error for invalid smtpConnectionUri", () => {
const config: Config = {
smtpConnectionUri:
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*[email protected]:465?pool=true&service=gmail",
Expand All @@ -185,48 +143,52 @@ describe("set server credentials helper function", () => {
defaultFrom: "",
};

// Expect an error to be thrown
expect(() => setSmtpCredentials(config)).toThrow(Error);

expect(consoleLogSpy).toBeCalledWith(
"Invalid URI: please reconfigure with a valid SMTP connection URI"
);
});
test("return valid smtpConnectionUri credentials with correct seprator", () => {
});

describe("isSendGrid function", () => {
test("return true for SendGrid SMTP URI", () => {
const config: Config = {
smtpConnectionUri:
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*[email protected]:465?pool=true&service=gmail",
smtpConnectionUri: "smtps://[email protected]:465",
location: "",
mailCollection: "",
defaultFrom: "",
};

expect(regex.test(config.smtpConnectionUri)).toBe(true);
expect(isSendGrid(config)).toBe(true);
});

test("return invalid smtpConnectionUri credentials with invalid seprator", () => {
test("return false for non-SendGrid SMTP URI", () => {
const config: Config = {
smtpConnectionUri:
"smtp://[email protected]:4,h?dhuNTbv9zMrP4&7&7%*3:smtp.gmail.com:465?pool=true&service=gmail",
"smtps://[email protected]:secret-password@smtp.gmail.com:465",
location: "",
mailCollection: "",
defaultFrom: "",
};

expect(regex.test(config.smtpConnectionUri)).toBe(false);
expect(isSendGrid(config)).toBe(false);
});
});

/* Test removed due to the new SendGrid transporter logic - see setSendGridTransport()
test("return a SendGrid transporter if the host is smtp.sendgrid.net", () => {
const config: Config = {
smtpConnectionUri: "smtps://[email protected]:465",
location: "",
mailCollection: "",
defaultFrom: "",
};
const credentials = setSmtpCredentials(config);
expect(credentials.transporter.name === "nodemailer-sendgrid").toBe(true);
});
*/
test("correctly detects SendGrid SMTP URI", () => {
const config: Config = {
smtpConnectionUri: "smtps://[email protected]:465",
location: "",
mailCollection: "",
defaultFrom: "",
};
expect(isSendGrid(config)).toBe(true);

const invalidConfig: Config = {
smtpConnectionUri: "smtps://[email protected]:465",
location: "",
mailCollection: "",
defaultFrom: "",
};
expect(isSendGrid(invalidConfig)).toBe(false);
});
Loading

0 comments on commit fa49236

Please sign in to comment.