"""Tests for the API utilities.""" from collections import namedtuple import os from unittest.mock import MagicMock import pytest from asset_transcoder.constants import asset_upload from asset_transcoder.utils import api_utils from asset_transcoder.utils.exceptions import OwsError Context = namedtuple('Context', 'identity_uuid identity_id orchard_user_id context_type') def test_get_pipeline_config(monkeypatch): """Test that we can get different configs for different apps.""" monkeypatch.setattr(asset_upload, 'OBJECT_APP_RELATION', { 'PODCAST': ('episode', 'podcast'), 'WORKSTATION': ('product', 'artist') }) os.environ['OUTPUT_ASSETS_PODCAST_BUCKET_NAME'] = 'podcast-output-bucket' os.environ['OUTPUT_ASSETS_WORKSTATION_BUCKET_NAME'] = 'workstation-output-bucket' os.environ['PREVIEW_ASSETS_PODCAST_BUCKET_NAME'] = 'podcast-output-bucket' os.environ['PREVIEW_ASSETS_WORKSTATION_BUCKET_NAME'] = 'asset-storage' os.environ['ELASTIC_TRANSCODER_PODCAST_ID'] = 'podcast-transcoder-id' os.environ['ELASTIC_TRANSCODER_WORKSTATION_ID'] = 'workstation-transcoder-id' podcast_config = api_utils.get_pipeline_config('podcast') assert podcast_config == { 'output_bucket_name': 'podcast-output-bucket', 'preview_bucket_name': 'podcast-output-bucket', 'elastic_transcoder_id': 'podcast-transcoder-id' } product_config = api_utils.get_pipeline_config('product') assert product_config == { 'output_bucket_name': 'workstation-output-bucket', 'preview_bucket_name': 'asset-storage', 'elastic_transcoder_id': 'workstation-transcoder-id' } def test_get_user_id_identity(monkeypatch): """Test get user id.""" context = Context(identity_uuid=None, identity_id=1, orchard_user_id=2, context_type='profile') monkeypatch.setattr(api_utils, '_get_request_context', MagicMock(return_value=context)) result = api_utils.get_user_id() assert result == 1 def test_get_user_id_orchard_id(monkeypatch): """Test gets orchard id.""" context = Context(identity_uuid=None, identity_id=None, orchard_user_id=2, context_type='account') monkeypatch.setattr(api_utils, '_get_request_context', MagicMock(return_value=context)) result = api_utils.get_user_id() assert result == 2 def test_get_user_id_uuid(monkeypatch): """Test gets orchard id.""" context = Context(identity_uuid=5, identity_id=None, orchard_user_id=2, context_type='account') monkeypatch.setattr(api_utils, '_get_request_context', MagicMock(return_value=context)) result = api_utils.get_user_id() assert result == 5 def test_get_user_id_error(monkeypatch): """Test no user.""" context = Context(identity_uuid=None, identity_id=None, orchard_user_id=None, context_type='error') monkeypatch.setattr(api_utils, '_get_request_context', MagicMock(return_value=context)) with pytest.raises(OwsError): api_utils.get_user_id()