File: test_alerts.py

package info (click to toggle)
python-django-debug-toolbar 1%3A6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: python: 7,555; javascript: 636; makefile: 67; sh: 16
file content (112 lines) | stat: -rw-r--r-- 4,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from django.http import HttpResponse, StreamingHttpResponse
from django.template import Context, Template

from ..base import BaseTestCase


class AlertsPanelTestCase(BaseTestCase):
    panel_id = "AlertsPanel"

    def test_alert_warning_display(self):
        """
        Test that the panel (does not) display[s] an alert when there are
        (no) problems.
        """
        self.panel.record_stats({"alerts": []})
        self.assertNotIn("alerts", self.panel.nav_subtitle)

        self.panel.record_stats({"alerts": ["Alert 1", "Alert 2"]})
        self.assertIn("2 alerts", self.panel.nav_subtitle)

    def test_file_form_without_enctype_multipart_form_data(self):
        """
        Test that the panel displays a form invalid message when there is
        a file input but encoding not set to multipart/form-data.
        """
        test_form = '<form id="test-form"><input type="file"></form>'
        result = self.panel.check_invalid_file_form_configuration(test_form)
        expected_error = (
            'Form with id "test-form" contains file input, '
            'but does not have the attribute enctype="multipart/form-data".'
        )
        self.assertEqual(result[0]["alert"], expected_error)
        self.assertEqual(len(result), 1)

    def test_file_form_no_id_without_enctype_multipart_form_data(self):
        """
        Test that the panel displays a form invalid message when there is
        a file input but encoding not set to multipart/form-data.

        This should use the message when the form has no id.
        """
        test_form = '<form><input type="file"></form>'
        result = self.panel.check_invalid_file_form_configuration(test_form)
        expected_error = (
            "Form contains file input, but does not have "
            'the attribute enctype="multipart/form-data".'
        )
        self.assertEqual(result[0]["alert"], expected_error)
        self.assertEqual(len(result), 1)

    def test_file_form_with_enctype_multipart_form_data(self):
        test_form = """<form id="test-form" enctype="multipart/form-data">
        <input type="file">
        </form>"""
        result = self.panel.check_invalid_file_form_configuration(test_form)

        self.assertEqual(len(result), 0)

    def test_file_form_with_enctype_multipart_form_data_in_button(self):
        test_form = """<form id="test-form">
        <input type="file">
        <input type="submit" formenctype="multipart/form-data">
        </form>"""
        result = self.panel.check_invalid_file_form_configuration(test_form)

        self.assertEqual(len(result), 0)

    def test_referenced_file_input_without_enctype_multipart_form_data(self):
        test_file_input = """<form id="test-form"></form>
        <input type="file" form = "test-form">"""
        result = self.panel.check_invalid_file_form_configuration(test_file_input)

        expected_error = (
            'Input element references form with id "test-form", '
            'but the form does not have the attribute enctype="multipart/form-data".'
        )
        self.assertEqual(result[0]["alert"], expected_error)
        self.assertEqual(len(result), 1)

    def test_referenced_file_input_with_enctype_multipart_form_data(self):
        test_file_input = """<form id="test-form" enctype="multipart/form-data">
        </form>
        <input type="file" form = "test-form">"""
        result = self.panel.check_invalid_file_form_configuration(test_file_input)

        self.assertEqual(len(result), 0)

    def test_integration_file_form_without_enctype_multipart_form_data(self):
        t = Template('<form id="test-form"><input type="file"></form>')
        c = Context({})
        rendered_template = t.render(c)
        response = HttpResponse(content=rendered_template)

        self.panel.generate_stats(self.request, response)

        self.assertIn("1 alert", self.panel.nav_subtitle)
        self.assertIn(
            "Form with id &quot;test-form&quot; contains file input, "
            "but does not have the attribute enctype=&quot;multipart/form-data&quot;.",
            self.panel.content,
        )

    def test_streaming_response(self):
        """Test to check for a streaming response."""

        def _render():
            yield "ok"

        response = StreamingHttpResponse(_render())

        self.panel.generate_stats(self.request, response)
        self.assertEqual(self.panel.get_stats(), {"alerts": []})