Add endpoint to get the email reply to addresses for a service

- Changed the dao_get_reply_to_by_service_id method to return a list of
results.
- Added a GET /service/<service_id>/email-reply-to endpoint
This commit is contained in:
Katie Smith
2017-09-13 16:58:36 +01:00
parent 63b62e74bb
commit 72760550bd
6 changed files with 84 additions and 12 deletions
+10 -5
View File
@@ -5,12 +5,17 @@ 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 reply_to:
reply_to.email_address = email_address
dao_update_reply_to_email(reply_to)
else:
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
@@ -23,7 +28,7 @@ def dao_get_reply_to_by_service_id(service_id):
ServiceEmailReplyTo
).filter(
ServiceEmailReplyTo.service_id == service_id
).first()
).all()
return reply_to
+8
View File
@@ -1343,3 +1343,11 @@ class ServiceEmailReplyTo(db.Model):
is_default = db.Column(db.Boolean, nullable=False, default=True)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
def serialize(self):
return {
'email_address': self.email_address,
'is_default': self.is_default,
'created_at': self.created_at,
'updated_at': self.updated_at
}
+7 -1
View File
@@ -46,7 +46,7 @@ from app.dao.service_whitelist_dao import (
dao_add_and_commit_whitelisted_contacts,
dao_remove_service_whitelist
)
from app.dao.service_email_reply_to_dao import create_or_update_email_reply_to
from app.dao.service_email_reply_to_dao import create_or_update_email_reply_to, dao_get_reply_to_by_service_id
from app.dao.provider_statistics_dao import get_fragment_count
from app.dao.users_dao import get_user_by_id
from app.errors import (
@@ -523,6 +523,12 @@ def create_one_off_notification(service_id):
return jsonify(resp), 201
@service_blueprint.route('/<uuid:service_id>/email-reply-to', methods=["GET"])
def get_email_reply_to_addresses(service_id):
result = dao_get_reply_to_by_service_id(service_id)
return jsonify([i.serialize() for i in result]), 200
@service_blueprint.route('/unique', methods=["GET"])
def is_service_name_unique():
name, email_from = check_request_args(request)
@@ -25,8 +25,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 +38,5 @@ 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'
+3 -1
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)
+51 -1
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']