Skip to content

Commit 7fa0f90

Browse files
committed
test(firestore-send-email): add edge case tests
1 parent 14390f9 commit 7fa0f90

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

firestore-send-email/functions/__tests__/prepare-payload.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ describe("preparePayload Template Merging", () => {
385385
});
386386

387387
describe("attachment validation", () => {
388-
it("should handle non-array attachments", async () => {
388+
it("should throw clear error for string attachments", async () => {
389389
const payload = {
390390
391391
message: {
@@ -395,7 +395,30 @@ describe("preparePayload Template Merging", () => {
395395
},
396396
};
397397

398-
await expect(preparePayload(payload)).rejects.toThrow();
398+
await expect(preparePayload(payload)).rejects.toThrow(
399+
"Invalid message configuration: Field 'message.attachments' must be an array"
400+
);
401+
});
402+
403+
it("should throw clear error for invalid attachment httpHeaders", async () => {
404+
const payload = {
405+
406+
message: {
407+
subject: "Test Subject",
408+
text: "Test text",
409+
attachments: [
410+
{
411+
filename: "test.txt",
412+
href: "https://example.com",
413+
httpHeaders: "invalid",
414+
},
415+
],
416+
},
417+
};
418+
419+
await expect(preparePayload(payload)).rejects.toThrow(
420+
"Invalid message configuration: Field 'message.attachments.0.httpHeaders' must be a map"
421+
);
399422
});
400423

401424
it("should handle null attachments as no attachments", async () => {

firestore-send-email/functions/__tests__/validation.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,95 @@ describe("validatePayload", () => {
612612
};
613613
expect(() => validatePayload(payload)).not.toThrow();
614614
});
615+
616+
it("should throw error for string attachments", () => {
617+
const payload = {
618+
619+
message: {
620+
subject: "Test Subject",
621+
text: "Test message",
622+
attachments: "invalid-string",
623+
},
624+
};
625+
expect(() => validatePayload(payload)).toThrow(ValidationError);
626+
expect(() => validatePayload(payload)).toThrow(
627+
"Invalid message configuration: Field 'message.attachments' must be an array"
628+
);
629+
});
630+
631+
it("should throw error for number attachments", () => {
632+
const payload = {
633+
634+
message: {
635+
subject: "Test Subject",
636+
text: "Test message",
637+
attachments: 123,
638+
},
639+
};
640+
expect(() => validatePayload(payload)).toThrow(ValidationError);
641+
expect(() => validatePayload(payload)).toThrow(
642+
"Invalid message configuration: Field 'message.attachments' must be an array"
643+
);
644+
});
645+
});
646+
});
647+
648+
describe("error messages", () => {
649+
it("should provide clear error for empty template name", () => {
650+
const payload = {
651+
652+
template: {
653+
name: "",
654+
},
655+
};
656+
expect(() => validatePayload(payload)).toThrow(ValidationError);
657+
expect(() => validatePayload(payload)).toThrow(
658+
"Invalid template configuration: Field 'template.name' cannot be empty"
659+
);
660+
});
661+
662+
it("should provide clear error for empty UID in array", () => {
663+
const payload = {
664+
toUids: ["valid-uid", ""],
665+
message: {
666+
subject: "Test",
667+
text: "Test",
668+
},
669+
};
670+
expect(() => validatePayload(payload)).toThrow(ValidationError);
671+
expect(() => validatePayload(payload)).toThrow(
672+
"Invalid email configuration: Field 'toUids.1' cannot be empty"
673+
);
674+
});
675+
676+
it("should provide clear error for cc as number", () => {
677+
const payload = {
678+
679+
cc: 123,
680+
message: {
681+
subject: "Test",
682+
text: "Test",
683+
},
684+
};
685+
expect(() => validatePayload(payload)).toThrow(ValidationError);
686+
expect(() => validatePayload(payload)).toThrow(
687+
"Invalid email configuration: Field 'cc' must be either a string or an array of strings"
688+
);
689+
});
690+
691+
it("should provide clear error for bcc as boolean", () => {
692+
const payload = {
693+
694+
bcc: true,
695+
message: {
696+
subject: "Test",
697+
text: "Test",
698+
},
699+
};
700+
expect(() => validatePayload(payload)).toThrow(ValidationError);
701+
expect(() => validatePayload(payload)).toThrow(
702+
"Invalid email configuration: Field 'bcc' must be either a string or an array of strings"
703+
);
615704
});
616705
});
617706
});

0 commit comments

Comments
 (0)