Skip to content

Commit aacafb7

Browse files
Add unit test
1 parent eb52f02 commit aacafb7

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

cmd/expiration-mailer/main_test.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
2424
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/letsencrypt/go-jose"
2525
"github.com/letsencrypt/boulder/Godeps/_workspace/src/gopkg.in/gorp.v1"
26+
2627
"github.com/letsencrypt/boulder/core"
2728
blog "github.com/letsencrypt/boulder/log"
2829
"github.com/letsencrypt/boulder/mocks"
@@ -42,21 +43,6 @@ func intFromB64(b64 string) int {
4243
return int(bigIntFromB64(b64).Int64())
4344
}
4445

45-
type mockMail struct {
46-
Messages []string
47-
}
48-
49-
func (m *mockMail) Clear() {
50-
m.Messages = []string{}
51-
}
52-
53-
func (m *mockMail) SendMail(to []string, subject, msg string) (err error) {
54-
for range to {
55-
m.Messages = append(m.Messages, msg)
56-
}
57-
return
58-
}
59-
6046
type fakeRegStore struct {
6147
RegByID map[int64]core.Registration
6248
}
@@ -104,7 +90,7 @@ var (
10490

10591
func TestSendNags(t *testing.T) {
10692
stats, _ := statsd.NewNoopClient(nil)
107-
mc := mockMail{}
93+
mc := mocks.Mailer{}
10894
rs := newFakeRegStore()
10995
fc := newFakeClock(t)
11096

@@ -461,7 +447,7 @@ func TestDontFindRevokedCert(t *testing.T) {
461447
type testCtx struct {
462448
dbMap *gorp.DbMap
463449
ssa core.StorageAdder
464-
mc *mockMail
450+
mc *mocks.Mailer
465451
fc clock.FakeClock
466452
m *mailer
467453
cleanUp func()
@@ -482,7 +468,7 @@ func setup(t *testing.T, nagTimes []time.Duration) *testCtx {
482468
cleanUp := test.ResetSATestDatabase(t)
483469

484470
stats, _ := statsd.NewNoopClient(nil)
485-
mc := &mockMail{}
471+
mc := &mocks.Mailer{}
486472

487473
offsetNags := make([]time.Duration, len(nagTimes))
488474
for i, t := range nagTimes {

mail/mailer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"net"
1212
"net/smtp"
1313
"strings"
14-
"time"
14+
15+
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
1516
)
1617

1718
// Mailer provides the interface for a mailer
@@ -25,6 +26,7 @@ type MailerImpl struct {
2526
Port string
2627
Auth smtp.Auth
2728
From string
29+
clk clock.Clock
2830
}
2931

3032
// New constructs a Mailer to represent an account on a particular mail
@@ -36,11 +38,13 @@ func New(server, port, username, password string) MailerImpl {
3638
Port: port,
3739
Auth: auth,
3840
From: username,
41+
clk: clock.Default(),
3942
}
4043
}
4144

4245
func (m *MailerImpl) generateMessage(to []string, subject, body string) []byte {
43-
now := time.Now().UTC()
46+
now := m.clk.Now().UTC()
47+
rand.Seed(int64(now.Nanosecond()))
4448
headers := []string{
4549
fmt.Sprintf("To: %s", strings.Join(to, ", ")),
4650
fmt.Sprintf("From: %s", m.From),

mail/mailer_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,28 @@
44
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
55

66
package mail
7+
8+
import (
9+
"strings"
10+
"testing"
11+
12+
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
13+
14+
"github.com/letsencrypt/boulder/test"
15+
)
16+
17+
func TestGenerateMessage(t *testing.T) {
18+
fc := clock.NewFake()
19+
m := MailerImpl{From: "send@email.com", clk: fc}
20+
message := string(m.generateMessage([]string{"recv@email.com"}, "test subject", "this is the body"))
21+
fields := strings.Split(message, "\r\n")
22+
test.AssertEquals(t, len(fields), 8)
23+
test.AssertEquals(t, fields[0], "To: recv@email.com")
24+
test.AssertEquals(t, fields[1], "From: send@email.com")
25+
test.AssertEquals(t, fields[2], "Subject: test subject")
26+
test.AssertEquals(t, fields[3], "Date: Thu Jan 1 1970 00:00:00 +0000")
27+
test.AssertEquals(t, fields[4], "Message-Id: <19700101T000000.8717895732742165505.send@email.com>")
28+
test.AssertEquals(t, fields[5], "")
29+
test.AssertEquals(t, fields[6], "this is the body")
30+
test.AssertEquals(t, fields[7], "")
31+
}

mocks/mocks.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,21 @@ func (s *Statter) Inc(metric string, value int64, rate float32) error {
422422
func NewStatter() Statter {
423423
return Statter{statsd.NoopClient{}, map[string]int64{}}
424424
}
425+
426+
// Mailer is a mock
427+
type Mailer struct {
428+
Messages []string
429+
}
430+
431+
// Clear removes any previously recorded messages
432+
func (m *Mailer) Clear() {
433+
m.Messages = []string{}
434+
}
435+
436+
// SendMail is a mock
437+
func (m *Mailer) SendMail(to []string, subject, msg string) (err error) {
438+
for range to {
439+
m.Messages = append(m.Messages, msg)
440+
}
441+
return
442+
}

0 commit comments

Comments
 (0)