from flask import url_for from urllib.parse import urlencode from core_notifications.helpers.common import replace_urls def test_replace_urls_for_no_html_body(app, faker): link = "https://test.com" body = f"My test link {link}" email_id = faker.pystr() template_id = faker.pyint() to = faker.email() test_analytic_url = url_for( "analytics.link_click", _external=True, ) with app.test_client(): params = { "email_id": email_id, "to": to, "template_id": template_id, "origin": link, } expected_result = body.replace( link, f"{test_analytic_url}?{urlencode(params)}" ) assert ( replace_urls( body=body, analytic_url=test_analytic_url, email_id=email_id, to=to, template_id=template_id, ) == expected_result ) def test_replace_urls_for_no_html_body_multiple_links(app, faker): link_1 = "https://test-1.com" link_2 = "https://test-2.com" body = f"My test link {link_1} and also {link_2}" email_id = faker.pystr() template_id = faker.pyint() to = faker.email() test_analytic_url = url_for( "analytics.link_click", _external=True, ) with app.test_client(): params = { "email_id": email_id, "to": to, "template_id": template_id, "origin": link_1, } expected_result = body.replace( link_1, f"{test_analytic_url}?{urlencode(params)}" ) params["origin"] = link_2 expected_result = expected_result.replace( link_2, f"{test_analytic_url}?{urlencode(params)}" ) assert ( replace_urls( body=body, analytic_url=test_analytic_url, email_id=email_id, to=to, template_id=template_id, ) == expected_result ) def test_replace_urls_for_html_body(app, faker): link = "https://test.com" body = ( f"