1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-07-05 20:10:37 +00:00
emailjs/test/authplain.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

describe('authorize plain', function() {
2018-05-27 04:25:08 +00:00
const { simpleParser: parser } = require('mailparser');
const { SMTPServer: smtpServer } = require('smtp-server');
const { expect } = require('chai');
const email = require('../email');
const port = 2526;
2018-05-27 04:25:08 +00:00
let server = null;
let smtp = null;
2018-05-27 04:25:08 +00:00
const send = function(message, verify, done) {
smtp.onData = function(stream, session, callback) {
parser(stream)
.then(verify)
.then(done)
.catch(done);
stream.on('end', callback);
};
2018-05-27 04:25:08 +00:00
server.send(message, function(err) {
2018-06-04 16:40:23 +00:00
if (err) {
throw err;
}
2018-05-27 04:25:08 +00:00
});
};
2018-05-27 04:25:08 +00:00
before(function(done) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // prevent CERT_HAS_EXPIRED errors
2018-05-27 04:25:08 +00:00
smtp = new smtpServer({ secure: true, authMethods: ['LOGIN'] });
smtp.listen(port, function() {
smtp.onAuth = function(auth, session, callback) {
if (auth.username == 'pooh' && auth.password == 'honey') {
callback(null, { user: 'pooh' });
} else {
return callback(new Error('invalid user / pass'));
}
};
2018-05-27 04:25:08 +00:00
server = email.server.connect({
port: port,
user: 'pooh',
password: 'honey',
ssl: true,
});
2018-05-27 04:25:08 +00:00
done();
});
});
2018-05-27 04:25:08 +00:00
after(function(done) {
smtp.close(done);
});
2018-05-27 04:25:08 +00:00
it('login', function(done) {
const message = {
2018-05-27 04:25:08 +00:00
subject: 'this is a test TEXT message from emailjs',
from: 'piglet@gmail.com',
to: 'pooh@gmail.com',
text: "It is hard to be brave when you're only a Very Small Animal.",
};
2018-05-27 04:25:08 +00:00
const created = email.message.create(message);
const callback = function(mail) {
expect(mail.text).to.equal(message.text + '\n\n\n');
expect(mail.subject).to.equal(message.subject);
expect(mail.from.text).to.equal(message.from);
expect(mail.to.text).to.equal(message.to);
};
2014-07-06 08:15:40 +00:00
2018-05-27 04:25:08 +00:00
send(created, callback, done);
});
});