[PATCH v4 03/10] REST: Add Projects to the API
Finucane, Stephen
stephen.finucane at intel.com
Mon May 23 20:15:23 AEST 2016
On 20 May 14:17, Andy Doan wrote:
> This exports projects via the REST API.
>
> Security Constraints:
> * Anyone (logged in or not) can read all objects.
> * No one can create/delete objects.
> * Project maintainers are allowed to update (ie "patch"
> attributes)
>
> Signed-off-by: Andy Doan <andy.doan at linaro.org>
> Inspired-by: Damien Lespiau <damien.lespiau at intel.com>
> Reviewed-by: Stephen Finucane <stephen.finucane at intel.com>
> ---
> patchwork/rest_serializers.py | 27 +++++++++
> patchwork/settings/base.py | 4 ++
> patchwork/tests/test_rest_api.py | 120 +++++++++++++++++++++++++++++++++++++++
> patchwork/urls.py | 3 +-
> patchwork/views/rest_api.py | 60 +++++++++++++++++++-
> 5 files changed, 211 insertions(+), 3 deletions(-)
> create mode 100644 patchwork/rest_serializers.py
> create mode 100644 patchwork/tests/test_rest_api.py
>
> diff --git a/patchwork/rest_serializers.py b/patchwork/rest_serializers.py
> new file mode 100644
> index 0000000..bbeae37
> --- /dev/null
> +++ b/patchwork/rest_serializers.py
> @@ -0,0 +1,27 @@
> +# Patchwork - automated patch tracking system
> +# Copyright (C) 2016 Linaro Corporation
> +#
> +# This file is part of the Patchwork package.
> +#
> +# Patchwork is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +#
> +# Patchwork is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with Patchwork; if not, write to the Free Software
> +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> +
> +from rest_framework.serializers import HyperlinkedModelSerializer
> +
> +from patchwork.models import Project
> +
> +
> +class ProjectSerializer(HyperlinkedModelSerializer):
> + class Meta:
> + model = Project
> diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
> index 14f3209..735c67a 100644
> --- a/patchwork/settings/base.py
> +++ b/patchwork/settings/base.py
> @@ -158,6 +158,10 @@ ENABLE_XMLRPC = False
>
> # Set to True to enable the Patchwork REST API
> ENABLE_REST_API = False
> +REST_RESULTS_PER_PAGE = 30
> +REST_FRAMEWORK = {
> + 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning'
> +}
>
> # Set to True to enable redirections or URLs from previous versions
> # of patchwork
> diff --git a/patchwork/tests/test_rest_api.py b/patchwork/tests/test_rest_api.py
> new file mode 100644
> index 0000000..56c07a4
> --- /dev/null
> +++ b/patchwork/tests/test_rest_api.py
> @@ -0,0 +1,120 @@
> +# Patchwork - automated patch tracking system
> +# Copyright (C) 2016 Linaro Corporation
> +#
> +# This file is part of the Patchwork package.
> +#
> +# Patchwork is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +#
> +# Patchwork is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with Patchwork; if not, write to the Free Software
> +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> +
> +import unittest
> +
> +from django.conf import settings
> +from django.core.urlresolvers import reverse
> +
> +from rest_framework import status
> +from rest_framework.test import APITestCase
> +
> +from patchwork.models import Project
> +from patchwork.tests.utils import defaults, create_maintainer, create_user
> +
> +
> + at unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API')
> +class TestProjectAPI(APITestCase):
> + fixtures = ['default_states']
> +
> + @staticmethod
> + def api_url(item=None):
> + if item is None:
> + return reverse('api_1.0:project-list')
> + return reverse('api_1.0:project-detail', args=[item])
> +
> + def test_list_simple(self):
> + """Validate we can list the default test project."""
> + defaults.project.save()
> + resp = self.client.get(self.api_url())
> + self.assertEqual(status.HTTP_200_OK, resp.status_code)
> + self.assertEqual(1, len(resp.data))
> + proj = resp.data[0]
> + self.assertEqual(defaults.project.linkname, proj['linkname'])
> + self.assertEqual(defaults.project.name, proj['name'])
> + self.assertEqual(defaults.project.listid, proj['listid'])
> +
> + def test_detail(self):
> + """Validate we can get a specific project."""
> + defaults.project.save()
> + resp = self.client.get(self.api_url(1))
> + self.assertEqual(status.HTTP_200_OK, resp.status_code)
> + self.assertEqual(defaults.project.name, resp.data['name'])
> +
> + def test_anonymous_create(self):
> + """Ensure anonymous POST operations are rejected."""
> + defaults.project.save()
> + resp = self.client.post(
> + self.api_url(),
> + {'linkname': 'l', 'name': 'n', 'listid': 'l', 'listemail': 'e'})
> + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
Think you might have misunderstood me. I meant to add an anonymous case
into the 'test_create', 'test_update' and 'test_delete' functions, i.e.
only have three functions. Not a big deal though :)
More information about the Patchwork
mailing list