"""Tests for ows-marketing Model.""" from unittest.mock import MagicMock from unittest.mock import Mock from owsrequest import request as requests from owsrequest.test_utils import mock_ows_requests from owsrequest.test_utils import MockOwsResponse from ows_product_physical import api from ows_product_physical.constant import marketing from ows_product_physical.constant import service_name from ows_product_physical.models import ows_marketing def raise_no_json_error(_): """Raise error when attempting to parse json body.""" raise ValueError('No json error!') def test_get_product_highlight_by_product_id_success( monkeypatch, valid_get_header, context): """Test getting a product's highlight.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={ 'pagination': { 'offset': 0, 'type': 'standard', 'total_records': 1, 'limit': 1}, 'items': [ {'scope': 'public', 'highlight_id': 39752, 'description': 'Kissed Madonna on stage at the VMAs', 'client': 'alw', 'attachment': 'N', 'mkt_program_id': marketing.PROGRAM_MARKETING_HIGHLIGHTS, 'entity_id': 1900566, 'entity': 'release', 'subject': 'Product Highlights Subject'}]}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) marketing_response = ows_marketing.\ get_product_highlight_by_product_id(1900566) assert requests.get.called assert requests.get.call_args[0][0] == service_name.OWS_MARKETING assert requests.get.call_args[0][1] == ( '/highlights/release/{}?mkt_program_id={}').format( 1900566, marketing.PROGRAM_MARKETING_HIGHLIGHTS) assert marketing_response.status == 200 assert marketing_response.message.get('entity_id') == 1900566 assert marketing_response.message.get('highlight_id') == 39752 assert marketing_response.message.get( 'mkt_program_id') == marketing.PROGRAM_MARKETING_HIGHLIGHTS def test_get_product_highlight_by_product_id_fail( monkeypatch, valid_get_header, context): """Test getting a product's highlight that doesn't exist.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value={ 'code': 'not_found_error', 'message': None }) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) marketing_response = ows_marketing.\ get_product_highlight_by_product_id(1900566) assert requests.get.called assert requests.get.call_args[0][0] == service_name.OWS_MARKETING assert requests.get.call_args[0][1] == ( '/highlights/release/{}?mkt_program_id={}').format( 1900566, marketing.PROGRAM_MARKETING_HIGHLIGHTS) assert marketing_response.status == 404 assert marketing_response.errors.get('code') == 'not_found_error' def test_get_product_highlight_by_product_id_fatal_error( monkeypatch, valid_get_header, context): """Test getting a product's highlight returns a fatal error.""" with context, api.app.test_request_context(headers=valid_get_header): request_spec = [{ 'service': 'ows-marketing', 'path': '/highlights/release/1900566?mkt_program_id=20', 'status': 500 }] monkeypatch.setattr(MockOwsResponse, 'json', raise_no_json_error) monkeypatch.setattr(requests, 'get', mock_ows_requests(request_spec)) marketing_response = ows_marketing.\ get_product_highlight_by_product_id(1900566) assert marketing_response.status == 500 assert marketing_response.errors['code'] == 'Internal Error' def test_create_product_highlight_success( monkeypatch, valid_get_header, context): """Test creating a product's product highlight success state.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=201) mock_response.json = MagicMock(return_value={ 'description': 'new product highlight', 'entity': 'release', 'scope': 'public', 'subject': 'Product Highlight', 'entity_id': 1900566, 'highlight_id': 39754, 'attachment': 'N', 'client': 'alw', 'mkt_program_id': marketing.PROGRAM_MARKETING_HIGHLIGHTS}) monkeypatch.setattr(requests, 'post', MagicMock( return_value=mock_response)) marketing_response = ows_marketing.create_product_highlight( 1900566, 'new product highlight') assert requests.post.called assert requests.post.call_args[0][0] == service_name.OWS_MARKETING assert requests.post.call_args[0][1] == '/highlights' assert marketing_response.status == 201 assert marketing_response.message.get('entity_id') == 1900566 assert marketing_response.message.get('highlight_id') == 39754 assert marketing_response.message.get( 'mkt_program_id') == marketing.PROGRAM_MARKETING_HIGHLIGHTS def test_create_product_highlight_fail(monkeypatch, valid_get_header, context): """Test creating a product's product highlight fail state.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=400) mock_response.json = MagicMock(return_value={ 'code': 'validation_error', 'message': { 'attachment': "'attachment' is a required property", 'scope': "'scope' is a required property", 'mkt_program_id': "'mkt_program_id' is a required property" }}) monkeypatch.setattr(requests, 'post', MagicMock( return_value=mock_response)) marketing_response = ows_marketing.\ create_product_highlight( 1900566, 'new product highlight') assert requests.post.called assert requests.post.call_args[0][0] == service_name.OWS_MARKETING assert requests.post.call_args[0][1] == '/highlights' assert marketing_response.status == 400 assert marketing_response.errors.get('code') == 'validation_error' def test_create_product_highlight_fatal_error( monkeypatch, valid_get_header, context): """Test creating a product's product highlight returns a fatal error.""" with context, api.app.test_request_context(headers=valid_get_header): request_spec = [{ 'status': 500, 'path': '/highlights', 'service': 'ows-marketing' }] monkeypatch.setattr(MockOwsResponse, 'json', raise_no_json_error) monkeypatch.setattr(requests, 'post', mock_ows_requests(request_spec)) marketing_response = ows_marketing.\ create_product_highlight( 1900566, 'new product highlight') assert marketing_response.status == 500 assert marketing_response.errors['code'] == 'Internal Error' def test_update_product_highlight_success( monkeypatch, valid_get_header, context): """Test updating a product's product highlight success.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={ 'description': 'updated product highlight', 'entity': 'release', 'scope': 'public', 'subject': 'Product Highlight', 'entity_id': 1900566, 'highlight_id': 39757, 'attachment': 'N', 'client': 'alw', 'mkt_program_id': marketing.PROGRAM_MARKETING_HIGHLIGHTS}) monkeypatch.setattr(requests, 'put', MagicMock( return_value=mock_response)) highlight = 'updated product highlight' marketing_response = ows_marketing.update_product_highlight( 39757, highlight) assert requests.put.called assert requests.put.call_args[0][0] == service_name.OWS_MARKETING assert requests.put.call_args[0][1] == ( '/highlights/{HIGHLIGHT_ID}').format( HIGHLIGHT_ID=39757) assert marketing_response.status == 200 assert marketing_response.message.get('entity_id') == 1900566 assert marketing_response.message.get('highlight_id') == 39757 assert marketing_response.message.get( 'mkt_program_id') == marketing.PROGRAM_MARKETING_HIGHLIGHTS assert marketing_response.message.get('description') == highlight def test_update_product_highlight_fail(monkeypatch, valid_get_header, context): """Test updating a product's product highlight fail state.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = Mock() mock_response.status_code = 400 mock_response.content = b'{"foo": "bar"}' monkeypatch.setattr(requests, 'put', MagicMock( return_value=mock_response)) marketing_response = ows_marketing.update_product_highlight( 39757, 'updated product highlight') assert requests.put.called assert requests.put.call_args[0][0] == service_name.OWS_MARKETING assert requests.put.call_args[0][1] == ( '/highlights/{HIGHLIGHT_ID}').format( HIGHLIGHT_ID=39757) assert marketing_response.status == 400 def test_update_product_highlight_fatal_error( monkeypatch, valid_get_header, context): """Test updating a product's product highlight returns a fatal error.""" with context, api.app.test_request_context(headers=valid_get_header): request_spec = [{ 'status': 500, 'service': 'ows-marketing', 'path': '/highlights/123' }] monkeypatch.setattr(MockOwsResponse, 'json', raise_no_json_error) monkeypatch.setattr(requests, 'put', mock_ows_requests(request_spec)) marketing_response = ows_marketing.update_product_highlight( 123, 'updated product highlight') assert marketing_response.status == 500 assert marketing_response.errors['code'] == 'Internal Error'