from flask import url_for from urllib.parse import unquote, quote from core_notifications.consts import SystemEvents def test_link_click(app, mocker, faker, authorized_app): mocked_email_events = mocker.patch( "core_notifications.analytics.collectors.Collector.log" ) email_id = faker.pystr() template_id = faker.pystr() template_name = faker.pystr() to = faker.email() origin_url = "https%3A%2F%2Fwww.w3scho%0Dols.com" subject = "some title" campaign_id = faker.pystr() url = url_for( "analytics.link_click", origin=unquote(origin_url), email_id=email_id, user_id=authorized_app.id, user_name=authorized_app.name, to=to, template_id=template_id, template_name=template_name, subject=quote(subject), campaign_id=campaign_id, _external=True, ) with app.test_client() as client: resp = client.get(url) expected_url = unquote(origin_url).replace("\r", "") assert mocked_email_events.call_args_list == [ mocker.call( SystemEvents.email_click_by_link, url=expected_url, email_id=email_id, user_id=unquote(authorized_app.id), user_name=authorized_app.name, to=to, template_id=template_id, template_name=template_name, campaign_id=campaign_id, subject=unquote(subject), ) ] assert resp.status_code == 302 assert resp.location == expected_url