219 lines
7.2 KiB
Python
219 lines
7.2 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 name (position stays same for single 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": 0},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["name"] == "Updated Name"
|
|
assert data["pos"] == 0
|
|
|
|
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
|
|
|
|
def test_update_list_position_reorders_others(
|
|
self, client, db_session, regular_user, auth_headers
|
|
):
|
|
"""Test updating list position reorders other lists in the board"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
# Create 3 lists in sequential positions
|
|
list1 = List(name="List 1", board_id=board.id, pos=0)
|
|
list2 = List(name="List 2", board_id=board.id, pos=1)
|
|
list3 = List(name="List 3", board_id=board.id, pos=2)
|
|
db_session.add(list1)
|
|
db_session.add(list2)
|
|
db_session.add(list3)
|
|
db_session.commit()
|
|
|
|
# Move list3 from position 2 to position 0 (front)
|
|
response = client.put(
|
|
f"/api/lists/{list3.id}",
|
|
headers=auth_headers,
|
|
json={"name": "List 3", "pos": 0},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Verify all lists have unique, sequential positions
|
|
updated_lists = List.query.filter_by(board_id=board.id).order_by(List.pos).all()
|
|
assert len(updated_lists) == 3
|
|
assert updated_lists[0].id == list3.id
|
|
assert updated_lists[0].pos == 0.0
|
|
assert updated_lists[1].id == list1.id
|
|
assert updated_lists[1].pos == 1.0
|
|
assert updated_lists[2].id == list2.id
|
|
assert updated_lists[2].pos == 2.0
|
|
|
|
def test_update_list_position_no_change(
|
|
self, client, db_session, regular_user, auth_headers
|
|
):
|
|
"""Test updating list with same position doesn't reorder others"""
|
|
board = Board(name="Test Board", user_id=regular_user.id)
|
|
db_session.add(board)
|
|
db_session.flush()
|
|
|
|
list1 = List(name="List 1", board_id=board.id, pos=0)
|
|
list2 = List(name="List 2", board_id=board.id, pos=1)
|
|
db_session.add(list1)
|
|
db_session.add(list2)
|
|
db_session.commit()
|
|
|
|
original_pos1 = list1.pos
|
|
original_pos2 = list2.pos
|
|
|
|
# Update list2 but keep same position
|
|
response = client.put(
|
|
f"/api/lists/{list2.id}",
|
|
headers=auth_headers,
|
|
json={"name": "Updated List 2", "pos": original_pos2},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Verify positions unchanged
|
|
updated_list1 = db.session.get(List, list1.id)
|
|
updated_list2 = db.session.get(List, list2.id)
|
|
assert updated_list1.pos == original_pos1
|
|
assert updated_list2.pos == original_pos2
|