Example #1
0
    def test_accept(self):
        widget = ClearableFileInput()
        assert 'accept' not in widget.render(name='file', value='test.jpg')
        assert ["starts-with", "$Content-Type",
                ""] in widget.get_conditions(None)

        widget = ClearableFileInput(attrs={'accept': 'image/*'})
        assert 'accept="image/*"' in widget.render(name='file',
                                                   value='test.jpg')
        assert ["starts-with", "$Content-Type",
                "image/"] in widget.get_conditions('image/*')

        widget = ClearableFileInput(attrs={'accept': 'image/jpeg'})
        assert 'accept="image/jpeg"' in widget.render(name='file',
                                                      value='test.jpg')
        assert {
            "Content-Type": 'image/jpeg'
        } in widget.get_conditions('image/jpeg')

        widget = ClearableFileInput(
            attrs={'accept': 'application/pdf,image/*'})
        assert 'accept="application/pdf,image/*"' in widget.render(
            name='file',
            value='test.jpg',
        )
        assert ["starts-with", "$Content-Type",
                ""] in widget.get_conditions('application/pdf,image/*')
        assert {
            "Content-Type": 'application/pdf'
        } not in widget.get_conditions('application/pdf,image/*')
Example #2
0
    def test_accept(self):
        widget = ClearableFileInput()
        assert "accept" not in widget.render(name="file", value="test.jpg")
        assert ["starts-with", "$Content-Type",
                ""] in widget.get_conditions(None)

        widget = ClearableFileInput(attrs={"accept": "image/*"})
        assert 'accept="image/*"' in widget.render(name="file",
                                                   value="test.jpg")
        assert ["starts-with", "$Content-Type",
                "image/"] in widget.get_conditions("image/*")

        widget = ClearableFileInput(attrs={"accept": "image/jpeg"})
        assert 'accept="image/jpeg"' in widget.render(name="file",
                                                      value="test.jpg")
        assert {
            "Content-Type": "image/jpeg"
        } in widget.get_conditions("image/jpeg")

        widget = ClearableFileInput(
            attrs={"accept": "application/pdf,image/*"})
        assert 'accept="application/pdf,image/*"' in widget.render(
            name="file",
            value="test.jpg",
        )
        assert ["starts-with", "$Content-Type",
                ""] in widget.get_conditions("application/pdf,image/*")
        assert {
            "Content-Type": "application/pdf"
        } not in widget.get_conditions("application/pdf,image/*")
    def test_html_escaped(self):
        """
        A ClearableFileInput should escape name, filename, and URL
        when rendering HTML (#15182).
        """
        @python_2_unicode_compatible
        class StrangeFieldFile(object):
            url = "something?chapter=1&sect=2&copy=3&lang=en"

            def __str__(self):
                return '''something<div onclick="alert('oops')">.jpg'''

        widget = ClearableFileInput()
        field = StrangeFieldFile()
        output = widget.render('my<div>file', field)
        self.assertNotIn(field.url, output)
        self.assertIn(
            'href="something?chapter=1&amp;sect=2&amp;copy=3&amp;lang=en"',
            output)
        self.assertNotIn(six.text_type(field), output)
        self.assertIn(
            'something&lt;div onclick=&quot;alert(&#39;oops&#39;)&quot;&gt;.jpg',
            output)
        self.assertIn('my&lt;div&gt;file', output)
        self.assertNotIn('my<div>file', output)
    def test_html_does_not_mask_exceptions(self):
        """
        A ClearableFileInput should not mask exceptions produced while
        checking that it has a value.
        """
        @python_2_unicode_compatible
        class FailingURLFieldFile(object):
            @property
            def url(self):
                raise RuntimeError('Canary')

            def __str__(self):
                return 'value'

        widget = ClearableFileInput()
        field = FailingURLFieldFile()
        with self.assertRaisesMessage(RuntimeError, 'Canary'):
            widget.render('myfile', field)
    def test_html_does_not_mask_exceptions(self):
        """
        A ClearableFileInput should not mask exceptions produced while
        checking that it has a value.
        """
        @python_2_unicode_compatible
        class FailingURLFieldFile(object):
            @property
            def url(self):
                raise RuntimeError('Canary')

            def __str__(self):
                return 'value'

        widget = ClearableFileInput()
        field = FailingURLFieldFile()
        with self.assertRaisesMessage(RuntimeError, 'Canary'):
            widget.render('myfile', field)
Example #6
0
    def test_accept(self):
        widget = ClearableFileInput()
        assert 'accept' not in widget.render(name='file', value='test.jpg')
        assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(None)

        widget = ClearableFileInput(attrs={'accept': 'image/*'})
        assert 'accept="image/*"' in widget.render(name='file', value='test.jpg')
        assert ["starts-with", "$Content-Type", "image/"] in widget.get_conditions('image/*')

        widget = ClearableFileInput(attrs={'accept': 'image/jpeg'})
        assert 'accept="image/jpeg"' in widget.render(name='file', value='test.jpg')
        assert {"Content-Type": 'image/jpeg'} in widget.get_conditions('image/jpeg')

        widget = ClearableFileInput(attrs={'accept': 'application/pdf,image/*'})
        assert 'accept="application/pdf,image/*"' in widget.render(name='file', value='test.jpg')
        assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(
            'application/pdf,image/*')
        assert {"Content-Type": 'application/pdf'} not in widget.get_conditions(
            'application/pdf,image/*')
Example #7
0
 def test_render_custom_template(self):
     widget = ClearableFileInput()
     widget.template_with_initial = (
         '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> '
         '%(clear_template)s<br />%(input_text)s: %(input)s')
     self.assertHTMLEqual(
         widget.render('myfile', FakeFieldFile()),
         'Currently: <img src="something" alt="something" /> '
         '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> '
         '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />'
     )
Example #8
0
 def test_render_custom_template(self):
     widget = ClearableFileInput()
     widget.template_with_initial = (
         '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> '
         '%(clear_template)s<br />%(input_text)s: %(input)s'
     )
     self.assertHTMLEqual(
         widget.render('myfile', FakeFieldFile()),
         'Currently: <img src="something" alt="something" /> '
         '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> '
         '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />'
     )
Example #9
0
    def test_html_escaped(self):
        """
        A ClearableFileInput should escape name, filename, and URL
        when rendering HTML (#15182).
        """
        @python_2_unicode_compatible
        class StrangeFieldFile(object):
            url = "something?chapter=1&sect=2&copy=3&lang=en"

            def __str__(self):
                return '''something<div onclick="alert('oops')">.jpg'''

        widget = ClearableFileInput()
        field = StrangeFieldFile()
        output = widget.render('my<div>file', field)
        self.assertNotIn(field.url, output)
        self.assertIn('href="something?chapter=1&amp;sect=2&amp;copy=3&amp;lang=en"', output)
        self.assertNotIn(six.text_type(field), output)
        self.assertIn('something&lt;div onclick=&quot;alert(&#39;oops&#39;)&quot;&gt;.jpg', output)
        self.assertIn('my&lt;div&gt;file', output)
        self.assertNotIn('my<div>file', output)
Example #10
0
 def render(self, name, value, attrs=None, renderer=None, **kwargs):
     output = ClearableFileInput.render(self, name, value, attrs)
     output += self.output_extra_data(value)
     return mark_safe(output)
Example #11
0
 def render(self, name, value, attrs=None):
     output = ClearableFileInput.render(self, name, value, attrs)
     output += self.output_extra_data(value)
     return mark_safe(output)