Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #33 from danielireson/refactor-handler-validation
Browse files Browse the repository at this point in the history
Refactor handler and validation
  • Loading branch information
danielireson authored Jun 28, 2021
2 parents b2e7115 + 7fbd8f0 commit 1b00c06
Show file tree
Hide file tree
Showing 20 changed files with 532 additions and 739 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"encrypt": "node ./scripts/encrypt.js",
"decrypt": "node ./scripts/decrypt.js",
"lint": "eslint src",
"mocha": "NODE_ENV=test mocha src/**/*.spec.js --exit",
"mocha": "NODE_ENV=test mocha src/** --exit",
"test": "npm run lint && npm run mocha",
"prettier": "prettier src --write"
},
Expand Down
58 changes: 0 additions & 58 deletions src/email/Email.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const { isEmail } = require("validator");
const InternalServerError = require("../error/InternalServerError");

class Email {
constructor(sender, senderArn, subject, recipients, params) {
this.Source = this._source(sender, senderArn);
Expand All @@ -22,61 +19,6 @@ class Email {
};
}

validate() {
const sourceEmail = this.Source.match(/<(.)+>/gi);

if (!sourceEmail || sourceEmail.length === 0) {
return new InternalServerError(
"Source should contain a valid email address"
);
}

if (!this.Message.Subject.Data || this.Message.Subject.Data.length === 0) {
return new InternalServerError("Subject is invalid");
}

if (
!this.Message.Body.Text.Data ||
this.Message.Body.Text.Data.length === 0
) {
return new InternalServerError("Body is invalid");
}

const invalidReplyToEmail = this.ReplyToAddresses.find((e) => !isEmail(e));

if (invalidReplyToEmail) {
return new InternalServerError(
`Invalid reply to recipient: ${invalidReplyToEmail}`
);
}

const invalidToEmail = this.Destination.ToAddresses.find(
(e) => !isEmail(e)
);

if (invalidToEmail) {
return new InternalServerError(`Invalid to recipient: ${invalidToEmail}`);
}

const invalidCcEmail = this.Destination.CcAddresses.find(
(e) => !isEmail(e)
);

if (invalidCcEmail) {
return new InternalServerError(`Invalid cc recipient: ${invalidCcEmail}`);
}

const invalidBccEmail = this.Destination.BccAddresses.find(
(e) => !isEmail(e)
);

if (invalidBccEmail) {
return new InternalServerError(
`Invalid bcc recipient: ${invalidBccEmail}`
);
}
}

_source(sender, senderArn) {
const senderArnAsArray = (senderArn ?? "").split("/");
const email = senderArnAsArray[senderArnAsArray.length - 1];
Expand Down
130 changes: 0 additions & 130 deletions src/email/Email.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,138 +2,8 @@ const describe = require("mocha").describe;
const it = require("mocha").it;
const assert = require("chai").assert;
const Email = require("./Email");
const InternalServerError = require("../error/InternalServerError");

describe("Email", function () {
it("should reject an arn with an invalid sender", function () {
const recipients = { to: "", cc: [], bcc: [], replyTo: [] };
const userParameters = {};

const testSubject = new Email(
"sender",
null,
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(
error.message,
"Source should contain a valid email address"
);
});

it("should reject an invalid subject", function () {
const recipients = { to: "", cc: [], bcc: [], replyTo: [] };
const userParameters = {};

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
null,
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Subject is invalid");
});

it("should reject an invalid body", function () {
const recipients = { to: "", cc: [], bcc: [], replyTo: [] };
const userParameters = {};

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Body is invalid");
});

it("should reject an invalid 'to' email address", function () {
const recipients = { to: "invalid", cc: [], bcc: [], replyTo: [] };
const userParameters = { one: "var1" };

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Invalid to recipient: invalid");
});

it("should reject an invalid 'cc' email address", function () {
const recipients = { to: "", cc: ["invalid"], bcc: [], replyTo: [] };
const userParameters = { one: "var1" };

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Invalid cc recipient: invalid");
});

it("should reject an invalid 'bcc' email address", function () {
const recipients = { to: "", cc: [], bcc: ["invalid"], replyTo: [] };
const userParameters = { one: "var1" };

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Invalid bcc recipient: invalid");
});

it("should reject an invalid 'replyTo' email address", function () {
const recipients = { to: "", cc: [], bcc: [], replyTo: ["invalid"] };
const userParameters = { one: "var1" };

const testSubject = new Email(
"sender",
"arn:aws:ses:eu-west-1:123456789123:identity/johndoe@example.com",
"testing",
recipients,
userParameters
);

const error = testSubject.validate();

assert.instanceOf(error, InternalServerError);
assert.strictEqual(error.message, "Invalid reply to recipient: invalid");
});

it("should build the sender source correctly", function () {
const recipients = { to: "", cc: [], bcc: [], replyTo: [] };
const userParameters = {};
Expand Down
3 changes: 0 additions & 3 deletions src/error/ValidationError.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/error/ValidationError.spec.js

This file was deleted.

Loading

0 comments on commit 1b00c06

Please sign in to comment.