mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 15:56:45 -04:00
Add endpoint to add new letter contact block
Added the '/<uuid:service_id>/letter-contact' service endpoint.
This commit is contained in:
@@ -56,7 +56,8 @@ from app.dao.service_email_reply_to_dao import (
|
|||||||
from app.dao.service_letter_contact_dao import (
|
from app.dao.service_letter_contact_dao import (
|
||||||
dao_get_letter_contacts_by_service_id,
|
dao_get_letter_contacts_by_service_id,
|
||||||
create_or_update_letter_contact,
|
create_or_update_letter_contact,
|
||||||
dao_get_letter_contact_by_id
|
dao_get_letter_contact_by_id,
|
||||||
|
add_letter_contact_for_service,
|
||||||
)
|
)
|
||||||
from app.dao.provider_statistics_dao import get_fragment_count
|
from app.dao.provider_statistics_dao import get_fragment_count
|
||||||
from app.dao.users_dao import get_user_by_id
|
from app.dao.users_dao import get_user_by_id
|
||||||
@@ -68,7 +69,10 @@ from app.models import Service, ServiceInboundApi
|
|||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.service import statistics
|
from app.service import statistics
|
||||||
from app.service.service_inbound_api_schema import service_inbound_api, update_service_inbound_api_schema
|
from app.service.service_inbound_api_schema import service_inbound_api, update_service_inbound_api_schema
|
||||||
from app.service.service_senders_schema import add_service_email_reply_to_request
|
from app.service.service_senders_schema import (
|
||||||
|
add_service_email_reply_to_request,
|
||||||
|
add_service_letter_contact_block_request,
|
||||||
|
)
|
||||||
from app.service.utils import get_whitelist_objects
|
from app.service.utils import get_whitelist_objects
|
||||||
from app.service.sender import send_notification_to_service_users
|
from app.service.sender import send_notification_to_service_users
|
||||||
from app.service.send_notification import send_one_off_notification
|
from app.service.send_notification import send_one_off_notification
|
||||||
@@ -587,6 +591,17 @@ def get_letter_contact_by_id(service_id, letter_contact_id):
|
|||||||
return jsonify(result.serialize()), 200
|
return jsonify(result.serialize()), 200
|
||||||
|
|
||||||
|
|
||||||
|
@service_blueprint.route('/<uuid:service_id>/letter-contact', methods=['POST'])
|
||||||
|
def add_service_letter_contact(service_id):
|
||||||
|
# validate the service exists, throws ResultNotFound exception.
|
||||||
|
dao_fetch_service_by_id(service_id)
|
||||||
|
form = validate(request.get_json(), add_service_letter_contact_block_request)
|
||||||
|
new_letter_contact = add_letter_contact_for_service(service_id=service_id,
|
||||||
|
contact_block=form['contact_block'],
|
||||||
|
is_default=form.get('is_default', True))
|
||||||
|
return jsonify(data=new_letter_contact.serialize()), 201
|
||||||
|
|
||||||
|
|
||||||
@service_blueprint.route('/unique', methods=["GET"])
|
@service_blueprint.route('/unique', methods=["GET"])
|
||||||
def is_service_name_unique():
|
def is_service_name_unique():
|
||||||
name, email_from = check_request_args(request)
|
name, email_from = check_request_args(request)
|
||||||
|
|||||||
@@ -9,3 +9,16 @@ add_service_email_reply_to_request = {
|
|||||||
},
|
},
|
||||||
"required": ["email_address", "is_default"]
|
"required": ["email_address", "is_default"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
add_service_letter_contact_block_request = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST service letter contact block",
|
||||||
|
"type": "object",
|
||||||
|
"title": "Add new letter contact block for service",
|
||||||
|
"properties": {
|
||||||
|
"contact_block": {"type": "string"},
|
||||||
|
"is_default": {"type": "boolean"}
|
||||||
|
},
|
||||||
|
"required": ["contact_block", "is_default"]
|
||||||
|
}
|
||||||
|
|||||||
@@ -2397,3 +2397,57 @@ def test_get_letter_contact_return_404_when_invalid_contact_id(client, notify_db
|
|||||||
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||||
|
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_service_contact_block(client, sample_service):
|
||||||
|
data = json.dumps({"contact_block": "London, E1 8QS", "is_default": True})
|
||||||
|
response = client.post('/service/{}/letter-contact'.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 = ServiceLetterContact.query.all()
|
||||||
|
assert len(results) == 1
|
||||||
|
assert json_resp['data'] == results[0].serialize()
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_service_letter_contact_can_add_multiple_addresses(client, sample_service):
|
||||||
|
first = json.dumps({"contact_block": "London, E1 8QS", "is_default": True})
|
||||||
|
client.post('/service/{}/letter-contact'.format(sample_service.id),
|
||||||
|
data=first,
|
||||||
|
headers=[('Content-Type', 'application/json'), create_authorization_header()])
|
||||||
|
|
||||||
|
second = json.dumps({"contact_block": "Aberdeen, AB23 1XH", "is_default": True})
|
||||||
|
response = client.post('/service/{}/letter-contact'.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 = ServiceLetterContact.query.all()
|
||||||
|
assert len(results) == 2
|
||||||
|
default = [x for x in results if x.is_default]
|
||||||
|
assert json_resp['data'] == default[0].serialize()
|
||||||
|
first_letter_contact_not_default = [x for x in results if not x.is_default]
|
||||||
|
assert first_letter_contact_not_default[0].contact_block == 'London, E1 8QS'
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_service_letter_contact_block_raise_exception_if_no_default(client, sample_service):
|
||||||
|
data = json.dumps({"contact_block": "London, E1 8QS", "is_default": False})
|
||||||
|
response = client.post('/service/{}/letter-contact'.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 letter contact as the default.'
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_service_letter_contact_block_404s_when_invalid_service_id(client, notify_db, notify_db_session):
|
||||||
|
response = client.post('/service/{}/letter-contact'.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'
|
||||||
|
|||||||
Reference in New Issue
Block a user