"""Functional tests for creating/updating a marketing highlights. Test endpoint: GET /marketing/ """ import json from product_digital_marketing.constants import error from product_digital_marketing.constants import header from product_digital_marketing.constants import services from tests.unit.factories import highlight_factory from tests.unit.test_utils import db PRODUCT_ID = 42 @db.test_schema def test_upsert_highlight_success(client, request_engine, grass_headers): """Check that creating a highlight works.""" request_engine[services.OWS_PRODUCT].add_spec( 'HEAD', '/vendor/1234/product/{}'.format(PRODUCT_ID)) body = { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, } response = client.post( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers, data=json.dumps(body)) assert response.status_code == 201 data = json.loads(response.data.decode()) assert data == { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } @db.test_schema def test_upsert_highlight_success_already_exists( client, request_engine, grass_headers): """Check that creating a highlight 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', ) body = { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, } db.seed_models([highlight]) response = client.post( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers, data=json.dumps(body)) assert response.status_code == 201 data = json.loads(response.data.decode()) assert data == { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } def test_upsert_highlight_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.post( '/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_upsert_highlight_schema_fails( client, grass_headers, request_engine): """Check that endpoint is not with an invalid schema.""" request_engine[services.OWS_PRODUCT].add_spec( 'HEAD', '/vendor/1234/product/{}'.format(PRODUCT_ID)) body = { 'something_bad': 'Going hog wild on the requests over here', } response = client.post( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers, data=json.dumps(body)) assert response.status_code == 400 data = json.loads(response.data.decode()) assert data['code'] == error.VALIDATION_ERROR_CODE assert data['message']['apple_total'] == [ 'Missing data for required field.'] def test_upsert_highlight_invalid_headers(client, grass_headers): """Check that endpoint is not with wrong GRASS headers.""" del grass_headers[header.GRASS_ACCOUNT_ID] response = client.post( '/marketing/{}'.format(PRODUCT_ID), headers=grass_headers) assert response.status_code == 400 data = json.loads(response.data.decode()) assert data['message'] == 'Incomplete Grass Headers'