fix: properly unregister hooks in emailer tests

isekai-main
Julian Lam 3 years ago
parent dee9cca3c8
commit fc2c755c64

@ -49,22 +49,23 @@ describe('emailer', () => {
it('plugin hook should work', (done) => { it('plugin hook should work', (done) => {
const error = new Error(); const error = new Error();
const method = function (data, next) {
Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send',
method: function (data, next) {
assert(data); assert(data);
assert.equal(data.to, email); assert.equal(data.to, email);
assert.equal(data.subject, `[NodeBB] ${params.subject}`); assert.equal(data.subject, `[NodeBB] ${params.subject}`);
next(error); next(error);
}, };
Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send',
method,
}); });
Emailer.sendToEmail(template, email, language, params, (err) => { Emailer.sendToEmail(template, email, language, params, (err) => {
assert.equal(err, error); assert.equal(err, error);
Plugins.hooks.unregister('emailer-test', 'filter:email.send'); Plugins.hooks.unregister('emailer-test', 'filter:email.send', method);
done(); done();
}); });
}); });
@ -152,37 +153,40 @@ describe('emailer', () => {
}); });
it('should not send email to a banned user', async () => { it('should not send email to a banned user', async () => {
const method = async () => {
assert(false); // if thrown, email was sent
};
Plugins.hooks.register('emailer-test', { Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send', hook: 'filter:email.send',
method: async () => { method,
assert(false); // if thrown, email was sent
},
}); });
await user.bans.ban(recipientUid); await user.bans.ban(recipientUid);
await Emailer.send('test', recipientUid, {}); await Emailer.send('test', recipientUid, {});
Plugins.hooks.unregister('emailer-test', 'filter:email.send'); Plugins.hooks.unregister('emailer-test', 'filter:email.send', method);
}); });
it('should return true if the template is "banned"', async () => { it('should return true if the template is "banned"', async () => {
const method = async () => {
assert(true); // if thrown, email was sent
};
Plugins.hooks.register('emailer-test', { Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send', hook: 'filter:email.send',
method: async () => { method,
assert(true); // if thrown, email was sent
},
}); });
await Emailer.send('banned', recipientUid, {}); await Emailer.send('banned', recipientUid, {});
Plugins.hooks.unregister('emailer-test', 'filter:email.send'); Plugins.hooks.unregister('emailer-test', 'filter:email.send', method);
}); });
it('should return true if system settings allow sending to banned users', async () => { it('should return true if system settings allow sending to banned users', async () => {
const method = async () => {
assert(true); // if thrown, email was sent
};
Plugins.hooks.register('emailer-test', { Plugins.hooks.register('emailer-test', {
hook: 'filter:email.send', hook: 'filter:email.send',
method: async () => { method,
assert(true); // if thrown, email was sent
},
}); });
meta.config.sendEmailToBanned = 1; meta.config.sendEmailToBanned = 1;
@ -190,7 +194,7 @@ describe('emailer', () => {
meta.config.sendEmailToBanned = 0; meta.config.sendEmailToBanned = 0;
await user.bans.unban(recipientUid); await user.bans.unban(recipientUid);
Plugins.hooks.unregister('emailer-test', 'filter:email.send'); Plugins.hooks.unregister('emailer-test', 'filter:email.send', method);
}); });
}); });
}); });

Loading…
Cancel
Save