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)
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]
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)
"""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
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."""
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
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)