Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Unified Diff: impl/memory/mail_test.go

Issue 1525923002: Implement Mail service. (Closed) Base URL: https://github.com/luci/gae.git@filter_user
Patch Set: tests and words Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « impl/memory/mail_static_lists.go ('k') | impl/memory/user.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: impl/memory/mail_test.go
diff --git a/impl/memory/mail_test.go b/impl/memory/mail_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b559ff8e6f5d7ebf36b2f9833c2b5e91303be41e
--- /dev/null
+++ b/impl/memory/mail_test.go
@@ -0,0 +1,238 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package memory
+
+import (
+ net_mail "net/mail"
+ "testing"
+
+ mailS "github.com/luci/gae/service/mail"
+ userS "github.com/luci/gae/service/user"
+ . "github.com/luci/luci-go/common/testing/assertions"
+ . "github.com/smartystreets/goconvey/convey"
+ "golang.org/x/net/context"
+)
+
+func TestMail(t *testing.T) {
+ t.Parallel()
+
+ Convey("mail", t, func() {
+ c := Use(context.Background())
+ user := userS.Get(c)
+ mail := mailS.Get(c)
+
+ Convey("good cases", func() {
+ Convey("start with an empty set of messages", func() {
+ So(mail.Testable().SentMessages(), ShouldBeEmpty)
+ })
+
+ Convey("can send a message from the admin", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"Valued Customer <customer@example.com>"},
+ Subject: "You are valued.",
+ Body: "We value you.",
+ }), ShouldBeNil)
+
+ Convey("and it shows up in sent messages", func() {
+ So(mail.Testable().SentMessages(), ShouldResembleV, []*mailS.TestMessage{
+ {Message: mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"Valued Customer <customer@example.com>"},
+ Subject: "You are valued.",
+ Body: "We value you.",
+ }},
+ })
+
+ Convey("which can be reset", func() {
+ mail.Testable().Reset()
+ So(mail.Testable().SentMessages(), ShouldBeEmpty)
+ })
+ })
+ })
+
+ Convey("can send a message on behalf of a user", func() {
+ user.Testable().Login("dood@example.com", "", false)
+ So(mail.Send(&mailS.Message{
+ Sender: "Friendly Person <dood@example.com>",
+ To: []string{"Other Friendly Person <dudette@example.com>"},
+ Subject: "Hi",
+ Body: "An app is sending a message for me. It's the future.",
+ }), ShouldBeNil)
+ })
+
+ Convey("can send a message to the admins", func() {
+ So(mail.SendToAdmins(&mailS.Message{
+ Sender: "admin@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ }), ShouldBeNil)
+
+ So(mail.Testable().SentMessages(), ShouldResembleV, []*mailS.TestMessage{
+ {Message: mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"admin@example.com"},
+ Subject: "Reminder",
+ Body: "I forgot",
+ }},
+ })
+ })
+
+ Convey("can set admin emails", func() {
+ mail.Testable().SetAdminEmails(
+ "Friendly <hello@example.com>",
+ "Epic <nerdsnipe@example.com>",
+ )
+
+ So(mail.SendToAdmins(&mailS.Message{
+ Sender: "hello@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ }), ShouldBeNil)
+
+ So(mail.Testable().SentMessages(), ShouldResembleV, []*mailS.TestMessage{
+ {Message: mailS.Message{
+ Sender: "hello@example.com",
+ To: []string{
+ "Friendly <hello@example.com>",
+ "Epic <nerdsnipe@example.com>",
+ },
+ Subject: "Reminder",
+ Body: "I forgot",
+ }},
+ })
+ })
+
+ Convey("attachments get mimetypes assigned to them", func() {
+ So(mail.SendToAdmins(&mailS.Message{
+ Sender: "admin@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ Attachments: []mailS.Attachment{
+ {Name: "reminder.txt", Data: []byte("bananas")},
+ {Name: "coolthing", Data: []byte("bananas")},
+ },
+ }), ShouldBeNil)
+
+ So(mail.Testable().SentMessages(), ShouldResembleV, []*mailS.TestMessage{
+ {
+ Message: mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"admin@example.com"},
+ Subject: "Reminder",
+ Body: "I forgot",
+ Attachments: []mailS.Attachment{
+ {Name: "reminder.txt", Data: []byte("bananas")},
+ {Name: "coolthing", Data: []byte("bananas")},
+ },
+ },
+ MIMETypes: []string{"text/plain", "application/octet-stream"}},
+ })
+ })
+
+ Convey("can have headers", func() {
+ So(mail.SendToAdmins(&mailS.Message{
+ Sender: "admin@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ Headers: net_mail.Header{
+ "in-reply-to": []string{"epicness"},
+ "List-Id": []string{"spam"},
+ },
+ }), ShouldBeNil)
+
+ So(mail.Testable().SentMessages(), ShouldResembleV, []*mailS.TestMessage{
+ {Message: mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"admin@example.com"},
+ Subject: "Reminder",
+ Body: "I forgot",
+ Headers: net_mail.Header{
+ "In-Reply-To": []string{"epicness"},
+ "List-Id": []string{"spam"},
+ },
+ }},
+ })
+
+ })
+ })
+
+ Convey("errors", func() {
+ Convey("setting a non-email is a panic", func() {
+ So(func() { mail.Testable().SetAdminEmails("i am a banana") },
+ ShouldPanicLike, `invalid email ("i am a banana"): mail: missing phrase`)
+ })
+
+ Convey("sending from a non-user, non-admin is an error", func() {
+ mail.Testable().SetAdminEmails("Friendly <hello@example.com>")
+
+ So(mail.Send(&mailS.Message{
+ Sender: "someone_else@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ }), ShouldErrLike, "invalid Sender: someone_else@example.com")
+ })
+
+ Convey("sending from a bogus address is a problem", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "lalal",
+ }), ShouldErrLike, "unparsable Sender address: lalal: mail: missing phrase")
+ })
+
+ Convey("sending with no recipients is a problem", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ }), ShouldErrLike, "one of To, Cc or Bcc must be non-empty")
+ })
+
+ Convey("bad addresses are a problem", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"wut"},
+ }), ShouldErrLike, `invalid email ("wut"): mail: missing phrase`)
+
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ Cc: []string{"wut"},
+ }), ShouldErrLike, `invalid email ("wut"): mail: missing phrase`)
+
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ Bcc: []string{"wut"},
+ }), ShouldErrLike, `invalid email ("wut"): mail: missing phrase`)
+ })
+
+ Convey("no body is a problem", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"wut@example.com"},
+ }), ShouldErrLike, `one of Body or HTMLBody must be non-empty`)
+ })
+
+ Convey("bad attachments are a problem", func() {
+ So(mail.Send(&mailS.Message{
+ Sender: "admin@example.com",
+ To: []string{"wut@example.com"},
+ Body: "nice thing",
+ Attachments: []mailS.Attachment{
+ {Name: "nice.exe", Data: []byte("boom")},
+ },
+ }), ShouldErrLike, `illegal attachment extension for "nice.exe"`)
+ })
+
+ Convey("bad headers are a problem", func() {
+ So(mail.SendToAdmins(&mailS.Message{
+ Sender: "admin@example.com",
+ Subject: "Reminder",
+ Body: "I forgot",
+ Headers: net_mail.Header{"x-spam-cool": []string{"value"}},
+ }), ShouldErrLike, `disallowed header: x-spam-cool`)
+
+ })
+
+ })
+
+ })
+}
« no previous file with comments | « impl/memory/mail_static_lists.go ('k') | impl/memory/user.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698