"""Tests for ownership validation.""" from oto import response as oto_response from oto import status from oto.adaptors.flask import flaskify from availability.validation import ownership def test_check_product_ownership(mocker): """Assert can check ownership of product.""" mocker.patch( 'availability.validation.ownership.request.head', return_value=flaskify(oto_response.Response())) response = ownership.check_products_ownership( product_ids=[1, 2, 3], account_type='vendor', account_id='1', correlation_id='test') assert isinstance(response, oto_response.Response) assert response.status == status.NO_CONTENT def test_check_product_ownership_not_authorized(mocker): """Assert not authorized access can be determined.""" error_response = oto_response.create_error_response( code='not_authorized', message='not authorized', status=status.FORBIDDEN) mocker.patch( 'availability.validation.ownership.request.head', return_value=flaskify(error_response)) response = ownership.check_products_ownership( product_ids=[1, 2, 3], account_type='vendor', account_id='1', correlation_id='test') assert response.status == status.FORBIDDEN def test_check_product_ownership_captures_exception(mocker): """Assert can handle unexpected exception.""" mocker.patch( 'availability.validation.ownership.request.head', side_effect=Exception) sentry_capture_exception_mock = mocker.patch( 'availability.validation.ownership.capture_exception') response = ownership.check_products_ownership( product_ids=[1, 2, 3], account_type='vendor', account_id='1', correlation_id='test') assert response.status == status.INTERNAL_ERROR assert sentry_capture_exception_mock.called is True def test_check_products_ownership_correctly_calls_owsrequest(mocker): """Assert request to ows-product is made correctly.""" mock_head = mocker.patch('availability.validation.ownership.request.head') ownership.check_products_ownership( product_ids=[1], account_type='vendor', account_id='1', correlation_id='test') mock_head.assert_called_with( 'ows-product', '/vendor/1/product/1', headers={'Correlation-Id': 'test'})