update SES backend to support domain identities and multiple recipients

includes test cases for ses client with boto3
This commit is contained in:
Jonathan Stewmon 2016-03-08 23:05:17 -06:00
commit fb06c6517e
5 changed files with 239 additions and 43 deletions

10
moto/ses/exceptions.py Normal file
View file

@ -0,0 +1,10 @@
from __future__ import unicode_literals
from moto.core.exceptions import RESTError
class MessageRejectedError(RESTError):
code = 400
def __init__(self, message):
super(MessageRejectedError, self).__init__(
"MessageRejected", message)

View file

@ -1,70 +1,96 @@
from __future__ import unicode_literals
import email
from moto.core import BaseBackend
from .exceptions import MessageRejectedError
from .utils import get_random_message_id
RECIPIENT_LIMIT = 50
class Message(object):
def __init__(self, message_id, source, subject, body, destination):
def __init__(self, message_id):
self.id = message_id
self.source = source
self.subject = subject
self.body = body
self.destination = destination
class RawMessage(object):
def __init__(self, message_id, source, destination, raw_data):
def __init__(self, message_id):
self.id = message_id
self.source = source
self.destination = destination
self.raw_data = raw_data
class SESQuota(object):
def __init__(self, messages):
self.messages = messages
def __init__(self, sent):
self.sent = sent
@property
def sent_past_24(self):
return len(self.messages)
return self.sent
class SESBackend(BaseBackend):
def __init__(self):
self.addresses = []
self.sent_messages = []
self.domains = []
self.sent_message_count = 0
def _is_verified_address(self, address):
if address in self.addresses:
return True
user, host = address.split('@', 1)
return host in self.domains
def verify_email_identity(self, address):
self.addresses.append(address)
def verify_domain(self, domain):
self.addresses.append(domain)
self.domains.append(domain)
def list_identities(self):
return self.addresses
return self.domains + self.addresses
def delete_identity(self, identity):
self.addresses.remove(identity)
if '@' in identity:
self.addresses.remove(identity)
else:
self.domains.remove(identity)
def send_email(self, source, subject, body, destination):
if source not in self.addresses:
return False
def send_email(self, source, subject, body, destinations):
recipient_count = sum(map(len, destinations.values()))
if recipient_count > RECIPIENT_LIMIT:
raise MessageRejectedError('Too many recipients.')
if not self._is_verified_address(source):
raise MessageRejectedError(
"Email address not verified %s" % source
)
message_id = get_random_message_id()
message = Message(message_id, source, subject, body, destination)
self.sent_messages.append(message)
message = Message(message_id)
self.sent_message_count += recipient_count
return message
def send_raw_email(self, source, destination, raw_data):
def send_raw_email(self, source, destinations, raw_data):
if source not in self.addresses:
return False
raise MessageRejectedError(
"Did not have authority to send from email %s" % source
)
recipient_count = len(destinations)
message = email.message_from_string(raw_data)
for header in 'TO', 'CC', 'BCC':
recipient_count += sum(
d.strip() and 1 or 0
for d in message.get(header, '').split(',')
)
if recipient_count > RECIPIENT_LIMIT:
raise MessageRejectedError('Too many recipients.')
self.sent_message_count += recipient_count
message_id = get_random_message_id()
message = RawMessage(message_id, source, destination, raw_data)
self.sent_messages.append(message)
return message
return RawMessage(message_id)
def get_send_quota(self):
return SESQuota(self.sent_messages)
return SESQuota(self.sent_message_count)
ses_backend = SESBackend()

View file

@ -1,4 +1,7 @@
from __future__ import unicode_literals
import base64
import six
from moto.core.responses import BaseResponse
from .models import ses_backend
@ -26,7 +29,7 @@ class EmailResponse(BaseResponse):
def verify_domain_identity(self):
domain = self.querystring.get('Domain')[0]
ses_backend.verify_domain(domain)
template = self.response_template(VERIFY_DOMAIN_DKIM_RESPONSE)
template = self.response_template(VERIFY_DOMAIN_IDENTITY_RESPONSE)
return template.render()
def delete_identity(self):
@ -42,21 +45,40 @@ class EmailResponse(BaseResponse):
body = self.querystring.get(bodydatakey)[0]
source = self.querystring.get('Source')[0]
subject = self.querystring.get('Message.Subject.Data')[0]
destination = self.querystring.get('Destination.ToAddresses.member.1')[0]
message = ses_backend.send_email(source, subject, body, destination)
if not message:
return "Did not have authority to send from email {0}".format(source), dict(status=400)
destinations = {
'ToAddresses': [],
'CcAddresses': [],
'BccAddresses': [],
}
for dest_type in destinations:
# consume up to 51 to allow exception
for i in six.moves.range(1, 52):
field = 'Destination.%s.member.%s' % (dest_type, i)
address = self.querystring.get(field)
if address is None:
break
destinations[dest_type].append(address[0])
message = ses_backend.send_email(source, subject, body, destinations)
template = self.response_template(SEND_EMAIL_RESPONSE)
return template.render(message=message)
def send_raw_email(self):
source = self.querystring.get('Source')[0]
destination = self.querystring.get('Destinations.member.1')[0]
raw_data = self.querystring.get('RawMessage.Data')[0]
raw_data = base64.b64decode(raw_data)
if six.PY3:
raw_data = raw_data.decode('utf-8')
destinations = []
# consume up to 51 to allow exception
for i in six.moves.range(1, 52):
field = 'Destinations.member.%s' % i
address = self.querystring.get(field)
if address is None:
break
destinations.append(address[0])
message = ses_backend.send_raw_email(source, destination, raw_data)
if not message:
return "Did not have authority to send from email {0}".format(source), dict(status=400)
message = ses_backend.send_raw_email(source, destinations, raw_data)
template = self.response_template(SEND_RAW_EMAIL_RESPONSE)
return template.render(message=message)
@ -99,6 +121,16 @@ VERIFY_DOMAIN_DKIM_RESPONSE = """<VerifyDomainDkimResponse xmlns="http://ses.ama
</ResponseMetadata>
</VerifyDomainDkimResponse>"""
VERIFY_DOMAIN_IDENTITY_RESPONSE = """\
<VerifyDomainIdentityResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<VerifyDomainIdentityResult>
<VerificationToken>QTKknzFg2J4ygwa+XvHAxUl1hyHoY0gVfZdfjIedHZ0=</VerificationToken>
</VerifyDomainIdentityResult>
<ResponseMetadata>
<RequestId>94f6368e-9bf2-11e1-8ee7-c98a0037a2b6</RequestId>
</ResponseMetadata>
</VerifyDomainIdentityResponse>"""
DELETE_IDENTITY_RESPONSE = """<DeleteIdentityResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<DeleteIdentityResult/>
<ResponseMetadata>