From a69021f91860c382092da5f4a3d1bcec412baf62 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Fri, 24 Jul 2015 14:53:50 -0700 Subject: [PATCH] Add sendWarning test --- cmd/expiration-mailer/main.go | 10 +++++----- mail/mailer.go | 14 +++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/expiration-mailer/main.go b/cmd/expiration-mailer/main.go index 7bb53e607..d62aa5199 100644 --- a/cmd/expiration-mailer/main.go +++ b/cmd/expiration-mailer/main.go @@ -37,7 +37,7 @@ type mailer struct { stats statsd.Statter log *blog.AuditLogger dbMap *gorp.DbMap - Mailer *mail.Mailer + Mailer mail.Mailer EmailTemplate *template.Template WarningDays []int } @@ -86,8 +86,7 @@ func (m *mailer) findExpiringCertificates() error { if err != nil { return err } - expiresIn := int(time.Now().Sub(parsedCert.NotAfter).Hours() / 24) - err = m.sendWarning(parsedCert, reg, expiresIn) + err = m.sendWarning(parsedCert, reg.Contact) if err != nil { return err } @@ -133,9 +132,10 @@ func (m *mailer) findExpiringCertificates() error { return err } -func (m *mailer) sendWarning(parsedCert *x509.Certificate, reg core.Registration, expiresIn int) error { +func (m *mailer) sendWarning(parsedCert *x509.Certificate, contacts []core.AcmeURL) error { + expiresIn := int(parsedCert.NotAfter.Sub(time.Now()).Hours() / 24) emails := []string{} - for _, contact := range reg.Contact { + for _, contact := range contacts { if contact.Scheme == "mailto" { emails = append(emails, contact.Opaque) } diff --git a/mail/mailer.go b/mail/mailer.go index 88241b243..097c138e0 100644 --- a/mail/mailer.go +++ b/mail/mailer.go @@ -10,8 +10,12 @@ import ( "net/smtp" ) -// Mailer defines a mail transfer agent to use for sending mail -type Mailer struct { +type Mailer interface { + SendMail([]string, string) error +} + +// MailerImpl defines a mail transfer agent to use for sending mail +type MailerImpl struct { Server string Port string Auth smtp.Auth @@ -20,9 +24,9 @@ type Mailer struct { // NewMailer constructs a Mailer to represent an account on a particular mail // transfer agent. -func NewMailer(server, port, username, password string) Mailer { +func NewMailer(server, port, username, password string) MailerImpl { auth := smtp.PlainAuth("", username, password, server) - return Mailer{ + return MailerImpl{ Server: server, Port: port, Auth: auth, @@ -32,7 +36,7 @@ func NewMailer(server, port, username, password string) Mailer { // SendMail sends an email to the provided list of recipients. The email body // is simple text. -func (m *Mailer) SendMail(to []string, msg string) (err error) { +func (m *MailerImpl) SendMail(to []string, msg string) (err error) { err = smtp.SendMail(net.JoinHostPort(m.Server, m.Port), m.Auth, m.From, to, []byte(msg)) return }