From 48c40ed9d42ed8f1d6607b8fd5d31bbb1ac52f29 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 12 Aug 2024 15:24:39 +0200
Subject: [PATCH] Fix bug of /day POSTS breaking on empty new_todo fields.

---
 plomtask/http.py | 6 +++---
 tests/days.py    | 8 ++++----
 tests/misc.py    | 5 +++--
 tests/utils.py   | 2 +-
 4 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index e242a36..bb6e4ed 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -46,11 +46,11 @@ class InputsParser:
             return []
         return self.inputs[key]
 
-    def get_all_int(self, key: str) -> list[int]:
+    def get_all_int(self, key: str, fail_on_empty: bool = False) -> list[int]:
         """Retrieve list of int values at key."""
         all_str = self.get_all_str(key)
         try:
-            return [int(s) for s in all_str]
+            return [int(s) for s in all_str if fail_on_empty or s != '']
         except ValueError as e:
             msg = f'cannot int a form field value for key {key} in: {all_str}'
             raise BadFormatException(msg) from e
@@ -306,7 +306,7 @@ class TaskHandler(BaseHTTPRequestHandler):
                 # (because pylint here fails to detect the use of wrapper as a
                 # method to self with respective access privileges)
                 id_ = None
-                for val in self._params.get_all_int('id'):
+                for val in self._params.get_all_int('id', fail_on_empty=True):
                     id_ = val
                 if target_class.can_create_by_id:
                     item = target_class.by_id_or_create(self._conn, id_)
diff --git a/tests/days.py b/tests/days.py
index 5edec50..f79284c 100644
--- a/tests/days.py
+++ b/tests/days.py
@@ -240,16 +240,16 @@ class TestsWithServer(TestCaseWithServer):
         """Test basic (no Processes/Conditions/Todos) POST /day.
 
         Check POST requests properly parse 'today', 'tomorrow', 'yesterday',
-        and actual date strings;
-        preserve 'make_type' setting in redirect even if nonsensical;
-        and store 'day_comment'.
+        and actual date strings; store 'day_comment'; preserve 'make_type'
+        setting in redirect even if nonsensical; and allow '' as 'new_todo'.
         """
         for name, dist, test_str in [('2024-01-01', None, 'a'),
                                      ('today', 0, 'b'),
                                      ('yesterday', -1, 'c'),
                                      ('tomorrow', +1, 'd')]:
             date = name if dist is None else _testing_date_in_n_days(dist)
-            post = {'day_comment': test_str, 'make_type': f'x:{test_str}'}
+            post = {'day_comment': test_str, 'make_type': f'x:{test_str}',
+                    'new_todo': ['', '']}
             post_url = f'/day?date={name}'
             redir_url = f'{post_url}&make_type={post["make_type"]}'
             self.check_post(post, post_url, 302, redir_url)
diff --git a/tests/misc.py b/tests/misc.py
index 8159124..3aa1314 100644
--- a/tests/misc.py
+++ b/tests/misc.py
@@ -140,15 +140,16 @@ class TestsSansServer(TestCase):
         parser = InputsParser({'foo': ['bar', 'baz']})
         self.assertEqual(['bar', 'baz'], parser.get_all_str('foo'))
 
-    def test_InputsParser_strict_get_all_int(self) -> None:
+    def test_InputsParser_get_all_int(self) -> None:
         """Test InputsParser.get_all_int."""
         parser = InputsParser({})
         self.assertEqual([], parser.get_all_int('foo'))
         parser = InputsParser({'foo': []})
         self.assertEqual([], parser.get_all_int('foo'))
         parser = InputsParser({'foo': ['']})
+        parser.get_all_int('foo')
         with self.assertRaises(BadFormatException):
-            parser.get_all_int('foo')
+            parser.get_all_int('foo', fail_on_empty=True)
         parser = InputsParser({'foo': ['0']})
         self.assertEqual([0], parser.get_all_int('foo'))
         parser = InputsParser({'foo': ['0', '17']})
diff --git a/tests/utils.py b/tests/utils.py
index 75c7e50..9d5f88e 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -679,7 +679,7 @@ class Expected:
                 for todo in self.lib_all('Todo'):
                     if next_id <= todo['id']:
                         next_id = todo['id'] + 1
-                for proc_id in sorted(v):
+                for proc_id in sorted([id_ for id_  in v if id_]):
                     todo = self.todo_as_dict(next_id, proc_id, date)
                     self.lib_set('Todo', [todo])
                     next_id += 1
-- 
2.30.2