|
1 | 1 | from cms.api import add_plugin
|
2 | 2 | from cms.test_utils.testcases import CMSTestCase
|
| 3 | +from cms.utils.urlutils import admin_reverse |
3 | 4 | from django.http import HttpRequest
|
4 | 5 |
|
5 | 6 | from djangocms_frontend import settings
|
@@ -116,19 +117,6 @@ def test_plugin(self):
|
116 | 117 | f'Cound not find class="btn btn-outline-primary" in {response.content.decode("utf-8")}',
|
117 | 118 | )
|
118 | 119 |
|
119 |
| - def test_smart_link_field(self): |
120 |
| - slf = SmartLinkField() |
121 |
| - choices = get_choices(None) |
122 |
| - self.assertEqual("example.com", choices[1][0]) # Site name |
123 |
| - self.assertIn(("2-1", "home"), choices[1][1]) |
124 |
| - |
125 |
| - cleaned = slf.clean("2-1") |
126 |
| - self.assertEqual(dict(model="cms.page", pk=1), cleaned) |
127 |
| - |
128 |
| - self.assertEqual(slf.prepare_value("blabla"), "") |
129 |
| - self.assertEqual(slf.prepare_value(dict(model="cms.page", pk=1)), "2-1") |
130 |
| - self.assertEqual(slf.prepare_value(self.home), "2-1") |
131 |
| - |
132 | 120 | def test_link_form(self):
|
133 | 121 | request = HttpRequest()
|
134 | 122 | request.POST = {
|
@@ -165,3 +153,43 @@ def test_link_form(self):
|
165 | 153 | request.POST["external_link"] = None
|
166 | 154 | form = LinkForm(request.POST)
|
167 | 155 | self.assertFalse(form.is_valid()) # no anchor for mail
|
| 156 | + |
| 157 | + |
| 158 | +class AutocompleteViewTestCase(TestFixture, CMSTestCase): |
| 159 | + |
| 160 | + def test_smart_link_field(self): |
| 161 | + slf = SmartLinkField() |
| 162 | + choices = get_choices(None) |
| 163 | + self.assertEqual("example.com", choices[0][0]) # Site name |
| 164 | + self.assertIn(("2-1", "home"), choices[0][1]) |
| 165 | + |
| 166 | + cleaned = slf.clean("2-1") |
| 167 | + self.assertEqual(dict(model="cms.page", pk=1), cleaned) |
| 168 | + |
| 169 | + self.assertEqual(slf.prepare_value("blabla"), "") |
| 170 | + self.assertEqual(slf.prepare_value(dict(model="cms.page", pk=1)), "2-1") |
| 171 | + self.assertEqual(slf.prepare_value(self.home), "2-1") |
| 172 | + |
| 173 | + def test_autocomplete_view(self): |
| 174 | + tricky_title = """d'acceuil: <script>alert("XSS");</script>""" |
| 175 | + page = self.create_page( |
| 176 | + title=tricky_title, |
| 177 | + template="page.html", |
| 178 | + ) |
| 179 | + expected_choices = [ |
| 180 | + "home", "content", tricky_title, |
| 181 | + ] |
| 182 | + |
| 183 | + self.publish(page, self.language) |
| 184 | + autocomplete_url = admin_reverse("link_link_autocomplete") |
| 185 | + |
| 186 | + with self.login_user_context(self.superuser): |
| 187 | + response = self.client.get(autocomplete_url) |
| 188 | + |
| 189 | + autocomplete_result = response.json() |
| 190 | + choices = autocomplete_result.get("results")[0] |
| 191 | + |
| 192 | + self.assertFalse((autocomplete_result.get("pagination") or {}).get("more")) |
| 193 | + |
| 194 | + for expected, sent in zip(expected_choices, choices.get("children")): |
| 195 | + self.assertEqual(expected, sent.get("text")) |
0 commit comments