22
33import pytest
44from django .core .files .storage import default_storage
5+ from django .forms import ClearableFileInput
56from selenium .common .exceptions import NoSuchElementException
67from selenium .webdriver .support .expected_conditions import staleness_of
78from selenium .webdriver .support .wait import WebDriverWait
@@ -25,11 +26,16 @@ def wait_for_page_load(driver, timeout=30):
2526 )
2627
2728
28- class TestS3FileInput ( object ) :
29+ class TestS3FileInput :
2930 @property
3031 def url (self ):
3132 return reverse ('upload' )
3233
34+ @pytest .fixture (autouse = True )
35+ def patch (self ):
36+ ClearableFileInput .__new__ = \
37+ lambda cls , * args , ** kwargs : object .__new__ (S3FileInput )
38+
3339 @pytest .fixture
3440 def freeze (self , monkeypatch ):
3541 """Freeze datetime and UUID."""
@@ -86,7 +92,7 @@ def test_build_attr(self):
8692 assert S3FileInput ().build_attrs ({'class' : 'my-class' })['class' ] == 'my-class s3file'
8793
8894 def test_get_conditions (self , freeze ):
89- conditions = S3FileInput ().get_conditions ()
95+ conditions = S3FileInput ().get_conditions (None )
9096 assert all (condition in conditions for condition in [
9197 {"bucket" : 'test-bucket' },
9298 {"success_action_status" : "201" },
@@ -96,19 +102,16 @@ def test_get_conditions(self, freeze):
96102
97103 def test_accept (self ):
98104 widget = S3FileInput ()
99- assert widget .mime_type is None
100105 assert 'accept' not in widget .render (name = 'file' , value = 'test.jpg' )
101- assert ["starts-with" , "$Content-Type" , "" ] in widget .get_conditions ()
106+ assert ["starts-with" , "$Content-Type" , "" ] in widget .get_conditions (None )
102107
103108 widget = S3FileInput (attrs = {'accept' : 'image/*' })
104- assert widget .mime_type == 'image/*'
105109 assert 'accept="image/*"' in widget .render (name = 'file' , value = 'test.jpg' )
106- assert ["starts-with" , "$Content-Type" , "image/" ] in widget .get_conditions ()
110+ assert ["starts-with" , "$Content-Type" , "image/" ] in widget .get_conditions ('image/*' )
107111
108112 widget = S3FileInput (attrs = {'accept' : 'image/jpeg' })
109- assert widget .mime_type == 'image/jpeg'
110113 assert 'accept="image/jpeg"' in widget .render (name = 'file' , value = 'test.jpg' )
111- assert {"Content-Type" : 'image/jpeg' } in widget .get_conditions ()
114+ assert {"Content-Type" : 'image/jpeg' } in widget .get_conditions ('image/jpeg' )
112115
113116 def test_no_js_error (self , driver , live_server ):
114117 driver .get (live_server + self .url )
@@ -129,3 +132,6 @@ def test_file_insert(self, request, driver, live_server, upload_file, freeze):
129132 with pytest .raises (NoSuchElementException ):
130133 error = driver .find_element_by_xpath ('//body[@JSError]' )
131134 pytest .fail (error .get_attribute ('JSError' ))
135+
136+ def test_media (self ):
137+ assert ClearableFileInput ().media ._js == ['s3file/js/s3file.js' ]
0 commit comments