Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add _email_relay_version to EmailRelayMessage #136

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

## [Unreleased]

### Added

- A `_email_relay_version` field to `RelayEmailData` to track the version of the schema used to serialize the data. This should allow for future changes to the schema to be handled more gracefully.

## [0.3.0]

### Added
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ requires-python = ">=3.8"
dev = [
"bumpver",
"coverage[toml]",
"dirty-equals",
"django-stubs",
"django-stubs-ext",
"faker",
Expand Down
3 changes: 3 additions & 0 deletions src/email_relay/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from django.core.mail import EmailMessage
from django.core.mail import EmailMultiAlternatives

from email_relay import __version__


@dataclass(frozen=True)
class RelayEmailData:
Expand All @@ -23,6 +25,7 @@ class RelayEmailData:
extra_headers: dict[str, str] = field(default_factory=dict)
alternatives: list[tuple[str, str]] = field(default_factory=list)
attachments: list[dict[str, str]] = field(default_factory=list)
_email_relay_version: str = __version__

def to_dict(self) -> dict[str, str]:
return asdict(self)
Expand Down
109 changes: 67 additions & 42 deletions tests/test_email.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from dirty_equals import IsPartialDict
from django.core.mail import EmailMessage
from django.core.mail import EmailMultiAlternatives

from email_relay.email import RelayEmailData
from email_relay.email import __version__


def test_from_email_message():
Expand Down Expand Up @@ -75,18 +77,20 @@ def test_to_dict():

email_dict = RelayEmailData.from_email_message(email_message).to_dict()

assert email_dict == {
"subject": email_message.subject,
"body": email_message.body,
"from_email": email_message.from_email,
"to": email_message.to,
"cc": email_message.cc,
"bcc": email_message.bcc,
"reply_to": email_message.reply_to,
"extra_headers": email_message.extra_headers,
"alternatives": [],
"attachments": [],
}
assert email_dict == IsPartialDict(
**{
"subject": email_message.subject,
"body": email_message.body,
"from_email": email_message.from_email,
"to": email_message.to,
"cc": email_message.cc,
"bcc": email_message.bcc,
"reply_to": email_message.reply_to,
"extra_headers": email_message.extra_headers,
"alternatives": [],
"attachments": [],
}
)


def test_to_dict_multi_alternatives():
Expand All @@ -106,18 +110,20 @@ def test_to_dict_multi_alternatives():

email_dict = RelayEmailData.from_email_message(email_multi_alternatives).to_dict()

assert email_dict == {
"subject": email_multi_alternatives.subject,
"body": email_multi_alternatives.body,
"from_email": email_multi_alternatives.from_email,
"to": email_multi_alternatives.to,
"cc": email_multi_alternatives.cc,
"bcc": email_multi_alternatives.bcc,
"reply_to": email_multi_alternatives.reply_to,
"extra_headers": email_multi_alternatives.extra_headers,
"alternatives": email_multi_alternatives.alternatives,
"attachments": [],
}
assert email_dict == IsPartialDict(
**{
"subject": email_multi_alternatives.subject,
"body": email_multi_alternatives.body,
"from_email": email_multi_alternatives.from_email,
"to": email_multi_alternatives.to,
"cc": email_multi_alternatives.cc,
"bcc": email_multi_alternatives.bcc,
"reply_to": email_multi_alternatives.reply_to,
"extra_headers": email_multi_alternatives.extra_headers,
"alternatives": email_multi_alternatives.alternatives,
"attachments": [],
}
)


def test_to_dict_with_attachment():
Expand All @@ -140,21 +146,40 @@ def test_to_dict_with_attachment():

email_dict = RelayEmailData.from_email_message(email).to_dict()

assert email_dict == {
"subject": email.subject,
"body": email.body,
"from_email": email.from_email,
"to": email.to,
"cc": email.cc,
"bcc": email.bcc,
"reply_to": email.reply_to,
"extra_headers": email.extra_headers,
"alternatives": [],
"attachments": [
{
"filename": "test.txt",
"content": attachment_content.decode(),
"mimetype": "text/plain",
}
],
}
assert email_dict == IsPartialDict(
**{
"subject": email.subject,
"body": email.body,
"from_email": email.from_email,
"to": email.to,
"cc": email.cc,
"bcc": email.bcc,
"reply_to": email.reply_to,
"extra_headers": email.extra_headers,
"alternatives": [],
"attachments": [
{
"filename": "test.txt",
"content": attachment_content.decode(),
"mimetype": "text/plain",
}
],
}
)


def test_email_message_version():
email_message = EmailMessage(
"Subject here",
"Here is the message.",
"from@example.com",
["to@example.com"],
cc=["cc@example.com"],
bcc=["bcc@example.com"],
reply_to=["reply_to@example.com"],
headers={"Test-Header": "Test Value"},
)

relay_email_data = RelayEmailData.from_email_message(email_message)

assert relay_email_data._email_relay_version == __version__
Loading