Skip to content

Commit

Permalink
Merge pull request #25 from arithmetric/feature/case_insensitive_forw…
Browse files Browse the repository at this point in the history
…ard_mapping

Recipient string comparison should be case insensitive
  • Loading branch information
arithmetric committed May 14, 2016
2 parents 3303daf + 74309ea commit ec7c967
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ console.log("AWS Lambda SES Forwarder // @arithmetric // Version 2.3.0");
// forward and the value is an array of email addresses to which to send the
// message. To match all email addresses on a domain, use a key without the
// name part of an email address before the "at" symbol (i.e. `@example.com`).
// The key must be lowercase.
var defaultConfig = {
fromEmail: "[email protected]",
emailBucket: "s3-bucket-name",
Expand Down Expand Up @@ -67,15 +68,16 @@ exports.transformRecipients = function(data, next) {
var newRecipients = [];
data.originalRecipients = data.recipients;
data.recipients.forEach(function(origEmail) {
if (data.config.forwardMapping.hasOwnProperty(origEmail)) {
var origEmailKey = origEmail.toLowerCase();
if (data.config.forwardMapping.hasOwnProperty(origEmailKey)) {
newRecipients = newRecipients.concat(
data.config.forwardMapping[origEmail]);
data.config.forwardMapping[origEmailKey]);
data.originalRecipient = origEmail;
} else {
var origEmailDomain;
var pos = origEmail.lastIndexOf("@");
var pos = origEmailKey.lastIndexOf("@");
if (pos !== -1) {
origEmailDomain = origEmail.slice(pos);
origEmailDomain = origEmailKey.slice(pos);
}
if (origEmailDomain &&
data.config.forwardMapping.hasOwnProperty(origEmailDomain)) {
Expand Down
48 changes: 44 additions & 4 deletions test/transformRecipients.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,49 @@ describe('index.js', function() {
"[email protected]",
"[email protected]"
]
},
log: console.log
}
}
},
context: {
succeed: function() {
assert.ok(false,
'context.succeed() was called, but should not be');
done();
}
},
log: console.log
};
index.transformRecipients(data, function(err, data) {
assert.ok(!err, "transformRecipients returned successfully");
assert.equal(data.recipients[0],
"[email protected]",
"parseEvent made 1/2 substitutions");
assert.equal(data.recipients[1],
"[email protected]",
"parseEvent made 2/2 substitutions");
done();
});
});

it('should transform recipients in a case insensitive way',
function(done) {
var data = {
recipients: ["[email protected]"],
config: {
forwardMapping: {
"[email protected]": [
"[email protected]",
"[email protected]"
]
}
},
context: {
succeed: function() {
assert.ok(false,
'context.succeed() was called, but should not be');
done();
}
},
log: console.log
};
index.transformRecipients(data, function(err, data) {
assert.ok(!err, "transformRecipients returned successfully");
Expand All @@ -36,7 +76,7 @@ describe('index.js', function() {
it('should transform recipients according a domain wildcard mapping',
function(done) {
var data = {
recipients: ["info@example.com"],
recipients: ["info@EXAMPLE.com"],
config: {
forwardMapping: {
"@example.com": [
Expand Down

0 comments on commit ec7c967

Please sign in to comment.