"""Test for AdAction logic tier.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from podcast.logic import ad_action_comment from podcast.logic import email from podcast.models import ad_action_comment as ad_action_comment_model from podcast.utils.exc import OwsError @patch('podcast.logic.user.current_user_has_access_to_ad_action_or_raise') @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_admin( mock_get_feature_flag, mock_current_user_has_access_to_ad_action_or_raise, monkeypatch, ad_read_comment_fixture, mock_current_admin_user): """Test get ad action comments for admin.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments', MagicMock(return_value=ad_read_comment_fixture) ) result = ad_action_comment.get_ad_action_comments(1, 50, 0) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 1 @patch('podcast.logic.user_v2.current_user_has_access_to_ad_action_or_raise') @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_get_ad_action_comments_admin_with_ia_restructure_on( mock_get_feature_flag, mock_current_user_has_access_to_ad_action_or_raise, monkeypatch, ad_read_comment_fixture, mock_current_admin_user): """Test get ad action comments for admin.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments', MagicMock(return_value=ad_read_comment_fixture) ) result = ad_action_comment.get_ad_action_comments(1, 50, 0) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 1 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_all_network_admin( mock_get_feature_flag, monkeypatch, ad_read_comment_fixture, mock_current_network_admin_user_all_access): """Test get ad action comments for all network admin.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments', MagicMock(return_value=ad_read_comment_fixture) ) result = ad_action_comment.get_ad_action_comments(1, 50, 0) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 1 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_no_access_network_admin( mock_get_feature_flag, monkeypatch, mock_current_network_admin_user_second): """Test get ad action comments when no network access to network admin.""" with pytest.raises(OwsError) as err: ad_action_comment.get_ad_action_comments(1, 50, 0) assert err.value.status == 403 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_network_admin( mock_get_feature_flag, monkeypatch, ad_read_comment_fixture, mock_current_network_admin_user): """Test get ad action comments for network admin.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments', MagicMock(return_value=ad_read_comment_fixture) ) result = ad_action_comment.get_ad_action_comments(1, 50, 0) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 1 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_producer( mock_get_feature_flag, monkeypatch, ad_read_comment_fixture, mock_current_user): """Test get ad action comments for producer.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments', MagicMock(return_value=ad_read_comment_fixture) ) result = ad_action_comment.get_ad_action_comments(1, 50, 0) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 1 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_get_ad_action_comments_no_access_producer( mock_get_feature_flag, monkeypatch, mock_current_podcast_level_user): """Test get ad action comments when no network access to producer.""" with pytest.raises(OwsError) as err: ad_action_comment.get_ad_action_comments(2, 50, 0) assert err.value.status == 403 @patch('podcast.logic.user.current_user_has_access_to_ad_action_or_raise') @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_admin( mock_get_feature_flag, mock_current_user_has_access_to_ad_action_or_raise, monkeypatch, new_ad_read_comment_fixture, mock_current_admin_user): """Test create ad action comment by admin.""" monkeypatch.setattr( ad_action_comment_model, 'create_ad_action_comment', MagicMock(return_value='test comment') ) monkeypatch.setattr(email, 'send_new_ad_action_comment', MagicMock()) result = ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert result == 'test comment' mock_current_user_has_access_to_ad_action_or_raise.assert_called_once() email.send_new_ad_action_comment.assert_called_with(1, 'test comment', 2) @patch('podcast.logic.user_v2.current_user_has_access_to_ad_action_or_raise') @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=True) def test_create_ad_action_comment_admin_with_ia_restructure_on( mock_get_feature_flag, mock_current_user_has_access_to_ad_action_or_raise, monkeypatch, new_ad_read_comment_fixture, mock_current_admin_user): """Test create ad action comment by admin.""" monkeypatch.setattr( ad_action_comment_model, 'create_ad_action_comment', MagicMock(return_value='test comment') ) monkeypatch.setattr(email, 'send_new_ad_action_comment', MagicMock()) result = ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert result == 'test comment' mock_current_user_has_access_to_ad_action_or_raise.assert_called_once() email.send_new_ad_action_comment.assert_called_with(1, 'test comment', 2) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_all_network_admin( mock_get_feature_flag, monkeypatch, new_ad_read_comment_fixture, mock_current_network_admin_user_all_access): """Test create ad action comment for all network admin.""" monkeypatch.setattr( ad_action_comment_model, 'create_ad_action_comment', MagicMock(return_value='test comment') ) monkeypatch.setattr(email, 'send_new_ad_action_comment', MagicMock()) result = ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert result == 'test comment' email.send_new_ad_action_comment.assert_called_with(1, 'test comment', 13) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_no_access_network_admin( mock_get_feature_flag, monkeypatch, new_ad_read_comment_fixture, mock_current_network_admin_user_second): """Test create ad action comment when no network access to network admin.""" with pytest.raises(OwsError) as err: ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert err.value.status == 403 @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_network_admin( mock_get_feature_flag, monkeypatch, new_ad_read_comment_fixture, mock_current_network_admin_user): """Test create ad action comment for network admin.""" monkeypatch.setattr( ad_action_comment_model, 'create_ad_action_comment', MagicMock(return_value='test comment') ) monkeypatch.setattr(email, 'send_new_ad_action_comment', MagicMock()) result = ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert result == 'test comment' email.send_new_ad_action_comment.assert_called_with(1, 'test comment', 6) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_producer( mock_get_feature_flag, monkeypatch, new_ad_read_comment_fixture, mock_current_user): """Test create ad action comment for producer.""" monkeypatch.setattr( ad_action_comment_model, 'create_ad_action_comment', MagicMock(return_value='test comment') ) monkeypatch.setattr(email, 'send_new_ad_action_comment', MagicMock()) result = ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert result == 'test comment' email.send_new_ad_action_comment.assert_called_with(1, 'test comment', 1) @patch('podcast.utils.feature_flag_utils.get_feature_flag', return_value=False) def test_create_ad_action_comment_no_access_producer( mock_get_feature_flag, monkeypatch, new_ad_read_comment_fixture, mock_current_podcast_level_user): """Test create ad action comment when no network access to producer.""" new_ad_read_comment_fixture['ad_action_id'] = 2 with pytest.raises(OwsError) as err: ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert err.value.status == 403 def test_create_ad_action_comment_read_only( monkeypatch, new_ad_read_comment_fixture, mock_current_read_only_user): """Test create ad action comment for read only user.""" with pytest.raises(OwsError) as err: ad_action_comment.create_ad_action_comment(new_ad_read_comment_fixture) assert err.value.status == 403 def test_delete_ad_action_comment_admin(monkeypatch, mock_current_admin_user): """Test delete ad action comment for admin.""" monkeypatch.setattr( ad_action_comment_model, 'delete_ad_action_comment', MagicMock(return_value='test comment') ) ad_action_comment.delete_ad_action_comment(1) ad_action_comment_model.delete_ad_action_comment.assert_called_once_with(1, 2) def test_delete_ad_action_comment_network_admin(monkeypatch, mock_current_network_admin_user): """Test delete ad action comment for network admin.""" monkeypatch.setattr( ad_action_comment_model, 'delete_ad_action_comment', MagicMock(return_value='test comment') ) ad_action_comment.delete_ad_action_comment(1) ad_action_comment_model.delete_ad_action_comment.assert_called_once_with(1, 6) def test_delete_ad_action_comment_producer(monkeypatch, mock_current_user): """Test delete ad action comment for producer.""" monkeypatch.setattr( ad_action_comment_model, 'delete_ad_action_comment', MagicMock(return_value='test comment') ) ad_action_comment.delete_ad_action_comment(1) ad_action_comment_model.delete_ad_action_comment.assert_called_once_with(1, 1) def test_delete_ad_action_comment_read_only(monkeypatch, mock_current_read_only_user): """Test delete ad action comment for read only user.""" with pytest.raises(OwsError) as err: ad_action_comment.delete_ad_action_comment(1) assert err.value.status == 403 def test_get_ad_action_comments_count_admin(monkeypatch, mock_current_admin_user): """Test get ad action comments count for admin.""" monkeypatch.setattr( ad_action_comment_model, 'get_ad_action_comments_count', MagicMock(return_value='test') ) result = ad_action_comment.get_ad_action_comments_count([1, 2]) assert result == 'test'