11import os
22import shutil
33import tempfile
4+ import uuid
45from datetime import datetime , timedelta
56from unittest import mock
67
@@ -531,19 +532,28 @@ def setUp(self):
531532 def test_preview_modified (self , update_mock ):
532533 addon = Addon .objects .get (pk = 3615 )
533534 name = 'transparent.png'
535+ upload_hash = uuid .uuid4 ().hex
534536 form = forms .PreviewForm (
535- {'caption' : 'test' , 'upload_hash' : name , 'position' : 1 }
537+ {'caption' : 'test' , 'upload_hash' : upload_hash , 'position' : 1 }
536538 )
537- with storage .open (os .path .join (self .dest , name ), 'wb' ) as f :
539+ with storage .open (os .path .join (self .dest , upload_hash ), 'wb' ) as f :
538540 shutil .copyfileobj (open (get_image_path (name ), 'rb' ), f )
539541 assert form .is_valid ()
540542 form .save (addon )
541543 assert update_mock .called
542544
545+ def test_preview_invalid_hash (self ):
546+ form = forms .PreviewForm (
547+ {'caption' : 'test' , 'upload_hash' : 'something.png' , 'position' : 1 }
548+ )
549+ assert not form .is_valid ()
550+ assert form .errors == {
551+ 'upload_hash' : ['There was an error uploading your image.' ]
552+ }
553+
543554 def test_caption_too_long (self ):
544- name = 'transparent.png'
545555 form = forms .PreviewForm (
546- {'caption' : 'û' * 281 , 'upload_hash' : name , 'position' : 1 }
556+ {'caption' : 'û' * 281 , 'upload_hash' : uuid . uuid4 (). hex , 'position' : 1 }
547557 )
548558 assert form .fields ['caption' ].max_length == 280
549559 assert form .fields ['caption' ].widget .attrs ['maxlength' ] == '280'
@@ -555,11 +565,11 @@ def test_caption_too_long(self):
555565 def test_preview_transparency (self ):
556566 addon = Addon .objects .get (pk = 3615 )
557567 name = 'transparent-cotton'
558- hash = '12345678abcd'
568+ upload_hash = uuid . uuid4 (). hex
559569 form = forms .PreviewForm (
560- {'caption' : 'test' , 'upload_hash' : hash , 'position' : 1 }
570+ {'caption' : 'test' , 'upload_hash' : upload_hash , 'position' : 1 }
561571 )
562- with storage .open (os .path .join (self .dest , hash ), 'wb' ) as f :
572+ with storage .open (os .path .join (self .dest , upload_hash ), 'wb' ) as f :
563573 shutil .copyfileobj (open (get_image_path (name + '.png' ), 'rb' ), f )
564574 assert form .is_valid ()
565575 form .save (addon )
@@ -575,10 +585,11 @@ def test_preview_transparency(self):
575585 def test_preview_size (self , pngcrush_image_mock ):
576586 addon = Addon .objects .get (pk = 3615 )
577587 name = 'teamaddons.jpg'
588+ upload_hash = uuid .uuid4 ().hex
578589 form = forms .PreviewForm (
579- {'caption' : 'test' , 'upload_hash' : name , 'position' : 1 }
590+ {'caption' : 'test' , 'upload_hash' : upload_hash , 'position' : 1 }
580591 )
581- with storage .open (os .path .join (self .dest , name ), 'wb' ) as f :
592+ with storage .open (os .path .join (self .dest , upload_hash ), 'wb' ) as f :
582593 shutil .copyfileobj (open (get_image_path (name ), 'rb' ), f )
583594 assert form .is_valid ()
584595 form .save (addon )
@@ -1236,17 +1247,30 @@ def test_default_icons(self):
12361247 @mock .patch ('olympia.amo.models.ModelBase.update' )
12371248 def test_icon_modified (self , update_mock ):
12381249 name = 'transparent.png'
1250+ icon_upload_hash = uuid .uuid4 ().hex
12391251 form = forms .AddonFormMedia (
1240- {'icon_upload_hash' : name }, request = self .request , instance = self .addon
1252+ {'icon_upload_hash' : icon_upload_hash },
1253+ request = self .request ,
1254+ instance = self .addon ,
12411255 )
1242-
1243- dest = os .path .join (self .icon_path , name )
1256+ dest = os .path .join (self .icon_path , icon_upload_hash )
12441257 with storage .open (dest , 'wb' ) as f :
12451258 shutil .copyfileobj (open (get_image_path (name ), 'rb' ), f )
12461259 assert form .is_valid ()
12471260 form .save (addon = self .addon )
12481261 assert update_mock .called
12491262
1263+ def test_icon_invalid_hash (self ):
1264+ form = forms .AddonFormMedia (
1265+ {'icon_upload_hash' : 'some-icon.png' },
1266+ request = self .request ,
1267+ instance = self .addon ,
1268+ )
1269+ assert not form .is_valid ()
1270+ assert form .errors == {
1271+ 'icon_upload_hash' : ['There was an error uploading your image.' ]
1272+ }
1273+
12501274
12511275class TestCategoryForm (TestCase ):
12521276 def test_only_one_possible_category_for_dicts (self ):
0 commit comments