go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/mail/message_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mail
    16  
    17  import (
    18  	net_mail "net/mail"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func TestDataTypes(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("data types", t, func() {
    28  		m := &Message{
    29  			"from@example.com",
    30  			"reply_to@example.com",
    31  			[]string{"to1@example.com", "to2@example.com"},
    32  			[]string{"cc1@example.com", "cc2@example.com"},
    33  			[]string{"bcc1@example.com", "bcc2@example.com"},
    34  			"subject",
    35  			"body",
    36  			"<html><body>body</body></html>",
    37  			[]Attachment{{"name.doc", []byte("data"), "https://content_id"}},
    38  			net_mail.Header{
    39  				"In-Reply-To": []string{"cats"},
    40  			},
    41  		}
    42  
    43  		Convey("empty copy doesn't do extra allocations", func() {
    44  			m := (&Message{}).Copy()
    45  			So(m.To, ShouldBeNil)
    46  			So(m.Cc, ShouldBeNil)
    47  			So(m.Bcc, ShouldBeNil)
    48  			So(m.Attachments, ShouldBeNil)
    49  			So(m.Headers, ShouldBeNil)
    50  
    51  			tm := (&TestMessage{}).Copy()
    52  			So(tm.MIMETypes, ShouldBeNil)
    53  		})
    54  
    55  		Convey("can copy nil", func() {
    56  			So((*Message)(nil).Copy(), ShouldBeNil)
    57  			So((*Message)(nil).ToSDKMessage(), ShouldBeNil)
    58  			So((*TestMessage)(nil).Copy(), ShouldBeNil)
    59  		})
    60  
    61  		Convey("Message is copyable", func() {
    62  			m2 := m.Copy()
    63  			So(m2, ShouldResemble, m)
    64  
    65  			// make sure it's really a copy
    66  			m2.To[0] = "fake@faker.example.com"
    67  			So(m2.To, ShouldNotResemble, m.To)
    68  
    69  			m2.Headers["SomethingElse"] = []string{"noooo"}
    70  			So(m2.Headers, ShouldNotResemble, m.Headers)
    71  		})
    72  
    73  		Convey("TestMessage is copyable", func() {
    74  			tm := &TestMessage{*m, []string{"application/msword"}}
    75  			tm2 := tm.Copy()
    76  			So(tm, ShouldResemble, tm2)
    77  
    78  			tm2.MIMETypes[0] = "spam"
    79  			So(tm, ShouldNotResemble, tm2)
    80  		})
    81  
    82  		Convey("Message can be cast to an SDK Message", func() {
    83  			m2 := m.ToSDKMessage()
    84  			So(m2.Sender, ShouldResemble, m.Sender)
    85  			So(m2.ReplyTo, ShouldResemble, m.ReplyTo)
    86  			So(m2.To, ShouldResemble, m.To)
    87  			So(m2.Cc, ShouldResemble, m.Cc)
    88  			So(m2.Bcc, ShouldResemble, m.Bcc)
    89  			So(m2.Subject, ShouldResemble, m.Subject)
    90  			So(m2.Body, ShouldResemble, m.Body)
    91  			So(m2.HTMLBody, ShouldResemble, m.HTMLBody)
    92  			So(m2.Headers, ShouldResemble, m.Headers)
    93  
    94  			So((Attachment)(m2.Attachments[0]), ShouldResemble, m.Attachments[0])
    95  		})
    96  	})
    97  }