diff options
| author | Thomas Grainger <tom@yplanapp.com> | 2015-11-23 10:46:19 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-12-05 15:51:57 -0500 |
| commit | d638cdc42acec608c1967f44af6be32a477c239f (patch) | |
| tree | 44e102c555476dae8f2f339b6fa11bb57f982386 /tests | |
| parent | 105028eec64064c11dfd3d5415317ba1fab81214 (diff) | |
Fixed #25165 -- Removed inline JavaScript from the admin.
This allows setting a Content-Security-Policy HTTP header
(refs #15727).
Special thanks to blighj, the original author of this patch.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_custom_urls/tests.py | 1 | ||||
| -rw-r--r-- | tests/admin_inlines/test_templates.py | 16 | ||||
| -rw-r--r-- | tests/admin_inlines/tests.py | 22 | ||||
| -rw-r--r-- | tests/admin_views/tests.py | 48 | ||||
| -rw-r--r-- | tests/admin_widgets/tests.py | 10 |
5 files changed, 52 insertions, 45 deletions
diff --git a/tests/admin_custom_urls/tests.py b/tests/admin_custom_urls/tests.py index eff23931ef..5e9a119b6a 100644 --- a/tests/admin_custom_urls/tests.py +++ b/tests/admin_custom_urls/tests.py @@ -74,7 +74,6 @@ class AdminCustomUrlsTest(TestCase): "description": "Description of added action", } response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_action_add'), post_data) - self.assertContains(response, 'dismissAddRelatedObjectPopup') self.assertContains(response, 'Action added through a popup') def test_admin_URLs_no_clash(self): diff --git a/tests/admin_inlines/test_templates.py b/tests/admin_inlines/test_templates.py index 1ed52c9115..236ec5df7b 100644 --- a/tests/admin_inlines/test_templates.py +++ b/tests/admin_inlines/test_templates.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import json + from django.template.loader import render_to_string from django.test import SimpleTestCase @@ -8,14 +10,16 @@ class TestTemplates(SimpleTestCase): def test_javascript_escaping(self): context = { 'inline_admin_formset': { - 'formset': {'prefix': 'my-prefix'}, - 'opts': {'verbose_name': 'verbose name\\'}, + 'inline_formset_data': json.dumps({ + 'formset': {'prefix': 'my-prefix'}, + 'opts': {'verbose_name': 'verbose name\\'}, + }), }, } output = render_to_string('admin/edit_inline/stacked.html', context) - self.assertIn('prefix: "my\\u002Dprefix",', output) - self.assertIn('addText: "Add another Verbose name\\u005C"', output) + self.assertIn('"prefix": "my-prefix"', output) + self.assertIn('"verbose_name": "verbose name\\\\"', output) output = render_to_string('admin/edit_inline/tabular.html', context) - self.assertIn('prefix: "my\\u002Dprefix",', output) - self.assertIn('addText: "Add another Verbose name\\u005C"', output) + self.assertIn('"prefix": "my-prefix"', output) + self.assertIn('"verbose_name": "verbose name\\\\"', output) diff --git a/tests/admin_inlines/tests.py b/tests/admin_inlines/tests.py index 4f2377b2de..b2102480bc 100644 --- a/tests/admin_inlines/tests.py +++ b/tests/admin_inlines/tests.py @@ -76,7 +76,7 @@ class TestInline(TestDataMixin, TestCase): # The heading for the m2m inline block uses the right text self.assertContains(response, '<h2>Author-book relationships</h2>') # The "add another" label is correct - self.assertContains(response, 'Add another Author\\u002Dbook relationship') + self.assertContains(response, 'Add another Author-book relationship') # The '+' is dropped from the autogenerated form prefix (Author_books+) self.assertContains(response, 'id="id_Author_books-TOTAL_FORMS"') @@ -133,14 +133,20 @@ class TestInline(TestDataMixin, TestCase): response = self.client.get(reverse('admin:admin_inlines_novel_add')) self.assertEqual(response.status_code, 200) # View should have the child inlines section - self.assertContains(response, '<div class="inline-group" id="chapter_set-group">') + self.assertContains( + response, + '<div class="js-inline-admin-formset inline-group" id="chapter_set-group"' + ) def test_callable_lookup(self): """Admin inline should invoke local callable when its name is listed in readonly_fields""" response = self.client.get(reverse('admin:admin_inlines_poll_add')) self.assertEqual(response.status_code, 200) # Add parent object view should have the child inlines section - self.assertContains(response, '<div class="inline-group" id="question_set-group">') + self.assertContains( + response, + '<div class="js-inline-admin-formset inline-group" id="question_set-group"' + ) # The right callable should be used for the inline readonly_fields # column cells self.assertContains(response, '<p>Callable in QuestionInline</p>') @@ -548,7 +554,7 @@ class TestInlinePermissions(TestCase): response = self.client.get(reverse('admin:admin_inlines_author_add')) # No change permission on books, so no inline self.assertNotContains(response, '<h2>Author-book relationships</h2>') - self.assertNotContains(response, 'Add another Author\\u002DBook Relationship') + self.assertNotContains(response, 'Add another Author-Book Relationship') self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"') def test_inline_add_fk_noperm(self): @@ -562,7 +568,7 @@ class TestInlinePermissions(TestCase): response = self.client.get(self.author_change_url) # No change permission on books, so no inline self.assertNotContains(response, '<h2>Author-book relationships</h2>') - self.assertNotContains(response, 'Add another Author\\u002DBook Relationship') + self.assertNotContains(response, 'Add another Author-Book Relationship') self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"') def test_inline_change_fk_noperm(self): @@ -578,7 +584,7 @@ class TestInlinePermissions(TestCase): response = self.client.get(reverse('admin:admin_inlines_author_add')) # No change permission on Books, so no inline self.assertNotContains(response, '<h2>Author-book relationships</h2>') - self.assertNotContains(response, 'Add another Author\\u002DBook Relationship') + self.assertNotContains(response, 'Add another Author-Book Relationship') self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"') def test_inline_add_fk_add_perm(self): @@ -597,7 +603,7 @@ class TestInlinePermissions(TestCase): response = self.client.get(self.author_change_url) # No change permission on books, so no inline self.assertNotContains(response, '<h2>Author-book relationships</h2>') - self.assertNotContains(response, 'Add another Author\\u002DBook Relationship') + self.assertNotContains(response, 'Add another Author-Book Relationship') self.assertNotContains(response, 'id="id_Author_books-TOTAL_FORMS"') self.assertNotContains(response, 'id="id_Author_books-0-DELETE"') @@ -607,7 +613,7 @@ class TestInlinePermissions(TestCase): response = self.client.get(self.author_change_url) # We have change perm on books, so we can add/change/delete inlines self.assertContains(response, '<h2>Author-book relationships</h2>') - self.assertContains(response, 'Add another Author\\u002Dbook relationship') + self.assertContains(response, 'Add another Author-book relationship') self.assertContains(response, '<input type="hidden" id="id_Author_books-TOTAL_FORMS" ' 'value="4" name="Author_books-TOTAL_FORMS" />', html=True) self.assertContains( diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 7ccaf75c6a..db960b34b0 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import datetime +import json import os import re import unittest @@ -260,8 +261,7 @@ class AdminViewBasicTest(AdminViewBasicTestCase): 'date_1': '14:55:39', } response = self.client.post(reverse('admin:admin_views_article_add'), post_data) - self.assertContains(response, 'dismissAddRelatedObjectPopup') - self.assertContains(response, 'title with a new\\u000Aline') + self.assertContains(response, 'title with a new\\nline') def test_basic_edit_POST(self): """ @@ -734,7 +734,7 @@ class AdminViewBasicTest(AdminViewBasicTestCase): """ actor = Actor.objects.create(name="Palin", age=27) response = self.client.get("%s?%s" % (reverse('admin:admin_views_actor_changelist'), IS_POPUP_VAR)) - self.assertContains(response, "opener.dismissRelatedLookupPopup(window, '%s')" % actor.pk) + self.assertContains(response, 'data-popup-opener="%s"' % actor.pk) def test_hide_change_password(self): """ @@ -3445,27 +3445,23 @@ action)</option> self.assertEqual(response.template_name, 'admin/popup_response.html') def test_popup_template_escaping(self): - context = { + popup_response_data = json.dumps({ 'new_value': 'new_value\\', 'obj': 'obj\\', 'value': 'value\\', + }) + context = { + 'popup_response_data': popup_response_data, } output = render_to_string('admin/popup_response.html', context) self.assertIn( - 'opener.dismissAddRelatedObjectPopup(window, "value\\u005C", "obj\\u005C");', output + r'"value\\"', output ) - - context['action'] = 'change' - output = render_to_string('admin/popup_response.html', context) self.assertIn( - 'opener.dismissChangeRelatedObjectPopup(window, ' - '"value\\u005C", "obj\\u005C", "new_value\\u005C");', output + r'"new_value\\"', output ) - - context['action'] = 'delete' - output = render_to_string('admin/popup_response.html', context) self.assertIn( - 'opener.dismissDeleteRelatedObjectPopup(window, "value\\u005C");', output + r'"obj\\"', output ) @@ -4273,16 +4269,19 @@ class PrePopulatedTest(TestCase): def test_prepopulated_on(self): response = self.client.get(reverse('admin:admin_views_prepopulatedpost_add')) - self.assertContains(response, "id: '#id_slug',") - self.assertContains(response, "field['dependency_ids'].push('#id_title');") - self.assertContains(response, "id: '#id_prepopulatedsubpost_set-0-subslug',") + self.assertContains(response, ""id": "#id_slug"") + self.assertContains(response, ""dependency_ids": ["#id_title"]") + self.assertContains(response, ""id": "#id_prepopulatedsubpost_set-0-subslug"") def test_prepopulated_off(self): response = self.client.get(reverse('admin:admin_views_prepopulatedpost_change', args=(self.p1.pk,))) self.assertContains(response, "A Long Title") - self.assertNotContains(response, "id: '#id_slug'") - self.assertNotContains(response, "field['dependency_ids'].push('#id_title');") - self.assertNotContains(response, "id: '#id_prepopulatedsubpost_set-0-subslug',") + self.assertNotContains(response, ""id": "#id_slug"") + self.assertNotContains(response, ""dependency_ids": ["#id_title"]") + self.assertNotContains( + response, + ""id": "#id_prepopulatedsubpost_set-0-subslug"" + ) @override_settings(USE_THOUSAND_SEPARATOR=True, USE_L10N=True) def test_prepopulated_maxlength_localized(self): @@ -4291,7 +4290,7 @@ class PrePopulatedTest(TestCase): that maxLength (in the JavaScript) is rendered without separators. """ response = self.client.get(reverse('admin:admin_views_prepopulatedpostlargeslug_add')) - self.assertContains(response, "maxLength: 1000") # instead of 1,000 + self.assertContains(response, ""maxLength": 1000") # instead of 1,000 @override_settings(PASSWORD_HASHERS=['django.contrib.auth.hashers.SHA1PasswordHasher'], @@ -4933,7 +4932,7 @@ class UserAdminTest(TestCase): '_save': '1', } response = self.client.post(reverse('admin:auth_user_add') + '?_popup=1', data, follow=True) - self.assertContains(response, 'dismissAddRelatedObjectPopup') + self.assertContains(response, '"obj": "newuser"') def test_user_fk_change_popup(self): """User change through a FK popup should return the appropriate JavaScript response.""" @@ -4957,7 +4956,8 @@ class UserAdminTest(TestCase): '_save': '1', } response = self.client.post(url, data, follow=True) - self.assertContains(response, 'dismissChangeRelatedObjectPopup') + self.assertContains(response, '"obj": "newuser"') + self.assertContains(response, '"action": "change"') def test_user_fk_delete_popup(self): """User deletion through a FK popup should return the appropriate JavaScript response.""" @@ -4973,7 +4973,7 @@ class UserAdminTest(TestCase): '_popup': '1', } response = self.client.post(url, data, follow=True) - self.assertContains(response, 'dismissDeleteRelatedObjectPopup') + self.assertContains(response, '"action": "delete"') def test_save_add_another_button(self): user_count = User.objects.count() diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 99b7b85afa..46f2d2a76e 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -271,9 +271,8 @@ class FilteredSelectMultipleWidgetTest(SimpleTestCase): w = widgets.FilteredSelectMultiple('test\\', False) self.assertHTMLEqual( w.render('test', 'test'), - '<select multiple="multiple" name="test" class="selectfilter">\n</select>' - '<script type="text/javascript">addEvent(window, "load", function(e) ' - '{SelectFilter.init("id_test", "test\\u005C", 0); });</script>\n' + '<select multiple="multiple" name="test" class="selectfilter" ' + 'data-field-name="test\\" data-is-stacked="0">\n</select>' ) def test_stacked_render(self): @@ -281,9 +280,8 @@ class FilteredSelectMultipleWidgetTest(SimpleTestCase): w = widgets.FilteredSelectMultiple('test\\', True) self.assertHTMLEqual( w.render('test', 'test'), - '<select multiple="multiple" name="test" class="selectfilterstacked">\n</select>' - '<script type="text/javascript">addEvent(window, "load", function(e) ' - '{SelectFilter.init("id_test", "test\\u005C", 1); });</script>\n' + '<select multiple="multiple" name="test" class="selectfilterstacked" ' + 'data-field-name="test\\" data-is-stacked="1">\n</select>' ) |
