Merge branch 'master' of https://github.com/alphagov/notifications-api into Inbound-sms-ip

This commit is contained in:
venusbb
2017-09-14 16:55:04 +01:00
6 changed files with 122 additions and 23 deletions

View File

@@ -1,32 +1,39 @@
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 reply_to:
reply_to.email_address = email_address
dao_update_reply_to_email(reply_to)
else:
reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address)
dao_create_reply_to_email_address(reply_to)
@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
).filter(
ServiceEmailReplyTo.service_id == service_id
).first()
).all()
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)

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
}

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)

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']