2015-08-31 10:13:42 +08:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2017-07-19 23:24:27 +08:00
|
|
|
from django.forms import ClearableFileInput, MultiWidget
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
from .base import WidgetTest
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class FakeFieldFile:
|
2015-08-31 10:13:42 +08:00
|
|
|
"""
|
2017-01-21 05:04:05 +08:00
|
|
|
Quacks like a FieldFile (has a .url and string representation), but
|
2015-08-31 10:13:42 +08:00
|
|
|
doesn't require us to care about storages etc.
|
|
|
|
"""
|
|
|
|
url = 'something'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.url
|
|
|
|
|
|
|
|
|
|
|
|
class ClearableFileInputTest(WidgetTest):
|
|
|
|
widget = ClearableFileInput()
|
|
|
|
|
|
|
|
def test_clear_input_renders(self):
|
|
|
|
"""
|
|
|
|
A ClearableFileInput with is_required False and rendered with an
|
|
|
|
initial value that is a file renders a clear checkbox.
|
|
|
|
"""
|
|
|
|
self.check_html(self.widget, 'myfile', FakeFieldFile(), html=(
|
|
|
|
"""
|
|
|
|
Currently: <a href="something">something</a>
|
2018-01-21 15:09:10 +08:00
|
|
|
<input type="checkbox" name="myfile-clear" id="myfile-clear_id">
|
|
|
|
<label for="myfile-clear_id">Clear</label><br>
|
|
|
|
Change: <input type="file" name="myfile">
|
2015-08-31 10:13:42 +08:00
|
|
|
"""
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_html_escaped(self):
|
|
|
|
"""
|
|
|
|
A ClearableFileInput should escape name, filename, and URL
|
|
|
|
when rendering HTML (#15182).
|
|
|
|
"""
|
2017-01-19 15:39:46 +08:00
|
|
|
class StrangeFieldFile:
|
2015-08-31 10:13:42 +08:00
|
|
|
url = "something?chapter=1§=2©=3&lang=en"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '''something<div onclick="alert('oops')">.jpg'''
|
|
|
|
|
2016-05-14 22:31:32 +08:00
|
|
|
self.check_html(ClearableFileInput(), 'my<div>file', StrangeFieldFile(), html=(
|
|
|
|
"""
|
|
|
|
Currently: <a href="something?chapter=1&sect=2&copy=3&lang=en">
|
2019-04-24 19:30:34 +08:00
|
|
|
something<div onclick="alert('oops')">.jpg</a>
|
2018-01-21 15:09:10 +08:00
|
|
|
<input type="checkbox" name="my<div>file-clear" id="my<div>file-clear_id">
|
|
|
|
<label for="my<div>file-clear_id">Clear</label><br>
|
|
|
|
Change: <input type="file" name="my<div>file">
|
2016-05-14 22:31:32 +08:00
|
|
|
"""
|
|
|
|
))
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
def test_clear_input_renders_only_if_not_required(self):
|
|
|
|
"""
|
|
|
|
A ClearableFileInput with is_required=False does not render a clear
|
|
|
|
checkbox.
|
|
|
|
"""
|
|
|
|
widget = ClearableFileInput()
|
|
|
|
widget.is_required = True
|
|
|
|
self.check_html(widget, 'myfile', FakeFieldFile(), html=(
|
|
|
|
"""
|
2018-01-21 15:09:10 +08:00
|
|
|
Currently: <a href="something">something</a> <br>
|
|
|
|
Change: <input type="file" name="myfile">
|
2015-08-31 10:13:42 +08:00
|
|
|
"""
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_clear_input_renders_only_if_initial(self):
|
|
|
|
"""
|
|
|
|
A ClearableFileInput instantiated with no initial value does not render
|
|
|
|
a clear checkbox.
|
|
|
|
"""
|
2018-01-21 15:09:10 +08:00
|
|
|
self.check_html(self.widget, 'myfile', None, html='<input type="file" name="myfile">')
|
2015-08-31 10:13:42 +08:00
|
|
|
|
2017-07-19 23:24:27 +08:00
|
|
|
def test_render_as_subwidget(self):
|
|
|
|
"""A ClearableFileInput as a subwidget of MultiWidget."""
|
|
|
|
widget = MultiWidget(widgets=(self.widget,))
|
|
|
|
self.check_html(widget, 'myfile', [FakeFieldFile()], html=(
|
|
|
|
"""
|
|
|
|
Currently: <a href="something">something</a>
|
2018-01-21 15:09:10 +08:00
|
|
|
<input type="checkbox" name="myfile_0-clear" id="myfile_0-clear_id">
|
|
|
|
<label for="myfile_0-clear_id">Clear</label><br>
|
|
|
|
Change: <input type="file" name="myfile_0">
|
2017-07-19 23:24:27 +08:00
|
|
|
"""
|
|
|
|
))
|
|
|
|
|
2015-08-31 10:13:42 +08:00
|
|
|
def test_clear_input_checked_returns_false(self):
|
|
|
|
"""
|
|
|
|
ClearableFileInput.value_from_datadict returns False if the clear
|
|
|
|
checkbox is checked, if not required.
|
|
|
|
"""
|
|
|
|
value = self.widget.value_from_datadict(
|
|
|
|
data={'myfile-clear': True},
|
|
|
|
files={},
|
|
|
|
name='myfile',
|
|
|
|
)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(value, False)
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
def test_clear_input_checked_returns_false_only_if_not_required(self):
|
|
|
|
"""
|
|
|
|
ClearableFileInput.value_from_datadict never returns False if the field
|
|
|
|
is required.
|
|
|
|
"""
|
|
|
|
widget = ClearableFileInput()
|
|
|
|
widget.is_required = True
|
|
|
|
field = SimpleUploadedFile('something.txt', b'content')
|
|
|
|
|
|
|
|
value = widget.value_from_datadict(
|
|
|
|
data={'myfile-clear': True},
|
|
|
|
files={'myfile': field},
|
|
|
|
name='myfile',
|
|
|
|
)
|
|
|
|
self.assertEqual(value, field)
|
2016-02-15 05:28:26 +08:00
|
|
|
|
|
|
|
def test_html_does_not_mask_exceptions(self):
|
|
|
|
"""
|
|
|
|
A ClearableFileInput should not mask exceptions produced while
|
|
|
|
checking that it has a value.
|
|
|
|
"""
|
2017-01-19 15:39:46 +08:00
|
|
|
class FailingURLFieldFile:
|
2016-02-15 05:28:26 +08:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
raise ValueError('Canary')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'value'
|
|
|
|
|
|
|
|
with self.assertRaisesMessage(ValueError, 'Canary'):
|
|
|
|
self.widget.render('myfile', FailingURLFieldFile())
|
|
|
|
|
|
|
|
def test_url_as_property(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class URLFieldFile:
|
2016-02-15 05:28:26 +08:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return 'https://www.python.org/'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'value'
|
|
|
|
|
|
|
|
html = self.widget.render('myfile', URLFieldFile())
|
|
|
|
self.assertInHTML('<a href="https://www.python.org/">value</a>', html)
|
|
|
|
|
|
|
|
def test_return_false_if_url_does_not_exists(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class NoURLFieldFile:
|
2016-02-15 05:28:26 +08:00
|
|
|
def __str__(self):
|
|
|
|
return 'value'
|
|
|
|
|
|
|
|
html = self.widget.render('myfile', NoURLFieldFile())
|
2018-01-21 15:09:10 +08:00
|
|
|
self.assertHTMLEqual(html, '<input name="myfile" type="file">')
|
2016-08-13 01:59:01 +08:00
|
|
|
|
|
|
|
def test_use_required_attribute(self):
|
|
|
|
# False when initial data exists. The file input is left blank by the
|
|
|
|
# user to keep the existing, initial value.
|
|
|
|
self.assertIs(self.widget.use_required_attribute(None), True)
|
|
|
|
self.assertIs(self.widget.use_required_attribute('resume.txt'), False)
|
2017-02-04 00:11:39 +08:00
|
|
|
|
|
|
|
def test_value_omitted_from_data(self):
|
|
|
|
widget = ClearableFileInput()
|
|
|
|
self.assertIs(widget.value_omitted_from_data({}, {}, 'field'), True)
|
|
|
|
self.assertIs(widget.value_omitted_from_data({}, {'field': 'x'}, 'field'), False)
|
|
|
|
self.assertIs(widget.value_omitted_from_data({'field-clear': 'y'}, {}, 'field'), False)
|