1
+ import json
1
2
from django .urls import reverse
2
3
from tests .base import BaseCMSRestTestCase
3
4
4
5
from django .contrib .contenttypes .models import ContentType
5
6
from django .core .files import File
6
- from django .utils .html import escape
7
7
8
8
9
9
from cms import api
10
10
from cms .models import PageContent
11
11
from cms .toolbar .utils import get_object_edit_url
12
12
13
13
from filer .models .imagemodels import Image
14
+ from bs4 import BeautifulSoup
14
15
15
16
16
17
class PlaceholdersAPITestCase (BaseCMSRestTestCase ):
@@ -80,7 +81,20 @@ def create_image(self, filename=None, folder=None):
80
81
return image_obj
81
82
82
83
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
+
84
98
self .client .force_login (self .user )
85
99
response = self .client .get (self .endpoint )
86
100
api_response = self .client .get (
@@ -99,26 +113,10 @@ def test_in_sync_with_api_endpoint(self):
99
113
self .assertEqual (response .status_code , 200 )
100
114
self .assertEqual (api_response .status_code , 200 )
101
115
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 )
122
120
123
121
def test_edit_endpoint (self ):
124
122
self .client .force_login (self .user )
0 commit comments