"""Test for the ows_product_physical module.""" import json from flexmock import flexmock from owsrequest import request from owsrequest import test_utils import pytest import requests from label_copy_export.connectors import loggly from label_copy_export.constants import ows_services from label_copy_export.models import ows_product_physical logger = loggly.get_current_logger() TEST_PRODUCT_ID = '1234567890' def test_get_product_physical_data_no_product_id(monkeypatch): """Test that invalid product_id returns correct response.""" error_json = {'text': 'not found'} not_valid_product_id = None call_spec_physical = { 'service': ows_services.OWS_PRODUCT_PHYSICAL, 'path': '/product/None', 'status': 404, 'json': error_json } mocked_process = test_utils.mock_ows_requests([call_spec_physical]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) ows_product_physical.logger = flexmock(loggly.get_current_logger()) flexmock(ows_product_physical.logger).should_receive('warning').once() result = ows_product_physical.get_product_physical_data( not_valid_product_id) assert not result assert json.loads(result.errors) == error_json assert result.status == call_spec_physical['status'] def test_get_product_physical_data_with_valid_data(monkeypatch): """Test that with valid data the result will be successful.""" valid_json = {'product_code': 'ABCD1234'} call_spec_physical = { 'service': ows_services.OWS_PRODUCT_PHYSICAL, 'path': '/product/{}'.format(TEST_PRODUCT_ID), 'status': 200, 'json': valid_json } mocked_process = test_utils.mock_ows_requests([call_spec_physical]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_product_physical.get_product_physical_data(TEST_PRODUCT_ID) assert result assert result.message == valid_json def test_get_product_physical_data_invalid_json(monkeypatch): """Test get_product_physical_data with invalid JSON in response. Tested function should return decoded original message on JSON decode exception. """ not_valid_json = b'non-JSON content' response = flexmock(requests.models.Response()) response.status_code = 401 response._content = not_valid_json flexmock(request).should_receive('process').and_return(response) result = ows_product_physical.get_product_physical_tracks(TEST_PRODUCT_ID) assert not result assert result.errors == not_valid_json.decode('utf8') def test_get_product_physical_tracks_no_product_id(monkeypatch): """Test that invalid product_id returns correct response.""" error_json = b'{"message": null, "code": "not_found_error"}' not_valid_product_id = None response = flexmock(requests.models.Response()) response._content = error_json response.status_code = 404 flexmock(request).should_receive('process').and_return(response) result = ows_product_physical.get_product_physical_tracks( not_valid_product_id) assert not result assert result.errors == error_json.decode('utf8') @pytest.mark.parametrize( 'status_code', [200, 201] ) def test_get_product_physical_tracks_with_valid_data(monkeypatch, status_code): """Test that with valid data the result will be successful.""" valid_json = {'product_code': 'ABCD1234'} call_spec_physical = { 'service': ows_services.OWS_PRODUCT_PHYSICAL, 'path': '/product/{}/tracks'.format(TEST_PRODUCT_ID), 'status': status_code, 'json': valid_json } mocked_process = test_utils.mock_ows_requests([call_spec_physical]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_product_physical.get_product_physical_tracks(TEST_PRODUCT_ID) assert result assert result.message == valid_json def test_get_product_physical_tracks_invalid_json(monkeypatch): """Test get_product_physical_tracks with invalid JSON in response. Tested function should return decoded original message on JSON decode exception. """ not_valid_json = b'non-JSON content' response = flexmock(requests.models.Response()) response._content = not_valid_json response.status_code = 401 flexmock(request).should_receive('process').and_return(response) result = ows_product_physical.get_product_physical_tracks(TEST_PRODUCT_ID) assert not result assert result.errors == not_valid_json.decode('utf8') def test_get_packaging_options_with_valid_data(monkeypatch): """Test that with valid data the result will be successful.""" valid_response = {'id': 2, 'name': 'Blister Pack'} call_spec_physical = { 'service': ows_services.OWS_PRODUCT_PHYSICAL, 'path': '/packaging', 'status': 200, 'json': valid_response } mocked_process = test_utils.mock_ows_requests([call_spec_physical]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_product_physical.get_packaging_options() assert result assert result.message == valid_response def test_get_packaging_options_invalid_json(monkeypatch): """Test get_packaging_options with invalid JSON in response. Tested function should return decoded original message on JSON decode exception. """ not_valid_json = b'non-JSON content' response = flexmock(requests.models.Response()) response._content = not_valid_json response.status_code = 401 flexmock(request).should_receive('process').and_return(response) result = ows_product_physical.get_packaging_options() assert not result assert result.errors == not_valid_json.decode('utf8')