From 6f65b25e48153572751a8ecdf778345759ee3b5a Mon Sep 17 00:00:00 2001 From: Ssuching Yu Date: Fri, 17 Jan 2020 13:34:19 +0800 Subject: [PATCH 1/3] Tests for adding new podcasts API. --- mygpo/api/advanced/directory.py | 3 +-- mygpo/api/tests.py | 21 +++++++++++++++++++++ mygpo/api/urls.py | 4 +++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/mygpo/api/advanced/directory.py b/mygpo/api/advanced/directory.py index fed0c0e22..952ce5471 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,7 +113,7 @@ 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}' return response diff --git a/mygpo/api/tests.py b/mygpo/api/tests.py index 9bb643264..bd83cb311 100644 --- a/mygpo/api/tests.py +++ b/mygpo/api/tests.py @@ -185,6 +185,27 @@ 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') + 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, From e2095ef44c3df118a0ba2c5eaddb27191d751c3f Mon Sep 17 00:00:00 2001 From: Ssuching Yu Date: Fri, 17 Jan 2020 20:17:04 +0800 Subject: [PATCH 2/3] Correct Location header of response from adding podcast API for newly created podcasts. --- mygpo/api/advanced/directory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mygpo/api/advanced/directory.py b/mygpo/api/advanced/directory.py index 952ce5471..3364cf27d 100644 --- a/mygpo/api/advanced/directory.py +++ b/mygpo/api/advanced/directory.py @@ -115,7 +115,7 @@ def add_podcast(request): job_status_path = reverse( '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 From 2899ce1fd30a4d75ccf0a27ca004464a58b0604b Mon Sep 17 00:00:00 2001 From: Ssuching Yu Date: Sun, 19 Jan 2020 03:52:13 +0800 Subject: [PATCH 3/3] Test result of adding new podcast API. --- mygpo/api/tests.py | 4 ++++ mygpo/settings.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/mygpo/api/tests.py b/mygpo/api/tests.py index bd83cb311..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): @@ -205,6 +206,9 @@ def test_add_podcast_new(self): 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): 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'