def test_create_pil_image(self): pil_image = ImageDummy(width=300, height=150).create_pil_image() assert_pformat_equal(pil_image.width, 300) assert_pformat_equal(pil_image.height, 150) assert_pformat_equal(pil_image.verify(), None)
def test_in_memory_image_file(self): img = ImageDummy( width=1, height=1, format='png').in_memory_image_file(filename='test.png') assert isinstance(img, io.BytesIO) assert img.name == 'test.png' assert img.tell() == 0 assert img.read().startswith(b'\x89PNG\r\n')
def test_create_info_image(self): image_dummy = ImageDummy(width=400, height=200) assert_pformat_equal(image_dummy.text_color, "#ffffff") assert_pformat_equal(image_dummy.text_align, "center") pil_image = image_dummy.create_info_image(text="foo") assert_pformat_equal(pil_image.width, 400) assert_pformat_equal(pil_image.height, 200) assert_pformat_equal(pil_image.verify(), None)
def test_create_pil_image(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # trigger all warnings pil_image = ImageDummy(width=300, height=150).create_pil_image() assert_pformat_equal(pil_image.width, 300) assert_pformat_equal(pil_image.height, 150) assert_pformat_equal(pil_image.verify(), None) assert_pformat_equal(len(w), 0) # No warnings created
def test_create_info_image(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # trigger all warnings image_dummy = ImageDummy(width=400, height=200) assert_pformat_equal(image_dummy.text_color, "#ffffff") assert_pformat_equal(image_dummy.text_align, "center") pil_image = image_dummy.create_info_image(text="foo") assert_pformat_equal(pil_image.width, 400) assert_pformat_equal(pil_image.height, 200) assert_pformat_equal(pil_image.verify(), None) assert_pformat_equal(len(w), 0) # No warnings created
def test_create_temp_filer_info_image(self): assert_endswith( settings.MEDIA_ROOT, "/django_tools_test_project/media" ) user = self.login(usertype="normal") with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # trigger all warnings filer_info_image = ImageDummy( width=500, height=300, format="png" ).create_temp_filer_info_image( text="A test image", user=user ) assert_pformat_equal(filer_info_image.width, 500) assert_pformat_equal(filer_info_image.height, 300) assert_pformat_equal(filer_info_image.size, 1791) self.assertIn(".png", filer_info_image.path) path = filer_info_image.path path = path.replace("-", "_") # e.g.: /django-tools/ -> /django_tools/ self.assertIn("/django_tools_test_project/media/filer_public/", path) assert_pformat_equal(len(w), 0) # No warnings created
def test_new_item_with_image(self): """ https://github.com/jedie/PyInventory/issues/33 """ self.client.force_login(self.normaluser) img = ImageDummy( width=1, height=1, format='png').in_memory_image_file(filename='test.png') response = self.client.post( path='/admin/inventory/memomodel/add/', data={ 'version': 0, # VersionProtectBaseModel field 'name': 'The Memo Name', 'memo': 'This is a test Memo', 'memoimagemodel_set-TOTAL_FORMS': '1', 'memoimagemodel_set-INITIAL_FORMS': '0', 'memoimagemodel_set-MIN_NUM_FORMS': '0', 'memoimagemodel_set-MAX_NUM_FORMS': '1000', 'memoimagemodel_set-0-position': '0', 'memoimagemodel_set-__prefix__-position': '0', 'memoimagemodel_set-0-image': img, 'memofilemodel_set-TOTAL_FORMS': '0', 'memofilemodel_set-INITIAL_FORMS': '0', 'memofilemodel_set-MIN_NUM_FORMS': '0', 'memofilemodel_set-MAX_NUM_FORMS': '1000', 'memofilemodel_set-__prefix__-position': '0', 'memolinkmodel_set-TOTAL_FORMS': '0', 'memolinkmodel_set-INITIAL_FORMS': '0', 'memolinkmodel_set-MIN_NUM_FORMS': '0', 'memolinkmodel_set-MAX_NUM_FORMS': '1000', 'memolinkmodel_set-__prefix__-position': '0', '_save': 'Save', }, ) assert response.status_code == 302, response.content.decode( 'utf-8') # Form error? memo = MemoModel.objects.first() or MemoModel() self.assert_messages( response, expected_messages=[ f'The Memo “<a href="/admin/inventory/memomodel/{memo.pk}/change/">The Memo Name</a>”' ' was added successfully.' ]) self.assertRedirects(response, expected_url='/admin/inventory/memomodel/') data = list(MemoModel.objects.values_list('name', 'memo')) assert data == [('The Memo Name', 'This is a test Memo')] assert memo.user_id == self.normaluser.pk assert MemoImageModel.objects.count() == 1 image = MemoImageModel.objects.first() assert image.name == 'test.png' assert image.memo == memo assert image.user_id == self.normaluser.pk
def test_create_django_file_info_image(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # trigger all warnings django_file = ImageDummy(width=100, height=50).create_django_file_info_image(text="foo bar") self.assertIsInstance(django_file, DjangoFile) assert_pformat_equal(len(w), 0) # No warnings created
def test_export(self): user = self.login(usertype='superuser') instance = ImageModel() dummy = ImageDummy(width=100, height=50).create_temp_filer_info_image(text="foobar", user=user) instance.image = dummy instance.save() with StdoutStderrBuffer() as buff: call_command("export_filer_images", self.temp_path) output = buff.get_output() print(output) self.assertIn("1 items - django_cms_tools_test_project.test_cms_plugin.ImageModel", output) self.assertIn("All filer files saved:", output) self.assertIn("1 filer files saved", output)
def test_create_temp_filer_info_image(self): assert_endswith(settings.MEDIA_ROOT, "/django_tools_test_project/media") user = self.login(usertype="normal") filer_info_image = ImageDummy( width=500, height=300, format="png").create_temp_filer_info_image(text="A test image", user=user) assert_pformat_equal(filer_info_image.width, 500) assert_pformat_equal(filer_info_image.height, 300) assert_pformat_equal(filer_info_image.size, 1791) self.assertIn(".png", filer_info_image.path) path = filer_info_image.path path = path.replace("-", "_") # e.g.: /django-tools/ -> /django_tools/ self.assertIn("/django_tools_test_project/media/filer_public/", path)
def test_new_item_with_image(self): """ https://github.com/jedie/PyInventory/issues/33 """ self.client.force_login(self.normaluser) img = ImageDummy( width=1, height=1, format='png').in_memory_image_file(filename='test.png') post_data = dict(ITEM_FORM_DEFAULTS) post_data.update({ 'itemimagemodel_set-TOTAL_FORMS': '1', 'itemimagemodel_set-0-position': '0', 'itemimagemodel_set-0-image': img, }) response = self.client.post( path='/admin/inventory/itemmodel/add/', data=post_data, ) self.assertRedirects(response, expected_url='/admin/inventory/itemmodel/') data = list(ItemModel.objects.values_list('kind__name', 'name')) assert data == [('kind', 'name')] item = ItemModel.objects.first() self.assert_messages( response, expected_messages=[ f'The Item “<a href="/admin/inventory/itemmodel/{item.pk}/change/"> - name</a>”' ' was added successfully.' ]) assert item.user_id == self.normaluser.pk assert ItemImageModel.objects.count() == 1 image = ItemImageModel.objects.first() assert image.name == 'test.png' assert image.item == item assert image.user_id == self.normaluser.pk
def test_create_django_file_info_image(self): django_file = ImageDummy( width=100, height=50).create_django_file_info_image(text="foo bar") self.assertIsInstance(django_file, DjangoFile)
def test_via_model(self): with mock.patch('secrets.token_urlsafe', return_value='ABCDEFGHIJKLMNOPQRSTUVWXYZ'): user = baker.make(User, username='******') other_user = baker.make(User, username='******') with tempfile.TemporaryDirectory() as temp: with override_settings(MEDIA_ROOT=temp): with mock.patch('secrets.token_urlsafe', return_value='12345678901234567890'): instance = UserMediaFiles.objects.create( user=user, file=File(io.BytesIO('Test äöüß !'.encode()), name='filename.ext'), image=ImageDummy( width=1, height=1).create_django_file_info_image(text=None), ) token_instance = UserMediaTokenModel.objects.get(user=user) assert repr(token_instance) == ( f"<UserMediaTokenModel: user:{user.pk} token:'abcdefghijkl' ({token_instance.pk})>" ) user_token = UserMediaTokenModel.objects.get_user_token(user) assert user_token == 'abcdefghijkl' other_user_token = UserMediaTokenModel.objects.get_user_token( other_user) assert other_user_token != user_token urls = ( '/media/abcdefghijkl/12345678901234567890/filename.ext', '/media/abcdefghijkl/12345678901234567890/dummy.jpeg') assert instance.file.url == urls[0] assert instance.image.url == urls[1] # Anonymous has no access: for url in urls: response = self.client.get(url) assert response.status_code == 403 # Can't access with wrong user: self.client = Client() self.client.force_login(other_user) for url in urls: response = self.client.get(url) assert response.status_code == 403 # Can access with the right user: self.client = Client() self.client.force_login(user) response = self.client.get( '/media/abcdefghijkl/12345678901234567890/filename.ext') assert response.status_code == 200 assert isinstance(response, FileResponse) assert response.getvalue().decode('UTF-8') == 'Test äöüß !' response = self.client.get( '/media/abcdefghijkl/12345678901234567890/dummy.jpeg') assert response.status_code == 200 assert isinstance(response, FileResponse) assert len(response.getvalue()) # Test whats happen, if token was deleted UserMediaTokenModel.objects.all().delete() for url in urls: response = self.client.get(url) assert response.status_code == 400 # SuspiciousOperation -> HttpResponseBadRequest