def test_upload_image_variants(self): f1 = get_img_file(create_test_image(1000, 1000)) f2 = get_img_file(create_test_image(1000, 1000)) for r in (self.client.post(self.upload_url, { 'images[]': [f1], 'profile': 'simple1' }), self.client.post(self.upload_url, { 'images[]': [f2], 'profile': 'simple1', 'label': 'lab' })): self.assertEqual(r.status_code, 200) d = self.get_json(r) variants = d['uploaded'][0]['variants'] self.assertEqual(len(variants), 4) name = os.path.splitext(d['uploaded'][0]['img_id'])[0].split( ':', 1)[1] for var_data in variants.values(): self.assertIsInstance(var_data['url'], basestring) self.assertIsInstance(var_data['rel_url'], basestring) self.assertIn(name, var_data['url']) self.assertIn(name, var_data['rel_url']) self.assertTrue(variants['20x30']['url'].endswith('_20x30.jpeg')) self.assertTrue(variants['w20']['url'].endswith('_w20.jpeg')) self.assertTrue(variants['h30']['url'].endswith('_h30.jpeg')) self.assertTrue(variants['lab1']['url'].endswith('_lab1.jpeg')) self.assertUploadedFilesExist(d)
def test_get_files_by_img_id_removed_variants_ext(self): r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: # for i in xrange(len(item['variants'])): for var_data in item['variants'].values(): # remove ext for all variants files rel_url = var_data['rel_url'] rel_url_new = os.path.splitext(rel_url)[0] os.rename(media_path(rel_url), media_path(rel_url_new)) var_data['rel_url'] = rel_url_new r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_make_permalink(self): r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: new_img_id = make_permalink(item['img_id']) files = get_files_by_img_id(new_img_id) self.assertEqual(files['main'], remove_tmp_prefix_from_file_path(item['rel_url'])) new_item = { 'rel_url': files['main'], 'variants': {}, } for var_label, var_data in item['variants'].iteritems(): self.assertEqual( files['variants'][var_label], remove_tmp_prefix_from_file_path(var_data['rel_url']) ) new_item['variants'][var_label] = {'rel_url': files['variants'][var_label]} self.assertUploadedFilesExist({'uploaded': [new_item]})
def test_remove_all_files_of_img_id(self): r1 = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(800, 800)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r1.status_code, 200) d1 = self.get_json(r1) self.assertEqual(len(d1['errors']), 0) self.assertUploadedFilesExist(d1) r2 = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r2.status_code, 200) d2 = self.get_json(r2) self.assertEqual(len(d2['errors']), 0) self.assertUploadedFilesExist(d2) remove_all_files_of_img_id(d1['uploaded'][0]['img_id']) self.assertUploadedFilesExist(d2) self.assertUploadedFilesNotExist(d1) remove_all_files_of_img_id(d2['uploaded'][0]['img_id']) self.assertUploadedFilesNotExist(d2)
def test_get_files_by_img_id_removed_variants_ext(self): r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: # for i in xrange(len(item['variants'])): for var_data in item['variants'].values(): # remove ext for all variants files rel_url = var_data['rel_url'] rel_url_new = os.path.splitext(rel_url)[0] os.rename(media_path(rel_url), media_path(rel_url_new)) var_data['rel_url'] = rel_url_new r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_remove_all_files_of_img_id(self): r1 = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(800, 800)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r1.status_code, 200) d1 = self.get_json(r1) self.assertEqual(len(d1['errors']), 0) self.assertUploadedFilesExist(d1) r2 = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r2.status_code, 200) d2 = self.get_json(r2) self.assertEqual(len(d2['errors']), 0) self.assertUploadedFilesExist(d2) remove_all_files_of_img_id(d1['uploaded'][0]['img_id']) self.assertUploadedFilesExist(d2) self.assertUploadedFilesNotExist(d1) remove_all_files_of_img_id(d2['uploaded'][0]['img_id']) self.assertUploadedFilesNotExist(d2)
def tests(): img_200x200_cmyk = create_test_image(200, 200, c="CMYK") f_200x200_jpeg_cmyk = get_img_file(img_200x200_cmyk) t = adjust_image(f_200x200_jpeg_cmyk, return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, "RGB") img_200x200_p = create_test_image(200, 200, c="P") f_200x200_png_p = get_img_file(img_200x200_p, img_format="PNG") t = adjust_image(f_200x200_png_p, new_format="JPEG", return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, "RGB")
def test_upload_image_multiple_files(self): r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1' }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d)
def tests(): img_200x200_cmyk = create_test_image(200, 200, c='CMYK') f_200x200_jpeg_cmyk = get_img_file(img_200x200_cmyk) t = adjust_image(f_200x200_jpeg_cmyk, return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, 'RGB') img_200x200_p = create_test_image(200, 200, c='P') f_200x200_png_p = get_img_file(img_200x200_p, img_format='PNG') t = adjust_image(f_200x200_png_p, new_format='JPEG', return_new_image=True) self.assertIsInstance(t, (cStringIO.InputType, cStringIO.OutputType)) self.assertEqual(Image.open(t).mode, 'RGB')
def test_upload_image_without_profile(self): f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, {'images[]': [f]}) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 1) self.assertEqual(len(d['errors']), 0) item = d['uploaded'][0] self.assertIn('url', item) self.assertIn('rel_url', item) self.assertIn('img_id', item) self.assertIn('variants', item) self.assertIsInstance(item['url'], basestring) self.assertIsInstance(item['rel_url'], basestring) self.assertIsInstance(item['img_id'], basestring) self.assertIsInstance(item['variants'], dict) self.assertEqual(len(item['variants']), 0) self.assertTrue(item['img_id'].startswith('default:' + dju_settings.DJU_IMG_UPLOAD_TMP_PREFIX)) self.assertTrue(item['img_id'].endswith('.jpeg')) self.assertEqual(get_relative_path_from_img_id(item['img_id']), item['rel_url']) self.assertEqual(settings.MEDIA_URL + item['rel_url'], item['url']) self.assertUploadedFilesExist(d) f.seek(0) r = self.client.post(self.upload_url, {'images[]': [f], 'label': 'lab'}) self.assertEqual(r.status_code, 200) d = self.get_json(r) item = d['uploaded'][0] self.assertTrue(item['img_id'].endswith('_lab.jpeg')) self.assertUploadedFilesExist(d)
def test_upload_image_without_profile(self): f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, {'images[]': [f]}) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 1) self.assertEqual(len(d['errors']), 0) item = d['uploaded'][0] self.assertIn('url', item) self.assertIn('rel_url', item) self.assertIn('img_id', item) self.assertIn('variants', item) self.assertIsInstance(item['url'], basestring) self.assertIsInstance(item['rel_url'], basestring) self.assertIsInstance(item['img_id'], basestring) self.assertIsInstance(item['variants'], dict) self.assertEqual(len(item['variants']), 0) self.assertTrue( item['img_id'].startswith('default:' + dju_settings.DJU_IMG_UPLOAD_TMP_PREFIX)) self.assertTrue(item['img_id'].endswith('.jpeg')) self.assertEqual(get_relative_path_from_img_id(item['img_id']), item['rel_url']) self.assertEqual(settings.MEDIA_URL + item['rel_url'], item['url']) self.assertUploadedFilesExist(d) f.seek(0) r = self.client.post(self.upload_url, { 'images[]': [f], 'label': 'lab' }) self.assertEqual(r.status_code, 200) d = self.get_json(r) item = d['uploaded'][0] self.assertTrue(item['img_id'].endswith('_lab.jpeg')) self.assertUploadedFilesExist(d)
def test_get_files_by_img_id_with_invalid_hash_and_filename_pattern(self): r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: for var_data in item['variants'].values(): # add file with invalid hash rel_url = var_data['rel_url'] rel_url_new = re.sub( r'({suf})[a-z0-9]{{hs}}(_.+)'.replace( '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX ).replace( '{hs}', str(HASH_SIZE) ), r'\1{h}\2'.replace('{h}', 'z' * HASH_SIZE), rel_url ) shutil.copy(media_path(rel_url), media_path(rel_url_new)) # add file with invalid filename pattern rel_url = var_data['rel_url'] rel_url_new = re.sub( r'({suf})[a-z0-9]{{hs}}(_.+)'.replace( '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX ).replace( '{hs}', str(HASH_SIZE) ), r'\1{h}\2'.replace('{h}', 'z' * (HASH_SIZE + 1)), rel_url ) shutil.copy(media_path(rel_url), media_path(rel_url_new)) r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_upload_image_wrong_profile(self): f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, {'images[]': [f], 'profile': 'wrong'}) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 0) self.assertEqual(len(d['errors']), 1) self.assertEqual(d['errors'][0], unicode(TOOLS_ERRORS['unknown_profile']) % {'profile': 'wrong'})
def test_get_files_by_img_id_with_invalid_hash_and_filename_pattern(self): r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: for var_data in item['variants'].values(): # add file with invalid hash rel_url = var_data['rel_url'] rel_url_new = re.sub( r'({suf})[a-z0-9]{{hs}}(_.+)'.replace( '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX).replace( '{hs}', str(HASH_SIZE)), r'\1{h}\2'.replace('{h}', 'z' * HASH_SIZE), rel_url) shutil.copy(media_path(rel_url), media_path(rel_url_new)) # add file with invalid filename pattern rel_url = var_data['rel_url'] rel_url_new = re.sub( r'({suf})[a-z0-9]{{hs}}(_.+)'.replace( '{suf}', dju_settings.DJU_IMG_UPLOAD_VARIANT_SUFFIX).replace( '{hs}', str(HASH_SIZE)), r'\1{h}\2'.replace('{h}', 'z' * (HASH_SIZE + 1)), rel_url) shutil.copy(media_path(rel_url), media_path(rel_url_new)) r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_upload_image_multiple_files(self): r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1' }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d)
def test_is_img_id_exists(self): self.assertFalse(is_img_id_exists('default:abcde1234_ab12_myname.jpeg')) r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: self.assertTrue(item['img_id'])
def test_upload_image_variants(self): f1 = get_img_file(create_test_image(1000, 1000)) f2 = get_img_file(create_test_image(1000, 1000)) for r in (self.client.post(self.upload_url, {'images[]': [f1], 'profile': 'simple1'}), self.client.post(self.upload_url, {'images[]': [f2], 'profile': 'simple1', 'label': 'lab'})): self.assertEqual(r.status_code, 200) d = self.get_json(r) variants = d['uploaded'][0]['variants'] self.assertEqual(len(variants), 4) name = os.path.splitext(d['uploaded'][0]['img_id'])[0].split(':', 1)[1] for var_data in variants.values(): self.assertIsInstance(var_data['url'], basestring) self.assertIsInstance(var_data['rel_url'], basestring) self.assertIn(name, var_data['url']) self.assertIn(name, var_data['rel_url']) self.assertTrue(variants['20x30']['url'].endswith('_20x30.jpeg')) self.assertTrue(variants['w20']['url'].endswith('_w20.jpeg')) self.assertTrue(variants['h30']['url'].endswith('_h30.jpeg')) self.assertTrue(variants['lab1']['url'].endswith('_lab1.jpeg')) self.assertUploadedFilesExist(d)
def test_get_files_by_img_id(self): r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url']) r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1', 'label': 'world1', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_save_file(self): f = get_img_file(create_test_image(1000, 1000)) img_id = generate_img_id('simple1', ext=image_get_format(f), label='test-save-file') relative_path = get_relative_path_from_img_id(img_id) full_path = media_path(relative_path) save_file(f, full_path) file_exists = os.path.exists(full_path) self.assertTrue(file_exists) if file_exists: f.seek(0) h1 = hashlib.md5(f.read()).hexdigest() h2 = hashlib.md5(open(full_path, 'rb').read()).hexdigest() self.assertEqual(h1, h2)
def test_upload_image_wrong_profile(self): f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, { 'images[]': [f], 'profile': 'wrong' }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 0) self.assertEqual(len(d['errors']), 1) self.assertEqual( d['errors'][0], unicode(TOOLS_ERRORS['unknown_profile']) % {'profile': 'wrong'})
def test_is_img_id_exists(self): self.assertFalse( is_img_id_exists('default:abcde1234_ab12_myname.jpeg')) r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: self.assertTrue(item['img_id'])
def test_make_permalink(self): r = self.client.post( self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple0', 'label': 'world0', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: new_img_id = make_permalink(item['img_id']) files = get_files_by_img_id(new_img_id) self.assertEqual(files['main'], remove_tmp_prefix_from_file_path(item['rel_url'])) new_item = { 'rel_url': files['main'], 'variants': {}, } for var_label, var_data in item['variants'].iteritems(): self.assertEqual( files['variants'][var_label], remove_tmp_prefix_from_file_path(var_data['rel_url'])) new_item['variants'][var_label] = { 'rel_url': files['variants'][var_label] } self.assertUploadedFilesExist({'uploaded': [new_item]})
def test_upload_image_wrong_image_format(self): with safe_change_dju_settings(): dju_settings.DJU_IMG_UPLOAD_PROFILE_DEFAULT['TYPES'] = ('PNG',) clear_profile_configs_cache() f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, {'images[]': [f]}) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 0) self.assertEqual(len(d['errors']), 1) self.assertEqual( d['errors'][0], unicode(VIEWS_ERRORS['wrong_file_format']) % { 'name': 'images[]', 'formats': ', '.join(map(lambda t: t.upper(), dju_settings.DJU_IMG_UPLOAD_PROFILE_DEFAULT['TYPES'])) } )
def test_get_files_by_img_id(self): r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url']) r = self.client.post(self.upload_url, { 'images[]': [ get_img_file(create_test_image(1000, 1000)), get_img_file(create_test_image(900, 900)), get_img_file(create_test_image(800, 800)), get_img_file(create_test_image(700, 700)), ], 'profile': 'simple1', 'label': 'world1', }) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), dju_settings.DJU_IMG_UPLOAD_MAX_FILES) self.assertEqual(len(d['errors']), 0) self.assertUploadedFilesExist(d) for item in d['uploaded']: r = get_files_by_img_id(item['img_id']) self.assertEqual(r['main'], item['rel_url']) self.assertEqual(len(item['variants']), len(r['variants'])) for var_label, var_data in item['variants'].iteritems(): self.assertEqual(r['variants'][var_label], var_data['rel_url'])
def test_upload_image_wrong_image_format(self): with safe_change_dju_settings(): dju_settings.DJU_IMG_UPLOAD_PROFILE_DEFAULT['TYPES'] = ('PNG', ) clear_profile_configs_cache() f = get_img_file(create_test_image(1000, 1000)) r = self.client.post(self.upload_url, {'images[]': [f]}) self.assertEqual(r.status_code, 200) d = self.get_json(r) self.assertEqual(len(d['uploaded']), 0) self.assertEqual(len(d['errors']), 1) self.assertEqual( d['errors'][0], unicode(VIEWS_ERRORS['wrong_file_format']) % { 'name': 'images[]', 'formats': ', '.join( map( lambda t: t.upper(), dju_settings. DJU_IMG_UPLOAD_PROFILE_DEFAULT['TYPES'])) })
def setUp(self): self.img_200x200 = create_test_image(200, 200) self.img_300x300 = create_test_image(300, 300) self.img_400x200 = create_test_image(400, 200) self.img_200x400 = create_test_image(200, 400)
def setUp(self): self.img_jpeg = get_img_file(create_test_image(100, 100)) self.img_png = get_img_file(create_test_image(100, 100), img_format='PNG') self.img_gif = get_img_file(create_test_image(100, 100), img_format='GIF')
def test_upload_from_fs(self): fn = save_img_file('t1.jpeg', create_test_image(600, 600)) try: img_id = upload_from_fs(fn) except (ValueError, RuntimeError), e: raise self.failureException(e)
def setUp(self): self.img_jpeg = get_img_file(create_test_image(100, 100)) self.img_png = get_img_file(create_test_image(100, 100), img_format="PNG") self.img_gif = get_img_file(create_test_image(100, 100), img_format="GIF")
def test_upload_from_fileobject(self): f = get_img_file(create_test_image(600, 600)) try: img_id = upload_from_fileobject(f) except (ValueError, RuntimeError), e: raise self.failureException(e)
def setUp(self): super(OptimizePNGFile, self).setUp() self._clean_tmp_dir() self.png1 = create_test_image(200, 300) self.png1_fn = save_img_file("test1.png", self.png1, img_format="PNG") self.png1_f = get_img_file(self.png1, img_format="PNG")
self.assertUploadedFilesNotExist(d2) def test_upload_from_fs(self): fn = save_img_file('t1.jpeg', create_test_image(600, 600)) try: img_id = upload_from_fs(fn) except (ValueError, RuntimeError), e: raise self.failureException(e) else: self.assertTrue(is_img_id_valid(img_id)) self.assertTrue(is_img_id_exists(img_id)) with self.assertRaises(ValueError): upload_from_fs('none') fn = save_img_file('t2.jpeg', create_test_image(500, 500)) with self.assertRaises(RuntimeError): upload_from_fs(fn, profile='simple2') fn = save_img_file('t3.jpeg', create_test_image(400, 400)) try: img_id = upload_from_fs(fn, profile='simple1', label='ttt') except (ValueError, RuntimeError), e: raise self.failureException(e) else: self.assertTrue(is_img_id_valid(img_id)) self.assertTrue(is_img_id_exists(img_id)) self.assertTrue(get_files_by_img_id(img_id)['variants']) def test_img_id_has_tmp_prefix(self): img_id = generate_img_id('none', ext='png', label='zz', tmp=False)
def setUp(self): super(OptimizePNGFile, self).setUp() self._clean_tmp_dir() self.png1 = create_test_image(200, 300) self.png1_fn = save_img_file('test1.png', self.png1, img_format='PNG') self.png1_f = get_img_file(self.png1, img_format='PNG')
self.assertUploadedFilesNotExist(d2) def test_upload_from_fs(self): fn = save_img_file('t1.jpeg', create_test_image(600, 600)) try: img_id = upload_from_fs(fn) except (ValueError, RuntimeError), e: raise self.failureException(e) else: self.assertTrue(is_img_id_valid(img_id)) self.assertTrue(is_img_id_exists(img_id)) with self.assertRaises(ValueError): upload_from_fs('none') fn = save_img_file('t2.jpeg', create_test_image(500, 500)) with self.assertRaises(RuntimeError): upload_from_fs(fn, profile='simple2') fn = save_img_file('t3.jpeg', create_test_image(400, 400)) try: img_id = upload_from_fs(fn, profile='simple1', label='ttt') except (ValueError, RuntimeError), e: raise self.failureException(e) else: self.assertTrue(is_img_id_valid(img_id)) self.assertTrue(is_img_id_exists(img_id)) self.assertTrue(get_files_by_img_id(img_id)['variants']) def test_upload_from_fileobject(self): f = get_img_file(create_test_image(600, 600))