11import factory
2- from django .conf import settings
3- from django .core .files import File
42
53from kitsune .questions .tests import QuestionFactory
64from kitsune .sumo .tests import TestCase
75from kitsune .upload .models import ImageAttachment
86from kitsune .upload .storage import RenameFileStorage
9- from kitsune .upload .utils import FileTooLargeError , check_file_size , create_imageattachment
107from kitsune .users .tests import UserFactory
118
129
@@ -32,103 +29,3 @@ def check_file_info(file_info, name, width, height, delete_url, url, thumbnail_u
3229def get_file_name (name ):
3330 storage = RenameFileStorage ()
3431 return storage .get_available_name (name )
35-
36-
37- class CheckFileSizeTestCase (TestCase ):
38- """Tests for check_file_size"""
39-
40- def test_check_file_size_under (self ):
41- """No exception should be raised"""
42- with open ("kitsune/upload/tests/media/test.jpg" , "rb" ) as f :
43- up_file = File (f )
44- check_file_size (up_file , settings .IMAGE_MAX_FILESIZE )
45-
46- def test_check_file_size_over (self ):
47- """FileTooLargeError should be raised"""
48- with self .assertRaises (FileTooLargeError ):
49- with open ("kitsune/upload/tests/media/test.jpg" , "rb" ) as f :
50- up_file = File (f )
51- # This should raise
52- check_file_size (up_file , 0 )
53-
54-
55- class CreateImageAttachmentTestCase (TestCase ):
56- def setUp (self ):
57- super ().setUp ()
58- self .user = UserFactory ()
59- self .obj = QuestionFactory ()
60-
61- def tearDown (self ):
62- ImageAttachment .objects .all ().delete ()
63- super ().tearDown ()
64-
65- def test_create_imageattachment (self ):
66- """
67- An image attachment is created from an uploaded file.
68-
69- Verifies all appropriate fields are correctly set.
70- """
71- with open ("kitsune/upload/tests/media/test.jpg" , "rb" ) as f :
72- up_file = File (f )
73- file_info = create_imageattachment ({"image" : up_file }, self .user , self .obj )
74-
75- image = ImageAttachment .objects .all ()[0 ]
76- check_file_info (
77- file_info ,
78- name = "test.png" ,
79- width = 90 ,
80- height = 120 ,
81- delete_url = image .get_delete_url (),
82- url = image .get_absolute_url (),
83- thumbnail_url = image .thumbnail .url ,
84- )
85-
86- def test_create_imageattachment_when_animated (self ):
87- """
88- An image attachment is created from an uploaded animated GIF file.
89-
90- Verifies all appropriate fields are correctly set.
91- """
92- filepath = "kitsune/upload/tests/media/animated.gif"
93- with open (filepath , "rb" ) as f :
94- up_file = File (f )
95- file_info = create_imageattachment ({"image" : up_file }, self .user , self .obj )
96-
97- image = ImageAttachment .objects .all ()[0 ]
98- check_file_info (
99- file_info ,
100- name = filepath ,
101- width = 120 ,
102- height = 120 ,
103- delete_url = image .get_delete_url (),
104- url = image .get_absolute_url (),
105- thumbnail_url = image .thumbnail .url ,
106- )
107-
108-
109- class FileNameTestCase (TestCase ):
110- def _match_file_name (self , name , name_end ):
111- assert name .endswith (name_end ), '"{}" does not end with "{}"' .format (name , name_end )
112-
113- def test_empty_file_name (self ):
114- self ._match_file_name ("" , "" )
115-
116- def test_empty_file_name_with_extension (self ):
117- self ._match_file_name (get_file_name (".wtf" ), "3f8242" )
118-
119- def test_ascii (self ):
120- self ._match_file_name (get_file_name ("some ascii.jpg" ), "5959e0.jpg" )
121-
122- def test_ascii_dir (self ):
123- self ._match_file_name (get_file_name ("dir1/dir2/some ascii.jpg" ), "5959e0.jpg" )
124-
125- def test_low_unicode (self ):
126- self ._match_file_name (get_file_name ("157d9383e6aeba7180378fd8c1d46f80.gif" ), "bdaf1a.gif" )
127-
128- def test_high_unicode (self ):
129- self ._match_file_name (get_file_name ("\u6709 \u52b9 .jpeg" ), "ce1518.jpeg" )
130-
131- def test_full_mixed (self ):
132- self ._match_file_name (
133- get_file_name ("123\xe5 \xe5 \xee \xe9 \xf8 \xe7 \u6709 \u52b9 .png" ), "686c11.png"
134- )
0 commit comments