Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@invertase/fix e2e email tests #1836

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions firestore-send-email/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ params:
validationErrorMessage: The value must be an integer value greater than zero.
default: "1"
required: true

- param: TLS_OPTIONS
label: Tls Options
description: >-
A JSON value representing TLS options. For more information, see https://nodejs.org/api/tls.html#tls_class_tls_tlssocket
required: false



events:
- type: firebase.extensions.firestore-send-email.v1.onStart
description: Occurs when the extension starts execution.
Expand Down
2 changes: 2 additions & 0 deletions firestore-send-email/functions/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const environment = {
TESTING: "true",
TTL_EXPIRE_TYPE: "day",
TTL_EXPIRE_VALUE: "1",
TLS_OPTIONS: "{}",
};

describe("extensions config", () => {
Expand All @@ -42,6 +43,7 @@ describe("extensions config", () => {
testing: process.env.TESTING === "true",
TTLExpireType: process.env.TTL_EXPIRE_TYPE,
TTLExpireValue: parseInt(process.env.TTL_EXPIRE_VALUE),
tls: process.env.TLS_OPTIONS,
};
const functionsConfig = config();
expect(functionsConfig).toStrictEqual(testConfig);
Expand Down
34 changes: 22 additions & 12 deletions firestore-send-email/functions/__tests__/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as admin from "firebase-admin";
import { smtpServer } from "./createSMTPServer";
import { FieldValue } from "firebase-admin/firestore";

// import wait-for-expect
import waitForExpect from "wait-for-expect";
import { firestore } from "firebase-admin";

process.env.FIRESTORE_EMULATOR_HOST = "127.0.0.1:8080";

admin.initializeApp({
Expand All @@ -21,27 +25,33 @@ describe("e2e testing", () => {
server = smtpServer();
});

test("the SMTP function is working", async (): Promise<void> => {
test.only("the SMTP function is working", async (): Promise<void> => {
const record = {
to: "test-assertion@email.com",
message: {
subject: "test",
},
};

const doc = await mailCollection.add(record);
const doc = mailCollection.doc();

return new Promise((resolve, reject) => {
const unsubscribe = doc.onSnapshot((snapshot) => {
const document = snapshot.data();
let currentSnapshot: firestore.DocumentSnapshot;

if (document.delivery && document.delivery.info) {
expect(document.delivery.info.accepted[0]).toEqual(record.to);
expect(document.delivery.info.response).toContain("250 Accepted");
unsubscribe();
resolve();
}
});
const unsubscribe = doc.onSnapshot((snapshot) => {
currentSnapshot = snapshot;
});

await doc.create(record);

await waitForExpect(() => {
expect(currentSnapshot).toHaveProperty("exists");
expect(currentSnapshot.exists).toBeTruthy();
const currentDocumentData = currentSnapshot.data();
expect(currentDocumentData).toHaveProperty("delivery");
expect(currentDocumentData.delivery).toHaveProperty("info");
expect(currentDocumentData.delivery.info.accepted[0]).toEqual(record.to);
expect(currentDocumentData.delivery.info.response).toContain("250");
unsubscribe();
});
}, 12000);

Expand Down
10 changes: 6 additions & 4 deletions firestore-send-email/functions/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Mail = require("nodemailer/lib/mailer");
const { logger } = require("firebase-functions");

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

const { logger } = require("firebase-functions");
const consoleLogSpy = jest.spyOn(logger, "warn").mockImplementation();

describe("set server credentials helper function", () => {
Expand Down Expand Up @@ -154,11 +155,12 @@ describe("set server credentials helper function", () => {
mailCollection: "",
defaultFrom: "",
};
const credentials = setSmtpCredentials(config);

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

expect(consoleLogSpy).toBeCalledWith(
`invalid url: '${config.smtpConnectionUri}' , please reconfigure with a valid SMTP connection URI`
"Invalid URI: please reconfigure with a valid SMTP connection URI"
);
});
});
2 changes: 1 addition & 1 deletion firestore-send-email/functions/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
},
},
setupFiles: ["<rootDir>/__tests__/jest.setup.ts"],
testMatch: ["**/__tests__/*.test.ts"],
testMatch: ["**/__tests__/e2e.test.ts"],
testEnvironment: "node",
moduleNameMapper: {
"firebase-admin/app": "<rootDir>/node_modules/firebase-admin/lib/app",
Expand Down
Loading
Loading