mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 08:25:15 -05:00
Merge pull request #1237 from alphagov/set-sender-on-new-table
[2/5] Upsert into ServiceReplyToEmail table when updating a service
This commit is contained in:
32
app/dao/service_email_reply_to_dao.py
Normal file
32
app/dao/service_email_reply_to_dao.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
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()
|
||||
return reply_to
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_update_reply_to_email(reply_to):
|
||||
db.session.add(reply_to)
|
||||
@@ -1,5 +1,4 @@
|
||||
import itertools
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from flask import (
|
||||
@@ -11,7 +10,7 @@ from flask import (
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app.dao import notification_usage_dao, notifications_dao
|
||||
from app.dao import notifications_dao
|
||||
from app.dao.dao_utils import dao_rollback
|
||||
from app.dao.api_key_dao import (
|
||||
save_model_api_key,
|
||||
@@ -46,6 +45,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.provider_statistics_dao import get_fragment_count
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
@@ -132,9 +132,13 @@ def create_service():
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>', methods=['POST'])
|
||||
def update_service(service_id):
|
||||
req_json = request.get_json()
|
||||
fetched_service = dao_fetch_service_by_id(service_id)
|
||||
# Capture the status change here as Marshmallow changes this later
|
||||
service_going_live = fetched_service.restricted and not request.get_json().get('restricted', True)
|
||||
service_going_live = fetched_service.restricted and not req_json.get('restricted', True)
|
||||
|
||||
if 'reply_to_email_address' in req_json:
|
||||
create_or_update_email_reply_to(fetched_service.id, req_json['reply_to_email_address'])
|
||||
|
||||
current_data = dict(service_schema.dump(fetched_service).data.items())
|
||||
current_data.update(request.get_json())
|
||||
|
||||
41
tests/app/dao/test_service_email_reply_to_dao.py
Normal file
41
tests/app/dao/test_service_email_reply_to_dao.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from app.dao.service_email_reply_to_dao import (
|
||||
create_or_update_email_reply_to,
|
||||
dao_get_reply_to_by_service_id
|
||||
)
|
||||
from app.models import ServiceEmailReplyTo
|
||||
from tests.app.db import create_reply_to_email, create_service
|
||||
|
||||
|
||||
def test_create_or_update_email_reply_to_does_not_create_another_entry(notify_db_session):
|
||||
service = create_service()
|
||||
create_reply_to_email(service, 'test@mail.com')
|
||||
|
||||
create_or_update_email_reply_to(service.id, 'different@mail.com')
|
||||
|
||||
reply_to = dao_get_reply_to_by_service_id(service.id)
|
||||
|
||||
assert ServiceEmailReplyTo.query.count() == 1
|
||||
|
||||
|
||||
def test_create_or_update_email_reply_to_updates_existing_entry(notify_db_session):
|
||||
service = create_service()
|
||||
create_reply_to_email(service, 'test@mail.com')
|
||||
|
||||
create_or_update_email_reply_to(service.id, 'different@mail.com')
|
||||
|
||||
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'
|
||||
|
||||
|
||||
def test_create_or_update_email_reply_to_creates_new_entry(notify_db_session):
|
||||
service = create_service()
|
||||
|
||||
create_or_update_email_reply_to(service.id, 'test@mail.com')
|
||||
|
||||
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'
|
||||
@@ -25,7 +25,9 @@ from app.models import (
|
||||
SMS_TYPE,
|
||||
INBOUND_SMS_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
ServiceInboundApi)
|
||||
ServiceInboundApi,
|
||||
ServiceEmailReplyTo
|
||||
)
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
@@ -327,3 +329,19 @@ def create_monthly_billing_entry(
|
||||
db.session.commit()
|
||||
|
||||
return entry
|
||||
|
||||
|
||||
def create_reply_to_email(
|
||||
service,
|
||||
email_address
|
||||
):
|
||||
data = {
|
||||
'service': service,
|
||||
'email_address': email_address,
|
||||
}
|
||||
reply_to = ServiceEmailReplyTo(**data)
|
||||
|
||||
db.session.add(reply_to)
|
||||
db.session.commit()
|
||||
|
||||
return reply_to
|
||||
|
||||
@@ -2123,3 +2123,18 @@ def test_is_service_name_unique_returns_400_when_name_does_not_exist(client):
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp["message"][0]["name"] == ["Can't be empty"]
|
||||
assert json_resp["message"][1]["email_from"] == ["Can't be empty"]
|
||||
|
||||
|
||||
def test_update_service_reply_to_email_address_upserts_email_reply_to(mocker, admin_request, sample_service):
|
||||
update_mock = mocker.patch('app.service.rest.create_or_update_email_reply_to')
|
||||
|
||||
admin_request.post(
|
||||
'service.update_service',
|
||||
service_id=sample_service.id,
|
||||
_data={
|
||||
'reply_to_email_address': 'new@mail.com'
|
||||
},
|
||||
_expected_status=200
|
||||
)
|
||||
|
||||
assert update_mock.called
|
||||
|
||||
Reference in New Issue
Block a user