Skip to content

Commit 9f7f8f4

Browse files
committed
Improve tests
1 parent 36c07c6 commit 9f7f8f4

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

djangocms_rest/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib.sites.shortcuts import get_current_site
22

3-
from cms.models import PageContent, Placeholder
3+
from cms.models import Page, PageContent, Placeholder
44
from cms.utils.conf import get_languages
55
from cms.utils.page_permissions import user_can_view_page
66

@@ -54,6 +54,7 @@ def extend_placeholder_schema(func):
5454

5555
class LanguageListView(BaseAPIView):
5656
serializer_class = LanguageSerializer
57+
queryset = Page.objects.none() # Dummy queryset to satisfy DRF
5758

5859
def get(self, request: Request | None) -> Response:
5960
"""List of languages available for the site."""

tests/test_edit_endpoint.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import json
12
from django.urls import reverse
23
from tests.base import BaseCMSRestTestCase
34

45
from django.contrib.contenttypes.models import ContentType
56
from django.core.files import File
6-
from django.utils.html import escape
77

88

99
from cms import api
1010
from cms.models import PageContent
1111
from cms.toolbar.utils import get_object_edit_url
1212

1313
from filer.models.imagemodels import Image
14+
from bs4 import BeautifulSoup
1415

1516

1617
class PlaceholdersAPITestCase(BaseCMSRestTestCase):
@@ -80,7 +81,20 @@ def create_image(self, filename=None, folder=None):
8081
return image_obj
8182

8283
def test_in_sync_with_api_endpoint(self):
83-
# Needs to return the same content as the edit endpoint
84+
# Edit endpoint and api endpoint should return the same content
85+
86+
def get_text_from_html(html, selector):
87+
soup = BeautifulSoup(html, "html.parser")
88+
element = soup.select_one(selector)
89+
if element:
90+
return (
91+
element.get_text(strip=True)
92+
.replace(",]", "]")
93+
.replace(",}", "}")
94+
.rstrip(",")
95+
)
96+
return None
97+
8498
self.client.force_login(self.user)
8599
response = self.client.get(self.endpoint)
86100
api_response = self.client.get(
@@ -99,26 +113,10 @@ def test_in_sync_with_api_endpoint(self):
99113
self.assertEqual(response.status_code, 200)
100114
self.assertEqual(api_response.status_code, 200)
101115
content = response.content.decode("utf-8")
102-
api_content = api_response.json()["content"]
103-
104-
def test_object(obj):
105-
for key, value in obj.items():
106-
self.assertIn(f'<span class="key">"{key}"</span>:', content)
107-
if value is None:
108-
self.assertIn('<span class="null">null</span>', content)
109-
elif isinstance(value, dict):
110-
test_object(value)
111-
elif isinstance(value, list):
112-
for item in value:
113-
if isinstance(item, dict):
114-
test_object(item)
115-
else:
116-
self.assertIn(escape(str(item)), content)
117-
else:
118-
self.assertIn(escape(str(value)), content)
119-
120-
for plugin in api_content:
121-
test_object(plugin)
116+
117+
json_content = json.loads(get_text_from_html(content, "div.rest-placeholder"))
118+
api_content = api_response.json()
119+
self.assertEqual(json_content, api_content)
122120

123121
def test_edit_endpoint(self):
124122
self.client.force_login(self.user)

0 commit comments

Comments
 (0)