| 1 | package mailyak |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/md5" |
| 6 | "crypto/rand" |
| 7 | "crypto/tls" |
| 8 | "encoding/hex" |
| 9 | "net/smtp" |
| 10 | "os" |
| 11 | "reflect" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func tlsEndpoint(t *testing.T) string { |
| 17 | s := os.Getenv("MAILYAK_TLS_ENDPOINT") |
| 18 | if s == "" { |
| 19 | t.Log("set MAILYAK_TLS_ENDPOINT to run TLS integration tests") |
| 20 | t.SkipNow() |
| 21 | } |
| 22 | return s |
| 23 | } |
| 24 | |
| 25 | func plaintextEndpoint(t *testing.T) string { |
| 26 | s := os.Getenv("MAILYAK_PLAINTEXT_ENDPOINT") |
| 27 | if s == "" { |
| 28 | t.Log("set MAILYAK_PLAINTEXT_ENDPOINT to run plain-text integration tests") |
| 29 | t.SkipNow() |
| 30 | } |
| 31 | return s |
| 32 | } |
| 33 | |
| 34 | func TestIntegration_TLS(t *testing.T) { |
| 35 | t.Parallel() |
| 36 | |
| 37 | tests := []struct { |
| 38 | name string |
| 39 | auth smtp.Auth |
| 40 | |
| 41 | fn func(m *MailYak) |
| 42 | |
| 43 | wantErr error |
| 44 | }{ |
| 45 | { |
| 46 | name: "ok", |
| 47 | fn: func(m *MailYak) { |
| 48 | m.From("[email protected]") |
| 49 | m.FromName("From Example") |
| 50 | m.To("[email protected]") |
| 51 | m.Bcc("[email protected]", "[email protected]") |
| 52 | m.Subject("TLS test") |
| 53 | m.ReplyTo("[email protected]") |
| 54 | m.HTML().Set("HTML part: this is just a test.") |
| 55 | m.Plain().Set("Plain text part: this is also just a test.") |
| 56 | m.Attach("test.html", strings.NewReader("<html><head></head></html>")) |
| 57 | m.Attach("test2.html", strings.NewReader("<html><head></head></html>")) |
| 58 | m.AddHeader("Precedence", "bulk") |
| 59 | }, |
| 60 | wantErr: nil, |
| 61 | }, |
| 62 | { |
| 63 | name: "empty", |
| 64 | fn: func(m *MailYak) { |
| 65 | m.From("[email protected]") |
| 66 | m.FromName("From Example") |
| 67 | m.To("[email protected]") |
| 68 | m.Bcc("[email protected]", "[email protected]") |
| 69 | m.Subject("TLS empty") |
| 70 | m.ReplyTo("[email protected]") |
| 71 | }, |
| 72 | wantErr: nil, |
| 73 | }, |
| 74 | { |
| 75 | name: "authenticated", |
| 76 | auth: smtp.PlainAuth("ident", "user", "pass", "127.0.0.1"), |
| 77 | fn: func(m *MailYak) { |
| 78 | m.From("[email protected]") |
| 79 | m.FromName("From Example") |
| 80 | m.To("[email protected]") |
| 81 | m.Bcc("[email protected]", "[email protected]") |
| 82 | m.Subject("TLS test") |
| 83 | m.ReplyTo("[email protected]") |
| 84 | m.HTML().Set("HTML part: this is just a test.") |
| 85 | m.Plain().Set("Plain text part: this is also just a test.") |
| 86 | m.Attach("test.html", strings.NewReader("<html><head></head></html>")) |
| 87 | m.Attach("test2.html", strings.NewReader("<html><head></head></html>")) |
| 88 | m.AddHeader("Precedence", "bulk") |
| 89 | }, |
| 90 | wantErr: nil, |
| 91 | }, |
| 92 | { |
| 93 | name: "binary attachment", |
| 94 | fn: func(m *MailYak) { |
| 95 | data := make([]byte, 1024*5) |
| 96 | _, _ = rand.Read(data) |
| 97 | hash := md5.Sum(data) |
| 98 | hashString := hex.EncodeToString(hash[:]) |
| 99 | |
| 100 | m.From("[email protected]") |
| 101 | m.FromName("Dom") |
| 102 | m.To("[email protected]") |
| 103 | m.Subject("TLS Attachment test") |
| 104 | m.ReplyTo("[email protected]") |
| 105 | m.HTML().Set("Attachment MD5: " + hashString) |
| 106 | m.Attach("test.bin", bytes.NewReader(data)) |
| 107 | }, |
| 108 | wantErr: nil, |
| 109 | }, |
| 110 | } |
| 111 | |
| 112 | for _, tt := range tests { |
| 113 | var tt = tt |
| 114 | t.Run(tt.name, func(t *testing.T) { |
| 115 | t.Parallel() |
| 116 | |
| 117 | // Initialise a TLS mailyak instance with terrible security. |
| 118 | mail, err := NewWithTLS(tlsEndpoint(t), tt.auth, &tls.Config{ |
| 119 | // Please, never do this outside of a test. |
| 120 | InsecureSkipVerify: true, |
| 121 | ServerName: "127.0.0.1", |
| 122 | }) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | |
| 127 | // Apply some mutations to the email |
| 128 | tt.fn(mail) |
| 129 | |
| 130 | // Send the email |
| 131 | err = mail.Send() |
| 132 | if !reflect.DeepEqual(err, tt.wantErr) { |
| 133 | t.Errorf("got %v, want %v", err, tt.wantErr) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestIntegration_PlainText(t *testing.T) { |
| 140 | t.Parallel() |
| 141 | |
| 142 | tests := []struct { |
| 143 | name string |
| 144 | auth smtp.Auth |
| 145 | |
| 146 | fn func(m *MailYak) |
| 147 | |
| 148 | wantErr error |
| 149 | }{ |
| 150 | { |
| 151 | name: "ok", |
| 152 | fn: func(m *MailYak) { |
| 153 | m.From("[email protected]") |
| 154 | m.FromName("From Example") |
| 155 | m.To("[email protected]") |
| 156 | m.Bcc("[email protected]", "[email protected]") |
| 157 | m.Subject("PLAIN - Test subject") |
| 158 | m.ReplyTo("[email protected]") |
| 159 | m.HTML().Set("HTML part: this is just a test.") |
| 160 | m.Plain().Set("Plain text part: this is also just a test.") |
| 161 | m.Attach("test.html", strings.NewReader("<html><head></head></html>")) |
| 162 | m.Attach("test2.html", strings.NewReader("<html><head></head></html>")) |
| 163 | m.AddHeader("Precedence", "bulk") |
| 164 | }, |
| 165 | wantErr: nil, |
| 166 | }, |
| 167 | { |
| 168 | name: "empty", |
| 169 | fn: func(m *MailYak) { |
| 170 | m.From("[email protected]") |
| 171 | m.FromName("From Example") |
| 172 | m.To("[email protected]") |
| 173 | m.Bcc("[email protected]", "[email protected]") |
| 174 | m.Subject("Plaintext empty") |
| 175 | m.ReplyTo("[email protected]") |
| 176 | }, |
| 177 | wantErr: nil, |
| 178 | }, |
| 179 | { |
| 180 | name: "authenticated", |
| 181 | auth: smtp.PlainAuth("ident", "user", "pass", "127.0.0.1"), |
| 182 | fn: func(m *MailYak) { |
| 183 | m.From("[email protected]") |
| 184 | m.FromName("From Example") |
| 185 | m.To("[email protected]") |
| 186 | m.Bcc("[email protected]", "[email protected]") |
| 187 | m.Subject("PLAIN - TLS test") |
| 188 | m.ReplyTo("[email protected]") |
| 189 | m.HTML().Set("HTML part: this is just a test.") |
| 190 | m.Plain().Set("Plain text part: this is also just a test.") |
| 191 | m.Attach("test.html", strings.NewReader("<html><head></head></html>")) |
| 192 | m.Attach("test2.html", strings.NewReader("<html><head></head></html>")) |
| 193 | m.AddHeader("Precedence", "bulk") |
| 194 | }, |
| 195 | wantErr: nil, |
| 196 | }, |
| 197 | { |
| 198 | name: "binary attachment", |
| 199 | fn: func(m *MailYak) { |
| 200 | data := make([]byte, 1024*5) |
| 201 | _, _ = rand.Read(data) |
| 202 | hash := md5.Sum(data) |
| 203 | hashString := hex.EncodeToString(hash[:]) |
| 204 | |
| 205 | m.From("[email protected]") |
| 206 | m.FromName("Dom") |
| 207 | m.To("[email protected]") |
| 208 | m.Subject("PLAIN - Attachment test") |
| 209 | m.ReplyTo("[email protected]") |
| 210 | m.HTML().Set("Attachment MD5: " + hashString) |
| 211 | m.Attach("test.bin", bytes.NewReader(data)) |
| 212 | }, |
| 213 | wantErr: nil, |
| 214 | }, |
| 215 | } |
| 216 | |
| 217 | for _, tt := range tests { |
| 218 | var tt = tt |
| 219 | t.Run(tt.name, func(t *testing.T) { |
| 220 | t.Parallel() |
| 221 | |
| 222 | mail := New(plaintextEndpoint(t), tt.auth) |
| 223 | |
| 224 | // Apply some mutations to the email |
| 225 | tt.fn(mail) |
| 226 | |
| 227 | // Send the email |
| 228 | err := mail.Send() |
| 229 | if !reflect.DeepEqual(err, tt.wantErr) { |
| 230 | t.Errorf("got %v, want %v", err, tt.wantErr) |
| 231 | } |
| 232 | }) |
| 233 | } |
| 234 | } |