diff --git a/src/email_relay/models.py b/src/email_relay/models.py index fb287d0..1520310 100644 --- a/src/email_relay/models.py +++ b/src/email_relay/models.py @@ -77,7 +77,7 @@ class Meta: def __str__(self): try: - return f'{self.created_at} "{self.data["subject"]}" to {", ".join(self.data["to"])}' + return f'{self.created_at} "{self.data["subject"]}" to {", ".join(self.data["recipient_list"])}' except Exception: return f"{self.created_at} " diff --git a/tests/test_models.py b/tests/test_models.py index 84af117..5afee6e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -129,6 +129,7 @@ def data(self): return { "subject": "Test", "message": "Test", + "html_message": "

Test

", "from_email": "from@example.com", "recipient_list": ["to@example.com"], } @@ -142,8 +143,12 @@ def email(self): to=["to@example.com"], ) - def test_create_message(self, data): - message = Message.objects.create(data=data) + @pytest.fixture + def queued_message(self, data): + return baker.make("email_relay.Message", data=data, status=Status.QUEUED) + + def test_create(self, data): + message = baker.make("email_relay.Message", data=data) assert message.data == data assert message.priority == Priority.LOW @@ -152,6 +157,46 @@ def test_create_message(self, data): assert message.log == "" assert message.sent_at is None + def test_str(self, data): + message = baker.make("email_relay.Message", data=data) + + assert data["subject"] in str(message) + + def test_str_invalid_data(self): + message = baker.make("email_relay.Message", data={}) + + assert "invalid message" in str(message) + + def test_update_with_update_fields(self, data): + message = baker.make("email_relay.Message", data=data) + updated_at_original = message.updated_at + + message.retry_count = 1 + message.save(update_fields=["retry_count"]) + + assert message.updated_at != updated_at_original + + def test_mark_sent(self, queued_message): + queued_message.mark_sent() + + assert queued_message.status == Status.SENT + + def test_defer(self, queued_message): + queued_message.defer() + + assert queued_message.status == Status.DEFERRED + + def test_fail(self, queued_message): + queued_message.fail() + + assert queued_message.status == Status.FAILED + + def test_no_data(self): + message = baker.make("email_relay.Message", data={}) + + assert message.data == {} + assert message.email is None + def test_email_property(self, data): message = Message.objects.create(data=data)