[PATCH 8/8] xmlrpc: massively increase test coverage

Daniel Axtens dja at axtens.net
Sat Sep 3 17:07:20 AEST 2016


XMLRPC defines a bunch of very similar methods for getting and
listing a bunch of different models. So create a mixin that allows
us to test an arbitrary model, then use that to create test classes
to test the various models.

Add other tests as well.

Signed-off-by: Daniel Axtens <dja at axtens.net>
---
 patchwork/tests/test_xmlrpc.py | 196 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 179 insertions(+), 17 deletions(-)

diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py
index ec804ac668c3..d3b8b9fbf818 100644
--- a/patchwork/tests/test_xmlrpc.py
+++ b/patchwork/tests/test_xmlrpc.py
@@ -24,42 +24,204 @@ from django.core.urlresolvers import reverse
 from django.test import LiveServerTestCase
 from django.utils.six.moves import xmlrpc_client
 
-from patchwork.tests.utils import create_patches
+from patchwork.tests import utils
 
 
 @unittest.skipUnless(settings.ENABLE_XMLRPC,
                      'requires xmlrpc interface (use the ENABLE_XMLRPC '
                      'setting)')
 class XMLRPCTest(LiveServerTestCase):
-
     def setUp(self):
         self.url = self.live_server_url + reverse('xmlrpc')
         self.rpc = xmlrpc_client.Server(self.url)
 
+
+class XMLRPCGenericTest(XMLRPCTest):
+    def test_pw_rpc_version(self):
+        # If you update the RPC version, update the tests!
+        self.assertEqual(self.rpc.pw_rpc_version(), [1, 2, 0])
+
     def test_get_redirect(self):
         response = self.client.patch(self.url)
         self.assertRedirects(
             response, reverse('help', kwargs={'path': 'pwclient/'}))
 
+    def test_invalid_method(self):
+        with self.assertRaises(xmlrpc_client.Fault):
+            self.rpc.xyzzy()
+
+    def test_absent_auth(self):
+        with self.assertRaises(xmlrpc_client.Fault):
+            self.rpc.patch_set(0, {})
+
+
+class XMLRPCAuthenticatedTest(LiveServerTestCase):
+    def setUp(self):
+        self.url = self.live_server_url + reverse('xmlrpc')
+        # url is of the form http://localhost:PORT/PATH
+        # strip the http and replace it with the username/passwd of a user.
+        self.project = utils.create_project()
+        self.user = utils.create_maintainer(self.project)
+        self.url = ('http://%s:%s@' + self.url[7:]) % (self.user.username,
+                                                       self.user.username)
+        self.rpc = xmlrpc_client.Server(self.url)
+
+    def test_patch_set(self):
+        patch = utils.create_patch(project=self.project)
+        result = self.rpc.patch_get(patch.id)
+        self.assertFalse(result['archived'])
+
+        self.rpc.patch_set(patch.id, {'archived': True})
+
+        # reload the patch
+        result = self.rpc.patch_get(patch.id)
+        self.assertTrue(result['archived'])
+
+
+class XMLRPCModelTestMixin:
+    def CREATE_MULTIPLE(self, count):
+        return [self.CREATE_ONE() for i in range(count)]
+
+    def test_get_none(self):
+        self.assertEqual(self.GET_ENDPOINT(0), {})
+
+    def test_list_none(self):
+        self.assertEqual(self.LIST_ENDPOINT(), [])
+
     def test_list_single(self):
-        patch_objs = create_patches()
-        patches = self.rpc.patch_list()
-        self.assertEqual(len(patches), 1)
-        self.assertEqual(patches[0]['id'], patch_objs[0].id)
+        obj = self.CREATE_ONE()
+        result = self.LIST_ENDPOINT()
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], obj.id)
+
+    def test_list_named(self):
+        obj = self.CREATE_ONE(name='FOOBARBAZ')
+        self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT('oobarb')
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], obj.id)
+
+    def test_list_named_none(self):
+        self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT('invisible')
+        self.assertEqual(len(result), 0)
+
+    def test_get_single(self):
+        obj = self.CREATE_ONE()
+        result = self.GET_ENDPOINT(obj.id)
+        self.assertEqual(result['id'], obj.id)
+
+    def test_get_invalid(self):
+        obj = self.CREATE_ONE()
+        result = self.GET_ENDPOINT(obj.id + 1)
+        self.assertEqual(result, {})
 
     def test_list_multiple(self):
