from collections.abc import Iterator import httpx import pytest from owsclient import OwsClient from owsclient.test import OwsClientMock from ows_text_campaigns.adapters.ows_url_shortener import ( OwsUrlShortenerClient, ShortUrl, ) class TestOwsUrlShortenerClient: @pytest.fixture(scope="class") def ows_client(self) -> Iterator[OwsClient]: yield OwsClient(environment="test", service_name="ows-url-shortener") @pytest.fixture(scope="class") def ows_url_shortener_client(self, ows_client: OwsClient) -> OwsUrlShortenerClient: return OwsUrlShortenerClient(ows_client=ows_client) def test_check_path( self, ows_url_shortener_client: OwsUrlShortenerClient, ows_client_mock: OwsClientMock, ) -> None: ows_client_mock.get( service_name="ows-url-shortener", path="/check-path", params={"path": "custom-path"}, ).mock( httpx.Response( status_code=200, json={"is_available": True}, ) ) is_available = ows_url_shortener_client.check_path("custom-path") assert is_available is True def test_shorten_url( self, ows_url_shortener_client: OwsUrlShortenerClient, ows_client_mock: OwsClientMock, ) -> None: ows_client_mock.post( service_name="ows-url-shortener", path="/shorten", json={ "url": "https://example.com", "domain": "short.ly", "path": None, "path_prefix": None, "additional_attributes": None, }, ).mock( httpx.Response( status_code=200, json={ "url": "https://example.com", "short_url": "short.ly/custom-path", }, ) ) response = ows_url_shortener_client.shorten_url( url="https://example.com", domain="short.ly", ) assert response.url == "https://example.com" assert response.short_url == "https://short.ly/custom-path" def test_bulk_shorten( self, ows_url_shortener_client: OwsUrlShortenerClient, ows_client_mock: OwsClientMock, ) -> None: ows_client_mock.post( service_name="ows-url-shortener", path="/bulk-shorten", json={ "url": "https://example1.com", "domain": "short.ly", "path_prefix": None, "urls_count": 2, "additional_attributes": [ {"fan_id": "fan1", "campaign_id": "campaign1"}, {"fan_id": "fan2", "campaign_id": "campaign2"}, ], }, ).mock( httpx.Response( status_code=200, json={ "url": "https://example.com", "short_urls": [ { "short_url": "https://short.ly/1", "additional_attributes": { "fan_id": "fan1", "campaign_id": "campaign1", }, }, { "short_url": "https://short.ly/2", "additional_attributes": { "fan_id": "fan2", "campaign_id": "campaign2", }, }, ], }, ) ) response = ows_url_shortener_client.bulk_shorten( url="https://example1.com", domain="short.ly", additional_attributes=[ {"fan_id": "fan1", "campaign_id": "campaign1"}, {"fan_id": "fan2", "campaign_id": "campaign2"}, ], ) assert response.url == "https://example.com" assert response.short_urls == [ ShortUrl( short_url="https://short.ly/1", additional_attributes={"fan_id": "fan1", "campaign_id": "campaign1"}, ), ShortUrl( short_url="short.ly/2", additional_attributes={"fan_id": "fan2", "campaign_id": "campaign2"}, ), ] def test_delete_paths_no_paths( self, ows_url_shortener_client: OwsUrlShortenerClient, ) -> None: ows_url_shortener_client.delete_paths([]) assert True def test_delete_paths( self, ows_url_shortener_client: OwsUrlShortenerClient, ows_client_mock: OwsClientMock, ) -> None: ows_client_mock.post( service_name="ows-url-shortener", path="/delete-paths", json={"paths": ["path1", "path2"]}, ).mock(httpx.Response(status_code=204)) ows_url_shortener_client.delete_paths(["path1", "path2"]) assert True