"""Test for the Api to Podcasts.""" from unittest.mock import MagicMock from unittest.mock import patch from podcast.models.api_podcast import ApiPodcast def test_get_podcasts(): """Test the podcasts end point.""" podcast = ApiPodcast(podcast_network_id=1) podcast._paginated_get = MagicMock(return_value={'content': [{'id': 'cow'}]}) result = podcast.get_all(1, 20) assert result['content'][0]['id'] == 'cow' podcast._paginated_get.assert_called_with('podcasts?page=1&per_page=20') def test_get_podcast(): """Test the podcast end point.""" podcast = ApiPodcast(podcast_network_id=1) podcast._get = MagicMock(return_value={'id': 'cow'}) result = podcast.get('podcast_id') assert result['id'] == 'cow' podcast._get.assert_called_with('podcasts/podcast_id') def test_create_podcast(): """Test the create end point.""" podcast = ApiPodcast(podcast_network_id=1) podcast._post = MagicMock(return_value={'id': 'cow'}) podcast_data = { 'title': 'The Best Podcast in the Universe', 'description': 'The best podcast about things', 'itunesCategories': ['History', 'Society & Culture'], 'language': 'en', 'link': 'http://thebestpodcastintheuniverse.com', 'copyright': 'Supermegacorp', 'show_type': 'serial', 'host': 'Bob Smith', 'backgroundImageFileUrl': 'http://example.com/path/to/image', 'ownerName': 'Jane Smith' } result = podcast.create(podcast_data) assert result['id'] == 'cow' podcast._post.assert_called_with('podcasts', podcast_data) def test_delete_podcast(): """Test the delete podcast end point.""" podcast = ApiPodcast(podcast_network_id=1) podcast._delete = MagicMock(return_value={'success': 'message'}) result = podcast.delete('id') assert 'success' in result podcast._delete.assert_called_with('podcasts/id') def test_update_podcast(): """Test the update podcast end point.""" podcast = ApiPodcast(podcast_network_id=1) podcast._put = MagicMock(return_value={'title': 'Bingy!'}) data = {'title': 'Bingy!'} result = podcast.update('id', data) assert result['title'] == 'Bingy!' podcast._put.assert_called_with('podcasts/id', data) @patch('requests.post') def test_create_two_podcasts_in_different_networks(requests_mock): """Test that API uses different urls for different networks.""" requests_mock.return_value = MagicMock(status_code=200) api_podcast_1 = ApiPodcast(podcast_network_id=1) api_podcast_2 = ApiPodcast(podcast_network_id=2) podcast_data = { 'title': 'The Best Podcast in the Universe', 'description': 'The best podcast about things' } api_podcast_1.create(podcast_data) requests_mock.assert_called_with( 'https://cms.megaphone.fm/api/networks/64756b62-7e5c-4728-895a-2f42f8969e14/podcasts', headers=api_podcast_1._headers(), json=podcast_data ) api_podcast_2.create(podcast_data) requests_mock.assert_called_with( 'https://cms.megaphone.fm/api/networks/74756b62-7e5c-4728-895a-2f42f8969e15/podcasts', headers=api_podcast_2._headers(), json=podcast_data )