Skip to content

Commit

Permalink
WIP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Aug 29, 2024
1 parent cd7690b commit a7bad15
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
1 change: 0 additions & 1 deletion chat/src/handlers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions chat/src/helpers/apitoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
86 changes: 42 additions & 44 deletions chat/test/handlers/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")

0 comments on commit a7bad15

Please sign in to comment.