Skip to content

test: add pytest suite for utils.py and fix AttributeError on missing Content-Type header (T415715)#93

Open
devjadiya wants to merge 1 commit into
indictechcom:masterfrom
devjadiya:add-pytest-suite-t415715
Open

test: add pytest suite for utils.py and fix AttributeError on missing Content-Type header (T415715)#93
devjadiya wants to merge 1 commit into
indictechcom:masterfrom
devjadiya:add-pytest-suite-t415715

Conversation

@devjadiya
Copy link
Copy Markdown

Closes T415715 (test infrastructure)

What this PR does

Adds the first pytest test suite to the project. Zero automated tests currently exist in the codebase. This PR adds 18 tests covering all three core functions in utils.py, with all HTTP calls mocked via unittest.mock so no live network or database is needed.

Bug found and fixed by the tests

test_content_type_missing_returns_safely failed on the first run, catching a real production crash:

AttributeError: 'NoneType' object has no attribute 'replace'

In download_image(), the line:

filename = get_filename + "." + r.headers.get('content-type').replace('image/', '')

crashes whenever the Wikimedia server returns a response without a Content-Type header, or returns one with a charset suffix like image/jpeg; charset=utf-8.

Fix applied in utils.py:

content_type = r.headers.get('content-type') or 'image/jpeg'
file_ext = content_type.replace('image/', '').split(';')[0].strip()
filename = get_filename + "." + file_ext

This also handles the charset suffix case which the original code would have turned into a filename like 2026-01-01.jpeg; charset=utf-8.

Tests added (18 total, all pass)

getHeader()

  • returns dict with User-Agent key
  • User-Agent is not empty
  • User-Agent identifies the tool (contains "wikifile")

download_image()

  • returns filename on success
  • returns None when imageinfo key is missing from API response
  • generated filename uses underscores (no spaces or colons)
  • constructs correct endpoint URL from project + lang
  • does not crash when Content-Type header is absent (bug fixed above)

process_upload()

  • returns wikipage_url and file_link on success
  • returns None when upload response is missing imageinfo (e.g. bad token)
  • assembles filename as trfilename + "." + extension
  • includes CSRF token in the upload POST request

get_localized_wikitext()

  • replaces Article param when langlink found for target lang
  • preserves original when no langlink matches target lang
  • returns original string on any API exception (does not crash)
  • skips API call entirely when no matching templates found
  • does not modify wikitext that has no templates
  • documents the known gap: Category nodes are not currently localized (this is what the GSoC project will fix)

Test run output

18 passed in 0.41s

How to run

pip install pytest mwparserfromhell requests
pytest test_utils.py -v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant