From 933ed417b7ee3da00e6318addacc372c3518cbd2 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Thu, 1 Oct 2020 08:45:23 +0100 Subject: [PATCH 1/2] Make fewer assumptions in the assert_vwq_failure function, in anticipation of upcoming fixes --- tests/mock_vws/test_authorization_header.py | 10 ++++++ tests/mock_vws/test_content_length.py | 2 ++ tests/mock_vws/test_date_header.py | 4 +++ tests/mock_vws/test_invalid_json.py | 2 ++ tests/mock_vws/test_query.py | 36 +++++++++++++++++++++ tests/mock_vws/test_unexpected_json.py | 2 ++ tests/mock_vws/utils/assertions.py | 13 +++++--- 7 files changed, 64 insertions(+), 5 deletions(-) diff --git a/tests/mock_vws/test_authorization_header.py b/tests/mock_vws/test_authorization_header.py index 6d247c545..cbb7ed996 100644 --- a/tests/mock_vws/test_authorization_header.py +++ b/tests/mock_vws/test_authorization_header.py @@ -61,6 +61,8 @@ def test_missing(self, endpoint: Endpoint) -> None: response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='text/plain; charset=ISO-8859-1', + cache_control=None, + www_authenticate='VWS', ) assert response.text == 'Authorization header missing.' return @@ -113,6 +115,8 @@ def test_one_part( response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='text/plain; charset=ISO-8859-1', + cache_control=None, + www_authenticate='VWS', ) assert response.text == 'Malformed authorization header.' return @@ -160,6 +164,8 @@ def test_missing_signature( response=response, status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content_type='text/html; charset=ISO-8859-1', + cache_control='must-revalidate,no-cache,no-store', + www_authenticate=None, ) # We have seen multiple responses given. assert 'Powered by Jetty' in response.text @@ -220,6 +226,8 @@ def test_bad_access_key_query( response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='application/json', + cache_control=None, + www_authenticate='VWS', ) assert response.json().keys() == {'transaction_id', 'result_code'} @@ -276,6 +284,8 @@ def test_bad_secret_key_query( response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='application/json', + cache_control=None, + www_authenticate='VWS', ) assert response.json().keys() == {'transaction_id', 'result_code'} diff --git a/tests/mock_vws/test_content_length.py b/tests/mock_vws/test_content_length.py index bcd826fbe..a64596593 100644 --- a/tests/mock_vws/test_content_length.py +++ b/tests/mock_vws/test_content_length.py @@ -101,6 +101,8 @@ def test_too_small(self, endpoint: Endpoint) -> None: response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='application/json', + cache_control=None, + www_authenticate=None, ) return diff --git a/tests/mock_vws/test_date_header.py b/tests/mock_vws/test_date_header.py index cafd2312c..a24c493d4 100644 --- a/tests/mock_vws/test_date_header.py +++ b/tests/mock_vws/test_date_header.py @@ -75,6 +75,8 @@ def test_no_date_header( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type=expected_content_type, + cache_control=None, + www_authenticate=None, ) return @@ -141,6 +143,8 @@ def test_incorrect_date_format( response=response, status_code=HTTPStatus.UNAUTHORIZED, content_type='text/plain; charset=ISO-8859-1', + cache_control=None, + www_authenticate=None, ) return diff --git a/tests/mock_vws/test_invalid_json.py b/tests/mock_vws/test_invalid_json.py index 3168dd855..dc5e91587 100644 --- a/tests/mock_vws/test_invalid_json.py +++ b/tests/mock_vws/test_invalid_json.py @@ -99,6 +99,8 @@ def test_invalid_json( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type='text/html;charset=UTF-8', + cache_control=None, + www_authenticate=None, ) expected_text = ( 'java.lang.RuntimeException: RESTEASY007500: ' diff --git a/tests/mock_vws/test_query.py b/tests/mock_vws/test_query.py index d04194406..d27663926 100644 --- a/tests/mock_vws/test_query.py +++ b/tests/mock_vws/test_query.py @@ -148,6 +148,8 @@ def test_incorrect_no_boundary( response=response, status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE, content_type=None, + cache_control=None, + www_authenticate=None, ) def test_incorrect_with_boundary( @@ -202,6 +204,8 @@ def test_incorrect_with_boundary( response=response, status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE, content_type=None, + cache_control=None, + www_authenticate=None, ) @pytest.mark.parametrize( @@ -263,6 +267,8 @@ def test_no_boundary( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type='text/html;charset=UTF-8', + cache_control=None, + www_authenticate=None, ) def test_bogus_boundary( @@ -315,6 +321,8 @@ def test_bogus_boundary( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type='text/html;charset=UTF-8', + cache_control=None, + www_authenticate=None, ) def test_extra_section( @@ -505,6 +513,8 @@ def test_missing_image( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type='application/json', + cache_control=None, + www_authenticate=None, ) def test_extra_fields( @@ -528,6 +538,8 @@ def test_extra_fields( response=response, content_type='application/json', status_code=HTTPStatus.BAD_REQUEST, + cache_control=None, + www_authenticate=None, ) def test_missing_image_and_extra_fields( @@ -551,6 +563,8 @@ def test_missing_image_and_extra_fields( response=response, content_type='application/json', status_code=HTTPStatus.BAD_REQUEST, + cache_control=None, + www_authenticate=None, ) @@ -687,6 +701,8 @@ def test_out_of_range( response=response, content_type='application/json', status_code=HTTPStatus.BAD_REQUEST, + cache_control=None, + www_authenticate=None, ) @pytest.mark.parametrize( @@ -723,6 +739,8 @@ def test_invalid_type( response=response, content_type='application/json', status_code=HTTPStatus.BAD_REQUEST, + cache_control=None, + www_authenticate=None, ) @@ -904,6 +922,8 @@ def test_invalid_value( response=response, status_code=HTTPStatus.BAD_REQUEST, content_type='application/json', + cache_control=None, + www_authenticate=None, ) @@ -1014,6 +1034,8 @@ def test_invalid( response=response, status_code=HTTPStatus.NOT_ACCEPTABLE, content_type=None, + cache_control=None, + www_authenticate=None, ) @@ -1090,6 +1112,8 @@ def test_not_image( response=response, status_code=HTTPStatus.UNPROCESSABLE_ENTITY, content_type='application/json', + cache_control=None, + www_authenticate=None, ) assert response.json().keys() == {'transaction_id', 'result_code'} assert_valid_transaction_id(response=response) @@ -1290,6 +1314,8 @@ def test_max_height(self, vuforia_database: VuforiaDatabase) -> None: response=response, status_code=HTTPStatus.UNPROCESSABLE_ENTITY, content_type='application/json', + cache_control=None, + www_authenticate=None, ) assert response.json().keys() == {'transaction_id', 'result_code'} assert_valid_transaction_id(response=response) @@ -1346,6 +1372,8 @@ def test_max_width(self, vuforia_database: VuforiaDatabase) -> None: response=response, status_code=HTTPStatus.UNPROCESSABLE_ENTITY, content_type='application/json', + cache_control=None, + www_authenticate=None, ) assert response.json().keys() == {'transaction_id', 'result_code'} assert_valid_transaction_id(response=response) @@ -1413,6 +1441,8 @@ def test_unsupported( response=response, status_code=HTTPStatus.UNPROCESSABLE_ENTITY, content_type='application/json', + cache_control=None, + www_authenticate=None, ) assert response.json().keys() == {'transaction_id', 'result_code'} assert_valid_transaction_id(response=response) @@ -1500,6 +1530,8 @@ def test_processing( response=response, content_type='text/html; charset=ISO-8859-1', status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + cache_control='must-revalidate,no-cache,no-store', + www_authenticate=None, ) @@ -1637,6 +1669,8 @@ def test_deleted( response=response, content_type='text/html; charset=ISO-8859-1', status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + cache_control='must-revalidate,no-cache,no-store', + www_authenticate=None, ) return @@ -1876,6 +1910,8 @@ def test_inactive_project( response=response, status_code=HTTPStatus.FORBIDDEN, content_type='application/json', + cache_control=None, + www_authenticate=None, ) assert response.json().keys() == {'transaction_id', 'result_code'} assert_valid_transaction_id(response=response) diff --git a/tests/mock_vws/test_unexpected_json.py b/tests/mock_vws/test_unexpected_json.py index cb77caf30..c7d9056cb 100644 --- a/tests/mock_vws/test_unexpected_json.py +++ b/tests/mock_vws/test_unexpected_json.py @@ -77,6 +77,8 @@ def test_does_not_take_data( response=response, status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE, content_type=None, + cache_control=None, + www_authenticate=None, ) return diff --git a/tests/mock_vws/utils/assertions.py b/tests/mock_vws/utils/assertions.py index 8f9cfb162..ec68a5cba 100644 --- a/tests/mock_vws/utils/assertions.py +++ b/tests/mock_vws/utils/assertions.py @@ -199,6 +199,8 @@ def assert_vwq_failure( response: Response, status_code: int, content_type: Optional[str], + cache_control: Optional[str], + www_authenticate: Optional[str], ) -> None: """ Assert that a VWQ failure response is as expected. @@ -206,7 +208,9 @@ def assert_vwq_failure( Args: response: The response returned by a request to VWQ. content_type: The expected Content-Type header. - status_code: The expected status code of the response. + status_code: The expected status code. + cache_control: The expected Cache-Control header. + www_authenticate: The expected WWW-Authenticate header. Raises: AssertionError: The response is not in the expected VWQ error format @@ -220,18 +224,17 @@ def assert_vwq_failure( 'Server', } - if status_code == HTTPStatus.INTERNAL_SERVER_ERROR: + if cache_control is not None: response_header_keys.add('Cache-Control') - cache_control = 'must-revalidate,no-cache,no-store' assert response.headers['Cache-Control'] == cache_control if content_type is not None: response_header_keys.add('Content-Type') assert response.headers['Content-Type'] == content_type - if status_code == HTTPStatus.UNAUTHORIZED: + if www_authenticate is not None: response_header_keys.add('WWW-Authenticate') - assert response.headers['WWW-Authenticate'] == 'VWS' + assert response.headers['WWW-Authenticate'] == www_authenticate assert response.headers.keys() == response_header_keys assert response.headers['Connection'] == 'keep-alive' From 9b2a9dbfa9546d8412556eb4519764ca39c5d753 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Thu, 1 Oct 2020 08:53:50 +0100 Subject: [PATCH 2/2] Fixed a few failing tests by adding expected www_authenticate header --- tests/mock_vws/test_content_length.py | 2 +- tests/mock_vws/test_date_header.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mock_vws/test_content_length.py b/tests/mock_vws/test_content_length.py index a64596593..c3a5d149d 100644 --- a/tests/mock_vws/test_content_length.py +++ b/tests/mock_vws/test_content_length.py @@ -102,7 +102,7 @@ def test_too_small(self, endpoint: Endpoint) -> None: status_code=HTTPStatus.UNAUTHORIZED, content_type='application/json', cache_control=None, - www_authenticate=None, + www_authenticate='VWS', ) return diff --git a/tests/mock_vws/test_date_header.py b/tests/mock_vws/test_date_header.py index a24c493d4..5c11db650 100644 --- a/tests/mock_vws/test_date_header.py +++ b/tests/mock_vws/test_date_header.py @@ -144,7 +144,7 @@ def test_incorrect_date_format( status_code=HTTPStatus.UNAUTHORIZED, content_type='text/plain; charset=ISO-8859-1', cache_control=None, - www_authenticate=None, + www_authenticate='VWS', ) return