@webda/core
Version:
Expose API with Lambda
144 lines • 5.57 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { suite, test } from "@testdeck/mocha";
import * as assert from "assert";
import * as sinon from "sinon";
import { MailerParameters, User } from "...js";
import { WebdaTest } from "../test.js";
let MailerTest = class MailerTest extends WebdaTest {
async before() {
await super.before();
this.lastLevel = this.lastInfo = this.lastOptions = this.lastCallback = undefined;
this.ctx = await this.newContext();
this.mailer = this.getService("TrueMailer");
// Mocking the transporter
this.mailer._transporter = {};
this.mailer._transporter.sendMail = (options, callback) => {
this.lastOptions = options;
this.lastCallback = callback;
return Promise.resolve();
};
// Mocking the logger
this.mailer._webda.log = (level, ...args) => {
this.lastLevel = level;
this.lastInfo = args;
};
}
params() {
let params = new MailerParameters({
templates: "ts",
templatesEngine: "b"
});
assert.strictEqual(params.templates, "ts/");
assert.strictEqual(params.templatesEngine, "b");
}
async unknownTemplate() {
this.mailer._getTemplate("plop");
assert.strictEqual(this.lastLevel, "WARN");
assert.strictEqual(this.lastInfo[0], "No template found for");
assert.strictEqual(this.lastInfo[1], "plop");
await assert.rejects(() => this.mailer.send({
template: "plop",
from: "test@webda.io",
replacements: {}
}), /Unknown mail template/);
}
knownTemplate() {
this.mailer._getTemplate("PASSPORT_EMAIL_RECOVERY");
assert.strictEqual(this.lastLevel, undefined);
this.mailer._getTemplate("PASSPORT_EMAIL_RECOVERY");
assert.strictEqual(this.lastLevel, undefined);
}
async knownTemplateOnSend() {
await this.mailer.send({
template: "PASSPORT_EMAIL_RECOVERY"
});
assert.notStrictEqual(this.lastOptions, undefined);
assert.notStrictEqual(this.lastOptions.subject, undefined);
assert.notStrictEqual(this.lastOptions.html, undefined);
assert.notStrictEqual(this.lastOptions.text, undefined);
}
async sendManual() {
await this.mailer.send({ subject: "mine" });
assert.notStrictEqual(this.lastOptions, undefined);
assert.strictEqual(this.lastOptions.subject, "mine");
}
async templateNoResult() {
this.mailer._getTemplate = () => {
return {
renderAll: async () => {
return {};
}
};
};
await this.mailer.send({ template: "mine" });
assert.notStrictEqual(this.lastOptions, undefined);
assert.strictEqual(this.lastOptions.subject, undefined);
}
async noTransporter() {
this.mailer._transporter = undefined;
await assert.rejects(() => this.mailer.send({
template: "PASSPORT_EMAIL_RECOVERY",
from: "test@webda.io"
}), /Cannot send email as no transporter is defined/);
}
async handleNotificationFor() {
const user = new User();
assert.strictEqual(await this.mailer.handleNotificationFor(user), false);
user.load({ email: "test@test.com" });
assert.strictEqual(await this.mailer.handleNotificationFor(user), true);
}
async sendNotification() {
let stub = sinon.stub(this.mailer, "send").callsFake(async () => { });
try {
const user = new User();
assert.rejects(() => this.mailer.sendNotification(user, "", undefined, undefined), /Cannot find a valid email for user/);
user.load({ email: "test@test.com" });
await this.mailer.sendNotification(user, "", undefined, undefined);
}
finally {
stub.restore();
}
stub = sinon.stub(this.mailer, "send").callsFake(async () => {
throw new Error("Fake");
});
await assert.rejects(() => this.mailer.sendNotification(new User().load({ email: "test@test.com" }), "", undefined, undefined), /Fake/);
stub.restore();
}
};
__decorate([
test
], MailerTest.prototype, "params", null);
__decorate([
test
], MailerTest.prototype, "unknownTemplate", null);
__decorate([
test
], MailerTest.prototype, "knownTemplate", null);
__decorate([
test
], MailerTest.prototype, "knownTemplateOnSend", null);
__decorate([
test
], MailerTest.prototype, "sendManual", null);
__decorate([
test
], MailerTest.prototype, "templateNoResult", null);
__decorate([
test
], MailerTest.prototype, "noTransporter", null);
__decorate([
test
], MailerTest.prototype, "handleNotificationFor", null);
__decorate([
test
], MailerTest.prototype, "sendNotification", null);
MailerTest = __decorate([
suite
], MailerTest);
//# sourceMappingURL=mailer.spec.js.map