from unittest import mock import pytest from anydi import Container from dirty_equals import IsStr from faker import Faker from ows_text_campaigns.campaigns.enums import ShorteningMethod from ows_text_campaigns.campaigns.handlers import ( ValidateCampaignShortenedUrlHandler, ValidateCampaignShortenedUrlRequest, ) from ows_text_campaigns.campaigns.models import Campaign, UrlDomain from ows_text_campaigns.campaigns.types import ShortenedUrl from ows_text_campaigns.campaigns.utils import create_short_link_tag from ows_text_campaigns.campaigns.validators import ShortenedUrlValidator from tests.unit.types import BuildModel, CreateModel class TestValidateCampaignShortenedUrlHandler: @pytest.mark.db def test_validate_campaign_shortened_url_standard( self, container: Container, handler: ValidateCampaignShortenedUrlHandler, create_model: CreateModel, identity_id: str, faker: Faker, ) -> None: url = faker.url() shortening_method = ShorteningMethod.STANDARD url_domain = create_model(UrlDomain, domain="sme.com") campaign = create_model(Campaign) shortened_url_validator_mock = mock.MagicMock(spec=ShortenedUrlValidator) shortened_url_validator_mock.validate_allowed_domain.return_value = url_domain with container.override(ShortenedUrlValidator, shortened_url_validator_mock): shortened_url = handler.handle( ValidateCampaignShortenedUrlRequest( identity_id=identity_id, campaign_id=campaign.id, shortened_url_id=None, url=url, method=shortening_method, domain=url_domain.id, path="test1", ) ) assert shortened_url.id == IsStr() assert shortened_url.url == url assert shortened_url.method == shortening_method assert shortened_url.domain == url_domain.domain assert shortened_url.path == "test1" assert shortened_url.preview_url == "https://sme.com/test1" @pytest.mark.db def test_validate_campaign_shortened_url_standard_skip( self, container: Container, handler: ValidateCampaignShortenedUrlHandler, build_model: BuildModel, create_model: CreateModel, identity_id: str, faker: Faker, ) -> None: url_domain = create_model(UrlDomain, domain="sme.com") shortened_url = build_model( ShortenedUrl, method=ShorteningMethod.STANDARD, path="test123", domain=url_domain.domain, ) changed_url = faker.url() campaign = create_model(Campaign, content=create_short_link_tag(shortened_url)) shortened_url_validator_mock = mock.MagicMock(spec=ShortenedUrlValidator) shortened_url_validator_mock.validate_allowed_domain.return_value = url_domain with container.override(ShortenedUrlValidator, shortened_url_validator_mock): result = handler.handle( ValidateCampaignShortenedUrlRequest( identity_id=identity_id, campaign_id=campaign.id, shortened_url_id=shortened_url.id, url=changed_url, method=shortened_url.method, domain=shortened_url.domain, path=shortened_url.path, ) ) shortened_url_validator_mock.validate_availability.assert_not_called() assert result.url == changed_url @pytest.mark.db def test_validate_campaign_shortened_url_personalized( self, container: Container, handler: ValidateCampaignShortenedUrlHandler, create_model: CreateModel, identity_id: str, faker: Faker, ) -> None: shortened_url_id = faker.uuid4() url = faker.url() shortening_method = ShorteningMethod.PERSONALIZED url_domain = create_model(UrlDomain, domain="sme.com") campaign = create_model(Campaign) shortened_url_validator_mock = mock.MagicMock(spec=ShortenedUrlValidator) shortened_url_validator_mock.validate_allowed_domain.return_value = url_domain with container.override(ShortenedUrlValidator, shortened_url_validator_mock): shortened_url = handler.handle( ValidateCampaignShortenedUrlRequest( identity_id=identity_id, campaign_id=campaign.id, shortened_url_id=shortened_url_id, url=url, method=shortening_method, domain=url_domain.domain, path="test1", ) ) assert shortened_url.id == shortened_url_id assert shortened_url.url == url assert shortened_url.method == shortening_method assert shortened_url.domain == url_domain.domain assert shortened_url.path == "test1" assert shortened_url.preview_url == "https://sme.com/test1-abc123"