From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 20 Mar 2024 01:59:14 +0000 (+0100)
Subject: Add POST /day tests.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7Bprefix%7D%7D/condition_descriptions?a=commitdiff_plain;h=dcfff0be3d1a150fafb6b65d407990aaa6a154d5;p=plomtask

Add POST /day tests.
---

diff --git a/tests/test_days.py b/tests/test_days.py
index 7e06198..7daf749 100644
--- a/tests/test_days.py
+++ b/tests/test_days.py
@@ -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.http import TaskHandler, TaskServer
@@ -112,6 +113,24 @@ class TestsWithServer(TestsWithDB):
         self.server_thread.daemon = True
         self.server_thread.start()
 
+    def test_do_POST_day(self):
+        """Test POST /day and its effect on the database."""
+        conn = HTTPConnection(*self.httpd.server_address)
+        form_data = {'comment': 'foo'}
+        encoded_form_data = urlencode(form_data).encode('utf-8')
+        headers = {'Content-Type': 'application/x-www-form-urlencoded',
+                   'Content-Length': str(len(encoded_form_data))}
+        conn.request('POST', '/day?date=FOO',
+                     body=encoded_form_data, headers=headers)
+        self.assertEqual(conn.getresponse().status, 400)
+        self.assertEqual(Day.all(self.db_conn), [])
+        conn.request('POST', '/day?date=2024-01-01',
+                     body=encoded_form_data, headers=headers)
+        self.assertEqual(conn.getresponse().status, 302)
+        retrieved = Day.by_date(self.db_conn, '2024-01-01')
+        self.assertEqual(retrieved.comment, 'foo')
+        self.assertEqual(Day.all(self.db_conn), [retrieved])
+
     def test_do_GET(self):
         """Test /day and /calendar response codes."""
         conn = HTTPConnection(*self.httpd.server_address)