-
Notifications
You must be signed in to change notification settings - Fork 0
/
maildev_helper.js
89 lines (81 loc) · 2.18 KB
/
maildev_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const MailDev = require('maildev');
const chai = require('chai');
const assert = chai.assert;
/**
* CodeceptJS Helper for local maildev instance. Adds actions to check virtual email box.
*/
class MaildevHelper extends Helper {
/**
* Creates helper object
* @param config Configuration from CodeceptJS configuration file
*/
constructor(config) {
super(config);
this.port = config.port || 1025;
this.emails = [];
this.read = 0;
this.maildev = null;
this.addresses = [];
}
_after() {
if (this.maildev) {
this.maildev.close((err) => {
if (err) {
console.error('Can\'t stop maildev: ' + err);
} else {
console.debug('Maildev stopped');
}
});
this.maildev = null;
}
}
_startMaildev() {
this.maildev = new MailDev({
smtp: this.port,
ip:"127.0.0.1",
disableWeb: true
});
this.maildev.listen((err) => {
if (err) console.error("Maildev cannot listen on " + this.port + ": " + err);
else console.debug("Maildev is listening on " + this.port);
});
this.maildev.on('new', (email) => {
console.debug('Recipients: ' + email.to.map(t => t.address).join(', '));
if (this._checkAddress(email)) {
console.log('Got new email: ' + email.subject);
this.emails.push(email);
}
});
}
_checkAddress(email) {
for (var i = 0; i < this.addresses.length; i++) {
for (var j = 0; j < email.to.length; j++) {
if (this.addresses[i] === email.to[j].address) {
return true;
}
}
}
return false;
}
/**
* Creates virtual mailbox with given address.
* @param address Email address
*/
haveMailbox(address) {
if (!this.maildev) {
this._startMaildev();
}
this.addresses.push(address);
}
/**
* Grabs next unread email or fails if there are no unread emails.
* @return Promise with next unread email object (see maildev API).
*/
grabNextUnreadMail() {
return new Promise((resolve) => {
assert.isBelow(this.read, this.emails.length, 'There are no unread emails');
resolve(this.emails[this.read++]);
});
}
}
module.exports = MaildevHelper;