diff --git a/app/dao/service_email_reply_to_dao.py b/app/dao/service_email_reply_to_dao.py index bda9506a2..3066c229d 100644 --- a/app/dao/service_email_reply_to_dao.py +++ b/app/dao/service_email_reply_to_dao.py @@ -1,28 +1,9 @@ from app import db from app.dao.dao_utils import transactional +from app.errors import InvalidRequest from app.models import ServiceEmailReplyTo -def create_or_update_email_reply_to(service_id, email_address): - reply_to = dao_get_reply_to_by_service_id(service_id) - if len(reply_to) == 0: - reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address) - dao_create_reply_to_email_address(reply_to) - elif len(reply_to) == 1: - reply_to[0].email_address = email_address - dao_update_reply_to_email(reply_to[0]) - else: - raise InvalidRequest( - "Multiple reply to email addresses were found, this method should not be used.", - status_code=500 - ) - - -@transactional -def dao_create_reply_to_email_address(reply_to_email): - db.session.add(reply_to_email) - - def dao_get_reply_to_by_service_id(service_id): reply_to = db.session.query( ServiceEmailReplyTo @@ -32,6 +13,27 @@ def dao_get_reply_to_by_service_id(service_id): return reply_to +def create_or_update_email_reply_to(service_id, email_address): + reply_to = dao_get_reply_to_by_service_id(service_id) + if len(reply_to) == 0: + reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address) + dao_create_reply_to_email_address(reply_to) + elif len(reply_to) == 1: + reply_to[0].email_address = email_address + dao_update_reply_to_email(reply_to[0]) + else: + # Once we move allowing multiple email address this methods will be removed + raise InvalidRequest( + "Multiple reply to email addresses were found, this method should not be used.", + status_code=500 + ) + + +@transactional +def dao_create_reply_to_email_address(reply_to_email): + db.session.add(reply_to_email) + + @transactional def dao_update_reply_to_email(reply_to): db.session.add(reply_to) diff --git a/tests/app/dao/test_service_email_reply_to_dao.py b/tests/app/dao/test_service_email_reply_to_dao.py index aac026379..58ba416a8 100644 --- a/tests/app/dao/test_service_email_reply_to_dao.py +++ b/tests/app/dao/test_service_email_reply_to_dao.py @@ -1,7 +1,10 @@ +import pytest + from app.dao.service_email_reply_to_dao import ( create_or_update_email_reply_to, dao_get_reply_to_by_service_id ) +from app.errors import InvalidRequest from app.models import ServiceEmailReplyTo from tests.app.db import create_reply_to_email, create_service @@ -40,3 +43,25 @@ def test_create_or_update_email_reply_to_creates_new_entry(notify_db_session): assert ServiceEmailReplyTo.query.count() == 1 assert reply_to[0].service.id == service.id assert reply_to[0].email_address == 'test@mail.com' + + +def test_create_or_update_email_reply_to_raises_exception_if_multilple_email_addresses_exist(notify_db_session): + service = create_service() + create_reply_to_email(service=service, email_address='something@email.com') + create_reply_to_email(service=service, email_address='another@email.com', is_default=False) + + with pytest.raises(expected_exception=InvalidRequest) as e: + create_or_update_email_reply_to(service_id=service.id, email_address='third@email.com') + assert e.value.message == "Multiple reply to email addresses were found, this method should not be used." + + +def test_dao_get_reply_to_by_service_id(notify_db_session): + service = create_service() + default_reply_to = create_reply_to_email(service=service, email_address='something@email.com') + another_reply_to = create_reply_to_email(service=service, email_address='another@email.com', is_default=False) + + results = dao_get_reply_to_by_service_id(service_id=service.id) + + assert len(results) == 2 + assert default_reply_to in results + assert another_reply_to in results