From 7a90eb547f3ae92339cdb5fa88de404dfe754ca9 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Fri, 26 Jul 2024 04:21:42 +0000 Subject: [PATCH] test for flask handler. --- gamutrf/flask_handler.py | 7 +++++-- tests/test_flask_handler.py | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/test_flask_handler.py diff --git a/gamutrf/flask_handler.py b/gamutrf/flask_handler.py index 0ce79bc1..874d75e5 100644 --- a/gamutrf/flask_handler.py +++ b/gamutrf/flask_handler.py @@ -42,9 +42,12 @@ def reconf(self): continue if not hasattr(new_options, arg): return f"no such option {arg}", 400 - val_type = type(getattr(self.options, arg)) + val_type = getattr(self.options, arg) try: - setattr(new_options, arg, val_type(val)) + if val_type is None: + setattr(new_options, arg, val) + else: + setattr(new_options, arg, type(val_type)(val)) except (TypeError, ValueError) as err: return f"cannot set {arg} = {val}: {err}", 400 results = self.check_options(new_options) diff --git a/tests/test_flask_handler.py b/tests/test_flask_handler.py new file mode 100644 index 00000000..9485ac37 --- /dev/null +++ b/tests/test_flask_handler.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 +import unittest + +from gamutrf.flask_handler import FlaskHandler + + +class FakeOptions: + def __init__(self, apiport, foo, bar): + self.apiport = apiport + self.foo = foo + self.bar = bar + + +class FakeRequest: + def __init__(self, args): + self.args = args + + +class FlaskHandlerTestCase(unittest.TestCase): + def test_flask_handler(self): + def good_check_options(new_options): + return "" + + def bad_check_options(new_options): + return "bad options are bad" + + banned_args = [] + options = FakeOptions(apiport=2048, foo=123, bar=None) + request = FakeRequest(args={"foo": "999", "bar": None, "apiport": 123}) + handler = FlaskHandler(options, bad_check_options, banned_args) + handler.request = request + self.assertEqual(("bad options are bad", 400), handler.reconf()) + self.assertEqual(handler.options, options) + handler.check_options = good_check_options + self.assertEqual(("reconf", 200), handler.reconf()) + self.assertNotEqual(handler.options, options) + + +if __name__ == "__main__": # pragma: no cover + unittest.main()