-        create_patches(5)
-        patches = self.rpc.patch_list()
-        self.assertEqual(len(patches), 5)
+        self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT()
+        self.assertEqual(len(result), 5)
+
+    def test_list_max_count(self):
+        objs = self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT("", 2)
+        self.assertEqual(len(result), 2)
+        self.assertEqual(result[0]['id'], objs[0].id)
+
+    def test_list_negative_max_count(self):
+        objs = self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT("", -1)
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], objs[-1].id)
+
 
+class XMLRPCFilterModelTestMixin(XMLRPCModelTestMixin):
+    # override these tests due to the way you pass in filters
     def test_list_max_count(self):
-        patch_objs = create_patches(5)
-        patches = self.rpc.patch_list({'max_count': 2})
-        self.assertEqual(len(patches), 2)
-        self.assertEqual(patches[0]['id'], patch_objs[0].id)
+        objs = self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT({'max_count': 2})
+        self.assertEqual(len(result), 2)
+        self.assertEqual(result[0]['id'], objs[0].id)
 
     def test_list_negative_max_count(self):
-        patch_objs = create_patches(5)
-        patches = self.rpc.patch_list({'max_count': -1})
-        self.assertEqual(len(patches), 1)
-        self.assertEqual(patches[0]['id'], patch_objs[-1].id)
+        objs = self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT({'max_count': -1})
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], objs[-1].id)
+
+    def test_list_named(self):
+        obj = self.CREATE_ONE(name='FOOBARBAZ')
+        self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT({'name__icontains': 'oobarb'})
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], obj.id)
+
+    def test_list_named_none(self):
+        self.CREATE_MULTIPLE(5)
+        result = self.LIST_ENDPOINT({'name__icontains': 'invisible'})
+        self.assertEqual(len(result), 0)
+
+
+class XMLRPCPatchTest(XMLRPCTest, XMLRPCFilterModelTestMixin):
+    def setUp(self):
+        super(XMLRPCPatchTest, self).setUp()
+        self.GET_ENDPOINT = self.rpc.patch_get
+        self.LIST_ENDPOINT = self.rpc.patch_list
+        self.CREATE_MULTIPLE = utils.create_patches
+
+    def CREATE_ONE(self, **kwargs):
+        return utils.create_patches(**kwargs)[0]
+
+    def test_patch_check_get(self):
+        patch = self.CREATE_ONE()
+        check = utils.create_check(patch=patch)
+        result = self.rpc.patch_check_get(patch.id)
+        self.assertEqual(result['total'], 1)
+        self.assertEqual(result['checks'][0]['id'], check.id)
+        self.assertEqual(result['checks'][0]['patch_id'], patch.id)
+
+    def test_patch_get_by_hash(self):
+        patch = self.CREATE_ONE()
+        result = self.rpc.patch_get_by_hash(patch.hash)
+        self.assertEqual(result['id'], patch.id)
+
+
+class XMLRPCPersonTest(XMLRPCTest, XMLRPCModelTestMixin):
+    def setUp(self):
+        super(XMLRPCPersonTest, self).setUp()
+        self.GET_ENDPOINT = self.rpc.person_get
+        self.LIST_ENDPOINT = self.rpc.person_list
+        self.CREATE_ONE = utils.create_person
+
+
+class XMLRPCProjectTest(XMLRPCTest, XMLRPCModelTestMixin):
+    def setUp(self):
+        super(XMLRPCProjectTest, self).setUp()
+        self.GET_ENDPOINT = self.rpc.project_get
+        self.LIST_ENDPOINT = self.rpc.project_list
+        self.CREATE_ONE = utils.create_project
+
+    # project filters by linkname, not name!
+    def test_list_named(self):
+        obj = self.CREATE_ONE(linkname='FOOBARBAZ')
+        result = self.LIST_ENDPOINT('oobarb')
+        self.assertEqual(len(result), 1)
+        self.assertEqual(result[0]['id'], obj.id)
+
+
+class XMLRPCStateTest(XMLRPCTest, XMLRPCModelTestMixin):
+    def setUp(self):
+        super(XMLRPCStateTest, self).setUp()
+        self.GET_ENDPOINT = self.rpc.state_get
+        self.LIST_ENDPOINT = self.rpc.state_list
+        self.CREATE_ONE = utils.create_state
+
+
+class XMLRPCCheckTest(XMLRPCTest, XMLRPCFilterModelTestMixin):
+    def setUp(self):
+        super(XMLRPCCheckTest, self).setUp()
+        self.GET_ENDPOINT = self.rpc.check_get
+        self.LIST_ENDPOINT = self.rpc.check_list
+        self.CREATE_ONE = utils.create_check
+
+    def test_list_named(self):
+        pass
-- 
2.7.4



More information about the Patchwork mailing list