// TestTemplating checks that the template name and vars are set correctly func TestTemplating(t *testing.T) { // TestEmail adds recipients that have a piece of recipient-specific context e := testutils.TestEmail() // set some global template context e.TemplateContext["hello"] = "world" wrapper, err := b.mandrillWrapperForEmail(e) if err != nil { t.FailNow() } me := wrapper.Message if len(me.GlobalMergeVars) != 1 { t.FailNow() } if me.GlobalMergeVars[0].Name != "hello" { t.FailNow() } if me.GlobalMergeVars[0].Content != "world" { t.FailNow() } if len(me.MergeVars) != len(me.To) { t.FailNow() } }
// TestAttachments checks that attachments are being added properly. func TestAttachments(t *testing.T) { e := testutils.TestEmail() attachment := testutils.TestAttachment(t) e.Attachments = append(e.Attachments, attachment) params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } // the sendgrid backend will have read the contents of the reader, // so we need to seek back to the beginning offset, err := attachment.Data.(io.ReadSeeker).Seek(0, 0) if err != nil { t.FailNow() } else if offset != int64(0) { t.FailNow() } // check that the file data is there fileData, err := ioutil.ReadAll(attachment.Data) if err != nil { t.FailNow() } if params.Get(fmt.Sprintf("files[%v]", e.Attachments[0].Name)) != string(fileData) { t.FailNow() } }
// TestTemplating tests SendGrid's (yet to be released) template tags func TestTemplating(t *testing.T) { e := testutils.TestEmail() e.TemplateContext = make(map[string]string) // we haven't added any template info yet func() { params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } // should be no 'filters' in the xsmtpapi params xSMTPAPI := decodeXSMTPAPI(t, params) if _, ok := xSMTPAPI["filters"]; ok { t.FailNow() } // should be no 'body' param if params.Get("body") != "" { t.FailNow() } }() // add some invalid template data that should trigger an error func() { e.TemplateID = "test-template" e.TemplateContext["not-body"] = "some invalid variable" if _, err := b.paramsForEmail(e); err == nil { t.FailNow() } }() // finally some good data func() { e.TemplateID = "test-template" e.TemplateContext = map[string]string{"body": "legit"} params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } xSMTPAPI := decodeXSMTPAPI(t, params) filters := xSMTPAPI["filters"].(map[string]interface{}) templates := filters["templates"].(map[string]interface{}) settings := templates["settings"].(map[string]interface{}) if settings["enabled"] != float64(1) { t.FailNow() } if settings["template_id"] != e.TemplateID { t.FailNow() } }() }
// TestEmail checks that email properties are set correctly func TestEmail(t *testing.T) { e := testutils.TestEmail() wrapper, err := b.mandrillWrapperForEmail(e) if err != nil { t.FailNow() } me := wrapper.Message if e.Subject != me.Subject { t.FailNow() } if e.HTMLBody != me.HTML { t.FailNow() } if e.TextBody != me.Text { t.FailNow() } if e.From.Name != me.FromName { t.FailNow() } if e.From.Address != me.FromEmail { t.FailNow() } if e.ReplyTo.Address != me.Headers["Reply-To"] { t.FailNow() } if e.TrackClicks != me.TrackClicks { t.FailNow() } if e.TrackOpens != me.TrackOpens { t.FailNow() } if e.SubAccount != me.Subaccount { t.FailNow() } if len(e.Tags) != len(me.Tags) { t.FailNow() } if len(e.To) != len(me.To) { t.FailNow() } }
// TestCategories checks that the tags/categories are set correctly func TestCategories(t *testing.T) { e := testutils.TestEmail() params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } xSMTPAPI := decodeXSMTPAPI(t, params) if len(xSMTPAPI["category"].([]interface{})) != len(e.Tags) { t.FailNow() } }
// TestHeaders checks that the email headers are set correctly func TestHeaders(t *testing.T) { e := testutils.TestEmail() e.Headers.Set("hello", "world") wrapper, err := b.mandrillWrapperForEmail(e) if err != nil { t.FailNow() } if wrapper.Message.Headers["hello"] != "world" { t.FailNow() } }
// TestDummyLogging checks the logging functionality func TestDummyLogging(t *testing.T) { logs := make([]string, 0) logger := func(format string, vars ...interface{}) { logs = append(logs, fmt.Sprintf(format, vars...)) } b := NewBackend(logger) e := testutils.TestEmail() b.SendEmail(e) if len(logs) == 0 { t.FailNow() } }
// TestAttachments checks that the attachments are added correctly func TestAttachments(t *testing.T) { e := testutils.TestEmail() attachment := testutils.TestAttachment(t) e.Attachments = append(e.Attachments, attachment) wrapper, err := b.mandrillWrapperForEmail(e) if err != nil { t.FailNow() } me := wrapper.Message if len(me.Attachments) != len(e.Attachments) { t.FailNow() } mAttachment := me.Attachments[0] if attachment.Name != mAttachment.Name { t.FailNow() } if attachment.Mimetype != mAttachment.Type { t.FailNow() } // the mandrill backend will have read the contents of the reader, // so we need to seek back to the beginning offset, err := attachment.Data.(io.ReadSeeker).Seek(0, 0) if err != nil { t.FailNow() } else if offset != int64(0) { t.FailNow() } // check that the file data is there fileData, err := ioutil.ReadAll(attachment.Data) if err != nil { t.FailNow() } if base64.StdEncoding.EncodeToString(fileData) != mAttachment.Content { t.FailNow() } }
// TestWrapper checks the JSON wrapper we send to Mandrill func TestWrapper(t *testing.T) { e := testutils.TestEmail() // set some additional properties e.DeliveryTime = time.Now() e.TemplateID = "test-template" wrapper, err := b.mandrillWrapperForEmail(e) if err != nil { t.FailNow() } if wrapper.SendAt != e.DeliveryTime.Format(deliveryTimeFmt) { t.FailNow() } if wrapper.TemplateName != e.TemplateID { t.FailNow() } }
// TestHeaders checks that email headers are set correctly func TestHeaders(t *testing.T) { e := testutils.TestEmail() e.Headers.Set("hello", "world") params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } headersEncoded := params.Get("headers") headers := make(map[string]string) if err := json.Unmarshal([]byte(headersEncoded), &headers); err != nil { t.FailNow() } if headers["hello"] != "world" { t.FailNow() } }
// TestAttachments checks that attachments are added correctly func TestAttachments(t *testing.T) { e := testutils.TestEmail() attachment := testutils.TestAttachment(t) e.Attachments = append(e.Attachments, attachment) wrapper, err := b.wrapperForEmail(e) if err != nil { t.Fatal(err) } paAttachment, ok := wrapper.Arguments.Attachments[attachment.Name] if !ok { t.FailNow() } if paAttachment.ContentType != attachment.Mimetype { t.FailNow() } // the postageapp backend will have read the contents of the reader, // so we need to seek back to the beginning offset, err := attachment.Data.(io.ReadSeeker).Seek(0, 0) if err != nil { t.FailNow() } else if offset != int64(0) { t.FailNow() } // check that the file data is there fileData, err := ioutil.ReadAll(attachment.Data) if err != nil { t.FailNow() } if paAttachment.Content != base64.StdEncoding.EncodeToString(fileData) { t.FailNow() } }
// TestTemplating checks that template name/vars are set correctly func TestTemplating(t *testing.T) { e := testutils.TestEmail() e.TemplateID = "test-template" e.TemplateContext = map[string]string{"hello": "world"} wrapper, err := b.wrapperForEmail(e) if err != nil { t.Fatal(err) } if wrapper.Arguments.Template != e.TemplateID { t.FailNow() } if wrapper.Arguments.Variables["hello"] != e.TemplateContext["hello"] { t.FailNow() } for _, ctx := range wrapper.Arguments.Recipients { if _, ok := ctx["name"]; !ok { t.FailNow() } } }
// TestWrapper checks the JSON wrapper func TestWrapper(t *testing.T) { e := testutils.TestEmail() wrapper, err := b.wrapperForEmail(e) if err != nil { t.Fatal(err) } if wrapper.APIKey != apiKey { t.FailNow() } if len(wrapper.Arguments.Recipients) != len(e.To) { t.FailNow() } if wrapper.Arguments.Headers["subject"] != e.Subject { t.FailNow() } if wrapper.Arguments.Headers["from"] != e.From.String() { t.FailNow() } if wrapper.Arguments.Headers["reply-to"] != e.ReplyTo.String() { t.FailNow() } if wrapper.Arguments.Content["text/plain"] != e.TextBody { t.FailNow() } if wrapper.Arguments.Content["text/html"] != e.HTMLBody { t.FailNow() } }
// TestGeneral tests that the basic email params like subject and body are populated correctly. func TestGeneral(t *testing.T) { e := testutils.TestEmail() params, err := b.paramsForEmail(e) if err != nil { t.FailNow() } if params.Get("subject") != e.Subject { t.FailNow() } if params.Get("text") != e.TextBody { t.FailNow() } if params.Get("html") != e.HTMLBody { t.FailNow() } if params.Get("from") != e.From.Address { t.FailNow() } if params.Get("fromname") != e.From.Name { t.FailNow() } if params.Get("replyto") != e.ReplyTo.Address { t.FailNow() } if len(params["to[]"]) != len(e.To) || len(params["toname[]"]) != len(e.To) { t.FailNow() } }