From 65e28b6afa5f4ff70306fd564ecf668206a3c31e Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 15 May 2023 11:54:56 +0100 Subject: [PATCH] templates: Use REST API backend for pwclientrc If the REST API is enabled, we should generate a pwclientrc file that uses it by default. Signed-off-by: Stephen Finucane --- patchwork/templates/patchwork/pwclientrc | 9 +- patchwork/tests/views/test_about.py | 12 +-- patchwork/tests/views/test_pwclient.py | 89 +++++++++++++++++++ patchwork/urls.py | 14 +-- patchwork/views/pwclient.py | 25 ++++-- ...entrc-switch-to-rest-4a206883b8f16e4f.yaml | 7 ++ 6 files changed, 128 insertions(+), 28 deletions(-) create mode 100644 patchwork/tests/views/test_pwclient.py create mode 100644 releasenotes/notes/pwclientrc-switch-to-rest-4a206883b8f16e4f.yaml diff --git a/patchwork/templates/patchwork/pwclientrc b/patchwork/templates/patchwork/pwclientrc index 7d466d89..7d6743f0 100644 --- a/patchwork/templates/patchwork/pwclientrc +++ b/patchwork/templates/patchwork/pwclientrc @@ -8,8 +8,7 @@ # default={{ project.linkname }} [{{ project.linkname }}] -url = {{ scheme }}://{{ site.domain }}{% url 'xmlrpc' %} -{% if user.is_authenticated %} -username = {{ user.username }} -password = -{% endif %} +backend = {{ api_backend }} +url = {{ api_scheme }}://{{ site.domain }}{{ api_path }} +{% if user.is_authenticated %}username = {{ user.username }} +password = {% endif %} diff --git a/patchwork/tests/views/test_about.py b/patchwork/tests/views/test_about.py index 56605631..8f831661 100644 --- a/patchwork/tests/views/test_about.py +++ b/patchwork/tests/views/test_about.py @@ -3,9 +3,6 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -import unittest - -from django.conf import settings from django.test import TestCase from django.urls import reverse @@ -19,16 +16,9 @@ class AboutViewTest(TestCase): self.assertRedirects(response, redirect_url, 301) def test_redirects(self): - for view in ['help', 'help-about']: + for view in ['help', 'help-about', 'help-pwclient']: self._test_redirect(view) - @unittest.skipUnless( - settings.ENABLE_XMLRPC, - 'requires xmlrpc interface (use the ENABLE_XMLRPC setting)', - ) - def test_redirects_xmlrpc(self): - self._test_redirect('help-pwclient') - def test_xmlrpc(self): with self.settings(ENABLE_XMLRPC=False): response = self.client.get(reverse('about')) diff --git a/patchwork/tests/views/test_pwclient.py b/patchwork/tests/views/test_pwclient.py new file mode 100644 index 00000000..d16a6c23 --- /dev/null +++ b/patchwork/tests/views/test_pwclient.py @@ -0,0 +1,89 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2023 Stephen Finucane +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import configparser + +from django.test import TestCase +from django.test import override_settings +from django.urls import reverse + +from patchwork.tests.utils import create_project +from patchwork.tests.utils import create_user + + +class PwclientrcTest(TestCase): + def setUp(self): + super().setUp() + self.project = create_project() + + def _get_pwclientrc(self): + response = self.client.get( + reverse('pwclientrc', kwargs={'project_id': self.project.linkname}) + ) + + pwclientrc = configparser.ConfigParser() + pwclientrc.read_string(response.content.decode()) + + return pwclientrc + + @override_settings(ENABLE_REST_API=False) + def test_xmlrpc(self): + pwclientrc = self._get_pwclientrc() + + self.assertTrue(pwclientrc.has_section(self.project.linkname)) + self.assertTrue( + pwclientrc.has_option(self.project.linkname, 'backend') + ) + self.assertEqual( + 'xmlrpc', pwclientrc.get(self.project.linkname, 'backend') + ) + + @override_settings(ENABLE_REST_API=True) + def test_rest(self): + pwclientrc = self._get_pwclientrc() + + self.assertTrue(pwclientrc.has_section(self.project.linkname)) + self.assertTrue( + pwclientrc.has_option(self.project.linkname, 'backend') + ) + self.assertEqual( + 'rest', pwclientrc.get(self.project.linkname, 'backend') + ) + self.assertFalse(pwclientrc.has_option(self.project, 'username')) + self.assertFalse(pwclientrc.has_option(self.project, 'password')) + + @override_settings(ENABLE_REST_API=True) + def test_rest_auth(self): + user = create_user() + user.set_password('12345') + user.save() + self.client.login( + username=user.username, + password='12345', + ) + + pwclientrc = self._get_pwclientrc() + + self.assertTrue(pwclientrc.has_section(self.project.linkname)) + self.assertTrue( + pwclientrc.has_option(self.project.linkname, 'backend') + ) + self.assertEqual( + 'rest', pwclientrc.get(self.project.linkname, 'backend') + ) + self.assertTrue( + pwclientrc.has_option(self.project.linkname, 'username') + ) + self.assertEqual( + user.username, + pwclientrc.get(self.project.linkname, 'username'), + ) + self.assertTrue( + pwclientrc.has_option(self.project.linkname, 'password') + ) + self.assertEqual( + '', + pwclientrc.get(self.project.linkname, 'password'), + ) diff --git a/patchwork/urls.py b/patchwork/urls.py index bd48a4fd..ecd3668d 100644 --- a/patchwork/urls.py +++ b/patchwork/urls.py @@ -188,9 +188,16 @@ urlpatterns = [ path('mail/optin/', mail_views.optin, name='mail-optin'), # about path('about/', about_views.about, name='about'), + # pwclientrc + path( + 'project//pwclientrc/', + pwclient_views.pwclientrc, + name='pwclientrc', + ), # legacy redirects path('help/', about_views.redirect, name='help'), path('help/about/', about_views.redirect, name='help-about'), + path('help/pwclient/', about_views.redirect, name='help-pwclient'), ] if 'debug_toolbar' in settings.INSTALLED_APPS: @@ -203,13 +210,6 @@ if 'debug_toolbar' in settings.INSTALLED_APPS: if settings.ENABLE_XMLRPC: urlpatterns += [ path('xmlrpc/', xmlrpc_views.xmlrpc, name='xmlrpc'), - path( - 'project//pwclientrc/', - pwclient_views.pwclientrc, - name='pwclientrc', - ), - # legacy redirect - path('help/pwclient/', about_views.redirect, name='help-pwclient'), ] if settings.ENABLE_REST_API: diff --git a/patchwork/views/pwclient.py b/patchwork/views/pwclient.py index a8be425b..042485c7 100644 --- a/patchwork/views/pwclient.py +++ b/patchwork/views/pwclient.py @@ -6,6 +6,7 @@ from django.conf import settings from django.shortcuts import get_object_or_404 from django.shortcuts import render +from django.urls import reverse_lazy from patchwork.models import Project @@ -13,16 +14,30 @@ from patchwork.models import Project def pwclientrc(request, project_id): project = get_object_or_404(Project, linkname=project_id) + if settings.FORCE_HTTPS_LINKS or request.is_secure(): + api_scheme = 'https' + else: + api_scheme = 'http' + + if settings.ENABLE_REST_API: + api_backend = 'rest' + api_path = reverse_lazy('api-index') + else: + api_backend = 'xmlrpc' + api_path = reverse_lazy('xmlrpc') + context = { 'project': project, + 'api_backend': api_backend, + 'api_path': api_path, + 'api_scheme': api_scheme, } - if settings.FORCE_HTTPS_LINKS or request.is_secure(): - context['scheme'] = 'https' - else: - context['scheme'] = 'http' response = render( - request, 'patchwork/pwclientrc', context, content_type='text/plain' + request, + 'patchwork/pwclientrc', + context, + content_type='text/plain', ) response['Content-Disposition'] = 'attachment; filename=.pwclientrc' diff --git a/releasenotes/notes/pwclientrc-switch-to-rest-4a206883b8f16e4f.yaml b/releasenotes/notes/pwclientrc-switch-to-rest-4a206883b8f16e4f.yaml new file mode 100644 index 00000000..9f000142 --- /dev/null +++ b/releasenotes/notes/pwclientrc-switch-to-rest-4a206883b8f16e4f.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + The sample ``pwclientrc`` files found at ``/project//pwclientrc`` + will now use the ``rest`` backend by default if the REST API is enabled. + In addition, it is now possible to download a sample ``pwclientrc`` file + even if the XML-RPC API backend is disabled. -- 2.47.3