Skip to content

Commit

Permalink
Implement unit tests for auth endpoints
Browse files Browse the repository at this point in the history
Update dependencies to stable spec
Update dockerfile to resolve warnings
  • Loading branch information
NeonDaniel committed Nov 22, 2024
1 parent 757ff15 commit eb76f47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ FROM python:3.9-slim
LABEL vendor=neon.ai \
ai.neon.name="neon-hana"

ENV OVOS_CONFIG_BASE_FOLDER neon
ENV OVOS_CONFIG_FILENAME diana.yaml
ENV XDG_CONFIG_HOME /config
ENV OVOS_CONFIG_BASE_FOLDER=neon
ENV OVOS_CONFIG_FILENAME=diana.yaml
ENV XDG_CONFIG_HOME=/config

RUN apt update && apt install -y swig gcc libpulse-dev portaudio19-dev

Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ token-throttler~=1.4
neon-mq-connector~=0.7
ovos-config~=0.0,>=0.0.12
ovos-utils~=0.0,>=0.0.38
neon-data-models @ git+https://github.com/neongeckocom/neon-data-models@FEAT_UpdateUserDbCRUDOperations
neon-data-models
36 changes: 23 additions & 13 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from fastapi.testclient import TestClient

from neon_data_models.models.user import User

_TEST_CONFIG = {
"mq_default_timeout": 10,
"access_token_ttl": 86400, # 1 day
Expand Down Expand Up @@ -52,12 +54,14 @@ def test_app_init(self):

@patch("neon_hana.mq_service_api.send_mq_request")
def test_auth_login(self, send_request):
send_request.return_value = {} # TODO: Define valid login
valid_user = User(username="guest", password_hash="password")
send_request.return_value = {"user": valid_user.model_dump(),
"success": True}

# Valid Login
response = self.test_app.post("/auth/login",
json={"username": "guest",
"password": "password"})
json={"username": valid_user.username,
"password": valid_user.password_hash})
response_data = response.json()
self.assertEqual(response.status_code, 200, response.text)
self.assertEqual(response_data['username'], "guest")
Expand All @@ -66,7 +70,13 @@ def test_auth_login(self, send_request):
self.assertGreater(response_data['expiration'], time())

# Invalid Login
# TODO: Define invalid login request
send_request.return_value = {"code": 404, "error": "User not found"}
response = self.test_app.post("/auth/login",
json={"username": valid_user.username,
"password": valid_user.password_hash})
self.assertEqual(response.status_code, 404, response.status_code)
self.assertEqual(response.json()['detail'],
"User not found", response.text)

# Invalid Request
self.assertEqual(self.test_app.post("/auth/login").status_code, 422)
Expand All @@ -76,7 +86,9 @@ def test_auth_login(self, send_request):

@patch("neon_hana.mq_service_api.send_mq_request")
def test_auth_refresh(self, send_request):
send_request.return_value = {} # TODO: Define valid refresh
valid_user = User(username="guest", password_hash="password")
send_request.return_value = {"user": valid_user.model_dump(),
"success": True}

valid_tokens = self._get_tokens()

Expand All @@ -86,14 +98,12 @@ def test_auth_refresh(self, send_request):
response_data = response.json()
self.assertNotEqual(response_data, valid_tokens)

# # TODO
# # Refresh with old tokens fails
# response = self.test_app.post("/auth/refresh", json=valid_tokens)
# self.assertEqual(response.status_code, 422, response.text)

# Valid request with new tokens
response = self.test_app.post("/auth/refresh", json=response_data)
self.assertEqual(response.status_code, 200, response.text)
# Refresh with old tokens fails (mocked return from users service)
send_request.return_value = {"code": 422,
"detail": "Invalid token",
"success": False}
response = self.test_app.post("/auth/refresh", json=valid_tokens)
self.assertEqual(response.status_code, 422, response.text)

# TODO: Test with expired token

Expand Down

0 comments on commit eb76f47

Please sign in to comment.