home · contact · privacy
Extend POST tests, and handling of missing form data.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 01:33:28 +0000 (02:33 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 27 Mar 2024 01:33:28 +0000 (02:33 +0100)
plomtask/http.py
tests/days.py
tests/processes.py
tests/utils.py

index c1de98751778706330991d0427b41a4df1ea86ca..33961255889a2e00933e6110422ccb02e5aae88d 100644 (file)
@@ -107,6 +107,8 @@ class TaskHandler(BaseHTTPRequestHandler):
                     date: str, postvars: dict[str, list[str]]) -> None:
         """Update or insert Day of date and fields defined in postvars."""
         day = Day.by_date(conn, date, create=True)
+        if 'comment' not in postvars.keys():
+            raise BadFormatException('missing Day.comment value')
         day.comment = postvars['comment'][0]
         day.save(conn)
 
@@ -114,6 +116,12 @@ class TaskHandler(BaseHTTPRequestHandler):
                         postvars: dict[str, list[str]]) -> None:
         """Update or insert Process of id_ and fields defined in postvars."""
         process = Process.by_id(conn, id_, create=True)
+        if 'title' not in postvars.keys():
+            raise BadFormatException('missing Process.title value')
+        if 'description' not in postvars.keys():
+            raise BadFormatException('missing Process.description value')
+        if 'effort' not in postvars.keys():
+            raise BadFormatException('missing Process.effort value')
         process.title.set(postvars['title'][0])
         process.description.set(postvars['description'][0])
         effort = postvars['effort'][0]
index 7cb0f4fa6c9da13e8064e7e51a8c40e7284c7375..2850bb810c3af71e03a65e92e6f8f42d1768f6ad 100644 (file)
@@ -114,3 +114,17 @@ class TestsWithServer(TestCaseWithServer):
         self.assertEqual(response.getheader('Location'), '/day')
         self.conn.request('GET', '/foo')
         self.assertEqual(self.conn.getresponse().status, 404)
+
+    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)
index e8967f70273e2345df966d599d50fdbeb4069e39..88a7a6c006912dddc3fcc86af4a1691a5982db6c 100644 (file)
@@ -1,6 +1,5 @@
 """Test Processes module."""
 from unittest import TestCase
-from urllib.parse import urlencode
 from tests.utils import TestCaseWithDB, TestCaseWithServer
 from plomtask.processes import Process
 from plomtask.exceptions import NotFoundException, BadFormatException
@@ -68,26 +67,38 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_process(self) -> None:
         """Test POST /process and its effect on the database."""
-        def post_data_to_expect(form_data: dict[str, object],
-                                to_: str, expect: int) -> None:
-            encoded_form_data = urlencode(form_data).encode('utf-8')
-            headers = {'Content-Type': 'application/x-www-form-urlencoded',
-                       'Content-Length': str(len(encoded_form_data))}
-            self.conn.request('POST', to_,
-                              body=encoded_form_data, headers=headers)
-            self.assertEqual(self.conn.getresponse().status, expect)
         form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
-        post_data_to_expect(form_data, '/process?id=FOO', 400)
+        self.post_to(form_data, '/process?id=FOO')
+        self.assertEqual(self.conn.getresponse().status, 400)
         form_data['effort'] = 'foo'
-        post_data_to_expect(form_data, '/process?id=', 400)
+        self.post_to(form_data, '/process?id=')
+        self.assertEqual(self.conn.getresponse().status, 400)
         form_data['effort'] = None
-        post_data_to_expect(form_data, '/process?id=', 400)
+        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)
         form_data = {'title': None, 'description': 1, 'effort': 1.0}
-        post_data_to_expect(form_data, '/process?id=', 302)
+        self.post_to(form_data, '/process?id=')
+        self.check_redirect('/')
         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('/')
 
     def test_do_GET(self) -> None:
         """Test /process and /processes response codes."""
index 9964201bbc7d6312ed1e8cb29e1a5a359608e86f..3ca0cb90c8c4ef52519d40a45f5f1e92bbfbbdee 100644 (file)
@@ -2,6 +2,7 @@
 from unittest import TestCase
 from threading import Thread
 from http.client import HTTPConnection
+from urllib.parse import urlencode
 from datetime import datetime
 from os import remove as remove_file
 from plomtask.db import DatabaseFile, DatabaseConnection
@@ -39,3 +40,17 @@ class TestCaseWithServer(TestCaseWithDB):
         self.httpd.server_close()
         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)