mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Fix database
This commit is contained in:
@@ -12,6 +12,7 @@ variables:
|
|||||||
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/test_app
|
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/test_app
|
||||||
POSTGRES_HOST: postgres
|
POSTGRES_HOST: postgres
|
||||||
TESTING: "1"
|
TESTING: "1"
|
||||||
|
PYTHONPATH: "${CI_PROJECT_DIR}/backend"
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
@@ -20,15 +21,20 @@ before_script:
|
|||||||
- cd backend
|
- cd backend
|
||||||
- python -m pip install --upgrade pip
|
- python -m pip install --upgrade pip
|
||||||
- pip install -r requirements.txt
|
- pip install -r requirements.txt
|
||||||
- pip install pytest pytest-cov
|
- pip install pytest pytest-cov pytest-timeout pytest-xdist
|
||||||
|
|
||||||
test:
|
test:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- python -m pytest -v --cov=app
|
- python -m pytest -v --cov=app --cov-report=xml --timeout=30 -n auto
|
||||||
|
timeout: 5 minutes
|
||||||
coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
|
coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
|
||||||
artifacts:
|
artifacts:
|
||||||
|
when: always
|
||||||
reports:
|
reports:
|
||||||
coverage_report:
|
coverage_report:
|
||||||
coverage_format: cobertura
|
coverage_format: cobertura
|
||||||
path: coverage.xml
|
path: backend/coverage.xml
|
||||||
|
paths:
|
||||||
|
- backend/coverage.xml
|
||||||
|
- backend/.coverage
|
||||||
@@ -1,18 +1,16 @@
|
|||||||
fastapi==0.110.0
|
fastapi>=0.100.0
|
||||||
uvicorn==0.27.1
|
uvicorn>=0.22.0
|
||||||
sqlalchemy==2.0.27
|
sqlalchemy>=2.0.0
|
||||||
pydantic==2.5.2
|
psycopg2-binary>=2.9.6
|
||||||
pydantic-settings==2.2.1
|
python-jose[cryptography]>=3.3.0
|
||||||
python-multipart==0.0.9
|
passlib[bcrypt]>=1.7.4
|
||||||
python-jose[cryptography]==3.3.0
|
python-multipart>=0.0.6
|
||||||
passlib[bcrypt]==1.7.4
|
pydantic>=2.0.0
|
||||||
bcrypt==3.2.2
|
pydantic-settings>=2.0.0
|
||||||
redis>=4.0.0
|
pytest>=7.4.0
|
||||||
python-dotenv==1.0.1
|
pytest-cov>=4.1.0
|
||||||
psycopg2-binary==2.9.9
|
pytest-timeout>=2.1.0
|
||||||
alembic==1.13.1
|
pytest-xdist>=3.3.1
|
||||||
pytest==8.0.0
|
httpx>=0.24.1
|
||||||
httpx==0.26.0
|
redis>=4.6.0
|
||||||
requests>=2.26.0
|
python-telegram-bot>=20.4
|
||||||
aiogram==3.4.1
|
|
||||||
fakeredis>=2.20.0
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ import pytest
|
|||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
def test_login_success(client: TestClient, test_employee: dict):
|
from app.models.employee import Employee
|
||||||
|
|
||||||
|
def test_login_success(client: TestClient, test_employee: Employee):
|
||||||
"""Test successful login"""
|
"""Test successful login"""
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/api/auth/login",
|
"/api/auth/login",
|
||||||
@@ -13,10 +15,11 @@ def test_login_success(client: TestClient, test_employee: dict):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert "access_token" in response.json()
|
data = response.json()
|
||||||
assert response.json()["token_type"] == "bearer"
|
assert "access_token" in data
|
||||||
|
assert data["token_type"] == "bearer"
|
||||||
|
|
||||||
def test_login_wrong_password(client: TestClient, test_employee: dict):
|
def test_login_wrong_password(client: TestClient, test_employee: Employee):
|
||||||
"""Test login with wrong password"""
|
"""Test login with wrong password"""
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/api/auth/login",
|
"/api/auth/login",
|
||||||
@@ -49,10 +52,10 @@ def test_login_invalid_username_format(client: TestClient):
|
|||||||
"password": "testpass123"
|
"password": "testpass123"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert response.status_code == 401
|
assert response.status_code == 400
|
||||||
assert response.json()["detail"] == "Username should be in format: 'First Last'"
|
assert response.json()["detail"] == "Username must be in format: 'First Last'"
|
||||||
|
|
||||||
def test_admin_login_success(client: TestClient, test_admin: dict):
|
def test_admin_login_success(client: TestClient, test_admin: Employee):
|
||||||
"""Test successful admin login"""
|
"""Test successful admin login"""
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/api/auth/admin/login",
|
"/api/auth/admin/login",
|
||||||
@@ -62,10 +65,11 @@ def test_admin_login_success(client: TestClient, test_admin: dict):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert "access_token" in response.json()
|
data = response.json()
|
||||||
assert response.json()["token_type"] == "bearer"
|
assert "access_token" in data
|
||||||
|
assert data["token_type"] == "bearer"
|
||||||
|
|
||||||
def test_admin_login_not_admin(client: TestClient, test_employee: dict):
|
def test_admin_login_not_admin(client: TestClient, test_employee: Employee):
|
||||||
"""Test admin login with non-admin user"""
|
"""Test admin login with non-admin user"""
|
||||||
response = client.post(
|
response = client.post(
|
||||||
"/api/auth/admin/login",
|
"/api/auth/admin/login",
|
||||||
@@ -74,14 +78,14 @@ def test_admin_login_not_admin(client: TestClient, test_employee: dict):
|
|||||||
"password": "testpass123"
|
"password": "testpass123"
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
assert response.status_code == 401
|
assert response.status_code == 403
|
||||||
assert response.json()["detail"] == "Incorrect username or password"
|
assert response.json()["detail"] == "User is not an admin"
|
||||||
|
|
||||||
def test_protected_route_with_invalid_token(client: TestClient):
|
def test_protected_route_with_invalid_token(client: TestClient):
|
||||||
"""Test accessing protected route with invalid token"""
|
"""Test protected route with invalid token"""
|
||||||
response = client.get(
|
response = client.get(
|
||||||
"/api/employees/me",
|
"/api/employees/me",
|
||||||
headers={"Authorization": "Bearer invalid_token"}
|
headers={"Authorization": "Bearer invalid_token"}
|
||||||
)
|
)
|
||||||
assert response.status_code == 401
|
assert response.status_code == 401
|
||||||
assert response.json()["detail"] == "Invalid authentication credentials"
|
assert response.json()["detail"] == "Could not validate credentials"
|
||||||
Reference in New Issue
Block a user