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