55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import pytest
|
|
from django.test import Client
|
|
from core.models import AppUser, Library
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
return Client()
|
|
|
|
@pytest.fixture
|
|
def user(db):
|
|
return AppUser.objects.create_user(username="apiuser", email="api@example.com", password="password")
|
|
|
|
@pytest.fixture
|
|
def library(db, user):
|
|
return Library.objects.create(owner_user=user, name="Test Library", visibility="private")
|
|
|
|
@pytest.mark.django_db
|
|
def test_list_libraries(client, library):
|
|
response = client.get("/api/library/")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 1
|
|
assert data[0]["name"] == "Test Library"
|
|
assert data[0]["owner_user_id"] == library.owner_user.id
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_library(client, library):
|
|
response = client.get(f"/api/library/{library.id}")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["name"] == "Test Library"
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_library_not_found(client):
|
|
response = client.get("/api/library/999")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_library(client, user):
|
|
payload = {
|
|
"name": "New API Library",
|
|
"description": "Created via API",
|
|
"visibility": "public",
|
|
"owner_user_id": user.id
|
|
}
|
|
response = client.post("/api/library/", data=payload, content_type="application/json")
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["name"] == "New API Library"
|
|
assert data["visibility"] == "public"
|
|
|
|
# Verify it hit the DB
|
|
assert Library.objects.count() == 1
|
|
assert Library.objects.get(id=data["id"]).name == "New API Library"
|