From a7bad1507924676d11bf441b6d1fed6a165b52b5 Mon Sep 17 00:00:00 2001 From: Karen Shaw Date: Thu, 29 Aug 2024 20:37:58 +0000 Subject: [PATCH] WIP tests --- chat/src/handlers/chat.py | 1 - chat/src/helpers/apitoken.py | 1 + chat/test/handlers/test_chat.py | 86 ++++++++++++++++----------------- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/chat/src/handlers/chat.py b/chat/src/handlers/chat.py index d2d3bd3f..b1c1f0e2 100644 --- a/chat/src/handlers/chat.py +++ b/chat/src/handlers/chat.py @@ -31,7 +31,6 @@ def handler(event, context): return {"statusCode": 400, "body": "Question cannot be blank"} debug_message = config.debug_message() - print(f"Debug mode: {config.debug_mode}") if config.debug_mode: config.socket.send(debug_message) diff --git a/chat/src/helpers/apitoken.py b/chat/src/helpers/apitoken.py index 0cd03e29..7656fd19 100644 --- a/chat/src/helpers/apitoken.py +++ b/chat/src/helpers/apitoken.py @@ -23,6 +23,7 @@ def __init__(self, signed_token=None): try: secret = os.getenv("API_TOKEN_SECRET") self.token = jwt.decode(signed_token, secret, algorithms=["HS256"]) + print(self.token) except Exception: self.token = ApiToken.empty_token() diff --git a/chat/test/handlers/test_chat.py b/chat/test/handlers/test_chat.py index 532c1bc4..0d2b5bc7 100644 --- a/chat/test/handlers/test_chat.py +++ b/chat/test/handlers/test_chat.py @@ -32,35 +32,33 @@ def __init__(self): }, ) class TestHandler(TestCase): - def test_handler_unauthorized(self): - event = {"socket": Websocket(client=MockClient(), endpoint_url="test", connection_id="test", ref="test")} - self.assertEqual(handler(event, MockContext()), {'body': 'Unauthorized', 'statusCode': 401}) + # def test_handler_unauthorized(self): + # event = {"socket": Websocket(client=MockClient(), endpoint_url="test", connection_id="test", ref="test")} + # self.assertEqual(handler(event, MockContext()), {'body': 'Unauthorized', 'statusCode': 401}) - @patch.object(ApiToken, 'is_logged_in') - def test_handler_success(self, mock_is_logged_in): - mock_is_logged_in.return_value = True - event = {"socket": Websocket(client=MockClient(), endpoint_url="test", connection_id="test", ref="test"), "body": '{"question": "Question?"}' } - self.assertEqual(handler(event, MockContext()), {'statusCode': 200}) + # @patch.object(ApiToken, 'is_logged_in') + # def test_handler_success(self, mock_is_logged_in): + # mock_is_logged_in.return_value = True + # event = {"socket": Websocket(client=MockClient(), endpoint_url="test", connection_id="test", ref="test"), "body": '{"question": "Question?"}' } + # self.assertEqual(handler(event, MockContext()), {'statusCode': 200}) - @patch.object(ApiToken, 'is_logged_in') - @patch.object(ApiToken, 'is_superuser') - @patch.object(EventConfig, '_is_debug_mode_enabled') - def test_handler_debug_mode(self, mock_is_debug_enabled, mock_is_logged_in, mock_is_superuser): - mock_is_debug_enabled.return_value = True - mock_is_logged_in.return_value = True - mock_is_superuser.return_value = True - mock_client = MockClient() - mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") - event = {"socket": mock_websocket, "debug": True, "body": '{"question": "Question?"}' } - handler(event, MockContext()) - response = json.loads(mock_client.received_data) - self.assertEqual(response["type"], "debug") + # @patch.object(ApiToken, 'is_logged_in') + # @patch.object(ApiToken, 'is_superuser') + # @patch.object(EventConfig, '_is_debug_mode_enabled') + # def test_handler_debug_mode(self, mock_is_debug_enabled, mock_is_logged_in, mock_is_superuser): + # mock_is_debug_enabled.return_value = True + # mock_is_logged_in.return_value = True + # mock_is_superuser.return_value = True + # mock_client = MockClient() + # mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") + # event = {"socket": mock_websocket, "debug": True, "body": '{"question": "Question?"}' } + # handler(event, MockContext()) + # response = json.loads(mock_client.received_data) + # self.assertEqual(response["type"], "debug") @patch.object(ApiToken, 'is_logged_in') @patch.object(ApiToken, 'is_superuser') - @patch.object(EventConfig, '_is_debug_mode_enabled') - def test_handler_debug_mode_for_superusers_only(self, mock_is_debug_enabled, mock_is_logged_in, mock_is_superuser): - mock_is_debug_enabled.return_value = True + def test_handler_debug_mode_for_superusers_only(self, mock_is_logged_in, mock_is_superuser): mock_is_logged_in.return_value = True mock_is_superuser.return_value = False mock_client = MockClient() @@ -70,24 +68,24 @@ def test_handler_debug_mode_for_superusers_only(self, mock_is_debug_enabled, moc response = json.loads(mock_client.received_data) self.assertEqual(response["type"], "error") - @patch.object(ApiToken, 'is_logged_in') - def test_handler_question_missing(self, mock_is_logged_in): - mock_is_logged_in.return_value = True - mock_client = MockClient() - mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") - event = {"socket": mock_websocket} - handler(event, MockContext()) - response = json.loads(mock_client.received_data) - self.assertEqual(response["type"], "error") - self.assertEqual(response["message"], "Question cannot be blank") + # @patch.object(ApiToken, 'is_logged_in') + # def test_handler_question_missing(self, mock_is_logged_in): + # mock_is_logged_in.return_value = True + # mock_client = MockClient() + # mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") + # event = {"socket": mock_websocket} + # handler(event, MockContext()) + # response = json.loads(mock_client.received_data) + # self.assertEqual(response["type"], "error") + # self.assertEqual(response["message"], "Question cannot be blank") - @patch.object(ApiToken, 'is_logged_in') - def test_handler_question_blank(self, mock_is_logged_in): - mock_is_logged_in.return_value = True - mock_client = MockClient() - mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") - event = {"socket": mock_websocket, "body": '{"quesion": ""}'} - handler(event, MockContext()) - response = json.loads(mock_client.received_data) - self.assertEqual(response["type"], "error") - self.assertEqual(response["message"], "Question cannot be blank") + # @patch.object(ApiToken, 'is_logged_in') + # def test_handler_question_blank(self, mock_is_logged_in): + # mock_is_logged_in.return_value = True + # mock_client = MockClient() + # mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test") + # event = {"socket": mock_websocket, "body": '{"quesion": ""}'} + # handler(event, MockContext()) + # response = json.loads(mock_client.received_data) + # self.assertEqual(response["type"], "error") + # self.assertEqual(response["message"], "Question cannot be blank")