File: ses_test.go

package info (click to toggle)
golang-github-docker-goamz 0.0~git20160206.0.f0a21f5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,580 kB
  • sloc: makefile: 66
file content (163 lines) | stat: -rw-r--r-- 5,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ses_test

import (
	"encoding/base64"
	"testing"

	"github.com/docker/goamz/aws"
	"github.com/docker/goamz/exp/ses"
	"github.com/docker/goamz/testutil"
	"gopkg.in/check.v1"
)

func Test(t *testing.T) {
	check.TestingT(t)
}

var _ = check.Suite(&S{})
var testServer = testutil.NewHTTPServer()

type S struct {
	sesService *ses.SES
}

func (s *S) SetUpSuite(c *check.C) {
	testServer.Start()
	auth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
	sesService := ses.New(auth, aws.Region{SESEndpoint: testServer.URL})
	s.sesService = sesService
}

func (s *S) TearDownTest(c *check.C) {
	testServer.Flush()
}

func (s *S) TestSendEmailError(c *check.C) {
	testServer.Response(400, nil, TestSendEmailError)

	resp, err := s.sesService.SendEmail("foo@example.com",
		ses.NewDestination([]string{"unauthorized@example.com"}, []string{}, []string{}),
		ses.NewMessage("subject", "textBody", "htmlBody"))
	_ = testServer.WaitRequest()

	c.Assert(resp, check.IsNil)
	c.Assert(err.Error(), check.Equals, "Email address is not verified. (MessageRejected)")
}

func (s *S) TestSendEmail(c *check.C) {
	testServer.Response(200, nil, TestSendEmailOk)

	resp, err := s.sesService.SendEmail("foo@example.com",
		ses.NewDestination([]string{"to1@example.com", "to2@example.com"},
			[]string{"cc1@example.com", "cc2@example.com"},
			[]string{"bcc1@example.com", "bcc2@example.com"}),
		ses.NewMessage("subject", "textBody", "htmlBody"))
	req := testServer.WaitRequest()

	c.Assert(req.Method, check.Equals, "POST")
	c.Assert(req.URL.Path, check.Equals, "/")
	c.Assert(req.Header["Date"], check.Not(check.Equals), "")
	c.Assert(req.FormValue("Source"), check.Equals, "foo@example.com")
	c.Assert(req.FormValue("Destination.ToAddresses.member.1"), check.Equals, "to1@example.com")
	c.Assert(req.FormValue("Destination.ToAddresses.member.2"), check.Equals, "to2@example.com")
	c.Assert(req.FormValue("Destination.CcAddresses.member.1"), check.Equals, "cc1@example.com")
	c.Assert(req.FormValue("Destination.CcAddresses.member.2"), check.Equals, "cc2@example.com")
	c.Assert(req.FormValue("Destination.BccAddresses.member.1"), check.Equals, "bcc1@example.com")
	c.Assert(req.FormValue("Destination.BccAddresses.member.2"), check.Equals, "bcc2@example.com")

	c.Assert(req.FormValue("Message.Subject.Data"), check.Equals, "subject")
	c.Assert(req.FormValue("Message.Subject.Charset"), check.Equals, "utf-8")

	c.Assert(req.FormValue("Message.Body.Text.Data"), check.Equals, "textBody")
	c.Assert(req.FormValue("Message.Body.Text.Charset"), check.Equals, "utf-8")

	c.Assert(req.FormValue("Message.Body.Html.Data"), check.Equals, "htmlBody")
	c.Assert(req.FormValue("Message.Body.Html.Charset"), check.Equals, "utf-8")

	c.Assert(err, check.IsNil)
	c.Assert(resp.SendEmailResult, check.NotNil)
	c.Assert(resp.ResponseMetadata, check.NotNil)
}

func (s *S) TestSendRawEmailError(c *check.C) {
	testServer.Response(400, nil, TestSendEmailError)

	resp, err := s.sesService.SendRawEmail(nil, rawMessage)
	_ = testServer.WaitRequest()

	c.Assert(resp, check.IsNil)
	c.Assert(err.Error(), check.Equals, "Email address is not verified. (MessageRejected)")
}

func (s *S) TestSendRawEmailNoDestinations(c *check.C) {
	testServer.Response(200, nil, TestSendRawEmailOk)

	resp, err := s.sesService.SendRawEmail(nil, rawMessage)
	req := testServer.WaitRequest()

	c.Assert(req.Method, check.Equals, "POST")
	c.Assert(req.URL.Path, check.Equals, "/")
	c.Assert(req.Header["Date"], check.Not(check.Equals), "")
	c.Assert(req.FormValue("Source"), check.Equals, "")

	c.Assert(req.FormValue("RawMessage.Data"), check.Equals,
		base64.StdEncoding.EncodeToString(rawMessage))

	c.Assert(err, check.IsNil)
	c.Assert(resp.SendRawEmailResult, check.NotNil)
	c.Assert(resp.ResponseMetadata, check.NotNil)
}

func (s *S) TestSendRawEmailWithDestinations(c *check.C) {
	testServer.Response(200, nil, TestSendRawEmailOk)

	resp, err := s.sesService.SendRawEmail([]string{
		"to1@example.com",
		"cc2@example.com",
		"bcc1@example.com",
		"other@example.com",
	}, rawMessage)
	req := testServer.WaitRequest()

	c.Assert(req.Method, check.Equals, "POST")
	c.Assert(req.URL.Path, check.Equals, "/")
	c.Assert(req.Header["Date"], check.Not(check.Equals), "")
	c.Assert(req.FormValue("Source"), check.Equals, "")

	c.Assert(req.FormValue("Destinations.member.1"), check.Equals,
		"to1@example.com")
	c.Assert(req.FormValue("Destinations.member.2"), check.Equals,
		"cc2@example.com")
	c.Assert(req.FormValue("Destinations.member.3"), check.Equals,
		"bcc1@example.com")
	c.Assert(req.FormValue("Destinations.member.4"), check.Equals,
		"other@example.com")
	c.Assert(req.FormValue("RawMessage.Data"), check.Equals,
		base64.StdEncoding.EncodeToString(rawMessage))

	c.Assert(err, check.IsNil)
	c.Assert(resp.SendRawEmailResult, check.NotNil)
	c.Assert(resp.ResponseMetadata, check.NotNil)
}

var rawMessage = []byte(`To: "to1@example.com", "to2@example.com"
Cc: "cc1@example.com", "cc2@example.com"
Bcc: "bcc1@example.com", "bcc2@example.com"
From: foo@example.com
Subject: Test Subject
Content-Type: multipart/alternative; boundary=001a1147f9d0b5b8ce0525380c4b
MIME-Version: 1.0

--001a1147f9d0b5b8ce0525380c4b
Content-Type: text/plain; charset=UTF-8

Text Body

--001a1147f9d0b5b8ce0525380c4b
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<h1>HTML</h1><p>body</p>

--001a1147f9d0b5b8ce0525380c4b--
`)