From b85fcafd46acdf75bce8374e3dd20cbfe7a3243e Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 26 Jan 2021 12:00:19 +0000 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20allow=20broadcasts=20to=20be=20?= =?UTF-8?q?created=20from=20JSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Until we know we’re going to have real users for this, let’s not expose it. --- app/v2/broadcast/post_broadcast.py | 22 ++++----- tests/app/v2/broadcast/test_post_broadcast.py | 48 ++++--------------- 2 files changed, 19 insertions(+), 51 deletions(-) diff --git a/app/v2/broadcast/post_broadcast.py b/app/v2/broadcast/post_broadcast.py index dbebdd4f3..3631c05d3 100644 --- a/app/v2/broadcast/post_broadcast.py +++ b/app/v2/broadcast/post_broadcast.py @@ -21,22 +21,22 @@ def create_broadcast(): authenticated_service.permissions, ) - if request.content_type == 'application/json': - broadcast_json = request.get_json() - elif request.content_type == 'application/cap+xml': - cap_xml = request.get_data(as_text=True) - if not validate_xml(cap_xml, 'CAP-v1.2.xsd'): - raise BadRequestError( - message=f'Request data is not valid CAP XML', - status_code=400, - ) - broadcast_json = cap_xml_to_dict(cap_xml) - else: + if request.content_type != 'application/cap+xml': raise BadRequestError( message=f'Content type {request.content_type} not supported', status_code=400, ) + cap_xml = request.get_data(as_text=True) + + if not validate_xml(cap_xml, 'CAP-v1.2.xsd'): + raise BadRequestError( + message=f'Request data is not valid CAP XML', + status_code=400, + ) + + broadcast_json = cap_xml_to_dict(cap_xml) + validate(broadcast_json, post_broadcast_schema) polygons = Polygons(list(chain.from_iterable(( diff --git a/tests/app/v2/broadcast/test_post_broadcast.py b/tests/app/v2/broadcast/test_post_broadcast.py index f8d289e6a..85c4ff3b8 100644 --- a/tests/app/v2/broadcast/test_post_broadcast.py +++ b/tests/app/v2/broadcast/test_post_broadcast.py @@ -51,46 +51,14 @@ def test_valid_post_broadcast_returns_201( headers=[('Content-Type', 'application/json'), auth_header], ) - assert response.status_code == 201 - - response_json = json.loads(response.get_data(as_text=True)) - - assert response_json['approved_at'] is None - assert response_json['approved_by_id'] == None - assert response_json['areas'] == [ - 'Hackney Marshes' - ] - assert response_json['cancelled_at'] == None - assert response_json['cancelled_by_id'] == None - assert response_json['content'] == 'This is a test' - assert response_json['reference'] == 'abc123' - assert response_json['created_at'] # datetime generated by the DB so can’t freeze it - assert response_json['created_by_id'] == None - assert response_json['finishes_at'] is None - assert response_json['id'] == ANY - assert response_json['personalisation'] is None - assert response_json['service_id'] == str(sample_broadcast_service.id) - assert response_json['simple_polygons'] == [[ - [-0.03817522145265898, 51.557381351011166], - [-0.03791399800364216, 51.55758039392131], - [-0.030362635618559567, 51.56141279571522], - [-0.02986997783677049, 51.56152875195115], - [-0.029379069367567606, 51.561405599957745], - [-0.023537043373602105, 51.5583323982824], - [-0.02328416546450603, 51.55813395976017], - [-0.02355422144186266, 51.557933308587664], - [-0.0313493058222969, 51.55414241384808], - [-0.031840673207720764, 51.55403463730992], - [-0.032327132941933706, 51.55416275685022], - [-0.037918974384948616, 51.55717594115094], - [-0.03817522145265898, 51.557381351011166], - ]] - assert response_json['starts_at'] is None - assert response_json['status'] == 'pending-approval' - assert response_json['template_id'] is None - assert response_json['template_name'] is None - assert response_json['template_version'] is None - assert response_json['updated_at'] is None + assert response.status_code == 400 + assert json.loads(response.get_data(as_text=True)) == { + 'errors': [{ + 'error': 'BadRequestError', + 'message': 'Content type application/json not supported' + }], + 'status_code': 400, + } def test_valid_post_cap_xml_broadcast_returns_201(