...
1 package notmain
2
3 import (
4 "crypto/x509"
5 "crypto/x509/pkix"
6 "fmt"
7 "math/big"
8 "testing"
9 "time"
10
11 "github.com/letsencrypt/boulder/mocks"
12 "github.com/letsencrypt/boulder/test"
13 )
14
15 var (
16 email1 = "mailto:one@example.com"
17 email2 = "mailto:two@example.com"
18 )
19
20 func TestSendEarliestCertInfo(t *testing.T) {
21 expiresIn := 24 * time.Hour
22 ctx := setup(t, []time.Duration{expiresIn})
23 defer ctx.cleanUp()
24
25 rawCertA := newX509Cert("happy A",
26 ctx.fc.Now().AddDate(0, 0, 5),
27 []string{"example-A.com", "SHARED-example.com"},
28 serial1,
29 )
30 rawCertB := newX509Cert("happy B",
31 ctx.fc.Now().AddDate(0, 0, 2),
32 []string{"shared-example.com", "example-b.com"},
33 serial2,
34 )
35
36 conn, err := ctx.m.mailer.Connect()
37 test.AssertNotError(t, err, "connecting SMTP")
38 err = ctx.m.sendNags(conn, []string{email1, email2}, []*x509.Certificate{rawCertA, rawCertB})
39 if err != nil {
40 t.Fatal(err)
41 }
42 if len(ctx.mc.Messages) != 2 {
43 t.Errorf("num of messages, want %d, got %d", 2, len(ctx.mc.Messages))
44 }
45 if len(ctx.mc.Messages) == 0 {
46 t.Fatalf("no message sent")
47 }
48 domains := "example-a.com\nexample-b.com\nshared-example.com"
49 expected := mocks.MailerMessage{
50 Subject: "Testing: Let's Encrypt certificate expiration notice for domain \"example-a.com\" (and 2 more)",
51 Body: fmt.Sprintf(`hi, cert for DNS names %s is going to expire in 2 days (%s)`,
52 domains,
53 rawCertB.NotAfter.Format(time.DateOnly)),
54 }
55 expected.To = "one@example.com"
56 test.AssertEquals(t, expected, ctx.mc.Messages[0])
57 expected.To = "two@example.com"
58 test.AssertEquals(t, expected, ctx.mc.Messages[1])
59 }
60
61 func newX509Cert(commonName string, notAfter time.Time, dnsNames []string, serial *big.Int) *x509.Certificate {
62 return &x509.Certificate{
63 Subject: pkix.Name{
64 CommonName: commonName,
65 },
66 NotAfter: notAfter,
67 DNSNames: dnsNames,
68 SerialNumber: serial,
69 }
70
71 }
72
View as plain text