Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion apicore/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def list(self, request):
cache_key = f"userfile:{user_id}"
data = cache.get(cache_key)
if data is None:
with user_obj.user_file.open("r") as f:
with user_obj.user_file.open("rb") as f:
lines = [line.decode("utf-8") for line in f.readlines()]
data = LuaParser(lines).parse()
cache.set(cache_key, data, timeout=None)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,45 @@ def test_upload_does_not_500(self):
)
assert resp.status_code == 200

def test_addon_page_reads_uploaded_file(self):
"""Regression test: page=addon (and any non-header page reading the
uploaded file) used to crash with 'str' object has no attribute 'decode'
because the file was opened in text mode ("r") despite the parser
expecting bytes to decode itself."""
user_id = "u1"
ProfileUser.objects.create(user_id=user_id, user_file="", user_last_update=timezone.now())
c = self._authed_client(user_id)

body = (
"FazzToolsScraperDB = {\n"
"preamble\n"
'["alts"] = {\n'
'["Testchar-Realm"] = {\n'
'["gold"] = 12345,\n'
"},\n"
"},\n"
"}\n"
)
upload = SimpleUploadedFile(
"FazzToolsScraper.lua", body.encode(), content_type="text/plain"
)
c.put(
f"/api/profile/users/{user_id}/",
data=encode_multipart(
BOUNDARY,
{
"user_id": user_id,
"user_file": upload,
"user_last_update": timezone.now().isoformat(),
},
),
content_type=MULTIPART_CONTENT,
)

resp = c.get(f"/api/profile/users/?user={user_id}&page=addon")
assert resp.status_code == 200
assert resp.json() == []


# ---------------------------------------------------------------------------
# ProfileAltView — IsSessionUser enforcement
Expand Down
Loading