authelia/server/test/notifiers/EMailNotifier.test.ts

55 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-05-20 14:49:05 +07:00
import * as sinon from "sinon";
import * as Assert from "assert";
import BluebirdPromise = require("bluebird");
2017-05-20 14:49:05 +07:00
import { MailSenderStub } from "../mocks/notifiers/MailSenderStub";
import EMailNotifier = require("../../src/lib/notifiers/EMailNotifier");
2017-05-20 14:49:05 +07:00
describe("test email notifier", function () {
it("should send an email to given user", function () {
const mailSender = new MailSenderStub();
const options = {
username: "user_gmail",
password: "pass_gmail",
sender: "admin@example.com",
service: "gmail"
};
mailSender.sendStub.returns(BluebirdPromise.resolve());
const sender = new EMailNotifier.EMailNotifier(options, mailSender);
const subject = "subject";
const url = "http://test.com";
return sender.notify("user@example.com", subject, url)
.then(function () {
Assert.equal(mailSender.sendStub.getCall(0).args[0].to, "user@example.com");
Assert.equal(mailSender.sendStub.getCall(0).args[0].subject, "subject");
return BluebirdPromise.resolve();
});
});
it("should fail while sending an email", function () {
const mailSender = new MailSenderStub();
2017-05-20 14:49:05 +07:00
const options = {
username: "user_gmail",
password: "pass_gmail",
sender: "admin@example.com",
service: "gmail"
2017-05-20 14:49:05 +07:00
};
mailSender.sendStub.returns(BluebirdPromise.reject(new Error("Failed to send mail")));
const sender = new EMailNotifier.EMailNotifier(options, mailSender);
2017-05-20 14:49:05 +07:00
const subject = "subject";
const url = "http://test.com";
return sender.notify("user@example.com", subject, url)
2017-05-20 14:49:05 +07:00
.then(function () {
return BluebirdPromise.reject(new Error());
}, function() {
Assert.equal(mailSender.sendStub.getCall(0).args[0].from, "admin@example.com");
return BluebirdPromise.resolve();
2017-05-20 14:49:05 +07:00
});
});
});