"""Unit tests for Asset logic.""" import json from owsrequest import request as requests from owsrequest import test_utils from promo_player.constants import service_name from promo_player.logic import asset def test_get_product_assets_v2(monkeypatch): """Test retrieving a list of assets for a product from ows-assets.""" product_id = 1 json_response_mock = { 'assets': [ {'tuid': 1, 'stream': {'duration': 60}}, {'tuid': 2, 'stream': {'duration': 90}} ] } path = '/v2/asset/product/{product_id}'.format(product_id=product_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 200, 'json': json_response_mock }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_product_assets( product_id, vendor_id=1, subaccount_id=None) assert len(result.message['assets']) == 2 def test_get_product_assets_error(monkeypatch): """Test that an error response is returned if the request fails.""" product_id = 1 path = '/v2/asset/product/{product_id}'.format(product_id=product_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 404 }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_product_assets( product_id, vendor_id=1, subaccount_id=None) assert result.status == 404 def test_get_product_assets_no_product_id(monkeypatch): """Test that an empty response is returned if no product id.""" product_id = None vendor_id = None subaccount_id = None result = asset.get_product_assets(product_id, vendor_id, subaccount_id) assert result assert len(result.message['assets']) == 0 def test_get_product_cover_url(monkeypatch): """Test retrieving a cover URL for a product from ows-assets.""" product_id = 1 json_response_mock = 'https://qa-images.theorchard.io/blabla' path = '/image/product/large_cover/{product_id}/location'.format( product_id=product_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 200, 'json': json_response_mock }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_product_cover_url(product_id) assert result.message == json.dumps(json_response_mock) def test_get_product_cover_url_not_found(monkeypatch): """Test that a success response is returned if request returns a 404.""" product_id = 1 path = '/image/product/large_cover/{product_id}/location'.format( product_id=product_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 404 }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_product_cover_url(product_id) assert result.status == 200 assert result.message == '' def test_get_product_cover_url_error(monkeypatch): """Test that an error response is returned if the request fails.""" product_id = 1 path = '/image/product/large_cover/{product_id}/location'.format( product_id=product_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 500 }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_product_cover_url(product_id) assert result.status == 500 def test_get_product_cover_url_no_product_id(monkeypatch): """Test that an empty response is returned if no product id.""" product_id = None result = asset.get_product_cover_url(product_id) assert result assert result.message == '' def test_get_track_manifest_url(monkeypatch): """Test retrieving a HLS manifest URL from ows-assets.""" track_id = 1 json_response_mock = { 'url': 'https://manifest.test' } path = '/stream/track/{track_id}/hls'.format(track_id=track_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 200, 'json': json_response_mock }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_track_manifest_url(track_id) assert result.message['url'] == json_response_mock['url'] def test_get_track_manifest_url_error(monkeypatch): """Test that an error response is returned if the request fails.""" track_id = 1 path = '/stream/track/{track_id}/hls'.format(track_id=track_id) call_specs = [{ 'service': service_name.OWS_ASSETS, 'path': path, 'status': 404 }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = asset.get_track_manifest_url(track_id) assert result.status == 404