loopback/test/email.test.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

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
'use strict';
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() {
const connector = new MailConnector({transports: [
{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() {
const connector = new MailConnector({transports: [
{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() {
const connector = new MailConnector({transports: [
{type: 'stub', service: 'gmail'},
]});
2014-07-24 05:09:24 +00:00
assert(connector.transportForName('stub'));
});
it('should set up a single transport for SMTP', function() {
const connector = new MailConnector({transport:
{type: 'smtp', service: 'gmail'},
});
assert(connector.transportForName('smtp'));
});
it('should set up a aliased transport for SMTP', function() {
const connector = new MailConnector({transport:
{type: 'smtp', service: 'ses-us-east-1', alias: 'ses-smtp'},
});
assert(connector.transportForName('ses-smtp'));
});
2014-07-24 05:09:24 +00:00
});
2014-11-21 02:35:36 +00:00
describe('Email and SMTP', function() {
beforeEach(function() {
MyEmail = loopback.Email.extend('my-email');
const ds = loopback.createDataSource('email', {
connector: loopback.Mail,
transports: [{type: 'STUB'}],
});
MyEmail.attachTo(ds);
});
2014-11-21 02:35:36 +00:00
it('should have a send method', function() {
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) {
const options = {
to: 'to@to.com',
from: 'from@from.com',
subject: 'subject',
text: 'text',
html: '<h1>html</h1>',
};
MyEmail.send(options, function(err, mail) {
2014-07-15 23:46:43 +00:00
assert(!err);
assert(mail.response);
assert(mail.envelope);
assert(mail.messageId);
done(err);
});
});
2014-11-21 02:35:36 +00:00
it('myEmail.send(callback)', function(done) {
const message = new MyEmail({
to: 'to@to.com',
from: 'from@from.com',
subject: 'subject',
text: 'text',
html: '<h1>html</h1>',
});
2014-11-21 02:35:36 +00:00
message.send(function(err, mail) {
2014-07-15 23:46:43 +00:00
assert(mail.response);
assert(mail.envelope);
assert(mail.messageId);
done(err);
});
});
});
});