Skip to content

Commit

Permalink
feature: update backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerman70000 committed Feb 1, 2025
1 parent 9e2ebca commit af2cad7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,4 @@ jobs:
- name: Run tests with coverage
run: |
cd backend
uv run pytest --cov=app tests/ --cov-report=term-missing --cov-fail-under=60
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
uv run pytest --cov=app tests/ --cov-report=term-missing --cov-fail-under=60
18 changes: 13 additions & 5 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import uuid
import pytest
from datetime import datetime, timedelta

import pytest
from app import create_app, db
from app.models import Cart, Product, User
from sqlalchemy import create_engine, text
from sqlalchemy.orm import scoped_session, sessionmaker
from app import create_app, db
from app.models import User, Product, Cart, Order
from werkzeug.security import generate_password_hash


Expand Down Expand Up @@ -40,11 +41,15 @@ def _create_test_db():

class TestConfig:
"""Test configuration."""

TESTING = True
SQLALCHEMY_DATABASE_URI = f"postgresql://postgres:postgres@{get_database_host()}:5432/pharmacy_test_db"
SQLALCHEMY_DATABASE_URI = (
f"postgresql://postgres:postgres@{get_database_host()}:5432/pharmacy_test_db"
)
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = "test_secret_key"
TOKEN_SECRET_KEY = "test_token_secret_key" * 2
FRONTEND_URL = "http://localhost:8081"
MAIL_SERVER = "localhost"
MAIL_PORT = 25
MAIL_USE_TLS = False
Expand Down Expand Up @@ -107,7 +112,10 @@ def test_user(db_session):
email = f"test_{unique_id[:8]}@example.com"

user = User(
username=username, email=email, password=generate_password_hash("Test123!")
username=username,
email=email,
password=generate_password_hash("Test123!"),
email_verified=True,
)
db_session.add(user)
db_session.commit()
Expand Down
35 changes: 20 additions & 15 deletions backend/tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import pytest
from unittest.mock import patch

from flask import json


def test_register_success(client):
"""Test successful user registration."""
response = client.post(
"/api/auth/register",
json={
"username": "newuser",
"email": "[email protected]",
"password": "TestPass123!",
},
)
assert response.status_code == 201
data = json.loads(response.data)
assert "user" in data
assert data["user"]["username"] == "newuser"
assert data["user"]["email"] == "[email protected]"
with patch("app.routes.auth.send_verification_email") as mock_email:
mock_email.return_value = True

response = client.post(
"/api/auth/register",
json={
"username": "newuser",
"email": "[email protected]",
"password": "TestPass123!",
},
)
assert response.status_code == 201
data = json.loads(response.data)
assert "user" in data
assert data["user"]["username"] == "newuser"
assert data["user"]["email"] == "[email protected]"
assert not data["user"]["email_verified"]


def test_register_duplicate_username(client, test_user):
Expand Down Expand Up @@ -70,7 +75,7 @@ def test_login_success(client, test_user):
"/api/auth/login",
json={
"username": test_user.username,
"password": "Test123!", # Must match the password set in test_user fixture
"password": "Test123!",
},
)
assert response.status_code == 200
Expand Down

0 comments on commit af2cad7

Please sign in to comment.