From 9cd528da0e854af7df0a524f39c620042accb4d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4ggstr=C3=B6m?= Date: Tue, 18 Aug 2020 21:04:31 +0200 Subject: [PATCH 1/3] Make Python modules less dependent on current working directory. Needed to get py.test to work regardless of where it is run from. --- app/backend/src/couchers/config.py | 4 ++-- app/backend/src/couchers/email/__init__.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/backend/src/couchers/config.py b/app/backend/src/couchers/config.py index 5fbca10e73..e6be606ffe 100644 --- a/app/backend/src/couchers/config.py +++ b/app/backend/src/couchers/config.py @@ -37,12 +37,12 @@ ("MEDIA_SERVER_BASE_URL", str), ] -dot = Path(".") if "pytest" in sys.modules: logger.info("Running in TEST") - load_dotenv(dot / "test.env") + load_dotenv(Path(__file__).parent / ".." / ".." / "test.env") else: + dot = Path(".") if (dot / ".env").is_file(): load_dotenv(dot / ".env") else: diff --git a/app/backend/src/couchers/email/__init__.py b/app/backend/src/couchers/email/__init__.py index 688f0d02e8..9e4ba1475a 100644 --- a/app/backend/src/couchers/email/__init__.py +++ b/app/backend/src/couchers/email/__init__.py @@ -1,3 +1,4 @@ +from os.path import dirname from jinja2 import Environment, FileSystemLoader, Template from markdown2 import markdown from couchers.email.smtp import send_smtp_email @@ -5,7 +6,7 @@ from couchers.config import config env = Environment( - loader=FileSystemLoader("templates"), + loader=FileSystemLoader(dirname(__file__) + "/../../../templates"), trim_blocks=True, ) From 35bc7e1de6ad098e311b40264f82c13c0c75dec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4ggstr=C3=B6m?= Date: Tue, 18 Aug 2020 21:25:23 +0200 Subject: [PATCH 2/3] Make media test independent of current working directory --- app/media/src/tests/test_server.py | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/media/src/tests/test_server.py b/app/media/src/tests/test_server.py index 524fc73724..873767ee36 100644 --- a/app/media/src/tests/test_server.py +++ b/app/media/src/tests/test_server.py @@ -20,6 +20,8 @@ from PIL.JpegImagePlugin import JpegImageFile +DATADIR = os.path.dirname(__file__) + "/data" + class MockMainServer(media_pb2_grpc.MediaServicer): def __init__(self, bearer_token, accept_func): self._bearer_token = bearer_token @@ -118,7 +120,7 @@ def test_image_upload(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "1x1.jpg")}) assert json.loads(rv.data)["ok"] @@ -130,7 +132,7 @@ def test_image_resizing(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/5000x5000.jpg", "rb") as f: + with open(DATADIR + "/5000x5000.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -152,7 +154,7 @@ def test_avatar_downscaling(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/5000x5000.jpg", "rb") as f: + with open(DATADIR + "/5000x5000.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -172,7 +174,7 @@ def test_avatar_upscaling(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -209,7 +211,7 @@ def test_wrong_filename(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: # filename shouldn't matter rv = client.post(upload_path, data={"file": (f, "wrongname.exe")}) @@ -227,11 +229,11 @@ def test_strips_exif(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - img = Image.open("src/tests/data/exif.jpg") + img = Image.open(DATADIR + "/exif.jpg") assert img.getexif() assert img.info["comment"] == b"I am an EXIF comment!\0" - with open("src/tests/data/exif.jpg", "rb") as f: + with open(DATADIR + "/exif.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "1x1.jpg")}) assert json.loads(rv.data)["ok"] @@ -250,7 +252,7 @@ def test_jpg_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -267,7 +269,7 @@ def test_png_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.png", "rb") as f: + with open(DATADIR + "/1x1.png", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -284,7 +286,7 @@ def test_gif_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.gif", "rb") as f: + with open(DATADIR + "/1x1.gif", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -301,7 +303,7 @@ def test_bad_file(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/badfile.txt", "rb") as f: + with open(DATADIR + "/badfile.txt", "rb") as f: rv = client.post(upload_path, data={"file": (f, "badfile.txt")}) assert rv.status_code == 400 @@ -314,7 +316,7 @@ def test_cant_reuse(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert json.loads(rv.data)["ok"] @@ -322,7 +324,7 @@ def test_cant_reuse(client_with_secrets): rv = client.get(f"/img/full/{key}.jpg") assert rv.status_code == 200 - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400 @@ -336,7 +338,7 @@ def test_fails_wrong_sig(client_with_secrets): upload_path = generate_upload_path(request, wrong_secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400 @@ -362,7 +364,7 @@ def test_fails_expired(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open("src/tests/data/1x1.jpg", "rb") as f: + with open(DATADIR + "/1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400 From b0bec40683f90ced181b973a53dd967ece715590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4ggstr=C3=B6m?= Date: Wed, 19 Aug 2020 04:06:53 +0200 Subject: [PATCH 3/3] Switch to object-oriented path handling --- app/backend/src/couchers/email/__init__.py | 4 +-- app/media/src/tests/test_server.py | 33 +++++++++++----------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/backend/src/couchers/email/__init__.py b/app/backend/src/couchers/email/__init__.py index 9e4ba1475a..ef9da0cdd6 100644 --- a/app/backend/src/couchers/email/__init__.py +++ b/app/backend/src/couchers/email/__init__.py @@ -1,4 +1,4 @@ -from os.path import dirname +from pathlib import Path from jinja2 import Environment, FileSystemLoader, Template from markdown2 import markdown from couchers.email.smtp import send_smtp_email @@ -6,7 +6,7 @@ from couchers.config import config env = Environment( - loader=FileSystemLoader(dirname(__file__) + "/../../../templates"), + loader=FileSystemLoader(Path(__file__).parent / ".." / ".." / ".." / "templates"), trim_blocks=True, ) diff --git a/app/media/src/tests/test_server.py b/app/media/src/tests/test_server.py index 873767ee36..9556171642 100644 --- a/app/media/src/tests/test_server.py +++ b/app/media/src/tests/test_server.py @@ -2,6 +2,7 @@ import json import os import tempfile +from pathlib import Path from base64 import urlsafe_b64encode from concurrent import futures from contextlib import contextmanager @@ -20,7 +21,7 @@ from PIL.JpegImagePlugin import JpegImageFile -DATADIR = os.path.dirname(__file__) + "/data" +DATADIR = Path(__file__).parent / "data" class MockMainServer(media_pb2_grpc.MediaServicer): def __init__(self, bearer_token, accept_func): @@ -120,7 +121,7 @@ def test_image_upload(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "1x1.jpg")}) assert json.loads(rv.data)["ok"] @@ -132,7 +133,7 @@ def test_image_resizing(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/5000x5000.jpg", "rb") as f: + with open(DATADIR / "5000x5000.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -154,7 +155,7 @@ def test_avatar_downscaling(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/5000x5000.jpg", "rb") as f: + with open(DATADIR / "5000x5000.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -174,7 +175,7 @@ def test_avatar_upscaling(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "img.jpg")}) assert json.loads(rv.data)["ok"] @@ -211,7 +212,7 @@ def test_wrong_filename(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: # filename shouldn't matter rv = client.post(upload_path, data={"file": (f, "wrongname.exe")}) @@ -229,11 +230,11 @@ def test_strips_exif(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - img = Image.open(DATADIR + "/exif.jpg") + img = Image.open(DATADIR / "exif.jpg") assert img.getexif() assert img.info["comment"] == b"I am an EXIF comment!\0" - with open(DATADIR + "/exif.jpg", "rb") as f: + with open(DATADIR / "exif.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "1x1.jpg")}) assert json.loads(rv.data)["ok"] @@ -252,7 +253,7 @@ def test_jpg_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -269,7 +270,7 @@ def test_png_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.png", "rb") as f: + with open(DATADIR / "1x1.png", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -286,7 +287,7 @@ def test_gif_pixel(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.gif", "rb") as f: + with open(DATADIR / "1x1.gif", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel")}) assert json.loads(rv.data)["ok"] @@ -303,7 +304,7 @@ def test_bad_file(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/badfile.txt", "rb") as f: + with open(DATADIR / "badfile.txt", "rb") as f: rv = client.post(upload_path, data={"file": (f, "badfile.txt")}) assert rv.status_code == 400 @@ -316,7 +317,7 @@ def test_cant_reuse(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert json.loads(rv.data)["ok"] @@ -324,7 +325,7 @@ def test_cant_reuse(client_with_secrets): rv = client.get(f"/img/full/{key}.jpg") assert rv.status_code == 200 - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400 @@ -338,7 +339,7 @@ def test_fails_wrong_sig(client_with_secrets): upload_path = generate_upload_path(request, wrong_secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400 @@ -364,7 +365,7 @@ def test_fails_expired(client_with_secrets): upload_path = generate_upload_path(request, secret_key) with mock_main_server(bearer_token, lambda x: True): - with open(DATADIR + "/1x1.jpg", "rb") as f: + with open(DATADIR / "1x1.jpg", "rb") as f: rv = client.post(upload_path, data={"file": (f, "pixel.jpg")}) assert rv.status_code == 400