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

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()