Skip to content

Commit

Permalink
Unit Tests for /columns API Endpoint (#1060)
Browse files Browse the repository at this point in the history
* unit test for /columns django endpoint

* 🎨 Auto-generated directory tree for repository in Architecture.md

* 🎨 Format Python code with psf/black

* black formatting

* remove unneeded prints

---------

Co-authored-by: karkir0003 <[email protected]>
  • Loading branch information
karkir0003 and karkir0003 authored Nov 28, 2023
1 parent f8bbc59 commit 09ba30d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions training/tests/test_dataset_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from tests.utils.test_utils import mock_authenticate, get_test_bearer_token
from django.test import Client
from training.core.authenticator import FirebaseAuth


@pytest.mark.parametrize(
"dataset_name", ["IRIS", "CALIFORNIA_HOUSING", "DIABETES", "WINE"]
)
def test_columns_endpoint(monkeypatch, dataset_name):
client = Client()
# Use monkeypatch to replace FirebaseAuth.authenticate with our mock function
monkeypatch.setattr(FirebaseAuth, "authenticate", mock_authenticate)
# Set the Authorization header with the fake token
headers = get_test_bearer_token()
response = client.get(f"/api/datasets/default/{dataset_name}/columns", **headers)
assert response.status_code == 200
assert isinstance(response.json(), dict)


@pytest.mark.parametrize("dataset_name", ["TEST_DATASET", "HELLO", None])
def test_columns_invalid_default(monkeypatch, dataset_name):
client = Client()
# Use monkeypatch to replace FirebaseAuth.authenticate with our mock function
monkeypatch.setattr(FirebaseAuth, "authenticate", mock_authenticate)
headers = get_test_bearer_token()
response = client.get(f"/api/datasets/default/{dataset_name}/columns", **headers)
assert response.status_code == 404
assert response.content.decode("utf-8") == '{"message": "Dataset not found"}'
Empty file.
32 changes: 32 additions & 0 deletions training/tests/utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
File that houses helper functions for testing the training backend
"""
import jwt
import datetime


def mock_authenticate(*args, **kwargs) -> str:
"""
Function that gives a test JWT Token for testing (not necessarily real user data)
Django API Endpoints that require user authentication
Returns:
token: Bearer Token
"""
payload = {
"sub": "1234567890",
"name": "John Doe",
"iat": datetime.datetime.utcnow(),
"exp": datetime.datetime.utcnow() + datetime.timedelta(days=1),
}
secret = "secret"
token = jwt.encode(payload, secret, algorithm="HS256")
return token


def get_test_bearer_token() -> dict:
"""
Wrapper that uses mock_authenticate function to build a bearer token
in a format that Django accepts
"""
return {"HTTP_AUTHORIZATION": "Bearer " + mock_authenticate()}

0 comments on commit 09ba30d

Please sign in to comment.