"""Functional tests for retrieving a marketing highlights. Test endpoint: GET /marketing/ """ import json from product_digital_marketing.constants import header from product_digital_marketing.constants import services from tests.unit.factories import highlight_factory from tests.unit.factories import highlight_projection_factory from tests.unit.test_utils import db PRODUCT_ID = 42 TERRITORY1_ID = 100 TERRITORY2_ID = 200 @db.test_schema def test_get_highlights_success(client, request_engine, grass_headers): """Check that get highlights works.""" request_engine[services.OWS_PRODUCT].add_spec( 'HEAD', '/vendor/1234/product/{}'.format(PRODUCT_ID)) highlight = highlight_factory.HighlightFactory( product_id=PRODUCT_ID, global_highlight='Some general drivers', spotify_highlight='awesome spotify drivers', apple_highlight='crazy apple drivers', spotify_total=None, apple_total=300, downloads_total=100500, updated_by='OA:111', ) db.seed_models([highlight]) response = client.get( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers) assert response.status_code == 200 data = json.loads(response.data.decode()) assert data == { 'global_highlight': 'Some general drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } @db.test_schema def test_get_highlights_success_with_projections( client, request_engine, grass_headers): """Check that get highlights with projections works.""" request_engine[services.OWS_PRODUCT].add_spec( 'HEAD', '/vendor/1234/product/{}'.format(PRODUCT_ID)) highlight = highlight_factory.HighlightFactory( product_id=PRODUCT_ID, global_highlight='Some general drivers', spotify_highlight='awesome spotify drivers', apple_highlight='crazy apple drivers', spotify_total=None, apple_total=300, downloads_total=100500, updated_by='OA:111', ) db.seed_models([highlight]) highlight_projection1 = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY1_ID, spotify_projection=1, apple_projection=9000, downloads_projection=9001, highlight='Cool highlight 1', priority='A' ) highlight_projection2 = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY2_ID, spotify_projection=1, apple_projection=9000, downloads_projection=9001, highlight='Cool highlight 2', priority='B' ) db.seed_models([highlight_projection1, highlight_projection2]) response = client.get( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers) assert response.status_code == 200 data = json.loads(response.data.decode()) assert data == { 'global_highlight': 'Some general drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [ { 'id': highlight_projection1.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY1_ID, 'spotify_projection': 1, 'apple_projection': 9000, 'downloads_projection': 9001, 'highlight': 'Cool highlight 1', 'priority': 'A' }, { 'id': highlight_projection2.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY2_ID, 'spotify_projection': 1, 'apple_projection': 9000, 'downloads_projection': 9001, 'highlight': 'Cool highlight 2', 'priority': 'B' } ] } @db.test_schema def test_get_highlights_failure(client): """Check that get highlights fails when data is missing.""" response = client.get('/marketing/{}'.format(PRODUCT_ID)) assert response.status_code == 404 def test_get_highlights_ownership_fails(client, grass_headers, request_engine): """Check that endpoint is not accessible for wrong user.""" request_engine[services.OWS_PRODUCT].add_spec( 'HEAD', '/vendor/1234/product/{}'.format(PRODUCT_ID), status=403) response = client.get( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers) assert response.status_code == 403 data = json.loads(response.data.decode()) assert data['message'] == 'Product is not owned by account' def test_get_highlights_invalid_headers(client, grass_headers): """Check that endpoint is not with wrong GRASS headers.""" del grass_headers[header.GRASS_ACCOUNT_ID] response = client.get( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers) assert response.status_code == 400 data = json.loads(response.data.decode()) assert data['message'] == 'Incomplete Grass Headers'