fix: Skip setting existing context values

When setting up a context with user provided variables,
we should not override any existing values previously set.

Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-10-12 18:09:07 +02:00
parent 2166426d4c
commit 27cd533654
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
2 changed files with 11 additions and 1 deletions

View file

@ -112,7 +112,7 @@ async def make_variable_context(request: Request, title: str, next: str = None):
for k, v in to_copy.items(): for k, v in to_copy.items():
if k == "timezone": if k == "timezone":
context[k] = v if v in time.SUPPORTED_TIMEZONES else DEFAULT_TIMEZONE context[k] = v if v in time.SUPPORTED_TIMEZONES else DEFAULT_TIMEZONE
else: elif k not in context:
context[k] = v context[k] = v
context["q"] = dict(request.query_params) context["q"] = dict(request.query_params)

View file

@ -368,3 +368,13 @@ async def test_make_variable_context_timezone(user: User, package: Package):
request, "Test Details", next="/packages/test" request, "Test Details", next="/packages/test"
) )
assert context["timezone"] in time.SUPPORTED_TIMEZONES assert context["timezone"] in time.SUPPORTED_TIMEZONES
@pytest.mark.asyncio
async def test_make_variable_context_params():
request = Request(url="/test", query_params={"request": "test", "x": "test"})
context = await make_variable_context(request, "Test")
# make sure we can't override our Request object with a query parameter
assert context["request"] != "test"
assert context["x"] == "test"