]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
templates: Use REST API backend for pwclientrc
authorStephen Finucane <stephen@that.guru>
Mon, 15 May 2023 10:54:56 +0000 (11:54 +0100)
committerStephen Finucane <stephen@that.guru>
Mon, 15 May 2023 11:51:37 +0000 (12:51 +0100)
If the REST API is enabled, we should generate a pwclientrc file that
uses it by default.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/templates/patchwork/pwclientrc
patchwork/tests/views/test_about.py
patchwork/tests/views/test_pwclient.py [new file with mode: 0644]
patchwork/urls.py
patchwork/views/pwclient.py
releasenotes/notes/pwclientrc-switch-to-rest-4a206883b8f16e4f.yaml [new file with mode: 0644]

index 7d466d890da59bf1a877a403483c24c707504afe..7d6743f05062ec135101c2abdfdbc0620ba0d7bc 100644 (file)
@@ -8,8 +8,7 @@
 # default={{ project.linkname }}
 
 [{{ project.linkname }}]
-url = {{ scheme }}://{{ site.domain }}{% url 'xmlrpc' %}
-{% if user.is_authenticated %}
-username = {{ user.username }}
-password = <add your patchwork password here>
-{% endif %}
+backend = {{ api_backend }}
+url = {{ api_scheme }}://{{ site.domain }}{{ api_path }}
+{% if user.is_authenticated %}username = {{ user.username }}
+password = <add your patchwork password here>{% endif %}
index 56605631213c60a8f1d48ee64223ec768cacefa8..8f831661e74b5004ea626e942f7e0529b8e919c4 100644 (file)
@@ -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 (file)
index 0000000..d16a6c2
--- /dev/null
@@ -0,0 +1,89 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2023 Stephen Finucane <stephen@that.guru>
+#
+# 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(
+            '<add your patchwork password here>',
+            pwclientrc.get(self.project.linkname, 'password'),
+        )
index bd48a4fd34128b64325d5e5c9c32815022e48a7d..ecd3668de84ba279b2e07d3fcf4d7d179e787673 100644 (file)
@@ -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/<project_id>/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/<project_id>/pwclientrc/',
-            pwclient_views.pwclientrc,
-            name='pwclientrc',
-        ),
-        # legacy redirect
-        path('help/pwclient/', about_views.redirect, name='help-pwclient'),
     ]
 
 if settings.ENABLE_REST_API:
index a8be425b075dcab5ce0a105b48272b0bd7c0d994..042485c7327f00880c51fc768aa2cb17df529951 100644 (file)
@@ -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 (file)
index 0000000..9f00014
--- /dev/null
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    The sample ``pwclientrc`` files found at ``/project/<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.