151 lines
4.8 KiB
Python
151 lines
4.8 KiB
Python
import pytest
|
|
|
|
from app import db
|
|
from app.models import Board, Card, List
|
|
|
|
|
|
@pytest.mark.integration
|
|
class TestListRoutes:
|
|
"""Test List API routes"""
|
|
|
|
def test_create_list_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test creating a new list in a board"""
|
|
# Create a board
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/boards/{board.id}/lists",
|
|
headers=auth_headers,
|
|
json={"name": "To Do", "pos": 0},
|
|
)
|
|
|
|
assert response.status_code == 201
|
|
data = response.get_json()
|
|
assert data["name"] == "To Do"
|
|
assert data["board_id"] == board.id
|
|
|
|
def test_create_list_missing_name(
|
|
self, client, db_session, regular_user, auth_headers
|
|
):
|
|
"""Test creating a list without name"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/boards/{board.id}/lists",
|
|
headers=auth_headers,
|
|
json={"pos": 0},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
def test_create_list_unauthorized(self, client, db_session, regular_user):
|
|
"""Test creating a list without authentication"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.commit()
|
|
|
|
response = client.post(
|
|
f"/api/boards/{board.id}/lists",
|
|
json={"name": "To Do"},
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
|
|
def test_create_list_board_not_found(
|
|
self, client, db_session, regular_user, auth_headers
|
|
):
|
|
"""Test creating a list in a non-existent board"""
|
|
response = client.post(
|
|
"/api/boards/99999/lists",
|
|
headers=auth_headers,
|
|
json={"name": "To Do"},
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
def test_update_list_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test updating a list"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="Original", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.commit()
|
|
|
|
response = client.put(
|
|
f"/api/lists/{lst.id}",
|
|
headers=auth_headers,
|
|
json={"name": "Updated Name", "pos": 1},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["name"] == "Updated Name"
|
|
assert data["pos"] == 1
|
|
|
|
def test_update_list_not_found(self, client, db_session, auth_headers):
|
|
"""Test updating a non-existent list"""
|
|
response = client.put(
|
|
"/api/lists/99999",
|
|
headers=auth_headers,
|
|
json={"name": "Updated"},
|
|
)
|
|
|
|
assert response.status_code == 404
|
|
|
|
def test_delete_list_success(self, client, db_session, regular_user, auth_headers):
|
|
"""Test deleting a list"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Delete", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.commit()
|
|
|
|
response = client.delete(f"/api/lists/{lst.id}", headers=auth_headers)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert "message" in data
|
|
|
|
# Verify list is deleted
|
|
deleted_list = db.session.get(List, lst.id)
|
|
assert deleted_list is None
|
|
|
|
def test_delete_list_not_found(self, client, db_session, auth_headers):
|
|
"""Test deleting a non-existent list"""
|
|
response = client.delete("/api/lists/99999", headers=auth_headers)
|
|
|
|
assert response.status_code == 404
|
|
|
|
def test_delete_list_with_cards(
|
|
self, client, db_session, regular_user, auth_headers
|
|
):
|
|
"""Test deleting a list with cards (cascading delete)"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
lst = List(name="To Delete", board_id=board.id, pos=0)
|
|
db_session.add(lst)
|
|
db_session.flush()
|
|
|
|
card = Card(name="Card", board_id=board.id, list_id=lst.id, pos=0)
|
|
db_session.add(card)
|
|
db_session.commit()
|
|
|
|
response = client.delete(f"/api/lists/{lst.id}", headers=auth_headers)
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Verify both list and card are deleted
|
|
deleted_list = db.session.get(List, lst.id)
|
|
deleted_card = db.session.get(Card, card.id)
|
|
assert deleted_list is None
|
|
assert deleted_card is None
|