Merge pull request #1253 from alphagov/add-multiple-reply-to-email-endpoints

Add multiple reply to email endpoints
This commit is contained in:
Katie Smith
2017-09-14 13:30:45 +01:00
committed by GitHub
6 changed files with 122 additions and 23 deletions

View File

@@ -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
@@ -25,8 +28,9 @@ def test_create_or_update_email_reply_to_updates_existing_entry(notify_db_sessio
reply_to = dao_get_reply_to_by_service_id(service.id)
assert reply_to.service.id == service.id
assert reply_to.email_address == 'different@mail.com'
assert len(reply_to) == 1
assert reply_to[0].service.id == service.id
assert reply_to[0].email_address == 'different@mail.com'
def test_create_or_update_email_reply_to_creates_new_entry(notify_db_session):
@@ -37,5 +41,27 @@ def test_create_or_update_email_reply_to_creates_new_entry(notify_db_session):
reply_to = dao_get_reply_to_by_service_id(service.id)
assert ServiceEmailReplyTo.query.count() == 1
assert reply_to.service.id == service.id
assert reply_to.email_address == 'test@mail.com'
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

View File

@@ -333,11 +333,13 @@ def create_monthly_billing_entry(
def create_reply_to_email(
service,
email_address
email_address,
is_default=True
):
data = {
'service': service,
'email_address': email_address,
'is_default': is_default,
}
reply_to = ServiceEmailReplyTo(**data)

View File

@@ -25,7 +25,7 @@ from tests.app.conftest import (
sample_notification_history as create_notification_history,
sample_notification_with_job
)
from tests.app.db import create_template, create_service_inbound_api, create_notification
from tests.app.db import create_template, create_service_inbound_api, create_notification, create_reply_to_email
from tests.app.db import create_user
@@ -2148,3 +2148,53 @@ def test_update_service_reply_to_email_address_upserts_email_reply_to(admin_requ
assert service_reply_to_emails[0].email_address == 'new@mail.com'
assert service_reply_to_emails[0].is_default
assert response['data']['reply_to_email_address'] == 'new@mail.com'
def test_get_email_reply_to_addresses_when_there_are_no_reply_to_email_addresses(client, sample_service):
response = client.get('/service/{}/email-reply-to'.format(sample_service.id),
headers=[create_authorization_header()])
assert json.loads(response.get_data(as_text=True)) == []
assert response.status_code == 200
def test_get_email_reply_to_addresses_with_one_email_address(client, notify_db, notify_db_session):
service = create_service(notify_db=notify_db, notify_db_session=notify_db_session)
reply_to = create_reply_to_email(service, 'test@mail.com')
service.reply_to_email_address = 'test@mail.com'
response = client.get('/service/{}/email-reply-to'.format(service.id),
headers=[create_authorization_header()])
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response) == 1
assert json_response[0]['email_address'] == 'test@mail.com'
assert json_response[0]['is_default']
assert json_response[0]['created_at']
assert not json_response[0]['updated_at']
assert response.status_code == 200
def test_get_email_reply_to_addresses_with_multiple_email_addresses(client, notify_db, notify_db_session):
service = create_service(notify_db=notify_db, notify_db_session=notify_db_session)
reply_to_a = create_reply_to_email(service, 'test_a@mail.com')
reply_to_b = create_reply_to_email(service, 'test_b@mail.com', False)
service.reply_to_email_address = 'test_a@mail.com'
response = client.get('/service/{}/email-reply-to'.format(service.id),
headers=[create_authorization_header()])
json_response = json.loads(response.get_data(as_text=True))
assert len(json_response) == 2
assert response.status_code == 200
assert json_response[0]['email_address'] == 'test_a@mail.com'
assert json_response[0]['is_default']
assert json_response[0]['created_at']
assert not json_response[0]['updated_at']
assert json_response[1]['email_address'] == 'test_b@mail.com'
assert not json_response[1]['is_default']
assert json_response[1]['created_at']
assert not json_response[1]['updated_at']