Skip to content

Commit ebebe51

Browse files
Do softer version check. (#728)
* Do softer version check. * Fix typos * Emit warning for invalid tiled client version * test_client_version_check checks for warning and log entry --------- Co-authored-by: Padraic Shafer <76011594+padraic-shafer@users.noreply.github.com> Co-authored-by: Padraic Shafer <pshafer@bnl.gov>
1 parent a15471e commit ebebe51

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

tiled/_tests/test_client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from pathlib import Path
23

34
import httpx
@@ -22,7 +23,7 @@ def test_configurable_timeout():
2223
assert context.http_client.timeout.read == 17
2324

2425

25-
def test_client_version_check():
26+
def test_client_version_check(caplog):
2627
with Context.from_app(build_app(tree)) as context:
2728
client = from_context(context)
2829

@@ -31,11 +32,21 @@ def test_client_version_check():
3132
with fail_with_status_code(HTTP_400_BAD_REQUEST):
3233
list(client)
3334

34-
# Gibberish user agent should generate a 400.
35+
# Gibberish user agent should generate a warning and log entry.
3536
context.http_client.headers["user-agent"] = "python-tiled/gibberish"
36-
with fail_with_status_code(HTTP_400_BAD_REQUEST):
37+
caplog.set_level(logging.WARNING)
38+
with pytest.warns(UserWarning, match=r"gibberish"):
3739
list(client)
3840

41+
_, LOG_LEVEL, LOG_MESSAGE = range(3)
42+
logged_warnings = tuple(
43+
entry[LOG_MESSAGE]
44+
for entry in caplog.record_tuples
45+
if entry[LOG_LEVEL] == logging.WARNING
46+
)
47+
assert len(logged_warnings) > 0
48+
assert any("gibberish" in message for message in logged_warnings)
49+
3950

4051
def test_direct(tmpdir):
4152
profile_content = {

tiled/server/app.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import secrets
66
import sys
77
import urllib.parse
8+
import warnings
89
from contextlib import asynccontextmanager
910
from functools import lru_cache, partial
1011
from pathlib import Path
@@ -715,18 +716,18 @@ async def client_compatibility_check(request: Request, call_next):
715716
agent, _, raw_version = user_agent.partition("/")
716717
try:
717718
parsed_version = packaging.version.parse(raw_version)
718-
except Exception:
719-
return JSONResponse(
720-
status_code=HTTP_400_BAD_REQUEST,
721-
content={
722-
"detail": (
723-
f"Python Tiled client is version is reported as {raw_version}. "
724-
"This cannot be parsed as a valid version."
725-
),
726-
},
719+
except Exception as caught_exception:
720+
invalid_version_message = (
721+
f"Python Tiled client version is reported as {raw_version}. "
722+
"This cannot be parsed as a valid version."
727723
)
724+
logger.warning(invalid_version_message)
725+
if isinstance(caught_exception, packaging.version.InvalidVersion):
726+
warnings.warn(invalid_version_message)
728727
else:
729-
if parsed_version < MINIMUM_SUPPORTED_PYTHON_CLIENT_VERSION:
728+
if (not parsed_version.is_devrelease) and (
729+
parsed_version < MINIMUM_SUPPORTED_PYTHON_CLIENT_VERSION
730+
):
730731
return JSONResponse(
731732
status_code=HTTP_400_BAD_REQUEST,
732733
content={

0 commit comments

Comments
 (0)