From 742e4b3dcf0112ae8a49ba27308f1f7dc2aa347c Mon Sep 17 00:00:00 2001 From: m3rchant-man Date: Thu, 30 Oct 2025 11:46:55 -0400 Subject: [PATCH] feat: Adds api integration tests --- _scripts/run_tests.sh | 46 ++++++++++++++++++++++++++++++++++++ tests/requirements.txt | 2 ++ tests/test_api.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100755 _scripts/run_tests.sh create mode 100644 tests/requirements.txt create mode 100644 tests/test_api.py diff --git a/_scripts/run_tests.sh b/_scripts/run_tests.sh new file mode 100755 index 0000000..eb8a0cc --- /dev/null +++ b/_scripts/run_tests.sh @@ -0,0 +1,46 @@ +#!/bin/bash +set -eo pipefail + +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) +PROJECT_ROOT=$(dirname "$SCRIPT_DIR") + +VENV_PATH="$PROJECT_ROOT/tests/.venv" +REQS_PATH="$PROJECT_ROOT/tests/requirements.txt" + +setup_environment() { + echo "Setting up Python virtual environment..." + + if ! command -v python3 &> /dev/null; then + echo "Error: python3 is not installed or not in PATH." + exit 1 + fi + + # Clean up old environment if it exists + if [ -d "$VENV_PATH" ]; then + echo "Removing existing virtual environment." + rm -rf "$VENV_PATH" + fi + + echo "Creating virtual environment at $VENV_PATH..." + python3 -m venv "$VENV_PATH" + + echo "Installing dependencies from $REQS_PATH..." + "$VENV_PATH/bin/pip" install -r "$REQS_PATH" + echo "Setup complete." +} + +if [[ "$1" == "--build" ]]; then + setup_environment + shift # Consume the --build argument +fi + +if [ ! -d "$VENV_PATH" ]; then + echo "Virtual environment not found. Please run with the --build flag to create it:" + echo " $0 --build" + exit 1 +fi + +source "$VENV_PATH/bin/activate" + +echo "Running integration tests..." +pytest "$PROJECT_ROOT/tests/" "$@" diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..3363813 --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,2 @@ +pytest==8.2.0 +requests==2.31.0 diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..a135a05 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,53 @@ +import pytest +import requests +import uuid +import os + +BASE_URL = os.environ.get("API_BASE_URL", "http://localhost:8080") + + +@pytest.fixture +def unique_user_payload(): + """ + Pytest fixture to generate a unique user payload for registration. + """ + unique_id = uuid.uuid4() + return { + "username": f"testuser_{unique_id}", + "password": f"password_{uuid.uuid4()}", + "email": f"test_{unique_id}@example.com", + } + + +def test_health_check(): + """ + Tests if the /health endpoint is working. + """ + response = requests.get(f"{BASE_URL}/health") + assert response.status_code == 200 + assert response.text == "OK" + + +def test_user_registration_success(unique_user_payload): + """ + Tests successful user registration. + """ + response = requests.post(f"{BASE_URL}/register", json=unique_user_payload) + assert response.status_code == 201 + response_json = response.json() + assert response_json["status"] == "success" + assert response_json["message"] == "User registered successfully" + + +def test_user_registration_failure_duplicate_user(unique_user_payload): + """ + Tests that registration fails if the user already exists. + """ + # First registration should succeed + response1 = requests.post(f"{BASE_URL}/register", json=unique_user_payload) + assert response1.status_code == 201 + + # Second registration with the same data should fail + response2 = requests.post(f"{BASE_URL}/register", json=unique_user_payload) + assert response2.status_code == 409 # Conflict + assert "Username or email already exists" in response2.text