diff --git a/mygpo/api/advanced/directory.py b/mygpo/api/advanced/directory.py index fed0c0e22..3364cf27d 100644 --- a/mygpo/api/advanced/directory.py +++ b/mygpo/api/advanced/directory.py @@ -96,7 +96,6 @@ def episode_info(request): @allowed_methods(['POST']) @cors_origin() def add_podcast(request): - # TODO what if the url doesn't have a valid podcast? url = normalize_feed_url(json.loads(request.body.decode('utf-8')).get('url', '')) # 404 before we query for url, because query would complain @@ -114,9 +113,9 @@ def add_podcast(request): res = update_podcasts.delay([url]) response = HttpResponse(status=202) job_status_path = reverse( - 'api-add-podcast-status', kwargs={"job_id": res.task_id} + 'api-add-podcast-status', kwargs={'job_id': res.task_id} ) - response['Location'] = f'{job_status_path}?url={url}' + response['Location'] = f'{job_status_path}' return response diff --git a/mygpo/api/tests.py b/mygpo/api/tests.py index 9bb643264..9b554b454 100644 --- a/mygpo/api/tests.py +++ b/mygpo/api/tests.py @@ -18,6 +18,7 @@ from mygpo.history.models import EpisodeHistoryEntry from mygpo.test import create_auth_string from mygpo.utils import get_timestamp +from mygpo.data.tasks import update_podcasts class AdvancedAPITests(unittest.TestCase): @@ -185,6 +186,30 @@ def test_episode_info(self): self.assertEqual(resp.status_code, 200) + def test_add_podcast_existed(self): + """Test add podcast API for existed podcast""" + url = reverse('api-add-podcast') + body = {'url': self.podcast.url} + location = reverse('api-podcast-info') + '?url=' + self.podcast.url + add_resp = self.client.post(url, body, content_type='application/json') + self.assertEqual(add_resp.status_code, 302) + self.assertEqual(add_resp.get('Location'), location) + + def test_add_podcast_new(self): + """Test add podcast API for new podcast""" + podcast_url = 'http://example.com/new-podcast.xml' + add_url = reverse('api-add-podcast') + body = {'url': podcast_url} + add_resp = self.client.post(add_url, body, content_type='application/json') + self.assertEqual(add_resp.status_code, 202) + status_url = add_resp.get('Location') + status_resp = self.client.get(status_url) + self.assertEqual(status_resp.status_code, 200) + self.assertEqual(status_resp.json().get('status'), 'pending') + job_id = status_url.split('/')[-1] + result = update_podcasts.apply(task_id=job_id) + self.assertTrue(result.ready()) + class EpisodeActionTests(TestCase): def setUp(self): diff --git a/mygpo/api/urls.py b/mygpo/api/urls.py index 131e53356..f43638e44 100644 --- a/mygpo/api/urls.py +++ b/mygpo/api/urls.py @@ -69,7 +69,9 @@ advanced.directory.episode_info, name='api-episode-info', ), - path('api/2/podcasts/create', advanced.directory.add_podcast), + path( + 'api/2/podcasts/create', advanced.directory.add_podcast, name='api-add-podcast' + ), path( 'api/2/task/', advanced.directory.add_podcast_status, diff --git a/mygpo/settings.py b/mygpo/settings.py index 4b0368823..41e4923eb 100644 --- a/mygpo/settings.py +++ b/mygpo/settings.py @@ -401,3 +401,5 @@ def get_intOrNone(name, default): except (ImportError, ValueError): pass + +TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'