Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions _scripts/run_tests.sh
Original file line number Diff line number Diff line change
@@ -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/" "$@"
2 changes: 2 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==8.2.0
requests==2.31.0
53 changes: 53 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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