import traceback from collections.abc import Iterator from unittest import mock import faker import httpx import pytest import respx from fansifter_common.adapters.stripo import StripoClient @pytest.fixture def client() -> Iterator[StripoClient]: with StripoClient( base_url="https://stripo_base_url", client_id="client_id", client_secret="client_secret", ) as client: yield client def test_request_error(client: StripoClient, respx_mock: respx.MockRouter) -> None: payload = { "pluginId": "not_valid_plugin_id", "secretKey": "not_valid_key", } respx_mock.post( "base_url/test", json=payload, ).mock(return_value=httpx.Response(status_code=403)) try: client.request("GET", "base_url/test") output = "" except Exception as exc: output = "".join(traceback.format_exception(exc)) assert client.client_secret not in output def test_generate_auth_token( client: StripoClient, respx_mock: respx.MockRouter, faker: faker.Faker, ) -> None: auth_token = faker.pystr() respx_mock.post( "https://stripo_base_url/api/v1/auth", headers={ "Accept": "application/json", "Content-Type": "application/json", }, json={ "pluginId": "client_id", "secretKey": "client_secret", }, ).mock(return_value=httpx.Response(status_code=200, json={"token": auth_token})) result = client.generate_auth_token() assert result == auth_token def test_compress( client: StripoClient, respx_mock: respx.MockRouter, stripo_client_mock: mock.MagicMock, faker: faker.Faker, ) -> None: auth_token = faker.pystr() html = faker.pystr() css = faker.pystr() compress_html = faker.pystr() respx_mock.post( "https://stripo_base_url/api/v1/auth", headers={ "Accept": "application/json", "Content-Type": "application/json", }, json={ "pluginId": "client_id", "secretKey": "client_secret", }, ).mock(return_value=httpx.Response(status_code=200, json={"token": auth_token})) respx_mock.post( "https://stripo_base_url/api/v1/cleaner/v1/compress", headers={ "Accept": "application/json", "Content-Type": "application/json", "ES-PLUGIN-AUTH": f"Bearer {auth_token}", }, json={"html": html, "css": css, "minimize": True}, ).mock(return_value=httpx.Response(status_code=200, json={"html": compress_html})) result = client.compress(html=html, css=css) assert result == compress_html