github.com/blend/go-sdk@v1.20220411.3/email/message_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package email
     9  
    10  import (
    11  	"encoding/json"
    12  	"testing"
    13  
    14  	"gopkg.in/yaml.v3"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/ex"
    18  )
    19  
    20  func TestMessageValidate(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	assert.True(ex.Is(ErrMessageFieldUnset, Message{}.Validate()))
    24  	assert.True(ex.Is(ErrMessageFieldUnset, Message{
    25  		From: "foo@bar.com",
    26  	}.Validate()))
    27  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    28  		From: "foo\r@bar.com",
    29  	}.Validate()))
    30  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    31  		From: "foo\n@bar.com",
    32  	}.Validate()))
    33  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    34  		From: "foo\r\n@bar.com",
    35  	}.Validate()))
    36  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    37  		From: "foo@bar.com",
    38  		To:   []string{"moo@bar.com", "bad\n@bar.com"},
    39  	}.Validate()))
    40  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    41  		From: "foo@bar.com",
    42  		To:   []string{"moo@bar.com"},
    43  		CC:   []string{"bad\n@bar.com"},
    44  	}.Validate()))
    45  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    46  		From: "foo@bar.com",
    47  		To:   []string{"moo@bar.com"},
    48  		CC:   []string{"ok@bar.com"},
    49  		BCC:  []string{"bad\n@bar.com"},
    50  	}.Validate()))
    51  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    52  		From:    "foo@bar.com",
    53  		To:      []string{"moo@bar.com"},
    54  		Subject: "this is \n bad",
    55  	}.Validate()))
    56  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    57  		From:    "foo@bar.com",
    58  		To:      []string{"moo@bar.com"},
    59  		Subject: "this is \r bad",
    60  	}.Validate()))
    61  	assert.True(ex.Is(ErrMessageFieldNewlines, Message{
    62  		From:    "foo@bar.com",
    63  		To:      []string{"moo@bar.com"},
    64  		Subject: "this is \n\r bad",
    65  	}.Validate()))
    66  	assert.True(ex.Is(ErrMessageFieldUnset, Message{
    67  		From: "foo@bar.com",
    68  		To:   []string{"moo@bar.com"},
    69  	}.Validate()))
    70  
    71  	assert.Nil(Message{
    72  		From:     "foo@bar.com",
    73  		To:       []string{"moo@bar.com"},
    74  		TextBody: "stuff",
    75  	}.Validate())
    76  }
    77  
    78  func TestMessageSerializeJSON(t *testing.T) {
    79  	assert := assert.New(t)
    80  
    81  	contents, err := json.Marshal(&Message{})
    82  	assert.Nil(err)
    83  	assert.NotEmpty(contents)
    84  }
    85  
    86  func TestMessageSerializeYAML(t *testing.T) {
    87  	assert := assert.New(t)
    88  
    89  	contents, err := yaml.Marshal(&Message{})
    90  	assert.Nil(err)
    91  	assert.NotEmpty(contents)
    92  }