def test_upload_file(self, app, client): from app.helpers import upload_file img = FakeFile(filename="test.jpg") img_fail = FakeFile(filename="test.jpg", upload_should_fail=True) with tempfile.TemporaryDirectory() as tmpdir: success, filename = upload_file(img, tmpdir) self.assertTrue(success) self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) success, filename = upload_file(img, tmpdir, filename="fixname.jpg") self.assertTrue(success) self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) self.assertEqual(filename, "fixname.jpg") client.get("/") # flash() (used in case of failure) needs a http context with client.session_transaction(): success, filename = upload_file(img_fail, tmpdir) self.assertFalse(success) self.assertFalse(os.path.exists(os.path.join(tmpdir, filename))) flashes = get_flashed_messages() self.assertEqual(len(flashes), 1) self.assertTrue("intentionally" in flashes[0])
def tile_test(self, app, client): """ test tile endpoint """ img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: app.config["MAPTILES_DIR"] = tmpdir # copy image to fake upload folder img.save(os.path.join(tmpdir, img.filename)) self.assertHTTPOK(client, url_for("map.tile", filename="test.jpg"), url=True)
def test_generate_thumbnail(self, app, client): from app.helpers import generate_thumbnail from PIL import Image img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: os.mkdir(os.path.join(tmpdir, "thumbnails")) # copy image to fake upload folder img.save(os.path.join(tmpdir, img.filename)) self.assertTrue(generate_thumbnail(tmpdir, img.filename, 150, 150)) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", img.filename))) with Image.open(os.path.join(tmpdir, "thumbnails", img.filename)) as img: width, height = img.size self.assertEqual(height, 150) self.assertEqual(width, 150)
def profile_picture_test(self, app, client): """ test direct serving of profile pictures + thumbnails """ self.set_up_characters( ) # so that we have something to test (default) profile picture img = FakeFile(filename="test.jpg") self.char_admin.profile_picture = img.filename with tempfile.TemporaryDirectory() as tmpdir: app.config["PROFILE_PICTURE_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) # copy image to fake upload folder + thumbnail img.save(os.path.join(tmpdir, img.filename)) img.save(os.path.join(tmpdir, "thumbnails", img.filename)) # media3 is test.jpg self.assertHTTPOK(client, self.char_admin.profile_picture_url(), url=True) self.assertHTTPOK(client, self.char_admin.profile_thumbnail_url(), url=True)
def test_generate_media_thumbnail(self, app, client): import os from app.media.helpers import generate_media_thumbnail img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: app.config["MEDIA_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) # copy image to fake upload folder img.save(os.path.join(tmpdir, img.filename)) self.assertTrue(generate_media_thumbnail(img.filename)) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", img.filename))) # flash() needs context client.get("/") # try to generate thumbnail of non existing image (test fail path) with client.session_transaction(): self.assertFalse(generate_media_thumbnail("will_fail.png")) flashes = get_flashed_messages() self.assertEqual(len(flashes), 1) self.assertTrue("No such file" in flashes[0])
def test_upload_profile_picture(self, app, client): from app.helpers import upload_profile_picture from flask import current_app img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: current_app.config["PROFILE_PICTURE_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) success, filename = upload_profile_picture(img) self.assertTrue(success) self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", filename))) success, filename = upload_profile_picture(img, filename="fix.jpg") self.assertTrue(success) self.assertEqual(filename, "fix.jpg") self.assertTrue(os.path.exists(os.path.join(tmpdir, filename))) self.assertTrue(os.path.exists(os.path.join(tmpdir, "thumbnails", filename)))
def serve_test(self, app, client): """ test direct serving of files + thumbnails """ img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: app.config["MEDIA_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) # copy image to fake upload folder + thumbnail img.save(os.path.join(tmpdir, img.filename)) img.save(os.path.join(tmpdir, "thumbnails", img.filename)) # media3 is test.jpg self.assertHTTPOK(client, self.media3.serve_url(), url=True) self.assertHTTPOK(client, self.media3.thumbnail_url(), url=True)
def test_delete_profile_picture(self, app, client): from app.helpers import delete_profile_picture from flask import current_app img = FakeFile(filename="test.jpg") with tempfile.TemporaryDirectory() as tmpdir: current_app.config["PROFILE_PICTURE_DIR"] = tmpdir os.mkdir(os.path.join(tmpdir, "thumbnails")) img.save(os.path.join(tmpdir, img.filename)) img.save(os.path.join(tmpdir, "thumbnails", img.filename)) delete_profile_picture(img.filename) self.assertFalse(os.path.exists(os.path.join(tmpdir, img.filename))) self.assertFalse(os.path.exists(os.path.join(tmpdir, "thumbnails", img.filename))) # check that function does not throw on missing image # error path uses flash() -> need session client.get("/") with client.session_transaction(): delete_profile_picture("does_not_exist.jpg")