OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package memory |
| 6 |
| 7 import ( |
| 8 net_mail "net/mail" |
| 9 "testing" |
| 10 |
| 11 mailS "github.com/luci/gae/service/mail" |
| 12 userS "github.com/luci/gae/service/user" |
| 13 . "github.com/luci/luci-go/common/testing/assertions" |
| 14 . "github.com/smartystreets/goconvey/convey" |
| 15 "golang.org/x/net/context" |
| 16 ) |
| 17 |
| 18 func TestMail(t *testing.T) { |
| 19 t.Parallel() |
| 20 |
| 21 Convey("mail", t, func() { |
| 22 c := Use(context.Background()) |
| 23 user := userS.Get(c) |
| 24 mail := mailS.Get(c) |
| 25 |
| 26 Convey("good cases", func() { |
| 27 Convey("start with an empty set of messages", func() { |
| 28 So(mail.Testable().SentMessages(), ShouldBeEmpty
) |
| 29 }) |
| 30 |
| 31 Convey("can send a message from the admin", func() { |
| 32 So(mail.Send(&mailS.Message{ |
| 33 Sender: "admin@example.com", |
| 34 To: []string{"Valued Customer <cust
omer@example.com>"}, |
| 35 Subject: "You are valued.", |
| 36 Body: "We value you.", |
| 37 }), ShouldBeNil) |
| 38 |
| 39 Convey("and it shows up in sent messages", func(
) { |
| 40 So(mail.Testable().SentMessages(), Shoul
dResembleV, []*mailS.TestMessage{ |
| 41 {Message: mailS.Message{ |
| 42 Sender: "admin@example.
com", |
| 43 To: []string{"Value
d Customer <customer@example.com>"}, |
| 44 Subject: "You are valued
.", |
| 45 Body: "We value you."
, |
| 46 }}, |
| 47 }) |
| 48 |
| 49 Convey("which can be reset", func() { |
| 50 mail.Testable().Reset() |
| 51 So(mail.Testable().SentMessages(
), ShouldBeEmpty) |
| 52 }) |
| 53 }) |
| 54 }) |
| 55 |
| 56 Convey("can send a message on behalf of a user", func()
{ |
| 57 user.Testable().Login("dood@example.com", "", fa
lse) |
| 58 So(mail.Send(&mailS.Message{ |
| 59 Sender: "Friendly Person <dood@example.
com>", |
| 60 To: []string{"Other Friendly Person
<dudette@example.com>"}, |
| 61 Subject: "Hi", |
| 62 Body: "An app is sending a message fo
r me. It's the future.", |
| 63 }), ShouldBeNil) |
| 64 }) |
| 65 |
| 66 Convey("can send a message to the admins", func() { |
| 67 So(mail.SendToAdmins(&mailS.Message{ |
| 68 Sender: "admin@example.com", |
| 69 Subject: "Reminder", |
| 70 Body: "I forgot", |
| 71 }), ShouldBeNil) |
| 72 |
| 73 So(mail.Testable().SentMessages(), ShouldResembl
eV, []*mailS.TestMessage{ |
| 74 {Message: mailS.Message{ |
| 75 Sender: "admin@example.com", |
| 76 To: []string{"admin@example
.com"}, |
| 77 Subject: "Reminder", |
| 78 Body: "I forgot", |
| 79 }}, |
| 80 }) |
| 81 }) |
| 82 |
| 83 Convey("can set admin emails", func() { |
| 84 mail.Testable().SetAdminEmails( |
| 85 "Friendly <hello@example.com>", |
| 86 "Epic <nerdsnipe@example.com>", |
| 87 ) |
| 88 |
| 89 So(mail.SendToAdmins(&mailS.Message{ |
| 90 Sender: "hello@example.com", |
| 91 Subject: "Reminder", |
| 92 Body: "I forgot", |
| 93 }), ShouldBeNil) |
| 94 |
| 95 So(mail.Testable().SentMessages(), ShouldResembl
eV, []*mailS.TestMessage{ |
| 96 {Message: mailS.Message{ |
| 97 Sender: "hello@example.com", |
| 98 To: []string{ |
| 99 "Friendly <hello@example
.com>", |
| 100 "Epic <nerdsnipe@example
.com>", |
| 101 }, |
| 102 Subject: "Reminder", |
| 103 Body: "I forgot", |
| 104 }}, |
| 105 }) |
| 106 }) |
| 107 |
| 108 Convey("attachments get mimetypes assigned to them", fun
c() { |
| 109 So(mail.SendToAdmins(&mailS.Message{ |
| 110 Sender: "admin@example.com", |
| 111 Subject: "Reminder", |
| 112 Body: "I forgot", |
| 113 Attachments: []mailS.Attachment{ |
| 114 {Name: "reminder.txt", Data: []b
yte("bananas")}, |
| 115 {Name: "coolthing", Data: []byte
("bananas")}, |
| 116 }, |
| 117 }), ShouldBeNil) |
| 118 |
| 119 So(mail.Testable().SentMessages(), ShouldResembl
eV, []*mailS.TestMessage{ |
| 120 { |
| 121 Message: mailS.Message{ |
| 122 Sender: "admin@example.
com", |
| 123 To: []string{"admin
@example.com"}, |
| 124 Subject: "Reminder", |
| 125 Body: "I forgot", |
| 126 Attachments: []mailS.Att
achment{ |
| 127 {Name: "reminder
.txt", Data: []byte("bananas")}, |
| 128 {Name: "coolthin
g", Data: []byte("bananas")}, |
| 129 }, |
| 130 }, |
| 131 MIMETypes: []string{"text/plain"
, "application/octet-stream"}}, |
| 132 }) |
| 133 }) |
| 134 |
| 135 Convey("can have headers", func() { |
| 136 So(mail.SendToAdmins(&mailS.Message{ |
| 137 Sender: "admin@example.com", |
| 138 Subject: "Reminder", |
| 139 Body: "I forgot", |
| 140 Headers: net_mail.Header{ |
| 141 "in-reply-to": []string{"epicnes
s"}, |
| 142 "List-Id": []string{"spam"}, |
| 143 }, |
| 144 }), ShouldBeNil) |
| 145 |
| 146 So(mail.Testable().SentMessages(), ShouldResembl
eV, []*mailS.TestMessage{ |
| 147 {Message: mailS.Message{ |
| 148 Sender: "admin@example.com", |
| 149 To: []string{"admin@example
.com"}, |
| 150 Subject: "Reminder", |
| 151 Body: "I forgot", |
| 152 Headers: net_mail.Header{ |
| 153 "In-Reply-To": []string{
"epicness"}, |
| 154 "List-Id": []string{
"spam"}, |
| 155 }, |
| 156 }}, |
| 157 }) |
| 158 |
| 159 }) |
| 160 }) |
| 161 |
| 162 Convey("errors", func() { |
| 163 Convey("setting a non-email is a panic", func() { |
| 164 So(func() { mail.Testable().SetAdminEmails("i am
a banana") }, |
| 165 ShouldPanicLike, `invalid email ("i am a
banana"): mail: missing phrase`) |
| 166 }) |
| 167 |
| 168 Convey("sending from a non-user, non-admin is an error",
func() { |
| 169 mail.Testable().SetAdminEmails("Friendly <hello@
example.com>") |
| 170 |
| 171 So(mail.Send(&mailS.Message{ |
| 172 Sender: "someone_else@example.com", |
| 173 Subject: "Reminder", |
| 174 Body: "I forgot", |
| 175 }), ShouldErrLike, "invalid Sender: someone_else
@example.com") |
| 176 }) |
| 177 |
| 178 Convey("sending from a bogus address is a problem", func
() { |
| 179 So(mail.Send(&mailS.Message{ |
| 180 Sender: "lalal", |
| 181 }), ShouldErrLike, "unparsable Sender address: l
alal: mail: missing phrase") |
| 182 }) |
| 183 |
| 184 Convey("sending with no recipients is a problem", func()
{ |
| 185 So(mail.Send(&mailS.Message{ |
| 186 Sender: "admin@example.com", |
| 187 }), ShouldErrLike, "one of To, Cc or Bcc must be
non-empty") |
| 188 }) |
| 189 |
| 190 Convey("bad addresses are a problem", func() { |
| 191 So(mail.Send(&mailS.Message{ |
| 192 Sender: "admin@example.com", |
| 193 To: []string{"wut"}, |
| 194 }), ShouldErrLike, `invalid email ("wut"): mail:
missing phrase`) |
| 195 |
| 196 So(mail.Send(&mailS.Message{ |
| 197 Sender: "admin@example.com", |
| 198 Cc: []string{"wut"}, |
| 199 }), ShouldErrLike, `invalid email ("wut"): mail:
missing phrase`) |
| 200 |
| 201 So(mail.Send(&mailS.Message{ |
| 202 Sender: "admin@example.com", |
| 203 Bcc: []string{"wut"}, |
| 204 }), ShouldErrLike, `invalid email ("wut"): mail:
missing phrase`) |
| 205 }) |
| 206 |
| 207 Convey("no body is a problem", func() { |
| 208 So(mail.Send(&mailS.Message{ |
| 209 Sender: "admin@example.com", |
| 210 To: []string{"wut@example.com"}, |
| 211 }), ShouldErrLike, `one of Body or HTMLBody must
be non-empty`) |
| 212 }) |
| 213 |
| 214 Convey("bad attachments are a problem", func() { |
| 215 So(mail.Send(&mailS.Message{ |
| 216 Sender: "admin@example.com", |
| 217 To: []string{"wut@example.com"}, |
| 218 Body: "nice thing", |
| 219 Attachments: []mailS.Attachment{ |
| 220 {Name: "nice.exe", Data: []byte(
"boom")}, |
| 221 }, |
| 222 }), ShouldErrLike, `illegal attachment extension
for "nice.exe"`) |
| 223 }) |
| 224 |
| 225 Convey("bad headers are a problem", func() { |
| 226 So(mail.SendToAdmins(&mailS.Message{ |
| 227 Sender: "admin@example.com", |
| 228 Subject: "Reminder", |
| 229 Body: "I forgot", |
| 230 Headers: net_mail.Header{"x-spam-cool":
[]string{"value"}}, |
| 231 }), ShouldErrLike, `disallowed header: x-spam-co
ol`) |
| 232 |
| 233 }) |
| 234 |
| 235 }) |
| 236 |
| 237 }) |
| 238 } |
OLD | NEW |