home · contact · privacy
Catch POSTs to undefined targets, expand and refactor tests.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 04:34:30 +0000 (05:34 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 04:34:30 +0000 (05:34 +0100)
plomtask/http.py
tests/days.py
tests/misc.py [new file with mode: 0644]
tests/processes.py
tests/utils.py

index 3dde6e200ed50d5b7dab65e7c648dceacee1ed82..cd3e4459dbe96f6ad0a65cba6ddf45a3162f0f3f 100644 (file)
@@ -128,7 +128,10 @@ class TaskHandler(BaseHTTPRequestHandler):
             form_data = PostvarsParser(postvars)
             if site in ('day', 'process'):
                 getattr(self, f'do_POST_{site}')(conn, params, form_data)
-            conn.commit()
+                conn.commit()
+            else:
+                msg = f'Page not known as POST target: /{site}'
+                raise NotFoundException(msg)
             conn.close()
             self._redirect('/')
         except HandledException as error:
index 2850bb810c3af71e03a65e92e6f8f42d1768f6ad..81dbcf58c0cf3d4215044809a3851017fbfa169e 100644 (file)
@@ -92,39 +92,19 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_GET(self) -> None:
         """Test /day and /calendar response codes, and / redirect."""
-        self.conn.request('GET', '/day')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/day?date=3000-01-01')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/day?date=FOO')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('GET', '/calendar')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/calendar?start=&end=')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/calendar?start=today&end=today')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/calendar?start=2024-01-01&end=2025-01-01')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/calendar?start=foo')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('GET', '/')
-        response = self.conn.getresponse()
-        self.assertEqual(response.status, 302)
-        self.assertEqual(response.getheader('Location'), '/day')
-        self.conn.request('GET', '/foo')
-        self.assertEqual(self.conn.getresponse().status, 404)
+        self.check_get('/day', 200)
+        self.check_get('/day?date=3000-01-01', 200)
+        self.check_get('/day?date=FOO', 400)
+        self.check_get('/calendar', 200)
+        self.check_get('/calendar?start=&end=', 200)
+        self.check_get('/calendar?start=today&end=today', 200)
+        self.check_get('/calendar?start=2024-01-01&end=2025-01-01', 200)
+        self.check_get('/calendar?start=foo', 400)
 
     def test_do_POST_day(self) -> None:
         """Test POST /day."""
-        headers = {'Content-type': 'application/x-www-form-urlencoded'}
-        form_data = 'comment='
-        self.conn.request('POST', '/day', form_data, headers)
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('POST', '/day?date=foo', form_data, headers)
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('POST', '/day?date=2024-01-01', form_data, headers)
-        self.check_redirect('/')
-        form_data = 'foo='
-        self.conn.request('POST', '/day?date=2024-01-01', form_data, headers)
-        self.assertEqual(self.conn.getresponse().status, 400)
+        form_data = {'comment': ''}
+        self.check_post(form_data, '/day', 400)
+        self.check_post(form_data, '/day?date=foo', 400)
+        self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+        self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)
diff --git a/tests/misc.py b/tests/misc.py
new file mode 100644 (file)
index 0000000..893d67e
--- /dev/null
@@ -0,0 +1,17 @@
+"""Miscellaneous tests."""
+from tests.utils import TestCaseWithServer
+
+
+class TestsWithServer(TestCaseWithServer):
+    """Tests against our HTTP server/handler (and database)."""
+
+    def test_do_GET(self) -> None:
+        """Test / redirect, and unknown targets failing."""
+        self.conn.request('GET', '/')
+        self.check_redirect('/day')
+        self.check_get('/foo', 404)
+
+    def test_do_POST(self) -> None:
+        """Test POST to / and other unknown targets failing."""
+        self.check_post({}, '/', 404)
+        self.check_post({}, '/foo', 404)
index 88a7a6c006912dddc3fcc86af4a1691a5982db6c..fb572f55939a1c48ee45bb298d07ebefb17ab644 100644 (file)
@@ -68,47 +68,28 @@ class TestsWithServer(TestCaseWithServer):
     def test_do_POST_process(self) -> None:
         """Test POST /process and its effect on the database."""
         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
