Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w committed Sep 8, 2019
1 parent 5b13a08 commit 1e18552
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 74 deletions.
28 changes: 6 additions & 22 deletions opwen_email_server/actions.py
Expand Up @@ -4,7 +4,6 @@
from typing import Iterable
from typing import Tuple
from typing import Union
from uuid import uuid4

from libcloud.storage.types import ObjectDoesNotExistError

Expand All @@ -15,18 +14,17 @@
from opwen_email_server.services.storage import AzureObjectsStorage
from opwen_email_server.services.storage import AzureObjectStorage
from opwen_email_server.services.storage import AzureTextStorage
from opwen_email_server.utils.email_parser import format_attachments
from opwen_email_server.utils.email_parser import format_inline_images
from opwen_email_server.utils.email_parser import MimeEmailParser
from opwen_email_server.utils.email_parser import get_domain
from opwen_email_server.utils.email_parser import get_domains
from opwen_email_server.utils.email_parser import parse_mime_email
from opwen_email_server.utils.log import LogMixin
from opwen_email_server.utils.serialization import from_base64
from opwen_email_server.utils.serialization import from_jsonl_bytes
from opwen_email_server.utils.serialization import to_base64
from opwen_email_server.utils.serialization import to_jsonl_bytes
from opwen_email_server.utils.serialization import to_msgpack_bytes
from opwen_email_server.utils.string import is_lowercase
from opwen_email_server.utils.unique import new_client_id
from opwen_email_server.utils.unique import new_email_id

Response = Union[dict, Tuple[str, int]]

