from textwrap import dedent import pytest from bs4 import BeautifulSoup from ows_text_campaigns.campaigns.enums import ShorteningMethod from ows_text_campaigns.campaigns.exceptions import InvalidShortLinkTagError from ows_text_campaigns.campaigns.types import ShortenedUrl from ows_text_campaigns.campaigns.utils import ( extract_shortened_urls_from_markup, replace_short_link_tags, replace_standard_links_tags, ) def test_extract_shortened_urls_from_content() -> None: input_html = """ link1 link2 """ result = extract_shortened_urls_from_markup(input_html) assert result == [ ShortenedUrl( id="url1", url="https://sme.com/test1", method=ShorteningMethod.STANDARD, domain="sme.com", path="path1", ), ShortenedUrl( id="url2", url="https://sme.com/test2", method=ShorteningMethod.STANDARD, domain="sme.com", path="path2", ), ] def test_extract_shortened_url_from_invalid_content() -> None: input_html = """ link """ with pytest.raises(InvalidShortLinkTagError) as exc_info: extract_shortened_urls_from_markup(input_html, raise_on_error=True) assert exc_info.value.message == ( "Missing or invalid `data-short-link-*` attribute(s) in short link tag: " "method, domain" ) def test_replace_short_link_tags() -> None: input_html = dedent("""
""") result = replace_short_link_tags( input_html, shortened_urls={ "url1": "https://sme.com/link-abc1", "url2": "https://sme.com/link-abc2", }, ) expected = dedent("""https://sme.com/link-abc1 https://sme.com/link-abc2
""") assert result == expected def test_replace_standard_links_tags() -> None: input_html = """ """ expected_html = """ """ expected_soup = BeautifulSoup(expected_html, "html.parser") result = replace_standard_links_tags(input_html) result_soup = BeautifulSoup(result, "html.parser") assert result_soup.prettify() == expected_soup.prettify()