2015-08-18 05:57:27 +05:30
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewEmailConfigFromReader(t *testing.T) {
|
|
|
|
tests := []struct {
|
2016-06-28 12:04:55 +05:30
|
|
|
json string
|
|
|
|
want MailgunEmailerConfig
|
|
|
|
wantErr bool
|
|
|
|
wantInitErr bool // want error when calling Emailer() with no fromAddr
|
2015-08-18 05:57:27 +05:30
|
|
|
}{
|
|
|
|
{
|
2016-06-28 12:04:55 +05:30
|
|
|
json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public","domain":"example.com","from":"admin@example.com"}`,
|
2015-08-18 05:57:27 +05:30
|
|
|
want: MailgunEmailerConfig{
|
|
|
|
PrivateAPIKey: "private",
|
|
|
|
PublicAPIKey: "public",
|
|
|
|
Domain: "example.com",
|
2016-06-28 12:04:55 +05:30
|
|
|
FromAddr: "admin@example.com",
|
2015-08-18 05:57:27 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-06-28 12:04:55 +05:30
|
|
|
json: `{"type":"mailgun","id":"mg","publicAPIKey":"public","domain":"example.com",""}`,
|
2015-08-18 05:57:27 +05:30
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public"}`,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","domain":"example.com"}`,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2016-06-28 12:04:55 +05:30
|
|
|
{
|
|
|
|
json: `{"type":"mailgun","id":"mg","privateAPIKey":"private","publicAPIKey":"public","domain":"example.com"}`,
|
|
|
|
want: MailgunEmailerConfig{
|
|
|
|
PrivateAPIKey: "private",
|
|
|
|
PublicAPIKey: "public",
|
|
|
|
Domain: "example.com",
|
|
|
|
},
|
|
|
|
|
|
|
|
// No fromAddr email provided. Calling Emailer("") should error since fromAddr needs to be provided
|
|
|
|
// in the config or as a command line argument.
|
|
|
|
wantInitErr: true,
|
|
|
|
},
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
r := strings.NewReader(tt.json)
|
|
|
|
ec, err := newEmailerConfigFromReader(r)
|
|
|
|
if tt.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("case %d: want non-nil err.", i)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("case %d: want nil err: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if diff := pretty.Compare(tt.want, ec); diff != "" {
|
|
|
|
t.Errorf("case %d: Compare(want, got): %v", i, diff)
|
|
|
|
}
|
2016-06-28 12:04:55 +05:30
|
|
|
|
|
|
|
_, err = ec.Emailer("")
|
|
|
|
if err != nil && !tt.wantInitErr {
|
|
|
|
t.Errorf("case %d: failed to initialize emailer: %v", i, err)
|
|
|
|
}
|
|
|
|
if err == nil && tt.wantInitErr {
|
|
|
|
t.Errorf("case %d: expected error initializing emailer", i)
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|