167 lines
No EOL
5.5 KiB
Python
167 lines
No EOL
5.5 KiB
Python
import pytest
|
|
|
|
from app import db
|
|
from app.models import Board, List, Card
|
|
|
|
|
|
@pytest.mark.integration
|
|
class TestCardRoutes:
|
|
"""Test Card API routes"""
|
|
|
|
def test_create_card_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test creating a new card in a list"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/lists/{lst.id}/cards",
|
|
headers=auth_headers,
|
|
json={"name": "New Card", "description": "Card description", "pos": 0},
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
data = response.get_json()
|
|
assert data["name"] == "New Card"
|
|
assert data["description"] == "Card description"
|
|
assert data["list_id"] == lst.id
|
|
assert data["board_id"] == board.id
|
|
|
|
def test_create_card_missing_name(self, client, db_session, regular_user, auth_headers):
|
|
"""Test creating a card without name"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/lists/{lst.id}/cards",
|
|
headers=auth_headers,
|
|
json={"description": "Test"},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
def test_create_card_unauthorized(self, client, db_session, regular_user):
|
|
"""Test creating a card without authentication"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/lists/{lst.id}/cards",
|
|
json={"name": "New Card"},
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
|
|
def test_get_card_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test getting a single card with full details"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.flush()
|
|
|
|
card = Card(name="Test Card", board_id=board.id, list_id=lst.id, pos=0)
|
|
db_session.add(card)
|
|
db_session.commit()
|
|
|
|
response = client.get(f"/api/cards/{card.id}", headers=auth_headers)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["id"] == card.id
|
|
assert data["name"] == "Test Card"
|
|
assert "labels" in data
|
|
assert "checklists" in data
|
|
assert "comments" in data
|
|
|
|
def test_get_card_not_found(self, client, db_session, auth_headers):
|
|
"""Test getting a non-existent card"""
|
|
response = client.get("/api/cards/99999", headers=auth_headers)
|
|
|
|
assert response.status_code == 404
|
|
|
|
def test_update_card_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test updating a card"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst1 = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst1)
|
|
db_session.flush()
|
|
|
|
lst2 = List(name="Done", board_id=board.id, pos=1)
|
|
db_session.add(lst2)
|
|
db_session.flush()
|
|
|
|
card = Card(name="Original", board_id=board.id, list_id=lst1.id, pos=0)
|
|
db_session.add(card)
|
|
db_session.commit()
|
|
|
|
response = client.put(
|
|
f"/api/cards/{card.id}",
|
|
headers=auth_headers,
|
|
json={"name": "Updated Name", "list_id": lst2.id, "pos": 1},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["name"] == "Updated Name"
|
|
assert data["list_id"] == lst2.id
|
|
assert data["pos"] == 1
|
|
|
|
def test_update_card_not_found(self, client, db_session, auth_headers):
|
|
"""Test updating a non-existent card"""
|
|
response = client.put(
|
|
"/api/cards/99999",
|
|
headers=auth_headers,
|
|
json={"name": "Updated"},
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
def test_delete_card_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test deleting a card"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Do", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.flush()
|
|
|
|
card = Card(name="To Delete", board_id=board.id, list_id=lst.id, pos=0)
|
|
db_session.add(card)
|
|
db_session.commit()
|
|
|
|
response = client.delete(f"/api/cards/{card.id}", headers=auth_headers)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert "message" in data
|
|
|
|
# Verify card is deleted
|
|
deleted_card = db.session.get(Card, card.id)
|
|
assert deleted_card is None
|
|
|
|
def test_delete_card_not_found(self, client, db_session, auth_headers):
|
|
"""Test deleting a non-existent card"""
|
|
response = client.delete("/api/cards/99999", headers=auth_headers)
|
|
|
|
assert response.status_code == 404 |