2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const loopback = require('../');
|
|
|
|
let MyEmail;
|
|
|
|
const assert = require('assert');
|
|
|
|
const MailConnector = require('../lib/connectors/mail');
|
2014-07-24 05:09:24 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Email connector', function() {
|
|
|
|
it('should set up SMTP', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const connector = new MailConnector({transports: [
|
2016-11-15 21:46:23 +00:00
|
|
|
{type: 'smtp', service: 'gmail'},
|
|
|
|
]});
|
2014-07-24 05:09:24 +00:00
|
|
|
assert(connector.transportForName('smtp'));
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should set up DIRECT', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const connector = new MailConnector({transports: [
|
2016-11-15 21:46:23 +00:00
|
|
|
{type: 'direct', name: 'localhost'},
|
|
|
|
]});
|
2014-07-24 05:09:24 +00:00
|
|
|
assert(connector.transportForName('direct'));
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should set up STUB', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const connector = new MailConnector({transports: [
|
2016-11-15 21:46:23 +00:00
|
|
|
{type: 'stub', service: 'gmail'},
|
|
|
|
]});
|
2014-07-24 05:09:24 +00:00
|
|
|
assert(connector.transportForName('stub'));
|
|
|
|
});
|
2014-08-14 19:44:36 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
it('should set up a single transport for SMTP', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const connector = new MailConnector({transport:
|
2016-11-15 21:46:23 +00:00
|
|
|
{type: 'smtp', service: 'gmail'},
|
2014-08-14 19:44:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert(connector.transportForName('smtp'));
|
|
|
|
});
|
2016-07-05 15:47:30 +00:00
|
|
|
|
|
|
|
it('should set up a aliased transport for SMTP', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const connector = new MailConnector({transport:
|
2016-11-15 21:46:23 +00:00
|
|
|
{type: 'smtp', service: 'ses-us-east-1', alias: 'ses-smtp'},
|
2016-07-05 15:47:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert(connector.transportForName('ses-smtp'));
|
|
|
|
});
|
2014-07-24 05:09:24 +00:00
|
|
|
});
|
2013-10-12 02:06:16 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Email and SMTP', function() {
|
2014-01-23 22:25:54 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
MyEmail = loopback.Email.extend('my-email');
|
2019-10-07 09:45:34 +00:00
|
|
|
const ds = loopback.createDataSource('email', {
|
2016-01-25 09:36:51 +00:00
|
|
|
connector: loopback.Mail,
|
2016-11-15 21:46:23 +00:00
|
|
|
transports: [{type: 'STUB'}],
|
2016-01-25 09:36:51 +00:00
|
|
|
});
|
|
|
|
MyEmail.attachTo(ds);
|
2014-01-23 22:25:54 +00:00
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
|
|
|
|
it('should have a send method', function() {
|
2013-10-12 02:06:16 +00:00
|
|
|
assert(typeof MyEmail.send === 'function');
|
|
|
|
assert(typeof MyEmail.prototype.send === 'function');
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('MyEmail', function() {
|
|
|
|
it('MyEmail.send(options, callback)', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const options = {
|
2013-10-12 02:06:16 +00:00
|
|
|
to: 'to@to.com',
|
|
|
|
from: 'from@from.com',
|
|
|
|
subject: 'subject',
|
|
|
|
text: 'text',
|
2016-04-01 09:14:26 +00:00
|
|
|
html: '<h1>html</h1>',
|
2013-10-12 02:06:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MyEmail.send(options, function(err, mail) {
|
2014-07-15 23:46:43 +00:00
|
|
|
assert(!err);
|
|
|
|
assert(mail.response);
|
2013-10-12 02:06:16 +00:00
|
|
|
assert(mail.envelope);
|
|
|
|
assert(mail.messageId);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-10-12 02:06:16 +00:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('myEmail.send(callback)', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const message = new MyEmail({
|
2013-10-12 02:06:16 +00:00
|
|
|
to: 'to@to.com',
|
|
|
|
from: 'from@from.com',
|
|
|
|
subject: 'subject',
|
|
|
|
text: 'text',
|
2016-04-01 09:14:26 +00:00
|
|
|
html: '<h1>html</h1>',
|
2013-10-12 02:06:16 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
message.send(function(err, mail) {
|
2014-07-15 23:46:43 +00:00
|
|
|
assert(mail.response);
|
2013-10-12 02:06:16 +00:00
|
|
|
assert(mail.envelope);
|
|
|
|
assert(mail.messageId);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-10-12 02:06:16 +00:00
|
|
|
done(err);
|
|
|
|
});
|
2014-08-14 19:44:36 +00:00
|
|
|
});
|
|
|
|
});
|
2013-10-12 02:06:16 +00:00
|
|
|
});
|