You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.5 KiB
JavaScript
144 lines
3.5 KiB
JavaScript
7 years ago
|
'use strict';
|
||
|
|
||
4 years ago
|
const { SMTPServer } = require('smtp-server');
|
||
4 years ago
|
const assert = require('assert');
|
||
|
const fs = require('fs');
|
||
|
const path = require('path');
|
||
7 years ago
|
|
||
4 years ago
|
const db = require('./mocks/databasemock');
|
||
|
const Plugins = require('../src/plugins');
|
||
|
const Emailer = require('../src/emailer');
|
||
|
const Meta = require('../src/meta');
|
||
7 years ago
|
|
||
4 years ago
|
describe('emailer', () => {
|
||
4 years ago
|
let onMail = function (address, session, callback) { callback(); };
|
||
|
let onTo = function (address, session, callback) { callback(); };
|
||
7 years ago
|
|
||
4 years ago
|
const template = 'test';
|
||
|
const email = '[email protected]';
|
||
|
const language = 'en-GB';
|
||
|
const params = {
|
||
7 years ago
|
subject: 'Welcome to NodeBB',
|
||
|
};
|
||
|
|
||
4 years ago
|
before((done) => {
|
||
4 years ago
|
const server = new SMTPServer({
|
||
7 years ago
|
allowInsecureAuth: true,
|
||
|
onAuth: function (auth, session, callback) {
|
||
|
callback(null, {
|
||
|
user: auth.username,
|
||
|
});
|
||
|
},
|
||
|
onMailFrom: function (address, session, callback) {
|
||
|
onMail(address, session, callback);
|
||
|
},
|
||
|
onRcptTo: function (address, session, callback) {
|
||
|
onTo(address, session, callback);
|
||
|
},
|
||
|
});
|
||
|
|
||
4 years ago
|
server.on('error', (err) => {
|
||
7 years ago
|
throw err;
|
||
|
});
|
||
|
server.listen(4000, done);
|
||
|
});
|
||
|
|
||
|
// TODO: test sendmail here at some point
|
||
|
|
||
4 years ago
|
it('plugin hook should work', (done) => {
|
||
4 years ago
|
const error = new Error();
|
||
7 years ago
|
|
||
4 years ago
|
Plugins.hooks.register('emailer-test', {
|
||
7 years ago
|
hook: 'filter:email.send',
|
||
|
method: function (data, next) {
|
||
|
assert(data);
|
||
|
assert.equal(data.to, email);
|
||
4 years ago
|
assert.equal(data.subject, `[NodeBB] ${params.subject}`);
|
||
7 years ago
|
|
||
|
next(error);
|
||
|
},
|
||
|
});
|
||
|
|
||
4 years ago
|
Emailer.sendToEmail(template, email, language, params, (err) => {
|
||
7 years ago
|
assert.equal(err, error);
|
||
|
|
||
4 years ago
|
Plugins.hooks.unregister('emailer-test', 'filter:email.send');
|
||
7 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
4 years ago
|
it('should build custom template on config change', (done) => {
|
||
4 years ago
|
const text = 'a random string of text';
|
||
7 years ago
|
|
||
|
// make sure it's not already set
|
||
4 years ago
|
Emailer.renderAndTranslate('test', {}, 'en-GB', (err, output) => {
|
||
7 years ago
|
assert.ifError(err);
|
||
|
|
||
|
assert.notEqual(output, text);
|
||
|
|
||
4 years ago
|
Meta.configs.set('email:custom:test', text, (err) => {
|
||
7 years ago
|
assert.ifError(err);
|
||
|
|
||
|
// wait for pubsub stuff
|
||
4 years ago
|
setTimeout(() => {
|
||
|
Emailer.renderAndTranslate('test', {}, 'en-GB', (err, output) => {
|
||
7 years ago
|
assert.ifError(err);
|
||
|
|
||
|
assert.equal(output, text);
|
||
|
done();
|
||
|
});
|
||
|
}, 500);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
4 years ago
|
it('should send via SMTP', (done) => {
|
||
4 years ago
|
const from = '[email protected]';
|
||
|
const username = '[email protected]';
|
||
7 years ago
|
|
||
|
onMail = function (address, session, callback) {
|
||
|
assert.equal(address.address, from);
|
||
|
assert.equal(session.user, username);
|
||
|
|
||
|
callback();
|
||
|
};
|
||
|
|
||
|
onTo = function (address, session, callback) {
|
||
|
assert.equal(address.address, email);
|
||
|
|
||
|
callback();
|
||
|
done();
|
||
|
};
|
||
|
|
||
|
Meta.configs.setMultiple({
|
||
|
'email:smtpTransport:enabled': '1',
|
||
|
'email:smtpTransport:user': username,
|
||
7 years ago
|
'email:smtpTransport:pass': 'anything',
|
||
7 years ago
|
'email:smtpTransport:service': 'nodebb-custom-smtp',
|
||
|
'email:smtpTransport:port': 4000,
|
||
|
'email:smtpTransport:host': 'localhost',
|
||
|
'email:smtpTransport:security': 'NONE',
|
||
|
'email:from': from,
|
||
4 years ago
|
}, (err) => {
|
||
7 years ago
|
assert.ifError(err);
|
||
|
|
||
|
// delay so emailer has a chance to update after config changes
|
||
4 years ago
|
setTimeout(() => {
|
||
7 years ago
|
assert.equal(Emailer.fallbackTransport, Emailer.transports.smtp);
|
||
|
|
||
4 years ago
|
Emailer.sendToEmail(template, email, language, params, (err) => {
|
||
7 years ago
|
assert.ifError(err);
|
||
|
});
|
||
|
}, 200);
|
||
|
});
|
||
|
});
|
||
|
|
||
4 years ago
|
after((done) => {
|
||
7 years ago
|
fs.unlinkSync(path.join(__dirname, '../build/public/templates/emails/test.js'));
|
||
|
Meta.configs.setMultiple({
|
||
|
'email:smtpTransport:enabled': '0',
|
||
|
'email:custom:test': '',
|
||
|
}, done);
|
||
|
});
|
||
|
});
|