"""Test for ows_pricing model.""" from unittest.mock import MagicMock from flexmock import flexmock from oto import response from owsrequest import request import pytest from product_digital.constants import services from product_digital.models import ows_pricing from product_digital.models import ows_track @pytest.fixture def pricing_tier_message(): """Return a fake pricing tier message.""" return {'items': [ { 'is_default': True, 'sort_order': 1, 'name': 'Deluxe Four', 'orchard_pricing_tier_id': 8, 'pricing_family_id': 2 }, { 'is_default': False, 'sort_order': 2, 'name': 'Deluxe Five', 'orchard_pricing_tier_id': 15, 'pricing_family_id': 4 }]} def test_pricing_get_track_overrides_correct_call( product_id, track_ids_for_testing, track_pricing_overrides_payload): """Test expected call made to tracks pricing endpoint.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value=track_pricing_overrides_payload) mock_get_track_response = response.Response( message={'track_name': 'My Song'}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/track-overrides'.format(product_id=product_id), ).and_return(mock_response)) (flexmock(ows_track).should_receive('get_track_by_track_id').with_args( track_ids_for_testing[0]).and_return( mock_get_track_response).once()) ows_pricing.get_track_overrides(product_id) def test_pricing_get_track_overrides_success_returns_correct_payload( product_id, track_pricing_overrides_payload, track_ids_for_testing, active_track_overrides_payload): """Test expected payload returned for getting track overrides.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value=track_pricing_overrides_payload) mock_get_track_response = response.Response( message={'track_name': 'My Song'}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/track-overrides'.format(product_id=product_id), ).and_return(mock_response)) (flexmock(ows_track).should_receive('get_track_by_track_id').with_args( track_ids_for_testing[0]).and_return( mock_get_track_response).once()) result = ows_pricing.get_track_overrides(product_id) assert result.status == 200 assert result.message == active_track_overrides_payload def test_pricing_get_track_overrides_returns_error_when_error_received( product_id, generic_error_payload): """Test error result returned when error from ows-pricing.""" mock_error_response = MagicMock(status_code=404) mock_error_response.json = MagicMock(return_value=generic_error_payload) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/track-overrides'.format(product_id=product_id), ).and_return(mock_error_response)) result = ows_pricing.get_track_overrides(product_id) assert result.status == 404 assert result.errors.get('message') == generic_error_payload.get('message') assert result.errors.get('code') == generic_error_payload.get('code') def test_correct_call_made_to_ows_pricing_for_product_overrides(product_id): """Test expected call made to product overrides through owsrequest.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={'items': []}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/override'.format(product_id=product_id), ).and_return(mock_response)) ows_pricing.get_active_product_overrides(product_id) def test_pricing_get_active_product_overrides_success_returns_correct_payload( product_id, product_pricing_active_overrides_payload, product_pricing_overrides_payload): """Test expected payload returned for getting product overrides.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value=product_pricing_overrides_payload) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/override'.format(product_id=product_id), ).and_return(mock_response)) result = ows_pricing.get_active_product_overrides( product_id) assert result.status == 200 assert result.message == product_pricing_active_overrides_payload def test_pricing_get_active_product_overrides_returns_error_when_error( product_id, generic_error_payload): """Test error result returned when error from ows-pricing.""" mock_response = MagicMock(status_code=500) mock_response.json = MagicMock( return_value=generic_error_payload) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{product_id}/override'.format(product_id=product_id), ).and_return(mock_response)) result = ows_pricing.get_active_product_overrides( product_id) assert result.status == 500 assert result.errors.get('message') == generic_error_payload.get('message') assert result.errors.get('code') == generic_error_payload.get('code') def test_correct_call_made_to_ows_pricing_for_product_pricing_tier( product_id, pricing_tier_message): """Test expected call to get product pricing tier through ows-request.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value={'orchard_pricing_tier_id': 15}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, 2), ).and_return(mock_response)) mock_get_pricing_tiers_response = MagicMock(status_code=200) mock_get_pricing_tiers_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(2), ).and_return(mock_get_pricing_tiers_response)) ows_pricing.get_pricing_tier_by_product(product_id, 2) def test_correct_call_made_to_ows_pricing_for_track_pricing_tier( product_id, pricing_tier_message): """Test expected call to get track pricing tier through ows-request.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value={'orchard_pricing_tier_id': 15}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, 3), ).and_return(mock_response)) mock_get_pricing_tiers_response = MagicMock(status_code=200) mock_get_pricing_tiers_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(3), ).and_return(mock_get_pricing_tiers_response)) ows_pricing.get_pricing_tier_by_product(product_id, 3) def test_pricing_get_pricing_tier_by_product_success_returns_correct_payload( product_id, pricing_tier_message, product_pricing_tier_payload): """Test expected payload returned for getting product pricing tier.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value=product_pricing_tier_payload) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, 2), ).and_return(mock_response)) mock_get_pricing_tiers_response = MagicMock(status_code=200) mock_get_pricing_tiers_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(2), ).and_return(mock_get_pricing_tiers_response)) result = ows_pricing.get_pricing_tier_by_product( product_id, 2) assert result.status == 200 assert result.message == product_pricing_tier_payload def test_pricing_get_pricing_tier_by_product_returns_error_when_error_received( product_id, generic_error_payload, pricing_tier_message): """Test error result returned when error from ows-pricing.""" mock_response = MagicMock(status_code=403) mock_response.json = MagicMock( return_value=generic_error_payload) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, 2), ).and_return(mock_response)) mock_get_pricing_tiers_response = MagicMock(status_code=200) mock_get_pricing_tiers_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(2), ).and_return(mock_get_pricing_tiers_response)) result = ows_pricing.get_pricing_tier_by_product( product_id, 2) assert result.status == 403 assert result.errors.get('message') == generic_error_payload.get('message') assert result.errors.get('code') == generic_error_payload.get('code') def test_pricing_get_pricing_tier_by_product_when_empty_body_returned( product_id, pricing_tier_message): """Test that get_default_pricing is called when no pricing tier.""" mock_pricing_by_product_response = MagicMock(status_code=200) mock_pricing_by_product_response.text = '' mock_pricing_by_product_response.json = MagicMock( side_effect=KeyError('json error')) mock_get_pricing_tiers_response = MagicMock(status_code=200) mock_get_pricing_tiers_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, 2) ).and_return(mock_pricing_by_product_response)) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(2), ).and_return(mock_get_pricing_tiers_response)) result = ows_pricing.get_pricing_tier_by_product( product_id, 2) assert result.message == { 'is_default': True, 'sort_order': 1, 'name': 'Deluxe Four', 'orchard_pricing_tier_id': 8, 'pricing_family_id': 2} def test_correct_call_made_to_ows_pricing_for_pricing_tiers(product_id): """Test expected call to get default pricing tier through ows-request.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value={'items': [{'is_default': True}]}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(3), ).and_return(mock_response)) ows_pricing.get_pricing_tiers(3) def test_get_pricing_tiers( product_id, pricing_tier_message): """Test expected response when getting the default pricing tier.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value=pricing_tier_message) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/pricing-family/{}/orchard-pricing-tier'.format(3), ).and_return(mock_response)) response = ows_pricing.get_pricing_tiers(3) assert response.message == { 'items': [ { 'pricing_family_id': 2, 'sort_order': 1, 'orchard_pricing_tier_id': 8, 'name': 'Deluxe Four', 'is_default': True}, { 'pricing_family_id': 4, 'sort_order': 2, 'orchard_pricing_tier_id': 15, 'name': 'Deluxe Five', 'is_default': False}]} def test_get_product_pricing_validations(product_id, generic_error_payload): """Test expected response when validating pricing.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value={'is_valid': True, 'warnings': []}) (flexmock(request).should_receive('get').with_args( services.OWS_PRICING, '/product/{}/validate'.format(product_id), ).and_return(mock_response)) response = ows_pricing.get_product_pricing_validations(product_id) assert response.message == {'is_valid': True, 'warnings': []}