1import email
2
3import boto
4from boto.exception import BotoServerError
5
6import sure  # noqa # pylint: disable=unused-import
7
8from moto import mock_ses_deprecated
9
10
11# Has boto3 equivalent
12@mock_ses_deprecated
13def test_verify_email_identity():
14    conn = boto.connect_ses("the_key", "the_secret")
15    conn.verify_email_identity("test@example.com")
16
17    identities = conn.list_identities()
18    address = identities["ListIdentitiesResponse"]["ListIdentitiesResult"][
19        "Identities"
20    ][0]
21    address.should.equal("test@example.com")
22
23
24# Has boto3 equivalent
25@mock_ses_deprecated
26def test_domain_verify():
27    conn = boto.connect_ses("the_key", "the_secret")
28
29    conn.verify_domain_dkim("domain1.com")
30    conn.verify_domain_identity("domain2.com")
31
32    identities = conn.list_identities()
33    domains = list(
34        identities["ListIdentitiesResponse"]["ListIdentitiesResult"]["Identities"]
35    )
36    domains.should.equal(["domain1.com", "domain2.com"])
37
38
39# Has boto3 equivalent
40@mock_ses_deprecated
41def test_delete_identity():
42    conn = boto.connect_ses("the_key", "the_secret")
43    conn.verify_email_identity("test@example.com")
44
45    conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][
46        "Identities"
47    ].should.have.length_of(1)
48    conn.delete_identity("test@example.com")
49    conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][
50        "Identities"
51    ].should.have.length_of(0)
52
53
54# Has boto3 equivalent
55@mock_ses_deprecated
56def test_send_email():
57    conn = boto.connect_ses("the_key", "the_secret")
58
59    conn.send_email.when.called_with(
60        "test@example.com", "test subject", "test body", "test_to@example.com"
61    ).should.throw(BotoServerError)
62
63    conn.verify_email_identity("test@example.com")
64    conn.send_email(
65        "test@example.com", "test subject", "test body", "test_to@example.com"
66    )
67
68    send_quota = conn.get_send_quota()
69    sent_count = int(
70        send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
71    )
72    sent_count.should.equal(1)
73
74
75# Has boto3 equivalent
76@mock_ses_deprecated
77def test_send_html_email():
78    conn = boto.connect_ses("the_key", "the_secret")
79
80    conn.send_email.when.called_with(
81        "test@example.com",
82        "test subject",
83        "<span>test body</span>",
84        "test_to@example.com",
85        format="html",
86    ).should.throw(BotoServerError)
87
88    conn.verify_email_identity("test@example.com")
89    conn.send_email(
90        "test@example.com",
91        "test subject",
92        "<span>test body</span>",
93        "test_to@example.com",
94        format="html",
95    )
96
97    send_quota = conn.get_send_quota()
98    sent_count = int(
99        send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
100    )
101    sent_count.should.equal(1)
102
103
104# Has boto3 equivalent
105@mock_ses_deprecated
106def test_send_raw_email():
107    conn = boto.connect_ses("the_key", "the_secret")
108
109    message = email.mime.multipart.MIMEMultipart()
110    message["Subject"] = "Test"
111    message["From"] = "test@example.com"
112    message["To"] = "to@example.com"
113
114    # Message body
115    part = email.mime.text.MIMEText("test file attached")
116    message.attach(part)
117
118    # Attachment
119    part = email.mime.text.MIMEText("contents of test file here")
120    part.add_header("Content-Disposition", "attachment; filename=test.txt")
121    message.attach(part)
122
123    conn.send_raw_email.when.called_with(
124        source=message["From"], raw_message=message.as_string()
125    ).should.throw(BotoServerError)
126
127    conn.verify_email_identity("test@example.com")
128    conn.send_raw_email(source=message["From"], raw_message=message.as_string())
129
130    send_quota = conn.get_send_quota()
131    sent_count = int(
132        send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"]
133    )
134    sent_count.should.equal(1)
135
136
137# Has boto3 equivalent
138@mock_ses_deprecated
139def test_get_send_statistics():
140    conn = boto.connect_ses("the_key", "the_secret")
141
142    conn.send_email.when.called_with(
143        "test@example.com",
144        "test subject",
145        "<span>test body</span>",
146        "test_to@example.com",
147        format="html",
148    ).should.throw(BotoServerError)
149
150    # tests to verify rejects in get_send_statistics
151    result = conn.get_send_statistics()
152
153    reject_count = int(
154        result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
155            "SendDataPoints"
156        ][0]["Rejects"]
157    )
158    delivery_count = int(
159        result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
160            "SendDataPoints"
161        ][0]["DeliveryAttempts"]
162    )
163    reject_count.should.equal(1)
164    delivery_count.should.equal(0)
165
166    conn.verify_email_identity("test@example.com")
167    conn.send_email(
168        "test@example.com", "test subject", "test body", "test_to@example.com"
169    )
170
171    # tests to delivery attempts in get_send_statistics
172    result = conn.get_send_statistics()
173
174    reject_count = int(
175        result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
176            "SendDataPoints"
177        ][0]["Rejects"]
178    )
179    delivery_count = int(
180        result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][
181            "SendDataPoints"
182        ][0]["DeliveryAttempts"]
183    )
184    reject_count.should.equal(1)
185    delivery_count.should.equal(1)
186