| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const SMTPServer = require('smtp-server').SMTPServer; | 
					
						
							|  |  |  | const assert = require('assert'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | const Plugins = require('../src/plugins'); | 
					
						
							|  |  |  | const Emailer = require('../src/emailer'); | 
					
						
							|  |  |  | const Meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('emailer', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let onMail = function (address, session, callback) { callback(); }; | 
					
						
							|  |  |  | 	let onTo = function (address, session, callback) { callback(); }; | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	const template = 'test'; | 
					
						
							|  |  |  | 	const email = 'test@example.org'; | 
					
						
							|  |  |  | 	const language = 'en-GB'; | 
					
						
							|  |  |  | 	const params = { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 		subject: 'Welcome to NodeBB', | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	before((done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const server = new SMTPServer({ | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		server.on('error', (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			throw err; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		server.listen(4000, done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TODO: test sendmail here at some point
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('plugin hook should work', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const error = new Error(); | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-27 17:36:58 -05:00
										 |  |  | 		Plugins.hooks.register('emailer-test', { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			hook: 'filter:email.send', | 
					
						
							|  |  |  | 			method: function (data, next) { | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert.equal(data.to, email); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				assert.equal(data.subject, `[NodeBB] ${params.subject}`); | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				next(error); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		Emailer.sendToEmail(template, email, language, params, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			assert.equal(err, error); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-27 17:36:58 -05:00
										 |  |  | 			Plugins.hooks.unregister('emailer-test', 'filter:email.send'); | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should build custom template on config change', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const text = 'a random string of text'; | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// make sure it's not already set
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		Emailer.renderAndTranslate('test', {}, 'en-GB', (err, output) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.notEqual(output, text); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			Meta.configs.set('email:custom:test', text, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// wait for pubsub stuff
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				setTimeout(() => { | 
					
						
							|  |  |  | 					Emailer.renderAndTranslate('test', {}, 'en-GB', (err, output) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						assert.equal(output, text); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, 500); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should send via SMTP', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const from = 'admin@example.org'; | 
					
						
							|  |  |  | 		const username = 'another@example.com'; | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		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, | 
					
						
							| 
									
										
										
										
											2018-01-06 12:06:42 -07:00
										 |  |  | 			'email:smtpTransport:pass': 'anything', | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			'email:smtpTransport:service': 'nodebb-custom-smtp', | 
					
						
							|  |  |  | 			'email:smtpTransport:port': 4000, | 
					
						
							|  |  |  | 			'email:smtpTransport:host': 'localhost', | 
					
						
							|  |  |  | 			'email:smtpTransport:security': 'NONE', | 
					
						
							|  |  |  | 			'email:from': from, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		}, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// delay so emailer has a chance to update after config changes
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 				assert.equal(Emailer.fallbackTransport, Emailer.transports.smtp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				Emailer.sendToEmail(template, email, language, params, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	after((done) => { | 
					
						
							| 
									
										
										
										
											2017-11-21 08:51:20 -07:00
										 |  |  | 		fs.unlinkSync(path.join(__dirname, '../build/public/templates/emails/test.js')); | 
					
						
							|  |  |  | 		Meta.configs.setMultiple({ | 
					
						
							|  |  |  | 			'email:smtpTransport:enabled': '0', | 
					
						
							|  |  |  | 			'email:custom:test': '', | 
					
						
							|  |  |  | 		}, done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |