"""Tests for Pricing Helper.""" import datetime import time from unittest.mock import MagicMock from oto import response from pricing import config from pricing.connectors import s3 from pricing.logic import pricing_helper from pricing.logic import track_pricing_override from pricing.models import default_account_pricing_tier \ as default_account_pricing_tier_model from pricing.models import orchard_pricing_tier as orchard_pricing_tier_model from pricing.models import ows_product from pricing.models import product_orchard_pricing_tier\ as product_orchard_pricing_tier_model from pricing.models import product_pricing_override\ as product_pricing_override_model from pricing.models import token import pytest @pytest.fixture def fixture_product_details_for_vendor(): """Fixture for a single product details of vendor.""" return response.Response({'vendor_id': 1234, 'subaccount_id': ''}) @pytest.fixture def fixture_product_details_for_subaccount(): """Fixture for a single product details of subaccount.""" return response.Response({'vendor_id': 1234, 'subaccount_id': 5678}) @pytest.fixture def fixture_default_account_pricing_tier(): """Fixture for single default account pricing tier.""" return response.Response({'orchard_pricing_tier_id': 3}) @pytest.fixture def fixture_not_found(): """Fixture for not found response.""" return response.create_not_found_response() @pytest.fixture def fixture_orchard_pricing_tiers(): """Fixture for pricing families list.""" return response.Response( message={ 'items': [{ 'orchard_pricing_tier_id': 8, 'pricing_family_id': 2, 'name': 'Deluxe Four', 'is_default': False, 'sort_order': 1}] }, status=200) @pytest.fixture def fixture_product_pricing_overrides(): """Fixture for single product pricing override.""" return response.Response( message={ 'items': [{ 'product_id': 1, 'pricing_family_id': 1, 'product_pricing_override_id': 1, 'applies_worldwide': True, 'store_id': 1, 'orchard_pricing_tier_id': 3}] }, status=200) @pytest.fixture def fixture_track_pricing_overrides(): """Fixture for multiple track pricing overrides.""" return response.Response( message={ 'items': [ { 'track_id': 1, 'track_pricing_override_id': 1, 'applies_worldwide': True, 'store_id': 1, 'orchard_pricing_tier_id': 3 } ] }, status=200) @pytest.fixture def fixture_product_orchard_pricing_tier(): """Fixture for product based pricing tier.""" return response.Response({ 'orchard_pricing_tier_id': 8, 'pricing_family_id': 2, 'name': 'Deluxe Four', 'is_default': False, 'sort_order': 1 }) def test_apply_pricing_element_to_list(): """Test applying a pricing element to a list of elements.""" pricing_element_to_apply = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'HD', 'store_pricing_tier_name': 'Override', 'price_code': 'apply' } pricing_elements_target = [ { 'territories': ['US', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target1' }, { 'territories': ['BE', 'CA'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Mid', 'price_code': 'target2' } ] result = pricing_helper.apply_pricing_element_to_list( pricing_element_to_apply, pricing_elements_target, True) assert result == [ { 'territories': ['FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target1' }, { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'SD', 'store_pricing_tier_name': 'Front', 'price_code': 'target1' }, { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'HD', 'store_pricing_tier_name': 'Override', 'price_code': 'apply' }, { 'territories': ['BE', 'CA'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Mid', 'price_code': 'target2' } ] def test_apply_pricing_element_to_single_element_no_overlap_territory(): """Test applying a pricing element to a single element.""" pricing_element_to_apply = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'apply' } pricing_element_target = { 'territories': ['BE', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'target' } result = pricing_helper.apply_pricing_element_to_single_element( pricing_element_to_apply, pricing_element_target, True) assert result == [ { 'territories': ['BE', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'target' } ] def test_apply_pricing_element_to_single_element_no_overlap_resolution(): """Test applying a pricing element to a single element.""" pricing_element_to_apply = { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'HD', 'price_code': 'apply' } pricing_element_target = { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'SD', 'price_code': 'target' } result = pricing_helper.apply_pricing_element_to_single_element( pricing_element_to_apply, pricing_element_target, True) assert result == [ { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'SD', 'price_code': 'target' } ] def test_apply_pricing_element_to_single_element_partial_overlap_ovl_dates(): """Test applying a pricing element to a single element.""" pricing_element_to_apply = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'apply' } pricing_element_target = { 'territories': ['US', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target' } result = pricing_helper.apply_pricing_element_to_single_element( pricing_element_to_apply, pricing_element_target, True) assert result == [ { 'territories': ['FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target' }, { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Custom', 'price_code': 'apply' } ] def test_apply_pricing_element_to_single_element_partial_overlap_custom(): """Test applying a pricing element with custom price to an element.""" pricing_element_to_apply = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'apply', 'custom_price': '123', 'custom_currency_code': 'USD' } pricing_element_target = { 'territories': ['US', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target' } result = pricing_helper.apply_pricing_element_to_single_element( pricing_element_to_apply, pricing_element_target, True) assert result == [ { 'territories': ['FR'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'store_pricing_tier_name': 'Front', 'price_code': 'target' }, { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'resolution': 'All', 'price_code': 'apply', 'store_pricing_tier_name': 'Custom', 'custom_price': '123', 'custom_currency_code': 'USD' } ] def test_apply_pricing_element_to_single_element_track_ids_overlap_ovl_dates(): """Test applying a pricing element to a single element.""" pricing_element_to_apply = { 'track_ids': [1], 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'track_ids': [1, 2, 3, 4, 5], 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.apply_pricing_element_to_single_element( pricing_element_to_apply, pricing_element_target, True) assert result == [ { 'track_ids': [2, 3, 4, 5], 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' }, { 'track_ids': [1], 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply', 'store_pricing_tier_name': 'Custom' } ] def test_split_on_territories_no_overlap(): """Test splitting on territories works for no overlap.""" pricing_element_to_apply = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': ['CA', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'subtract': [pricing_element_target] } def test_split_on_territories_apply_worldwide(): """Test splitting on territories works for applying worldwide.""" pricing_element_to_apply = { 'territories': [], 'applies_worldwide': True, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': ['CA', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_territories_all_overlap(): """Test splitting on territories works for complete overlap.""" pricing_element_to_apply = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_territories_partial_overlap(): """Test splitting on territories works for partial overlap.""" pricing_element_to_apply = { 'territories': ['US', 'FR'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' }, 'subtract': [ { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } ] } def test_split_on_territories_apply_territories_to_worldwide(): """Test splitting on territories works for list onto worldwide.""" pricing_element_to_apply = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': [], 'applies_worldwide': True, 'territory_list_include': False, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' }, 'subtract': [ { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'target' } ] } def test_split_on_territories_apply_positive_to_negative_list(): """Test splitting on territories works for list onto negative list.""" pricing_element_to_apply = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'apply' } pricing_element_target = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) expected_result = { 'intersect': { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' }, 'subtract': [ { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'target' } ] } check_territories_correct( result['intersect'], expected_result['intersect']) check_territories_correct( result['subtract'][0], expected_result['subtract'][0]) def test_split_on_territories_apply_negative_to_positive_list(): """Test splitting on territories works for negative onto positive list.""" pricing_element_to_apply = { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'apply' } pricing_element_target = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': { 'territories': ['BE'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' }, 'subtract': [ { 'territories': ['US'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } ] } def test_split_on_territories_apply_negative_to_negative_list(): """Test splitting on territories works for negative onto negative list.""" pricing_element_to_apply = { 'territories': ['US', 'FR'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'apply' } pricing_element_target = { 'territories': ['US', 'BE'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'target' } result = pricing_helper.split_on_territories( pricing_element_to_apply, pricing_element_target) expected_result = { 'intersect': { 'territories': ['US', 'BE', 'FR'], 'applies_worldwide': False, 'territory_list_include': False, 'price_code': 'target' }, 'subtract': [ { 'territories': ['FR'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'target' } ] } check_territories_correct( result['intersect'], expected_result['intersect']) check_territories_correct( result['subtract'][0], expected_result['subtract'][0]) def test_split_on_resolutions_no_overlap(): """Test splitting on resolutions works for no overlap.""" pricing_element_to_apply = { 'resolution': 'HD', 'price_code': 'apply' } pricing_element_target = { 'resolution': 'SD', 'price_code': 'target' } result = pricing_helper.split_on_resolutions( pricing_element_to_apply, pricing_element_target) assert result == { 'subtract': [pricing_element_target] } def test_split_on_resolutions_no_resolution_on_target(): """Test splitting on resolutions works for target without resolution.""" pricing_element_to_apply = { 'resolution': 'HD', 'price_code': 'apply' } pricing_element_target = { 'price_code': 'target' } result = pricing_helper.split_on_resolutions( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_resolutions_partial_overlap(): """Test splitting on resolutions works for no overlap.""" pricing_element_to_apply = { 'resolution': 'HD', 'price_code': 'apply' } pricing_element_target = { 'resolution': 'All', 'price_code': 'target' } result = pricing_helper.split_on_resolutions( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': { 'resolution': 'HD', 'price_code': 'target' }, 'subtract': [ { 'resolution': 'SD', 'price_code': 'target' } ] } def test_split_on_resolutions_all_overlap(): """Test splitting on resolutions works for no overlap.""" pricing_element_to_apply = { 'resolution': 'HD', 'price_code': 'apply' } pricing_element_target = { 'resolution': 'HD', 'price_code': 'target' } result = pricing_helper.split_on_resolutions( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_resolutions_all_overlap_all(): """Test splitting on resolutions works for no overlap.""" pricing_element_to_apply = { 'resolution': 'All', 'price_code': 'apply' } pricing_element_target = { 'resolution': 'All', 'price_code': 'target' } result = pricing_helper.split_on_resolutions( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_dates_both_all_time_use_overlapping_dates(): """Test splitting on dates works for with no start or end dates.""" pricing_element_to_apply = { 'price_code': 'apply' } pricing_element_target = { 'price_code': 'target' } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_dates_apply_all_time_target_range_use_overlapping_dates(): """Test splitting on dates works when apply has no start or end dates.""" pricing_element_to_apply = { 'price_code': 'apply' } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2000, 1, 1), 'end_date': datetime.datetime(2020, 1, 1) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_split_on_dates_apply_range_to_all_time_use_overlapping_dates(): """Test splitting on dates works for partial overlap.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2000, 1, 1), 'end_date': datetime.datetime(2020, 1, 1) } pricing_element_target = { 'price_code': 'target' } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'intersect': { 'price_code': 'target', 'start_date': datetime.datetime(2000, 1, 1), 'end_date': datetime.datetime(2020, 1, 1) }, 'subtract': [ { 'price_code': 'target', 'end_date': datetime.datetime(2000, 1, 1) }, { 'price_code': 'target', 'start_date': datetime.datetime(2020, 1, 1) } ] } def test_split_on_dates_partial_overlap_apply_after_use_overlapping_dates(): """Test splitting on dates works for partial overlap.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 4, 4) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 3, 3) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'intersect': { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 3, 3) }, 'subtract': [ { 'price_code': 'target', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 2, 2) } ] } def test_split_on_dates_partial_overlap_apply_after_non_overlapping_dates(): """Test splitting on dates works for partial overlap.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 4, 4) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 3, 3) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, False) assert result == { 'intersect': { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 3, 3) }, 'subtract': [ { 'price_code': 'target', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 2, 1) } ] } def test_split_on_dates_partial_overlap_apply_before_use_overlapping_dates(): """Test splitting on dates works for partial overlap.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 3, 3) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 4, 4) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'intersect': { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 3, 3) }, 'subtract': [ { 'price_code': 'target', 'start_date': datetime.datetime(2017, 3, 3), 'end_date': datetime.datetime(2017, 4, 4) } ] } def test_split_on_dates_partial_overlap_apply_before_use_non_overlapping(): """Test splitting on dates works for partial overlap.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 3, 3) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 4, 4) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, False) assert result == { 'intersect': { 'price_code': 'target', 'start_date': datetime.datetime(2017, 2, 2), 'end_date': datetime.datetime(2017, 3, 3) }, 'subtract': [ { 'price_code': 'target', 'start_date': datetime.datetime(2017, 3, 4), 'end_date': datetime.datetime(2017, 4, 4) } ] } def test_split_on_dates_no_overlap_apply_before_use_overlapping_dates(): """Test splitting on dates works for no overlap with apply before.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 3, 3) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 4, 4), 'end_date': datetime.datetime(2017, 5, 5) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'subtract': [pricing_element_target] } def test_split_on_dates_no_overlap_apply_after_use_overlapping_dates(): """Test splitting on dates works for no overlap with apply before.""" pricing_element_to_apply = { 'price_code': 'apply', 'start_date': datetime.datetime(2017, 4, 4), 'end_date': datetime.datetime(2017, 5, 5) } pricing_element_target = { 'price_code': 'target', 'start_date': datetime.datetime(2017, 1, 1), 'end_date': datetime.datetime(2017, 2, 2) } result = pricing_helper.split_on_dates( pricing_element_to_apply, pricing_element_target, True) assert result == { 'subtract': [pricing_element_target] } def test_split_on_track_ids_no_overlap(): """Test splitting on track_ids works for no overlap.""" pricing_element_to_apply = { 'track_ids': [1], 'price_code': 'apply' } pricing_element_target = { 'track_ids': [2, 3, 4, 5], 'price_code': 'target' } result = pricing_helper.split_on_track_ids( pricing_element_to_apply, pricing_element_target) assert result == { 'subtract': [pricing_element_target] } def test_split_on_track_ids_partial_overlap(): """Test splitting on track_ids works for partial overlap.""" pricing_element_to_apply = { 'track_ids': [1], 'price_code': 'apply' } pricing_element_target = { 'track_ids': [1, 2, 3, 4, 5], 'price_code': 'target' } result = pricing_helper.split_on_track_ids( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': { 'track_ids': [1], 'price_code': 'target' }, 'subtract': [ { 'track_ids': [2, 3, 4, 5], 'price_code': 'target' } ] } def test_split_on_track_ids_all_overlap(): """Test splitting on track_ids works for a complete overlap.""" pricing_element_to_apply = { 'track_ids': [1], 'price_code': 'apply' } pricing_element_target = { 'track_ids': [1], 'price_code': 'target' } result = pricing_helper.split_on_track_ids( pricing_element_to_apply, pricing_element_target) assert result == { 'intersect': pricing_element_target, 'subtract': [] } def test_convert_and_default_dates(): """Test converting dates from timestamps works.""" now_timestamp = time.time() now_datetime = datetime.datetime.fromtimestamp(now_timestamp) pricing_element = { 'price_code': 'apply', 'start_date': now_timestamp, 'end_date': now_timestamp } pricing_helper.convert_and_default_dates(pricing_element) assert pricing_element['start_date'] == now_datetime assert pricing_element['end_date'] == now_datetime def check_territories_correct(item1, item2): """Test splitting on dates works for partial overlap.""" assert item1['applies_worldwide'] == item2['applies_worldwide'] assert item1['territory_list_include'] == item2['territory_list_include'] assert item1['price_code'] == item2['price_code'] assert set(item2['territories']) == set(item2['territories']) def test_get_default_account_pricing_tier_success_for_vendor( monkeypatch, fixture_product_details_for_vendor, fixture_default_account_pricing_tier): """Test to get default account pricing tier by pricing family of vendor.""" monkeypatch.setattr( ows_product, 'get_product_details', MagicMock(return_value=fixture_product_details_for_vendor)) monkeypatch.setattr( default_account_pricing_tier_model, 'get_by_account_and_pricing_family_id', MagicMock(return_value=fixture_default_account_pricing_tier)) result = pricing_helper.get_default_account_pricing_tier(1234, 1) assert result assert result.message == fixture_default_account_pricing_tier.message def test_get_default_account_pricing_tier_success_for_subaccount( monkeypatch, fixture_product_details_for_subaccount, fixture_default_account_pricing_tier): """Test to get default account pricing tier for subaccount.""" monkeypatch.setattr( ows_product, 'get_product_details', MagicMock(return_value=fixture_product_details_for_subaccount)) monkeypatch.setattr( default_account_pricing_tier_model, 'get_by_account_and_pricing_family_id', MagicMock(return_value=fixture_default_account_pricing_tier)) result = pricing_helper.get_default_account_pricing_tier(1234, 1) assert result assert result.message == fixture_default_account_pricing_tier.message def test_get_default_account_pricing_tier_not_found( monkeypatch, fixture_not_found): """Test to get default account pricing tier by pricing family not found.""" monkeypatch.setattr( ows_product, 'get_product_details', MagicMock(return_value=fixture_not_found)) result = pricing_helper.get_default_account_pricing_tier(1234, 1) assert result.message == fixture_not_found.message @pytest.fixture def fixture_username(): """Fixture to get a name representing a user.""" return 'anyone' @pytest.fixture def fixture_duration(): """Fixture to duration.""" return 1000 @pytest.fixture def fixture_fail(): """Fixture to get a negative response.""" return response.create_error_response('fail', 'now') @pytest.fixture def fixture_ok(): """Fixture to get a successful response.""" return response.Response('ok') @pytest.fixture def fixture_credential(): """Fixture to get an AWS credential dictionary.""" credential = {'token': 'a_token'} return response.Response(credential) def test__generate_filename(): """Test the generated filename does not contain hyphens.""" result = pricing_helper._generate_filename() assert '-' not in result def test_get_upload_permission_no_user(fixture_duration): """Test get_upload_permission fails without user.""" result = pricing_helper.get_upload_permission( False, fixture_duration) assert result.status == 400 def test_get_upload_permission( monkeypatch, fixture_credential, fixture_ok, fixture_username, fixture_duration): """Test get_upload_permission succeeds.""" bucket = 'a_bucket' folder = 'a_folder' filename = 'a_filename' monkeypatch.setattr(config, 'PRICING_BUCKET', bucket) monkeypatch.setattr(config, 'PRICING_FOLDER', folder) monkeypatch.setattr( pricing_helper, '_generate_filename', MagicMock( return_value=filename)) token_mock = MagicMock(return_value=fixture_credential) monkeypatch.setattr(token, 'get_s3_token', token_mock) result = pricing_helper.get_upload_permission( fixture_username, fixture_duration) assert result.message['bucket'] == f'{bucket}/{folder}' assert result.message['filename'] == filename assert result.message['credentials'] == fixture_credential.message def test_get_upload_permission_fails_credential_error(monkeypatch): """Test credentials fails.""" user_id = 123 duration = 900 mock_response = MagicMock( return_value=response.create_fatal_response ('sts_token_generation_failure')) monkeypatch.setattr(token, 'get_s3_token', mock_response) result = pricing_helper.get_upload_permission(user_id, duration) assert result.status == 500 def test_get_all_digital_product_pricing( monkeypatch, fixture_orchard_pricing_tiers, fixture_product_orchard_pricing_tier, fixture_product_pricing_overrides, fixture_track_pricing_overrides): """Test get_all_digital_product_pricing succeeds.""" monkeypatch.setattr( orchard_pricing_tier_model, 'get_orchard_pricing_tier_by_pricing_family_ids', MagicMock(return_value=fixture_orchard_pricing_tiers)) monkeypatch.setattr( product_orchard_pricing_tier_model, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=fixture_product_orchard_pricing_tier)) monkeypatch.setattr( product_pricing_override_model, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) monkeypatch.setattr( track_pricing_override, 'get_track_pricing_overrides_for_product', MagicMock(return_value=fixture_track_pricing_overrides)) result = pricing_helper.get_all_digital_product_pricing(1) assert result assert result.message['pricing_tier_options'] ==\ fixture_orchard_pricing_tiers.message assert result.message['album_pricing_tier'] ==\ fixture_product_orchard_pricing_tier.message assert result.message['track_pricing_tier'] ==\ fixture_product_orchard_pricing_tier.message assert result.message['album_pricing_override'] ==\ fixture_product_pricing_overrides.message assert result.message['track_pricing_override'] ==\ fixture_track_pricing_overrides.message @pytest.mark.asyncio def test_get_presigned_urls(monkeypatch): """Test get_presigned_urls succeeds""" mock_s3_client = MagicMock() monkeypatch.setattr( s3, 'get_s3_client', mock_s3_client ) filename = 'test.json' monkeypatch.setattr( pricing_helper, '_generate_filename', MagicMock( return_value=filename)) folder = 'folder' monkeypatch.setattr(config, 'PRICING_FOLDER', folder) mock_url = 'https://aws.com/upload_url' upload_token = 'upload-id-test' mock_s3_client_instance = mock_s3_client() mock_s3_client_instance.create_multipart_upload = MagicMock(return_value={'UploadId': upload_token, 'Key': filename}) mock_s3_client_instance.generate_presigned_url = MagicMock(return_value=mock_url) result = pricing_helper.get_presigned_urls(2) assert result.message['filename'] == f'{folder}/{filename}' assert result.message['upload_token'] == upload_token assert result.message['presigned_urls'] == [ { 'part_number': 1, 'url': mock_url }, { 'part_number': 2, 'url': mock_url } ] @pytest.mark.asyncio def test_complete_multipart_upload(monkeypatch): """Test complete_multipart_upload succeeds""" mock_s3_client = MagicMock() monkeypatch.setattr( s3, 'get_s3_client', mock_s3_client ) data = { 'filename': 'test.json', 'upload_token': 'upload_id_test', 'parts': [ { 'part_number': 1, 'etag': 'test-etag' }, { 'part_number': 2, 'etag': 'test-etag2' } ] } result = pricing_helper.complete_multipart_upload(data) assert result.message['status'] == 'success'