Skip to content

Commit

Permalink
Add support for verification codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Mar 8, 2022
1 parent e615a96 commit f68b2a1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/models/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Code {
constructor(data = {}) {
this.value = data.value;
}
}

module.exports = Code;
14 changes: 14 additions & 0 deletions lib/models/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export interface Link {
text?: string;
}

/**
* Data associated with an automatically-extracted verification code.
*/
export interface Code {
/**
* The value.
*/
value?: string;
}

/**
* Data associated with an image found within a message.
*/
Expand All @@ -53,6 +63,10 @@ export interface MessageContent {
* Any hyperlinks found within this content.
*/
links?: Link[];
/**
* Any verification codes found within this content.
*/
codes?: Code[];
/**
* Any images found within this content.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.SpamAnalysisResult = require('./spamAnalysisResult');
exports.MailosaurError = require('./mailosaurError');
exports.MessageAddress = require('./messageAddress');
exports.Link = require('./link');
exports.Code = require('./code');
exports.Image = require('./image');
exports.MessageContent = require('./messageContent');
exports.Attachment = require('./attachment');
Expand Down
2 changes: 2 additions & 0 deletions lib/models/messageContent.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const Link = require('./link');
const Code = require('./code');
const Image = require('./image');

class MessageContent {
constructor(data = {}) {
this.links = (data.links || []).map((i) => (new Link(i)));
this.codes = (data.codes || []).map((i) => (new Code(i)));

if (data.images) {
this.images = (data.images || []).map((i) => (new Image(i)));
Expand Down
10 changes: 10 additions & 0 deletions test/emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const validateHtml = (email) => {
assert.equal(email.html.links[2].href, 'http://invalid/', 'Third link should have href');
assert.equal(email.html.links[2].text, 'invalid', 'Third link should have text');

// Codes
assert.equal(email.html.codes.length, 2, 'Should have verification codes');
assert.equal(email.html.codes[0].value, '123456');
assert.equal(email.html.codes[1].value, 'G3H1Y2');

// Images
assert.match(email.html.images[1].src, /cid:/);
assert.equal(email.html.images[1].alt, 'Inline image 1', 'Second image should have alt text');
Expand All @@ -41,6 +46,11 @@ const validateText = (email) => {
assert.equal(email.text.links[0].text, email.text.links[0].href, 'First text link href & text should match');
assert.equal(email.text.links[1].href, 'https://mailosaur.com/', 'Second link should have href');
assert.equal(email.text.links[1].text, email.text.links[1].href, 'Second text link href & text should match');

// Codes
assert.equal(email.text.codes.length, 2, 'Should have verification codes');
assert.equal(email.text.codes[0].value, '654321');
assert.equal(email.text.codes[1].value, '5H0Y2');
};

const validateHeaders = (email) => {
Expand Down
12 changes: 11 additions & 1 deletion test/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ const smtpTransport = nodemailer.createTransport({
}
});

const getRandomString = (length) => {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.floor(Math.random() *
characters.length));
}
return result;
};

module.exports = {
sendEmails: (mailer, client, server, quantity) => {
const promises = [];
Expand All @@ -35,7 +45,7 @@ module.exports = {
},

sendEmail: (client, server, sendToAddress) => {
const randomString = (Math.random() + 1).toString(36).substring(7);
const randomString = getRandomString(7);
const randomFromAddress = `${randomString}@${verifiedDomain}`;
const randomToAddress = sendToAddress || client.servers.generateEmailAddress(server);

Expand Down
10 changes: 9 additions & 1 deletion test/resources/testEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<img src="cid:ii_1435fadb31d523f6" alt="Inline image 1">
</a>
</div>
<div>
Your verification code is 123456
<br>
</div>
<div>
Your special ID is G3H1Y2
<br>
</div>
<div>
<br>
</div>
Expand All @@ -25,4 +33,4 @@
</div>
</div>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion test/resources/testEmail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text

this is an image:[image: Inline image 1] <https://mailosaur.com/>

this is an invalid link: invalid
Your verification code is 654321

Your special ID is 5H0Y2

this is an invalid link: invalid

0 comments on commit f68b2a1

Please sign in to comment.