Skip to content

Commit

Permalink
Merge pull request #39 from fterh/no-throw-missing-alias
Browse files Browse the repository at this point in the history
Don't throw an error when receiving emails on non-existent aliases
  • Loading branch information
fterh authored Mar 23, 2020
2 parents 420dad4 + 581593c commit a7fe3f1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/forwardInbound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export default async (

const alias = await Alias.getAlias(aliasValue);
if (alias === undefined) {
throw new Error(`Alias=${aliasValue} not found in database!`);
console.log("Skipping forwarding received email, as alias does not exist");
return;
}
const mailOptions = await generateInboundMailOptions(alias, parsedMail);
await sendEmail(mailOptions);
Expand Down
5 changes: 5 additions & 0 deletions lib/models/__mocks__/Alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AliasData } from "../Alias";
export const now = new Date();
export const didReceiveEmailSpy = jest.fn();
export const didSendEmailSpy = jest.fn();
export const INVALID_ALIAS = "invalidalias";

export default class MockAlias implements AliasData {
value: string;
Expand Down Expand Up @@ -34,6 +35,10 @@ export default class MockAlias implements AliasData {
});

static getAlias = jest.fn(async (aliasValue: string) => {
if (aliasValue === INVALID_ALIAS) {
return;
}

return new MockAlias({
value: aliasValue,
description: "test description",
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/commands/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ it("should call the right command handler", async () => {

it("should throw if the command is unrecognized", async () => {
const res = handleCommand("edit", testEmailAuthenticated);
expect(res).rejects.toEqual(new Error(`"edit" is not a command`));
await expect(res).rejects.toEqual(new Error(`"edit" is not a command`));
});

it("should throw if the command is not authenticated", async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/commands/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ it("should throw if no alias has been provided", async () => {

const res = info(testEmail);

expect(res).rejects.toEqual(
await expect(res).rejects.toEqual(
new Error("Alias value (email subject) is undefined")
);
});
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/forwardInbound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { email as myEmail, operationalDomain } from "../../lib/env";
import forwardInbound, { generateFromHeader } from "../../lib/forwardInbound";
// @ts-ignore: We're using Jest's ES6 class mocks (https://jestjs.io/docs/en/es6-class-mocks)
import { didReceiveEmailSpy } from "../../lib/models/Alias";
import { INVALID_ALIAS } from "../../lib/models/__mocks__/Alias";
import sendEmail from "../../lib/sendEmail";
import senderAddressEncodeDecode from "../../lib/senderAddressEncodeDecode";
import assertEquivalentAttachments from "../utils/assertEquivalentAttachments";
Expand Down Expand Up @@ -154,3 +155,10 @@ it("should default to an empty string if there is no email subject", async () =>
await forwardInbound(testAlias, testEmail);
expect(_sendEmail.mock.calls[0][0].subject).toBe("");
});

it("should not throw if the alias does not exist", async () => {
const testEmail = await generateTestEmail(testEmailData1);
const res = forwardInbound(INVALID_ALIAS, testEmail);

await expect(res).resolves.toBeUndefined();
});

0 comments on commit a7fe3f1

Please sign in to comment.