import traceback from collections.abc import Iterator import faker import httpx import pytest import respx from fansifter_common.adapters.poeditor import POEditorClient @pytest.fixture def client() -> Iterator[POEditorClient]: with POEditorClient(api_token="poeditor_token") as client: yield client def test_request_error(client: POEditorClient, respx_mock: respx.MockRouter) -> None: respx_mock.get( "https://api.poeditor.com/v2/terms/test", data={"api_token": "poeditor_token"}, ).mock(return_value=httpx.Response(status_code=404)) try: client.request("GET", "https://api.poeditor.com/v2/terms/test") output = "" except Exception as exc: output = "".join(traceback.format_exception(exc)) assert client._api_token not in output def test_request_invalid_response( client: POEditorClient, respx_mock: respx.MockRouter ) -> None: response = { "response": { "status": "fail", "code": "403", "message": "You don't have permission to access this resource", }, } respx_mock.post( "https://api.poeditor.com/v2/terms/list", data={"api_token": "poeditor_token", "id": "invalid_id"}, ).mock(return_value=httpx.Response(status_code=200, json=response)) try: client.request("POST", "https://api.poeditor.com/v2/terms/list") output = "" except Exception as exc: output = "".join(traceback.format_exception(exc)) assert client._api_token not in output def test_get_project_terms( client: POEditorClient, respx_mock: respx.MockRouter, faker: faker.Faker, ) -> None: project_id = faker.pyint() language = faker.pystr() term_1 = faker.pystr() term_2 = faker.pystr() translation_1 = faker.pystr() translation_2 = faker.pystr() response = { "response": {"status": "success", "code": "200", "message": "OK"}, "result": { "terms": [ { "term": term_1, "translation": {"content": translation_1}, }, { "term": term_2, "translation": {"content": translation_2}, }, ] }, } respx_mock.post( "https://api.poeditor.com/v2/terms/list", data={ "api_token": "poeditor_token", "id": project_id, "language": language, }, ).mock(return_value=httpx.Response(status_code=200, json=response)) result = client.get_project_terms_translation_for_lang( project_id=project_id, language_code=language ) assert result == {term_1: translation_1, term_2: translation_2} def test_get_available_languages( client: POEditorClient, respx_mock: respx.MockRouter, faker: faker.Faker, ) -> None: project_id = faker.pyint() response = { "response": {"status": "success", "code": "200", "message": "OK"}, "result": { "languages": [ { "name": "English", "code": "en", "translations": 13, "percentage": 12.5, "updated": "2015-05-04T14:21:41+0000", }, { "name": "French", "code": "fr", "translations": 70, "percentage": 68.75, "updated": "2015-04-30T08:59:34+0000", }, ] }, } respx_mock.post( "https://api.poeditor.com/v2/languages/list", data={ "api_token": "poeditor_token", "id": project_id, }, ).mock(return_value=httpx.Response(status_code=200, json=response)) result = client.get_available_project_languages(project_id=project_id) assert result == {"en", "fr"}