[v2,2/2] tests/api: Test comments

Message ID 20180411174513.25938-2-vkabatov@redhat.com
State Superseded
Headers show
Series
  • [v2,1/2] api: Add comments to patch and cover endpoints
Related show

Commit Message

Veronika Kabatova April 11, 2018, 5:45 p.m.
From: Veronika Kabatova <vkabatov@redhat.com>

Check that comments field is added to patch / cover letter details with
API version 1.1. This required minor change to api_url() method to avoid
errors in case both the 'item' and 'version' parameters are passed
(can't use both *args and **kwargs with reverse()).

Signed-off-by: Veronika Kabatova <vkabatov@redhat.com>
---
 patchwork/tests/api/test_cover.py | 16 +++++++++++++++-
 patchwork/tests/api/test_patch.py | 24 +++++++++++++++++++++---
 2 files changed, 36 insertions(+), 4 deletions(-)

Patch

diff --git a/patchwork/tests/api/test_cover.py b/patchwork/tests/api/test_cover.py
index 3135b7e..7375a27 100644
--- a/patchwork/tests/api/test_cover.py
+++ b/patchwork/tests/api/test_cover.py
@@ -22,11 +22,13 @@  import unittest
 from django.conf import settings
 
 from patchwork.compat import reverse
+from patchwork.tests.utils import create_comment
 from patchwork.tests.utils import create_cover
 from patchwork.tests.utils import create_maintainer
 from patchwork.tests.utils import create_person
 from patchwork.tests.utils import create_project
 from patchwork.tests.utils import create_user
+from patchwork.tests.utils import SAMPLE_CONTENT
 
 if settings.ENABLE_REST_API:
     from rest_framework import status
@@ -49,7 +51,8 @@  class TestCoverLetterAPI(APITestCase):
 
         if item is None:
             return reverse('api-cover-list', kwargs=kwargs)
-        return reverse('api-cover-detail', args=[item], kwargs=kwargs)
+        kwargs['pk'] = item
+        return reverse('api-cover-detail', kwargs=kwargs)
 
     def assertSerialized(self, cover_obj, cover_json):
         self.assertEqual(cover_obj.id, cover_json['id'])
@@ -115,6 +118,17 @@  class TestCoverLetterAPI(APITestCase):
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
         self.assertSerialized(cover_obj, resp.data)
 
+        # test comments
+        comment_obj = create_comment(submission=cover_obj)
+        resp = self.client.get(self.api_url(cover_obj.id))
+        self.assertEqual(1, len(resp.data['comments']))
+        self.assertEqual(comment_obj.id, resp.data['comments'][0]['id'])
+        self.assertEqual(SAMPLE_CONTENT, resp.data['comments'][0]['content'])
+
+        # test old version of API
+        resp = self.client.get(self.api_url(cover_obj.id, version='1.0'))
+        self.assertNotIn('comments', resp.data)
+
     def test_create_update_delete(self):
         user = create_maintainer()
         user.is_superuser = True
diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py
index 909c1eb..57a269c 100644
--- a/patchwork/tests/api/test_patch.py
+++ b/patchwork/tests/api/test_patch.py
@@ -24,12 +24,14 @@  from django.conf import settings
 
 from patchwork.compat import reverse
 from patchwork.models import Patch
+from patchwork.tests.utils import create_comment
 from patchwork.tests.utils import create_maintainer
 from patchwork.tests.utils import create_patch
 from patchwork.tests.utils import create_person
 from patchwork.tests.utils import create_project
 from patchwork.tests.utils import create_state
 from patchwork.tests.utils import create_user
+from patchwork.tests.utils import SAMPLE_CONTENT
 
 if settings.ENABLE_REST_API:
     from rest_framework import status
@@ -45,10 +47,15 @@  class TestPatchAPI(APITestCase):
     fixtures = ['default_tags']
 
     @staticmethod
-    def api_url(item=None):
+    def api_url(item=None, version=None):
+        kwargs = {}
+        if version:
+            kwargs['version'] = version
+
         if item is None:
-            return reverse('api-patch-list')
-        return reverse('api-patch-detail', args=[item])
+            return reverse('api-patch-list', kwargs=kwargs)
+        kwargs['pk'] = item
+        return reverse('api-patch-detail', kwargs=kwargs)
 
     def assertSerialized(self, patch_obj, patch_json):
         self.assertEqual(patch_obj.id, patch_json['id'])
@@ -130,6 +137,17 @@  class TestPatchAPI(APITestCase):
         self.assertEqual(patch.diff, resp.data['diff'])
         self.assertEqual(0, len(resp.data['tags']))
 
+        # test comments
+        comment = create_comment(submission=patch)
+        resp = self.client.get(self.api_url(patch.id))
+        self.assertEqual(1, len(resp.data['comments']))
+        self.assertEqual(comment.id, resp.data['comments'][0]['id'])
+        self.assertEqual(SAMPLE_CONTENT, resp.data['comments'][0]['content'])
+
+        # test old version of API
+        resp = self.client.get(self.api_url(item=patch.id, version='1.0'))
+        self.assertNotIn('comments', resp.data)
+
     def test_create(self):
         """Ensure creations are rejected."""
         project = create_project()