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
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
cd webapi
if [ -f bank_db.db ]; then rm -f bank_db.db; fi
pytest tests/test_main.py -v
pytest tests/test_release.py -v

container-smoke-test:
name: Container Build & Healthcheck Smoke Test
Expand Down
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Python bytecode and cache
__pycache__/
*.pyc
*.pyo

# SQLite / local database files
*.db

# Pytest remnants
.pytest_cache/
.cache/
.pytest_cache/**

# Test and coverage artifacts
.coverage
coverage.xml
htmlcov/
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def test_profile_success(client, auth_header, created_user):
response = client.get("/api/v1/auth/profile", headers=auth_header)

assert response.status_code == 200
assert response.json()["profile darta"]["sub"] == created_user.username
assert response.json()["profile data"]["sub"] == created_user.username


def test_profile_invalid_token_returns_401(client):
response = client.get("/api/v1/auth/profile", headers={"Authorization": "Token abc"})

assert response.status_code == 401
assert response.json()["detail"] == "Unauthorized token"
assert response.status_code == 403
assert response.json()["detail"] == "Invalid authentication credentials"


def test_generate_password_user_not_found_returns_404(client):
Expand Down
42 changes: 24 additions & 18 deletions webapi/tests/test_main.py → webapi/tests/functional/test_cd.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
from fastapi.testclient import TestClient
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[1]))
sys.path.append(str(Path(__file__).resolve().parents[2]))

from main import myapp

client = TestClient(myapp)

def test_register_user():
response = client.post("/api/v1/auth/signup", json={
"username": "pytest",
"name": "pytest",
"last_name": "testing",
"phone": 3334353637,
"email": "pytestmyapp@testing.com",
"hashed_password": "pytest"
})
assert response.status_code == 200
assert response.json() == {
"message": "User created successfully"
}

def test_login_and_access_private():
# Ensure user exists; tolerate pre-existing data in local DB runs
signup_response = client.post(
"/api/v1/auth/signup",
json={
"username": "pytest",
"name": "Py",
"last_name": "Tester",
"phone": 5511111111,
"email": "pytest@example.com",
"hashed_password": "pytest",
},
)
assert signup_response.status_code in (200, 400)
if signup_response.status_code == 400:
assert signup_response.json().get("detail") == "username already taken"

# First, get a token
response = client.post("/api/v1/auth/login", json={ # <-- use json instead of data
"username": "pytest",
"password": "pytest"
})
response = client.post(
"/api/v1/auth/login",
json={
"username": "pytest",
"password": "pytest",
},
)
assert response.status_code == 200
token = response.json()["access_token"]

Expand Down
Empty file.
74 changes: 74 additions & 0 deletions webapi/tests/nonfunctional/test_ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from fastapi.testclient import TestClient
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[2]))
from main import myapp
client = TestClient(myapp)

TEST_USER = "testuser"
TEST_PSW = "testPSW"

def test_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {
"Portfolio App": "This is a simple app using FastAPI and mariadb."
}


def test_register_user():
response = client.post("/api/v1/auth/signup", json={
"username": TEST_USER,
"name": "user to test",
"last_name": "my webapp",
"phone": 3132333435,
"email": "pytestmyapp@testing.com",
"hashed_password": TEST_PSW
})
assert response.status_code == 200
assert response.json() == {
"message": "User created successfully"
}

def get_token():
response = client.post("/api/v1/auth/login", json={
"username": TEST_USER,
"password": TEST_PSW
})
assert response.status_code == 200
token = response.json()["access_token"]
return token


def test_register_prompt():
token = get_token()
headers = {"Authorization": f"Bearer {token}", "send_email": "false"}
response = client.post("/api/v1/prompts", json={
"user_id": 1,
"model_name": "gpt-4.1",
"prompt_text": "Generate a test response",
"category": "qa",
"rate": "high",
}, headers=headers)
assert response.status_code == 200
assert response.json() == {
"id": 1,
"user_id": 1,
"model_name": "gpt-4.1",
"prompt_text": "Generate a test response",
"category": "qa",
"rate": "high"
}

def test_read_users():
token = get_token()
headers = {"Authorization": f"Bearer {token}"}
response = client.get("/api/v1/users", headers=headers)
assert response.status_code == 200


def test_read_prompts():
token = get_token()
headers = {"Authorization": f"Bearer {token}"}
response = client.get("/api/v1/prompts", headers=headers)
assert response.status_code == 200
78 changes: 9 additions & 69 deletions webapi/tests/test_ci.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,14 @@
from fastapi.testclient import TestClient
import subprocess
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parents[1]))
from main import myapp
client = TestClient(myapp)

TEST_USER = "testuser"
TEST_PSW = "testPSW"

def test_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {
"Portfolio App": "This is a simple app using FastAPI and mariadb."
}
def test_run_nonfunctional_suite():
project_root = Path(__file__).resolve().parents[1]
result = subprocess.run(
[sys.executable, "-m", "pytest", "tests/nonfunctional", "-v"],
cwd=project_root,
check=False,
)
assert result.returncode == 0


def test_register_user():
response = client.post("/api/v1/auth/signup", json={
"username": TEST_USER,
"name": "user to test",
"last_name": "my webapp",
"phone": 3132333435,
"email": "pytestmyapp@testing.com",
"hashed_password": TEST_PSW
})
assert response.status_code == 200
assert response.json() == {
"message": "User created successfully"
}

def get_token():
response = client.post("/api/v1/auth/login", json={
"username": TEST_USER,
"password": TEST_PSW
})
assert response.status_code == 200
token = response.json()["access_token"]
return token


def test_register_prompt():
token = get_token()
headers = {"Authorization": f"Bearer {token}", "send_email": "false"}
response = client.post("/api/v1/prompts", json={
"user_id": 1,
"model_name": "gpt-4.1",
"prompt_text": "Generate a test response",
"category": "qa",
"rate": "high",
}, headers=headers)
assert response.status_code == 200
assert response.json() == {
"id": 1,
"user_id": 1,
"model_name": "gpt-4.1",
"prompt_text": "Generate a test response",
"category": "qa",
"rate": "high"
}

def test_read_users():
token = get_token()
headers = {"Authorization": f"Bearer {token}"}
response = client.get("/api/v1/users", headers=headers)
assert response.status_code == 200


def test_read_prompts():
token = get_token()
headers = {"Authorization": f"Bearer {token}"}
response = client.get("/api/v1/prompts", headers=headers)
assert response.status_code == 200
14 changes: 14 additions & 0 deletions webapi/tests/test_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import subprocess
import sys
from pathlib import Path


def test_run_functional_suite():
project_root = Path(__file__).resolve().parents[1]
result = subprocess.run(
[sys.executable, "-m", "pytest", "tests/functional", "-v"],
cwd=project_root,
check=False,
)
assert result.returncode == 0