"""Tests for ows-product-digital interface.""" import json from backend.constants import header as header_consts from backend.models import ows_product_digital from tests.testutils import mocks OWS_PRODUCT_DIGITAL_GET_HEADERS = { header_consts.CONTENT_TYPE: header_consts.CONTENT_TYPE_JSON_VAL} OWS_PRODUCT_DIGITAL_POST_HEADERS = { header_consts.CONTENT_TYPE: header_consts.CONTENT_TYPE_JSON_VAL, header_consts.ORCHARD_USER_ID: 'alw:123'} def test_get_product_by_product_id(mocker): """Get digital product metadata.""" data = {'release_id': 1, 'genre_id': 2} mock_request_get = mocks.get_response(mocker, json=data.copy()) res = ows_product_digital.get_product_by_product_id(product_id=1) mock_request_get.assert_called_with( 'ows-product-digital', '/product/audio/1', headers=OWS_PRODUCT_DIGITAL_GET_HEADERS) assert res assert res.message == data def test_get_product_by_product_id_not_found(mocker): """Get digital product metadata.""" error_obj = {'code': 'not_found_error', 'message': None} mock_request_get = mocks.get_response( mocker, json=error_obj.copy(), status_code=400) res = ows_product_digital.get_product_by_product_id(product_id=1) mock_request_get.assert_called_with( 'ows-product-digital', '/product/audio/1', headers=OWS_PRODUCT_DIGITAL_GET_HEADERS) assert not res assert res.errors['message'] == error_obj def test_post_track_correction(mocker, test_product_id, test_track_correction_data, test_correction_id, test_correlation_id, test_orchard_user_id): """Check post track correction.""" mock_request_post = mocks.post_response(mocker) expected_request = { 'items': test_track_correction_data } res = ows_product_digital.post_track_correction( test_product_id, test_correction_id, test_track_correction_data, test_correlation_id, test_orchard_user_id) mock_request_post.assert_called_with( 'ows-product-digital', '/product/{0}/correction/{1}/details'.format(test_product_id, test_correction_id), correlation_id=test_correlation_id, headers=OWS_PRODUCT_DIGITAL_POST_HEADERS, data=json.dumps(expected_request)) assert res def test_post_track_correction_error(mocker, test_product_id, test_track_correction_data, test_correction_id, test_correlation_id, test_orchard_user_id): """Post track correction data when internal error.""" error_obj = {'code': 'internal_error', 'message': None} mock_request_post = mocks.post_response( mocker, json=error_obj.copy(), status_code=500) expected_request = { 'items': test_track_correction_data } res = ows_product_digital.post_track_correction( test_product_id, test_correction_id, test_track_correction_data, test_correlation_id, test_orchard_user_id) mock_request_post.assert_called_with( 'ows-product-digital', '/product/{0}/correction/{1}/details'.format(test_product_id, test_correction_id), correlation_id=test_correlation_id, headers=OWS_PRODUCT_DIGITAL_POST_HEADERS, data=json.dumps(expected_request)) assert not res assert res.errors['message'] == error_obj def test_get_product_spatial(mocker): """Get product spatial record.""" data = {'product_id': 1, 'spatial_upc': '123456789012'} mock_request_get = mocks.get_response(mocker, json=data.copy()) res = ows_product_digital.get_product_spatial(product_id=1) mock_request_get.assert_called_with( 'ows-product-digital', '/product/1/spatial', headers=OWS_PRODUCT_DIGITAL_GET_HEADERS) assert res assert res.message == data def test_get_product_spatial_not_found(mocker): """Get product spatial record when not found.""" error_obj = {'code': 'not_found_error', 'message': None} mock_request_get = mocks.get_response( mocker, json=error_obj.copy(), status_code=404) res = ows_product_digital.get_product_spatial(product_id=1) mock_request_get.assert_called_with( 'ows-product-digital', '/product/1/spatial', headers=OWS_PRODUCT_DIGITAL_GET_HEADERS) assert not res assert res.errors['message'] == error_obj def test_create_product_spatial(mocker): """Create product spatial record.""" mock_request_post = mocks.post_response(mocker) res = ows_product_digital.create_product_spatial( product_id=1, orchard_user_id='alw:123') mock_request_post.assert_called_with( 'ows-product-digital', '/product/1/spatial', data=json.dumps({}), headers=OWS_PRODUCT_DIGITAL_POST_HEADERS) assert res def test_create_product_spatial_without_user(mocker): """Create product spatial record without orchard_user_id.""" mock_request_post = mocks.post_response(mocker) res = ows_product_digital.create_product_spatial(product_id=1) mock_request_post.assert_called_with( 'ows-product-digital', '/product/1/spatial', data=json.dumps({}), headers=OWS_PRODUCT_DIGITAL_GET_HEADERS) assert res def test_create_product_spatial_error(mocker): """Create product spatial record when internal error.""" error_obj = {'code': 'internal_error', 'message': None} mock_request_post = mocks.post_response( mocker, json=error_obj.copy(), status_code=500) res = ows_product_digital.create_product_spatial( product_id=1, orchard_user_id='alw:123') mock_request_post.assert_called_with( 'ows-product-digital', '/product/1/spatial', data=json.dumps({}), headers=OWS_PRODUCT_DIGITAL_POST_HEADERS) assert not res assert res.errors['message'] == error_obj