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

Add multiple reply to email endpoints
This commit is contained in:
Rebecca Law
2017-09-20 13:26:02 +01:00
committed by GitHub
6 changed files with 313 additions and 8 deletions

View File

@@ -2,8 +2,8 @@ import pytest
from app.dao.service_email_reply_to_dao import (
create_or_update_email_reply_to,
dao_get_reply_to_by_service_id
)
dao_get_reply_to_by_service_id,
add_reply_to_email_address_for_service, update_reply_to_email_address)
from app.errors import InvalidRequest
from app.models import ServiceEmailReplyTo
from tests.app.db import create_reply_to_email, create_service
@@ -65,3 +65,129 @@ def test_dao_get_reply_to_by_service_id(notify_db_session):
assert len(results) == 2
assert default_reply_to in results
assert another_reply_to in results
def test_add_reply_to_email_address_for_service_creates_first_email_for_service(notify_db_session):
service = create_service()
add_reply_to_email_address_for_service(service_id=service.id,
email_address='new@address.com',
is_default=True)
results = dao_get_reply_to_by_service_id(service_id=service.id)
assert len(results) == 1
assert results[0].email_address == 'new@address.com'
assert results[0].is_default
def test_add_reply_to_email_address_for_service_creates_another_email_for_service(notify_db_session):
service = create_service()
first_reply_to = create_reply_to_email(service=service, email_address="first@address.com")
add_reply_to_email_address_for_service(service_id=service.id, email_address='second@address.com', is_default=False)
results = dao_get_reply_to_by_service_id(service_id=service.id)
assert len(results) == 2
for x in results:
if x.email_address == 'first@address.com':
assert x.is_default
elif x.email_address == 'second@address.com':
assert not x.is_default
else:
assert False
def test_add_reply_to_email_address_new_reply_to_is_default_existing_reply_to_is_not(notify_db_session):
service = create_service()
first_reply_to = create_reply_to_email(service=service, email_address="first@address.com", is_default=True)
add_reply_to_email_address_for_service(service_id=service.id, email_address='second@address.com', is_default=True)
results = dao_get_reply_to_by_service_id(service_id=service.id)
assert len(results) == 2
for x in results:
if x.email_address == 'first@address.com':
assert not x.is_default
elif x.email_address == 'second@address.com':
assert x.is_default
else:
assert False
def test_add_reply_to_email_address_can_add_a_third_reply_to_address(sample_service):
add_reply_to_email_address_for_service(service_id=sample_service.id,
email_address="first@address.com",
is_default=True)
add_reply_to_email_address_for_service(service_id=sample_service.id, email_address='second@address.com',
is_default=False)
add_reply_to_email_address_for_service(service_id=sample_service.id, email_address='third@address.com',
is_default=False)
results = dao_get_reply_to_by_service_id(service_id=sample_service.id)
assert len(results) == 3
for x in results:
if x.email_address == 'first@address.com':
assert x.is_default
elif x.email_address == 'second@address.com':
assert not x.is_default
elif x.email_address == 'third@address.com':
assert not x.is_default
else:
assert False
def test_add_reply_to_email_address_ensures_first_reply_to_is_default(sample_service):
with pytest.raises(expected_exception=InvalidRequest):
add_reply_to_email_address_for_service(service_id=sample_service.id,
email_address="first@address.com", is_default=False)
def test_add_reply_to_email_address_ensure_there_is_not_more_than_one_default(sample_service):
create_reply_to_email(service=sample_service, email_address='first@email.com', is_default=True)
create_reply_to_email(service=sample_service, email_address='second@email.com', is_default=True)
with pytest.raises(Exception):
add_reply_to_email_address_for_service(service_id=sample_service.id,
email_address='third_email@address.com',
is_default=False)
def test_update_reply_to_email_address(sample_service):
first_reply_to = create_reply_to_email(service=sample_service, email_address="first@address.com")
update_reply_to_email_address(service_id=sample_service.id, reply_to_id=first_reply_to.id,
email_address='change_address@email.com',
is_default=True)
updated_reply_to = ServiceEmailReplyTo.query.get(first_reply_to.id)
assert updated_reply_to.email_address == 'change_address@email.com'
assert updated_reply_to.updated_at
assert updated_reply_to.is_default
def test_update_reply_to_email_address_set_updated_to_default(sample_service):
first_reply_to = create_reply_to_email(service=sample_service, email_address="first@address.com")
second_reply_to = create_reply_to_email(service=sample_service,
email_address="second@address.com",
is_default=False)
update_reply_to_email_address(service_id=sample_service.id,
reply_to_id=second_reply_to.id,
email_address='change_address@email.com',
is_default=True)
results = ServiceEmailReplyTo.query.all()
assert len(results) == 2
for x in results:
if x.email_address == 'change_address@email.com':
assert x.is_default
elif x.email_address == 'first@address.com':
assert not x.is_default
else:
assert False
def test_update_reply_to_email_address_raises_exception_if_single_reply_to_and_setting_default_to_false(sample_service):
first_reply_to = create_reply_to_email(service=sample_service, email_address="first@address.com")
with pytest.raises(expected_exception=InvalidRequest):
update_reply_to_email_address(service_id=sample_service.id,
reply_to_id=first_reply_to.id,
email_address='should@fail.com',
is_default=False)