-        self.post_to(form_data, '/process?id=FOO')
-        self.assertEqual(self.conn.getresponse().status, 400)
+        self.check_post(form_data, '/process?id=FOO', 400)
         form_data['effort'] = 'foo'
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
+        self.check_post(form_data, '/process?id=', 400)
         form_data['effort'] = None
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        form_data = {}
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        form_data = {'title': '', 'description': ''}
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        form_data = {'title': '', 'effort': 1}
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        form_data = {'description': '', 'effort': 1}
-        self.post_to(form_data, '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 400)
+        self.check_post(form_data, '/process?id=', 400)
+        self.check_post({}, '/process?id=', 400)
+        self.check_post({'title': '', 'description': ''}, '/process?id=', 400)
+        self.check_post({'title': '', 'effort': 1}, '/process?id=', 400)
+        self.check_post({'description': '', 'effort': 1}, '/process?id=', 400)
         form_data = {'title': None, 'description': 1, 'effort': 1.0}
-        self.post_to(form_data, '/process?id=')
-        self.check_redirect('/')
+        self.check_post(form_data, '/process?id=', 302, '/')
         retrieved = Process.by_id(self.db_conn, 1)
         self.assertEqual(retrieved.title.newest, 'None')
         self.assertEqual([p.id_ for p in Process.all(self.db_conn)],
                          [retrieved.id_])
-        self.post_to(form_data, '/process?id=1')
-        self.check_redirect('/')
-        self.post_to(form_data, '/process')
-        self.check_redirect('/')
+        self.check_post(form_data, '/process?id=1', 302, '/')
+        self.check_post(form_data, '/process', 302, '/')
 
     def test_do_GET(self) -> None:
         """Test /process and /processes response codes."""
-        self.conn.request('GET', '/process')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/process?id=')
-        self.assertEqual(self.conn.getresponse().status, 200)
-        self.conn.request('GET', '/process?id=0')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('GET', '/process?id=FOO')
-        self.assertEqual(self.conn.getresponse().status, 400)
-        self.conn.request('GET', '/processes')
-        self.assertEqual(self.conn.getresponse().status, 200)
+        self.check_get('/process', 200)
+        self.check_get('/process?id=', 200)
+        self.check_get('/process?id=0', 400)
+        self.check_get('/process?id=FOO', 400)
+        self.check_get('/processes', 200)
index 3ca0cb90c8c4ef52519d40a45f5f1e92bbfbbdee..d41e7b39ad5fe07abdc701be674d9f81678cd30f 100644 (file)
@@ -5,6 +5,7 @@ from http.client import HTTPConnection
 from urllib.parse import urlencode
 from datetime import datetime
 from os import remove as remove_file
+from typing import Mapping
 from plomtask.db import DatabaseFile, DatabaseConnection
 from plomtask.http import TaskHandler, TaskServer
 
@@ -41,16 +42,26 @@ class TestCaseWithServer(TestCaseWithDB):
         self.server_thread.join()
         super().tearDown()
 
-    def post_to(self, data: dict[str, object], target: str) -> None:
-        """Post form data to target URL."""
-        encoded_form_data = urlencode(data).encode('utf-8')
-        headers = {'Content-Type': 'application/x-www-form-urlencoded',
-                   'Content-Length': str(len(encoded_form_data))}
-        self.conn.request('POST', target,
-                          body=encoded_form_data, headers=headers)
-
     def check_redirect(self, target: str) -> None:
         """Check that self.conn answers with a 302 redirect to target."""
         response = self.conn.getresponse()
         self.assertEqual(response.status, 302)
         self.assertEqual(response.getheader('Location'), target)
+
+    def check_get(self, target: str, expected_code: int) -> None:
+        """Check that a GET to target yields expected_code."""
+        self.conn.request('GET', target)
+        self.assertEqual(self.conn.getresponse().status, expected_code)
+
+    def check_post(self, data: Mapping[str, object], target: str,
+                   expected_code: int, redirect_location: str = '') -> None:
+        """Check that POST of data to target yields expected_code."""
+        encoded_form_data = urlencode(data).encode('utf-8')
+        headers = {'Content-Type': 'application/x-www-form-urlencoded',
+                   'Content-Length': str(len(encoded_form_data))}
+        self.conn.request('POST', target,
+                          body=encoded_form_data, headers=headers)
+        if 302 == expected_code:
+            self.check_redirect(redirect_location)
+        else:
+            self.assertEqual(self.conn.getresponse().status, expected_code)