"""Test for podcast links logic tier.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from podcast.logic import podcast as podcast_logic from podcast.logic import podcast_links as podcast_links_logic from podcast.models import podcast as podcast_model from podcast.models import podcast_links as podcast_links_model from podcast.utils.exc import OwsError @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_podcast_links(mock_get_feature_flag, monkeypatch, podcast_fixture, mock_current_user): """Test get podcast links.""" monkeypatch.setattr(podcast_model, 'get_podcast_by_id', MagicMock(return_value=podcast_fixture)) monkeypatch.setattr(podcast_links_model, 'get_podcast_links', MagicMock(return_value='test')) result = podcast_links_logic.get_podcast_links(1) assert result == 'test' @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_podcast_links_no_access(mock_get_feature_flag, podcast_fixture_2, monkeypatch, mock_current_user): """Test get podcast links no access.""" monkeypatch.setattr(podcast_model, 'get_podcast_by_id', MagicMock(return_value=podcast_fixture_2)) with pytest.raises(OwsError) as err: podcast_links_logic.get_podcast_links(2) assert err.value.status == 403 def test_create_podcast_links_no_access(mock_current_user): """Test create podcast links no access unless admin.""" with pytest.raises(OwsError) as err: podcast_links_logic.create_podcast_links({}) assert err.value.status == 403 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_saves_apple_id(mock_get_feature_flag, mock_current_admin_user): """Test saves the apple id.""" link = 'https://podcasts.apple.com/us/podcast/broken-seeking-justice/id1478460758' podcast_links_logic.create_podcast_links({ 'podcast_id': 1, 'links': [{'store_id': 1, 'link': link}] }) result = podcast_logic.get_podcasts_by_ids([1]) assert result['items'][0]['apple_id'] == '1478460758' @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_removes_apple_id(mock_get_feature_flag, mock_current_admin_user): """Test removes the apple id.""" link = 'https://podcasts.apple.com/us/podcast/broken-seeking-justice/id1478460758' podcast_links_logic.create_podcast_links({ 'podcast_id': 1, 'links': [{'store_id': 1, 'link': link}] }) podcast_links_logic.create_podcast_links({ 'podcast_id': 1, 'links': [] }) result = podcast_logic.get_podcasts_by_ids([1]) assert result['items'][0]['apple_id'] is None def test_create_embed_code_success(mock_current_admin_user): """Test create embed code success.""" link = 'https://playlist.megaphone.fm?p=SONY1234567890' result = podcast_links_logic.create_podcast_links({ 'podcast_id': 1, 'links': [{'store_id': 12, 'link': link}] }) assert result['items'][0] == { 'id': 4, 'link': f'', 'podcast_id': 1, 'store_id': 12 } def test_create_embed_code_failure(mock_current_admin_user): """Test create embed code failure for non-megaphone format src.""" link = 'https://test.fm?p=SONY1234567890' with pytest.raises(OwsError) as err: podcast_links_logic.create_podcast_links({ 'podcast_id': 1, 'links': [{'store_id': 12, 'link': link}] }) assert err.value.status == 400 assert err.value.message == 'Invalid embed code src link' @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_get_podcast_links_show_family_access( mock_get_feature_flag, monkeypatch, podcast_fixture, mock_current_user): """Test get podcast links.""" monkeypatch.setattr(podcast_model, 'get_podcast_by_id', MagicMock(return_value=podcast_fixture)) monkeypatch.setattr(podcast_links_model, 'get_podcast_links', MagicMock(return_value='test')) result = podcast_links_logic.get_podcast_links(1) assert result == 'test' @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_get_podcast_links_show_family_access_failure( mock_get_feature_flag, podcast_fixture_2, monkeypatch, mock_current_user): """Test get podcast links no access.""" monkeypatch.setattr(podcast_model, 'get_podcast_by_id', MagicMock(return_value=podcast_fixture_2)) with pytest.raises(OwsError) as err: podcast_links_logic.get_podcast_links(2) assert err.value.status == 403