Coverage for src/tests/api/v1/pages/test_endpoints.py: 100%
40 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for testing the pages endpoint."""
3from fastapi import status
4from fastapi.testclient import TestClient
5from kwai_bc_portal.domain.page import PageIdentifier
6from kwai_bc_portal.pages.page_db_repository import PageDbRepository
7from kwai_core.db.uow import Database, UnitOfWork
10def test_get_pages(client: TestClient):
11 """Test GET /api/v1/pages."""
12 response = client.get("/api/v1/pages")
13 assert response.status_code == status.HTTP_200_OK
15 json = response.json()
16 assert "meta" in json, "There should be a meta object in the response"
17 assert "data" in json, "There should be a data list in the response"
20async def test_get_page(client: TestClient, make_page_in_db):
21 """Test /api/v1/pages/{id}."""
22 page = await make_page_in_db()
23 response = client.get(f"/api/v1/pages/{page.id}")
24 json = response.json()
25 assert response.status_code == status.HTTP_200_OK
26 assert len(json["data"]) > 0, "There should be at least one page."
28 json = response.json()
29 assert "data" in json, "There should be data in the response"
31 training_resource = json["data"]
32 assert training_resource is not None, f"Page with id {page.id} should exist"
35async def test_create_page(
36 secure_client: TestClient, database: Database, make_application_in_db
37):
38 """Test POST /api/v1/pages."""
39 application = await make_application_in_db()
40 payload = {
41 "data": {
42 "type": "pages",
43 "attributes": {
44 "texts": [
45 {
46 "locale": "en",
47 "format": "md",
48 "title": "Test New Website",
49 "summary": "Testing the new website",
50 "original_summary": "Testing the new website",
51 "content": "Testing the new website content",
52 "original_content": "Testing the new website content",
53 }
54 ],
55 "enabled": True,
56 "priority": 0,
57 "remark": "",
58 },
59 "relationships": {
60 "application": {
61 "data": {"type": "applications", "id": str(application.id)}
62 }
63 },
64 },
65 }
66 response = secure_client.post("/api/v1/pages", json=payload)
67 assert response.status_code == status.HTTP_201_CREATED, response.json()
69 async with UnitOfWork(database):
70 page_repo = PageDbRepository(database)
71 page = await page_repo.get_by_id(PageIdentifier(response.json()["data"]["id"]))
72 await page_repo.delete(page)
75async def test_patch_page(
76 secure_client: TestClient, make_page_in_db, make_application_in_db
77):
78 """Test PATCH /api/v1/pages."""
79 page = await make_page_in_db()
80 application = await make_application_in_db()
81 payload = {
82 "data": {
83 "id": str(page.id),
84 "type": "pages",
85 "attributes": {
86 "texts": [
87 {
88 "locale": "en",
89 "format": "md",
90 "title": "Test New Website",
91 "summary": "Testing the new website (update)",
92 "original_summary": "Testing the new website (update)",
93 "content": "Testing the new website content (update)",
94 "original_content": "Testing the new website content (update)",
95 }
96 ],
97 "enabled": True,
98 "priority": 0,
99 "remark": "Updated",
100 },
101 "relationships": {
102 "application": {
103 "data": {"type": "applications", "id": str(application.id)}
104 }
105 },
106 },
107 }
108 response = secure_client.patch(f"/api/v1/pages/{page.id}", json=payload)
109 assert response.status_code == status.HTTP_200_OK, response.json()
112async def test_delete_page(secure_client: TestClient, make_page_in_db):
113 """Test DELETE /api/v1/pages."""
114 page = await make_page_in_db()
115 response = secure_client.delete(f"/api/v1/pages/{page.id}")
116 assert response.status_code == status.HTTP_200_OK, response.json()