Ghost/core/test/unit/mail_spec.js

248 lines
8.4 KiB
JavaScript
Raw Normal View History

Improve bootstrap flow of a Ghost application addresses #1789, #1364 - Moves ./core/server/loader -> ./core/bootstrap. The bootstrap file is only accessed once during startup, and it’s sole job is to ensure a config.js file exists (creating one if it doesn’t) and then validates the contents of the config file. Since this is directly related to the initializing the application is is appropriate to have it in the ./core folder, named bootstrap as that is what it does. This also improves the dependency graph, as now the bootstrap file require’s the ./core/server/config module and is responsible for passing in the validated config file. Whereas before we had ./core/server/config require’ing ./core/server/loader and running its init code and then passing that value back to itself, the flow is now more straight forward of ./core/bootstrap handling initialization and then instatiation of config module - Merges ./core/server/config/paths into ./core/server/config This flow was always confusing me to that some config options were on the config object, and some were on the paths object. This change now incorporates all of the variables previously defined in config/paths directly into the config module, and in extension, the config.js file. This means that you now have the option of deciding at startup where the content directory for ghost should reside. - broke out loader tests in config_spec to bootstrap_spec - updated all relevant files to now use config().paths - moved urlFor and urlForPost function into ./server/config/url.js
2014-01-05 10:40:53 +04:00
var should = require('should'),
Promise = require('bluebird'),
// Stuff we are testing
GhostMail = require('../../server/mail'),
configUtils = require('../utils/configUtils'),
i18n = require('../../server/i18n'),
mailer,
// Mock SMTP config
SMTP = {
transport: 'SMTP',
options: {
service: 'Gmail',
auth: {
user: 'nil',
pass: '123'
}
2013-08-21 00:19:47 +04:00
}
},
// test data
mailDataNoDomain = {
to: 'joe@doesntexistexample091283zalgo.com',
subject: 'testemail',
html: '<p>This</p>'
},
mailDataNoServer = {
to: 'joe@example.com',
subject: 'testemail',
html: '<p>This</p>'
},
mailDataIncomplete = {
subject: 'testemail',
html: '<p>This</p>'
};
i18n.init();
2013-08-21 00:19:47 +04:00
2014-06-05 01:26:03 +04:00
describe('Mail', function () {
2013-08-21 00:19:47 +04:00
afterEach(function () {
mailer = null;
configUtils.restore();
2013-08-21 00:19:47 +04:00
});
it('should attach mail provider to ghost instance', function () {
mailer = new GhostMail();
should.exist(mailer);
mailer.should.have.property('send').and.be.a.Function();
2013-08-21 00:19:47 +04:00
});
it('should setup SMTP transport on initialization', function () {
configUtils.set({mail: SMTP});
mailer = new GhostMail();
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('SMTP');
mailer.transport.sendMail.should.be.a.Function();
2013-08-21 00:19:47 +04:00
});
it('should fallback to direct if config is empty', function () {
configUtils.set({mail: {}});
mailer = new GhostMail();
mailer.should.have.property('transport');
mailer.transport.transportType.should.eql('DIRECT');
2013-08-21 00:19:47 +04:00
});
it('sends valid message successfully ', function (done) {
configUtils.set({mail: {transport: 'stub'}});
mailer = new GhostMail();
mailer.transport.transportType.should.eql('STUB');
mailer.send(mailDataNoServer).then(function (response) {
should.exist(response.message);
should.exist(response.envelope);
response.envelope.to.should.containEql('joe@example.com');
done();
}).catch(done);
});
it('handles failure', function (done) {
configUtils.set({mail: {transport: 'stub', options: {error: 'Stub made a boo boo :('}}});
mailer = new GhostMail();
mailer.transport.transportType.should.eql('STUB');
mailer.send(mailDataNoServer).then(function () {
done(new Error('Stub did not error'));
}).catch(function (error) {
error.message.should.eql('Error: Stub made a boo boo :(');
done();
}).catch(done);
});
2013-08-21 00:19:47 +04:00
it('should fail to send messages when given insufficient data', function (done) {
mailer = new GhostMail();
Promise.all([
mailer.send().reflect(),
mailer.send({}).reflect(),
mailer.send({subject: '123'}).reflect(),
mailer.send({subject: '', html: '123'}).reflect()
2013-08-21 00:19:47 +04:00
]).then(function (descriptors) {
descriptors.forEach(function (d) {
d.isFulfilled().should.be.false();
d.reason().should.be.an.instanceOf(Error);
2015-10-16 13:05:24 +03:00
d.reason().message.should.eql('Error: Incomplete message data.');
2013-08-21 00:19:47 +04:00
});
done();
}).catch(done);
2013-08-21 00:19:47 +04:00
});
describe('Direct', function () {
beforeEach(function () {
configUtils.set({mail: {}});
mailer = new GhostMail();
});
afterEach(function () {
mailer = null;
});
it('return correct failure message for domain doesn\'t exist', function (done) {
mailer.transport.transportType.should.eql('DIRECT');
mailer.send(mailDataNoDomain).then(function () {
done(new Error('Error message not shown.'));
}, function (error) {
error.message.should.startWith('Error: Failed to send email');
done();
}).catch(done);
});
it('return correct failure message for no mail server at this address', function (done) {
mailer.transport.transportType.should.eql('DIRECT');
mailer.send(mailDataNoServer).then(function () {
done(new Error('Error message not shown.'));
}, function (error) {
error.message.should.eql('Error: Failed to send email.');
done();
}).catch(done);
});
it('return correct failure message for incomplete data', function (done) {
mailer.transport.transportType.should.eql('DIRECT');
mailer.send(mailDataIncomplete).then(function () {
done(new Error('Error message not shown.'));
}, function (error) {
error.message.should.eql('Error: Incomplete message data.');
done();
}).catch(done);
});
});
describe('From address', function () {
it('should use the config', function () {
configUtils.set({
mail: {
from: '"Blog Title" <static@example.com>'
}
});
mailer = new GhostMail();
mailer.from().should.equal('"Blog Title" <static@example.com>');
});
it('should fall back to [blog.title] <ghost@[blog.url]>', function () {
// Standard domain
configUtils.set({url: 'http://default.com', mail: {from: null}, theme: {title: 'Test'}});
mailer = new GhostMail();
mailer.from().should.equal('"Test" <ghost@default.com>');
// Trailing slash
configUtils.set({url: 'http://default.com/', mail: {from: null}, theme: {title: 'Test'}});
mailer.from().should.equal('"Test" <ghost@default.com>');
// Strip Port
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}, theme: {title: 'Test'}});
mailer.from().should.equal('"Test" <ghost@default.com>');
});
it('should use mail.from if both from and fromaddress are present', function () {
// Standard domain
configUtils.set({mail: {from: '"bar" <from@default.com>', fromaddress: '"Qux" <fa@default.com>'}});
mailer = new GhostMail();
mailer.from().should.equal('"bar" <from@default.com>');
});
it('should attach blog title if from or fromaddress are only email addresses', function () {
// from and fromaddress are both set
configUtils.set({mail: {from: 'from@default.com', fromaddress: 'fa@default.com'}, theme: {title: 'Test'}});
mailer = new GhostMail();
mailer.from().should.equal('"Test" <from@default.com>');
// only from set
configUtils.set({mail: {from: 'from@default.com', fromaddress: null}, theme: {title: 'Test'}});
mailer.from().should.equal('"Test" <from@default.com>');
// only fromaddress set
configUtils.set({mail: {from: null, fromaddress: 'fa@default.com'}, theme: {title: 'Test'}});
mailer.from().should.equal('"Test" <fa@default.com>');
});
it('should ignore theme title if from address is Title <email@address.com> format', function () {
// from and fromaddress are both set
configUtils.set({mail: {from: '"R2D2" <from@default.com>', fromaddress: '"C3PO" <fa@default.com>'}, theme: {title: 'Test'}});
mailer = new GhostMail();
mailer.from().should.equal('"R2D2" <from@default.com>');
// only from set
configUtils.set({mail: {from: '"R2D2" <from@default.com>', fromaddress: null}, theme: {title: 'Test'}});
mailer.from().should.equal('"R2D2" <from@default.com>');
// only fromaddress set
configUtils.set({mail: {from: null, fromaddress: '"C3PO" <fa@default.com>'}, theme: {title: 'Test'}});
mailer.from().should.equal('"C3PO" <fa@default.com>');
});
it('should use default title if not theme title is provided', function () {
configUtils.set({url: 'http://default.com:2368/', mail: {from: null}, theme: {title: null}});
mailer = new GhostMail();
mailer.from().should.equal('"Ghost at default.com" <ghost@default.com>');
});
});
2013-08-21 00:19:47 +04:00
});