Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/test_upload_refusal_hides_temp_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import re
import tempfile
from pathlib import PurePosixPath, PureWindowsPath

import pytest
from fastapi.testclient import TestClient
Expand Down Expand Up @@ -143,3 +144,36 @@ def test_a_good_upload_is_unchanged(url, name, body):

assert response.status_code == 200, response.text
assert response.json()["filename"] == name


# Each spelling on its own (#475 follow-up). The endpoint tests above pass
# with any ONE of repr / doubled / as_posix removed: repr's inner text IS the
# doubled spelling, so each covers for the other, and no loader writes a
# forward-slash path. These call the function directly with a pure path, so
# a Windows path is a Windows path on CI's Linux too, and each spelling is
# the only one that can match its message.

WINDOWS_TEMP = PureWindowsPath(r"C:\Users\<user>\AppData\Local\Temp\tmpab12cd.json")
LINUX_TEMP = PurePosixPath("/tmp/pytest-of-ci/netwise-temp-canary" + chr(92) + "x/tmpab12cd.json")


@pytest.mark.parametrize("temp_path", [WINDOWS_TEMP, LINUX_TEMP], ids=["windows", "linux-backslash"])
@pytest.mark.parametrize("spell", [
pytest.param(lambda p: repr(str(p)), id="repr"),
pytest.param(lambda p: str(p).replace("\\", "\\\\"), id="doubled-unquoted"),
pytest.param(lambda p: str(p), id="plain"),
])
def test_each_spelling_alone_is_replaced_exactly(temp_path, spell):
message = f"cannot read {spell(temp_path)}: Permission denied"

shown = main._refusal_without_temp_path(message, temp_path, "my-policy.json")

assert shown == "cannot read 'my-policy.json': Permission denied", shown


def test_a_forward_slash_spelling_of_a_windows_path_is_replaced():
message = f"{WINDOWS_TEMP.as_posix()} is not valid JSON"

shown = main._refusal_without_temp_path(message, WINDOWS_TEMP, "my-policy.json")

assert shown == "'my-policy.json' is not valid JSON", shown