Expand Down Expand Up @@ -76,7 +74,7 @@ def __init__(self,
self._raw_email_storage = raw_email_storage
self._email_storage = email_storage
self._pending_factory = pending_factory
self._email_parser = email_parser or self._parse_mime_email
self._email_parser = email_parser or MimeEmailParser()

def _action(self, resource_id): # type: ignore
try:
Expand All @@ -94,7 +92,7 @@ def _action(self, resource_id): # type: ignore
return 'OK', 200

def _store_inbound_email(self, email: dict):
email_id = self._to_id(email)
email_id = new_email_id(email)
email['_uid'] = email_id

self._email_storage.store_object(email_id, email)
Expand All @@ -103,16 +101,6 @@ def _store_inbound_email(self, email: dict):
pending_storage = self._pending_factory(domain)
pending_storage.store_text(email_id, 'pending')

def _parse_mime_email(self, mime_email: str) -> dict:
email = parse_mime_email(mime_email)
email = format_attachments(email)
email = format_inline_images(email, self.log_warning)
return email

@classmethod
def _to_id(cls, email: dict) -> str:
return sha256(to_msgpack_bytes(email)).hexdigest()


class StoreWrittenClientEmails(_Action):
def __init__(self, client_storage: AzureObjectsStorage, email_storage: AzureObjectStorage,
Expand Down Expand Up @@ -273,7 +261,7 @@ def __init__(self,
self._client_storage = client_storage
self._setup_mailbox = setup_mailbox
self._setup_mx_records = setup_mx_records
self._client_id_source = client_id_source or self._new_client_id
self._client_id_source = client_id_source or new_client_id

def _action(self, client, **auth_args): # type: ignore
domain = client['domain']
Expand All @@ -298,10 +286,6 @@ def _action(self, client, **auth_args): # type: ignore
'resource_container': access_info.container,
}

@classmethod
def _new_client_id(cls) -> str:
return str(uuid4())


class CalculatePendingEmailsMetric(_Action):
def __init__(self, auth: AzureAuth, pending_factory: Callable[[str], AzureTextStorage]):
Expand Down
9 changes: 2 additions & 7 deletions opwen_email_server/services/storage.py
Expand Up @@ -8,7 +8,6 @@
from typing import Iterator
from typing import Optional
from typing import Tuple
from uuid import uuid4

from cached_property import cached_property
from libcloud.storage.base import Container
Expand All @@ -29,6 +28,7 @@
from opwen_email_server.utils.serialization import to_msgpack_bytes
from opwen_email_server.utils.temporary import create_tempfilename
from opwen_email_server.utils.temporary import removing
from opwen_email_server.utils.unique import new_resource_id

AccessInfo = namedtuple('AccessInfo', ['account', 'key', 'container'])

Expand Down Expand Up @@ -163,9 +163,8 @@ class AzureObjectsStorage(LogMixin):
_compression_level = 20

def __init__(self, file_storage: AzureFileStorage, resource_id_source: Callable[[], str] = None):

self._file_storage = file_storage
self._resource_id_source = resource_id_source or self._new_resource_id
self._resource_id_source = resource_id_source or new_resource_id

def _open_archive_file(self, archive: TarFile, name: str) -> IO[bytes]:
while True:
Expand Down Expand Up @@ -254,10 +253,6 @@ def fetch_objects(self, resource_id: str, download: Download) -> Iterable[dict]:
def delete(self, resource_id: str):
self._file_storage.delete(resource_id)

@classmethod
def _new_resource_id(cls) -> str:
return str(uuid4())


class AzureObjectStorage(_AzureBytesStorage):
_extension = 'msgpack'
Expand Down
9 changes: 9 additions & 0 deletions opwen_email_server/utils/email_parser.py
Expand Up @@ -21,6 +21,7 @@

from opwen_email_server.config import MAX_HEIGHT_IMAGES
from opwen_email_server.config import MAX_WIDTH_IMAGES
from opwen_email_server.utils.log import LogMixin
from opwen_email_server.utils.serialization import to_base64


Expand Down Expand Up @@ -217,3 +218,11 @@ def format_inline_images(email: dict, on_error: Callable) -> dict:
new_email = dict(email)
new_email['body'] = str(soup)
return new_email


class MimeEmailParser(LogMixin):
def __call__(self, mime_email: str) -> dict:
email = parse_mime_email(mime_email)
email = format_attachments(email)
email = format_inline_images(email, self.log_warning)
return email
8 changes: 2 additions & 6 deletions opwen_email_server/utils/log.py
Expand Up @@ -30,11 +30,7 @@

@singleton
def _create_telemetry_channel() -> TelemetryChannel:
if not APPINSIGHTS_KEY:
sender = NullSender()
else:
sender = AsynchronousSender(APPINSIGHTS_HOST)

sender = AsynchronousSender(APPINSIGHTS_HOST) if APPINSIGHTS_HOST else NullSender()
queue = AsynchronousQueue(sender)
return TelemetryChannel(queue=queue)

Expand Down Expand Up @@ -97,5 +93,5 @@ def _log(self, level: int, log_message: str, log_args: Iterable[Any]):
self._telemetry_client.track_trace(message % tuple(args), severity=getLevelName(level))

def log_event(self, event_name: str, properties: Optional[dict] = None):
self._logger.info('%s%s%s', event_name, SEPARATOR, properties)
self.log_info('%s%s%s', event_name, SEPARATOR, properties)
self._telemetry_client.track_event(event_name, properties)
16 changes: 16 additions & 0 deletions opwen_email_server/utils/unique.py
@@ -0,0 +1,16 @@
from hashlib import sha256
from uuid import uuid4

from opwen_email_server.utils.serialization import to_msgpack_bytes


def new_email_id(email: dict) -> str:
return sha256(to_msgpack_bytes(email)).hexdigest()


def new_client_id() -> str:
return str(uuid4())


def new_resource_id() -> str:
return str(uuid4())
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -3,6 +3,7 @@ source =
opwen_email_server/

omit =
opwen_email_server/constants/*
opwen_email_server/integration/*

[coverage:report]
Expand Down
9 changes: 9 additions & 0 deletions tests/opwen_email_server/services/test_storage.py
Expand Up @@ -203,6 +203,15 @@ def test_does_not_create_file_without_objects(self):
self.assertIsNone(resource_id)
self.assertContainerHasNumFiles(0)

def test_deletes_objects(self):
name = 'file'
objs = [{'foo': 'bar'}, {'baz': [1, 2, 3]}]

resource_id = self._storage.store_objects((name, objs, to_jsonl_bytes))
self._storage.delete(resource_id)

self.assertContainerHasNumFiles(0)

def assertContainerHasNumFiles(self, count: int, suffix: str = ''):
container_files = listdir(join(self._folder, self._container))
matches = [entry for entry in container_files if entry.endswith(suffix)]
Expand Down

0 comments on commit 1e18552

Please sign in to comment.