Raise edit API errors with response details
This commit is contained in:
parent
9486d9cb8a
commit
bd0c1a26c2
2 changed files with 58 additions and 5 deletions
46
test_mediawiki_api.py
Normal file
46
test_mediawiki_api.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from add_links import mediawiki_api
|
||||
|
||||
|
||||
class EditPageTests(unittest.TestCase):
|
||||
def test_edit_page_raises_api_error_with_mediawiki_message(self) -> None:
|
||||
with patch(
|
||||
"add_links.mediawiki_api.call",
|
||||
return_value={"error": {"code": "badtoken", "info": "Invalid CSRF token."}},
|
||||
):
|
||||
with self.assertRaises(mediawiki_api.APIError) as ctx:
|
||||
mediawiki_api.edit_page(
|
||||
pageid=1,
|
||||
section=0,
|
||||
text="text",
|
||||
summary="summary",
|
||||
baserevid="123",
|
||||
token="bad",
|
||||
)
|
||||
|
||||
self.assertEqual(str(ctx.exception), "Invalid CSRF token.")
|
||||
|
||||
def test_edit_page_raises_api_error_for_unexpected_response(self) -> None:
|
||||
response = {"servedby": "mw-api-ext.eqiad.main"}
|
||||
|
||||
with patch("add_links.mediawiki_api.call", return_value=response):
|
||||
with self.assertRaises(mediawiki_api.APIError) as ctx:
|
||||
mediawiki_api.edit_page(
|
||||
pageid=1,
|
||||
section=0,
|
||||
text="text",
|
||||
summary="summary",
|
||||
baserevid="123",
|
||||
token="token",
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
str(ctx.exception),
|
||||
"Unexpected MediaWiki API response: {'servedby': 'mw-api-ext.eqiad.main'}",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue