Added basic users and tasks endpoints tests

This commit is contained in:
Marcin-Ramotowski 2025-03-27 19:14:12 +00:00
parent f8c0c39345
commit d870bcc613
3 changed files with 104 additions and 0 deletions

15
api/tests/conftest.py Normal file
View File

@ -0,0 +1,15 @@
import pytest
from app import create_app
from models import db
@pytest.fixture
def test_client():
"""Creates a new instance of test app."""
app = create_app("testing")
with app.test_client() as client:
with app.app_context():
db.create_all()
yield client
db.session.remove()
db.drop_all()

42
api/tests/test_tasks.py Normal file
View File

@ -0,0 +1,42 @@
from datetime import datetime
import json
from models import Task, User, db
from flask_jwt_extended import create_access_token
def test_create_task(test_client):
"""Task creation test by logged in user"""
user = User(username="testuser", email="test@example.com", password="hashed_pass", role="User")
db.session.add(user)
db.session.commit()
access_token = create_access_token(identity=str(user.id))
headers = {"Authorization": f"Bearer {access_token}"}
response = test_client.post(
"/tasks",
data=json.dumps({"title": "Test Task", "description": "Opis", "due_date": "20-03-2025 12:00", "done": 0}),
headers=headers,
content_type="application/json",
)
assert response.status_code == 200 # Create task should be successful
data = response.get_json()
assert data["title"] == "Test Task"
def test_get_tasks(test_client):
"""User task get test"""
user = User(username="taskuser", email="task@example.com", password="hashed_pass", role="User")
db.session.add(user)
db.session.commit()
task = Task(title="Zadanie", description="Opis zadania", due_date=datetime.strptime("20-03-2025 12:00", '%d-%m-%Y %H:%M'), done=0, user_id=user.id)
db.session.add(task)
db.session.commit()
access_token = create_access_token(identity=str(user.id))
headers = {"Authorization": f"Bearer {access_token}"}
response = test_client.get(f"/tasks/user/{user.id}", headers=headers)
assert response.status_code == 200
data = response.get_json()
assert len(data) == 1
assert data[0]["title"] == "Zadanie"

47
api/tests/test_users.py Normal file
View File

@ -0,0 +1,47 @@
import json
from models import User, db
from flask_jwt_extended import create_access_token
def test_create_user(test_client):
"""New user registration test"""
response = test_client.post(
"/users",
data=json.dumps({"username": "testuser", "email": "test@example.com", "password": "testpass", "role": "User"}),
content_type="application/json",
)
assert response.status_code == 201 # User should be created successfully
data = response.get_json()
assert data["username"] == "testuser"
def test_login(test_client):
"""User login test with wrong password"""
user = User(username="testuser", email="test@example.com", password="hashed_pass", role="User")
db.session.add(user)
db.session.commit()
response = test_client.post(
"/login",
data=json.dumps({"username": "testuser", "password": "testpass"}),
content_type="application/json",
)
assert response.status_code == 401 # User should not be logged - wrong password
def test_get_users(test_client):
"""Get all users test - JWT required"""
response = test_client.get("/users")
assert response.status_code == 401
def test_get_user_with_token(test_client):
"""Test to get user data after auth using JWT token"""
user = User(username="admin", email="admin@example.com", password="hashed_pass", role="Administrator")
print(user.id)
db.session.add(user)
db.session.commit()
access_token = create_access_token(identity=str(user.id))
headers = {"Authorization": f"Bearer {access_token}"}
response = test_client.get(f"/users/{user.id}", headers=headers)
assert response.status_code == 200
data = response.get_json()
assert data["username"] == "admin"