"""Tests for Insertion Point logic tier.""" from unittest.mock import ANY from unittest.mock import MagicMock from unittest.mock import patch from oto import status import pytest from podcast.logic import insertion_point from podcast.logic import megaphone from podcast.logic import user as user_logic from podcast.models import episode as episode_model from podcast.models import insertion_point as point_model from podcast.models import ows_asset_transcoder as oat from podcast.models import podcast as podcast_model from podcast.utils.exc import OwsError @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_insertion_points( mock_get_feature_flag, monkeypatch, db_session_fixture, get_assets_mock, mock_current_admin_user): """Test create points model tier called properly.""" podcast_id = 2 episode_id = 3 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] monkeypatch.setattr(user_logic, 'current_user_has_read_only_access_then_raise', MagicMock()) monkeypatch.setattr(user_logic, 'current_user_owns_podcast_or_raise', MagicMock()) monkeypatch.setattr(point_model, 'create_insertion_points', MagicMock(return_value={'items': new_points})) monkeypatch.setattr(episode_model, 'update_episode', MagicMock(return_value={'is_reviewed': True})) monkeypatch.setattr(megaphone, 'create_or_update_episode', MagicMock()) monkeypatch.setattr(podcast_model, 'get_podcast_by_id', MagicMock( return_value={'megaphone_id': 12, 'network_id': 1, 'feed_type': 'private-rss'})) result = insertion_point.create_insertion_points(podcast_id, episode_id, new_points) point_model.create_insertion_points.assert_called_once_with(episode_id, new_points, db_session_fixture) megaphone.create_or_update_episode.assert_called_once() oat._get_assets.assert_called_once() episode_model.update_episode.assert_called_once_with(episode_id, { 'planned_pre_roll_count': 0, 'planned_mid_roll_count': 10, 'planned_post_roll_count': 0 }, db_session_fixture) assert result['items'] == new_points @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_insertion_points_no_access(mock_get_feature_flag, mock_current_user): """Test create points fails with no access.""" podcast_id = 2 episode_id = 1 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert err.value.status == 403 def test_create_insertion_points_read_only(mock_current_read_only_user): """Test create points fails if user is read only.""" podcast_id = 1 episode_id = 1 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert err.value.status == 403 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_insertion_points_fails(mock_get_feature_flag, monkeypatch, mock_current_user): """Test create points model tier not called as there is no episode_id in Episode.""" podcast_id = 1 episode_id = 11 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] monkeypatch.setattr(point_model, 'create_insertion_points', MagicMock(return_value={'items': new_points})) with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert not point_model.create_insertion_points.called assert err.value.status == status.NOT_FOUND @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_insertion_points_show_family_access_success( mock_get_feature_flag, monkeypatch, get_assets_mock, mock_current_network_admin_user ): """Test create points model tier called properly with show family access.""" podcast_id = 1 episode_id = 1 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] monkeypatch.setattr(point_model, 'create_insertion_points', MagicMock(return_value={'items': new_points})) monkeypatch.setattr(episode_model, 'update_episode', MagicMock(return_value={'is_reviewed': True})) monkeypatch.setattr(megaphone, 'create_or_update_episode', MagicMock()) result = insertion_point.create_insertion_points(podcast_id, episode_id, new_points) point_model.create_insertion_points.assert_called_once_with(episode_id, new_points, ANY) megaphone.create_or_update_episode.assert_called_once() oat._get_assets.assert_called_once() episode_model.update_episode.assert_called_once_with(episode_id, { 'planned_pre_roll_count': 0, 'planned_mid_roll_count': 10, 'planned_post_roll_count': 0 }, ANY) assert result['items'] == new_points @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_insertion_points_no_show_family_access( mock_get_feature_flag, mock_current_podcast_level_user): """Test create points fails with no show family access.""" podcast_id = 2 episode_id = 1 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert err.value.status == 403 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_insertion_points_and_not_reviewed_not_sent_to_megaphone( mock_get_feature_flag, monkeypatch, get_assets_mock, mock_current_network_admin_user ): """Test create points model tier called properly with show family access. If is_reviewed is False then megaphone's update api is not called. """ podcast_id = 1 episode_id = 2 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] monkeypatch.setattr(point_model, 'create_insertion_points', MagicMock(return_value={'items': new_points})) monkeypatch.setattr(episode_model, 'update_episode', MagicMock(return_value={'is_reviewed': False})) monkeypatch.setattr(megaphone, 'create_or_update_episode', MagicMock()) result = insertion_point.create_insertion_points(podcast_id, episode_id, new_points) point_model.create_insertion_points.assert_called_once_with(episode_id, new_points, ANY) assert not megaphone.create_or_update_episode.called assert not oat._get_assets.called episode_model.update_episode.assert_called_once_with(episode_id, { 'planned_pre_roll_count': 0, 'planned_mid_roll_count': 10, 'planned_post_roll_count': 0 }, ANY) assert result['items'] == new_points @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_insertion_points_error_for_apple_subscription_feeds( mock_get_feature_flag, monkeypatch, get_assets_mock, mock_current_network_admin_user ): """Test create points error for apple subscription feed.""" podcast_id = 3 episode_id = 2 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert err.value.status == 400 assert err.value.message == 'Invalid feed type. Feed type should be one of public-rss or private-rss.' @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_insertion_points_error_for_youtube_feeds( mock_get_feature_flag, monkeypatch, get_assets_mock, mock_current_admin_user ): """Test create points error for youtube feed.""" podcast_id = 4 episode_id = 2 new_points = [ {'id': 1, 'point_type': 'mid', 'count': 10, 'timecode': '55.55'}, ] with pytest.raises(OwsError) as err: insertion_point.create_insertion_points(podcast_id, episode_id, new_points) assert err.value.status == 400 assert err.value.message == 'Invalid feed type. Feed type should be one of public-rss or private-rss.'