mirror of
https://github.com/wahyd4/links.git
synced 2026-08-09 05:06:16 +10:00
84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
"""
|
|
Integration tests for the Posts REST API (/api/posts).
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.django_db
|
|
class TestPostsAPI:
|
|
BASE = "/api/posts"
|
|
|
|
def test_list_empty(self, api_client):
|
|
r = api_client.get(f"{self.BASE}")
|
|
assert r.status_code == 200
|
|
assert r.json()["count"] == 0
|
|
|
|
def test_create_post(self, api_client):
|
|
r = api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": "Hello World", "content": "# Hello\n\nThis is a test post."},
|
|
format="json",
|
|
)
|
|
assert r.status_code == 201
|
|
data = r.json()
|
|
assert data["title"] == "Hello World"
|
|
assert "id" in data
|
|
|
|
def test_list_after_create(self, api_client):
|
|
for i in range(3):
|
|
api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": f"Post {i}", "content": f"Content {i}"},
|
|
format="json",
|
|
)
|
|
r = api_client.get(f"{self.BASE}")
|
|
assert r.json()["count"] == 3
|
|
|
|
def test_get_post_detail(self, api_client):
|
|
created = api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": "Detail Test", "content": "Some content"},
|
|
format="json",
|
|
).json()
|
|
r = api_client.get(f"{self.BASE}/{created['id']}")
|
|
assert r.status_code == 200
|
|
assert r.json()["title"] == "Detail Test"
|
|
|
|
def test_update_post(self, api_client):
|
|
created = api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": "Original", "content": "Original content"},
|
|
format="json",
|
|
).json()
|
|
r = api_client.patch(
|
|
f"{self.BASE}/{created['id']}",
|
|
{"title": "Updated"},
|
|
format="json",
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["title"] == "Updated"
|
|
|
|
def test_delete_post(self, api_client):
|
|
created = api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": "Delete Me", "content": "Content"},
|
|
format="json",
|
|
).json()
|
|
r = api_client.delete(f"{self.BASE}/{created['id']}")
|
|
assert r.status_code == 204
|
|
assert api_client.get(f"{self.BASE}/{created['id']}").status_code == 404
|
|
|
|
def test_create_post_missing_required_fields(self, api_client):
|
|
r = api_client.post(f"{self.BASE}", {"title": "No content"}, format="json")
|
|
assert r.status_code == 400
|
|
|
|
def test_create_post_with_summary(self, api_client):
|
|
r = api_client.post(
|
|
f"{self.BASE}",
|
|
{"title": "With Summary", "content": "Body", "summary": "Short summary"},
|
|
format="json",
|
|
)
|
|
assert r.status_code == 201
|
|
assert r.json()["summary"] == "Short summary"
|