Skip to content

Commit

Permalink
Merge pull request #223 from nulib/4798-malformed-query
Browse files Browse the repository at this point in the history
Reject missing/empty questions
  • Loading branch information
bmquinn authored Jun 24, 2024
2 parents 8aa0395 + a34ae16 commit cfab6f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions chat/src/handlers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def handler(event, context):
if not config.is_logged_in:
config.socket.send({"type": "error", "message": "Unauthorized"})
return {"statusCode": 401, "body": "Unauthorized"}

if config.question is None or config.question == "":
config.socket.send({"type": "error", "message": "Question cannot be blank"})
return {"statusCode": 400, "body": "Question cannot be blank"}

debug_message = config.debug_message()
if config.debug_mode:
Expand Down
30 changes: 26 additions & 4 deletions chat/test/handlers/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ 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})

@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")}
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')
Expand All @@ -51,7 +51,7 @@ def test_handler_debug_mode(self, mock_is_debug_enabled, mock_is_logged_in, mock
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}
event = {"socket": mock_websocket, "debug": True, "body": '{"question": "Question?"}' }
handler(event, MockContext())
response = json.loads(mock_client.received_data)
self.assertEqual(response["type"], "debug")
Expand All @@ -65,7 +65,29 @@ def test_handler_debug_mode_for_superusers_only(self, mock_is_debug_enabled, moc
mock_is_superuser.return_value = False
mock_client = MockClient()
mock_websocket = Websocket(client=mock_client, endpoint_url="test", connection_id="test", ref="test")
event = {"socket": mock_websocket, "debug": True}
event = {"socket": mock_websocket, "debug": True, "body": '{"question": "Question?"}' }
handler(event, MockContext())
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_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")

0 comments on commit cfab6f9

Please sign in to comment.