Skip to content

Release firestore-send-email #2434

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

Merged
merged 2 commits into from
May 30, 2025
Merged
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
4 changes: 4 additions & 0 deletions firestore-send-email/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.2.3

fix: remove strict validation of email "from" field

## Version 0.2.2

fix: fix validation of payloads
Expand Down
2 changes: 1 addition & 1 deletion firestore-send-email/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

name: firestore-send-email
version: 0.2.2
version: 0.2.3
specVersion: v1beta

displayName: Trigger Email from Firestore
Expand Down
43 changes: 41 additions & 2 deletions firestore-send-email/functions/__tests__/e2e/sendgrid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ const TEST_COLLECTIONS = ["emailCollection", "emailTemplates"] as const;
beforeAll(() => {
// Initialize with emulator settings
admin.initializeApp({
projectId: "dev-extensions-testing",
databaseURL: "http://localhost:8080?ns=dev-extensions-testing",
projectId: "demo-test",
});

// Point Firestore to the emulator
Expand Down Expand Up @@ -141,5 +140,45 @@ const TEST_COLLECTIONS = ["emailCollection", "emailTemplates"] as const;
expect(updatedData?.delivery.info?.rejected).toEqual([]);
expect(updatedData?.delivery.info?.pending).toEqual([]);
});

test("should process an email with friendly name in from field", async () => {
const db = admin.firestore();

const testData = {
message: {
attachments: [],
html: "<p>Test email with friendly name</p>",
text: "Test email with friendly name",
subject: "Test Friendly Name",
from: "Friendly Firebaser [email protected]",
},
to: TEST_EMAIL,
};

// Write the document to the emulator
const docRef = db.collection("emailCollection").doc("test-friendly-name");
await docRef.set(testData);

// Wait a bit for the function to process
await new Promise((resolve) => setTimeout(resolve, 2000));

// Verify the document was updated
const doc = await docRef.get();
const updatedData = doc.data();

// Assert the delivery state was updated to SUCCESS
console.log("updatedData with friendly name", updatedData);
expect(updatedData?.delivery.state).toBe("SUCCESS");
expect(updatedData?.delivery.attempts).toBe(1);
expect(updatedData?.delivery.endTime).toBeDefined();
expect(updatedData?.delivery.error).toBeNull();

// Verify SendGrid specific info
expect(updatedData?.delivery.info).toBeDefined();
expect(updatedData?.delivery.info?.messageId).toBeDefined();
expect(updatedData?.delivery.info?.accepted).toContain(TEST_EMAIL);
expect(updatedData?.delivery.info?.rejected).toEqual([]);
expect(updatedData?.delivery.info?.pending).toEqual([]);
});
}
);
12 changes: 12 additions & 0 deletions firestore-send-email/functions/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ describe("validatePayload", () => {
expect(() => validatePayload(validPayload)).not.toThrow();
});

it("should validate payload with friendly name in from field", () => {
const validPayload = {
to: "[email protected]",
from: "Friendly Firebaser [email protected]",
message: {
subject: "Test Subject",
text: "Test message",
},
};
expect(() => validatePayload(validPayload)).not.toThrow();
});

it("should validate a template payload without html/text fields", () => {
const validPayload = {
to: "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion firestore-send-email/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:local": "concurrently --kill-others \"npm run local:emulator\" \"npm run testIfEmulatorRunning\"",
"test:watch": "concurrently \"npm run local:emulator\" \"jest --watch\"",
"test:coverage": "concurrently --kill-others \"npm run local:emulator\" \"wait-on tcp:4001 && jest --coverage\"",
"test:e2e:sendgrid": "E2E_SENDGRID=true jest __tests__/e2e/sendgrid.test.ts",
"test:e2e:sendgrid": "cd ../../_emulator && firebase emulators:exec --project=demo-test \" cd ../firestore-send-email/functions && E2E_SENDGRID=true jest __tests__/e2e/sendgrid.test.ts\"",
"generate-readme": "firebase ext:info .. --markdown > ../README.md"
},
"keywords": [],
Expand Down
9 changes: 3 additions & 6 deletions firestore-send-email/functions/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,7 @@ const templateSchema = z.object({
/**
* Schema for email recipients (single email or array of emails).
*/
const recipientSchema = z.union([
z.string().email(),
z.array(z.string().email()),
]);
const recipientSchema = z.union([z.string(), z.array(z.string())]);

/**
* Schema for arrays of UIDs.
Expand All @@ -137,8 +134,8 @@ const payloadSchema = z
toUids: uidArraySchema.optional(),
ccUids: uidArraySchema.optional(),
bccUids: uidArraySchema.optional(),
from: z.string().email().optional(),
replyTo: z.string().email().optional(),
from: z.string().optional(),
replyTo: z.string().optional(),
message: baseMessageSchema.optional(),
template: templateSchema.optional(),
sendGrid: sendGridSchema.optional(),
Expand Down
Loading