View File

@@ -2180,8 +2180,6 @@ def test_get_email_reply_to_addresses_with_multiple_email_addresses(client, noti
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))
@@ -2198,3 +2196,94 @@ def test_get_email_reply_to_addresses_with_multiple_email_addresses(client, noti
assert not json_response[1]['is_default']
assert json_response[1]['created_at']
assert not json_response[1]['updated_at']
def test_add_service_reply_to_email_address(client, sample_service):
data = json.dumps({"email_address": "new@reply.com", "is_default": True})
response = client.post('/service/{}/email-reply-to'.format(sample_service.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 201
json_resp = json.loads(response.get_data(as_text=True))
results = ServiceEmailReplyTo.query.all()
assert len(results) == 1
assert json_resp['data'] == results[0].serialize()
def test_add_service_reply_to_email_address_can_add_multiple_addresses(client, sample_service):
data = json.dumps({"email_address": "first@reply.com", "is_default": True})
client.post('/service/{}/email-reply-to'.format(sample_service.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
second = json.dumps({"email_address": "second@reply.com", "is_default": True})
response = client.post('/service/{}/email-reply-to'.format(sample_service.id),
data=second,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 201
json_resp = json.loads(response.get_data(as_text=True))
results = ServiceEmailReplyTo.query.all()
assert len(results) == 2
default = [x for x in results if x.is_default]
assert json_resp['data'] == default[0].serialize()
first_reply_to_not_default = [x for x in results if not x.is_default]
assert first_reply_to_not_default[0].email_address == 'first@reply.com'
def test_add_service_reply_to_email_address_raise_exception_if_no_default(client, sample_service):
data = json.dumps({"email_address": "first@reply.com", "is_default": False})
response = client.post('/service/{}/email-reply-to'.format(sample_service.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['message'] == 'You must have at least one reply to email address as the default.'
def test_add_service_reply_to_email_address_404s_when_invalid_service_id(client, notify_db, notify_db_session):
response = client.post('/service/{}/email-reply-to'.format(uuid.uuid4()),
data={},
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 404
result = json.loads(response.get_data(as_text=True))
assert result['result'] == 'error'
assert result['message'] == 'No result found'
def test_update_service_reply_to_email_address(client, sample_service):
original_reply_to = create_reply_to_email(service=sample_service, email_address="some@email.com")
data = json.dumps({"email_address": "changed@reply.com", "is_default": True})
response = client.post('/service/{}/email-reply-to/{}'.format(sample_service.id, original_reply_to.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
results = ServiceEmailReplyTo.query.all()
assert len(results) == 1
assert json_resp['data'] == results[0].serialize()
def test_update_service_reply_to_email_address_returns_400_when_no_default(client, sample_service):
original_reply_to = create_reply_to_email(service=sample_service, email_address="some@email.com")
data = json.dumps({"email_address": "changed@reply.com", "is_default": False})
response = client.post('/service/{}/email-reply-to/{}'.format(sample_service.id, original_reply_to.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['message'] == 'You must have at least one reply to email address as the default.'
def test_update_service_reply_to_email_address_404s_when_invalid_service_id(client, notify_db, notify_db_session):
response = client.post('/service/{}/email-reply-to/{}'.format(uuid.uuid4(), uuid.uuid4()),
data={},
headers=[('Content-Type', 'application/json'), create_authorization_header()])
assert response.status_code == 404
result = json.loads(response.get_data(as_text=True))
assert result['result'] == 'error'
assert result['message'] == 'No result found'