"""Functional test for post image method.""" import json import httpretty from images import config from images.constants import errors from images.constants import workstation as workstation_constants def test_post_image_invalid_request(client, header): """Endpoint return an error when the json does not pass validation.""" invalid_json = {'blah': 'bleh'} request = client.post( '/image', data=json.dumps(invalid_json), headers=header) assert request.status_code == 400 def test_post_image_no_php_session_cookie( client, header, valid_post_image_data): """Endpoint return an error when the PHP session cookie is missing.""" request = client.post( '/image', data=json.dumps(valid_post_image_data), headers=header) returned_message = json.loads(request.data.decode()).get('message') expected_message = 'No session cookie found.' assert request.status_code == 500 assert returned_message == expected_message @httpretty.activate def test_post_image_parses_response_from_workstation( client, header_with_php_session_cookie, workstation_response_body, valid_post_image_data, valid_post_image_response): """Endpoint return the expected parsed response from workstation.""" httpretty.register_uri( httpretty.POST, config.WORKSTATION_URL + workstation_constants.GETDESTINATIONLIST_PATH, body=json.dumps(workstation_response_body), content_type='application/json') request = client.post( '/image', data=json.dumps(valid_post_image_data), headers=header_with_php_session_cookie) assert request.status_code == 200 assert json.loads(request.data.decode()) == valid_post_image_response @httpretty.activate def test_post_image_responds_with_error_on_bad_workstation_request( client, header_with_php_session_cookie, valid_post_image_data): """Endpoint return an error response with bad workstation response.""" httpretty.register_uri( httpretty.POST, config.WORKSTATION_URL + workstation_constants.GETDESTINATIONLIST_PATH, status=500) request = client.post( '/image', data=json.dumps(valid_post_image_data), headers=header_with_php_session_cookie) returned_message = json.loads(request.data.decode()).get('message') expected_message = 'Workstation returned unexpected status code of 500' assert request.status_code == 500 assert returned_message == expected_message @httpretty.activate def test_post_image_responds_with_error_on_workstation_meta_not_found( client, header_with_php_session_cookie, valid_post_image_data, meta_not_found_workstation_response_body): """Endpoint returns an error response with meta_not_found message.""" httpretty.register_uri( httpretty.POST, config.WORKSTATION_URL + workstation_constants.GETDESTINATIONLIST_PATH, body=json.dumps(meta_not_found_workstation_response_body), content_type='application/json') request = client.post( '/image', data=json.dumps(valid_post_image_data), headers=header_with_php_session_cookie) returned_message = json.loads(request.data.decode()).get('message') expected_message = errors.META_NOT_FOUND_MESSAGE assert request.status_code == 500 assert returned_message == expected_message