This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mail: allow sending a text version of mail
- Loading branch information
1 parent
1e1ab77
commit acc7e0d
Showing
6 changed files
with
54 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package mailer | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -11,56 +11,51 @@ import ( | |
|
||
func TestSendEmail(t *testing.T) { | ||
hostname, smtpPort, apiPort, cleanup := CreateTestMailCatcher(t) | ||
// Test InitSMTPMailer with valid path for templates. | ||
settings := helpers.Settings{ | ||
SMTPHost: hostname, | ||
SMTPPort: smtpPort, | ||
SMTPFrom: "[email protected]", | ||
} | ||
mailer, err := InitSMTPMailer(settings) | ||
if mailer == nil { | ||
t.Error("Expected non nil mailer.") | ||
} | ||
mailer, err := NewSMTPMailer(settings) | ||
if err != nil { | ||
t.Errorf("Expected nil error, found %s", err.Error()) | ||
t.Fatal(err) | ||
} | ||
body := bytes.NewBufferString("test html here") | ||
err = mailer.SendEmail("[email protected]", "sample subject", body.Bytes()) | ||
if err != nil { | ||
t.Errorf("Expected nil error, found %s", err.Error()) | ||
html := []byte("<strong>html</strong>") | ||
text := []byte("text") | ||
if err := mailer.SendEmail("[email protected]", "subject", html, text); err != nil { | ||
t.Errorf("SendEmail() err = %v, want none", err) | ||
} | ||
receivedData, err := GetLatestMailMessageData(hostname, apiPort) | ||
b, err := GetLatestMailMessageData(hostname, apiPort) | ||
if err != nil { | ||
t.Errorf("Expected nil error, found %s", err.Error()) | ||
t.Fatal(err) | ||
} | ||
if !strings.Contains(string(receivedData), `"sender":"<[email protected]>"`) { | ||
t.Error("Expected to find sender metadata") | ||
message := string(b) | ||
if want := "[email protected]"; !strings.Contains(message, fmt.Sprintf(`"sender":"<%s>"`, want)) { | ||
t.Errorf("sender was wrong, want %q", want) | ||
} | ||
if !strings.Contains(string(receivedData), `"recipients":["<[email protected]>"]`) { | ||
t.Error("Expected to find receipient metadata") | ||
if want := "[email protected]"; !strings.Contains(message, fmt.Sprintf(`"recipients":["<%s>"]`, want)) { | ||
t.Errorf("recipient was wrong, want %q", want) | ||
} | ||
if !strings.Contains(string(receivedData), `"subject":"sample subject"`) { | ||
t.Error("Expected to find receipient metadata") | ||
if want := "subject"; !strings.Contains(message, fmt.Sprintf(`"subject":"%s"`, want)) { | ||
t.Errorf("subject was wrong, want %q", want) | ||
} | ||
// Useful for generating test data. Undo to learn more about the data. | ||
// log.Println(string(receivedData)) | ||
|
||
cleanup() // Destroy the mail server. | ||
cleanup() | ||
|
||
// Try sending mail to bad server. | ||
err = mailer.SendEmail("test@receiver.com", "sample subject", body.Bytes()) | ||
err = mailer.SendEmail("recipient@example.com", "sample subject", html, text) | ||
if err == nil { | ||
t.Error("Expected non nil error") | ||
t.Error("got nil error, want something") | ||
} | ||
} | ||
|
||
func TestInitSMTPMailer(t *testing.T) { | ||
func TestNewSMTPMailer(t *testing.T) { | ||
settings := helpers.Settings{} | ||
mailer, err := InitSMTPMailer(settings) | ||
if mailer == nil { | ||
t.Error("Expected non nil mailer.") | ||
} | ||
mailer, err := NewSMTPMailer(settings) | ||
if err != nil { | ||
t.Errorf("Expected nil error, found %s", err.Error()) | ||
t.Errorf("error = %v, want nil", err) | ||
} | ||
if mailer == nil { | ||
t.Error("mailer = nil, want something") | ||
} | ||
} |