from flask import url_for from core_notifications import services from core_notifications.consts import SendingTaskStatus from core_notifications.settings import Settings from tests.core_notifications.factories import TemplateFactory from core_notifications.templating.services import RenderLiquidService from core_notifications.consts import SystemEvents def test_schedule_emails_batch_raw_body(app, mocker, authorized_app): mocked_task = mocker.patch( "core_notifications.tasks.send_email_task.apply_async" ) mocked_task.return_value.task_id = "test_id" mocked_analytics_log = mocker.patch( "core_notifications.analytics.emails_events.log" ) test_data = [ { "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": "test_cc1@example.com", "campaign_id": "test_campaign_id", }, { "subject": "some title", "to": ["test2@example.com", "test3@example.com"], "body": "Hello world!", "cc": "test_cc1@example.com", "campaign_id": "test_campaign_id", }, { "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": None, "campaign_id": "test_campaign_id", }, { "subject": "some title", "to": ["test2@example.com", "test3@example.com"], "body": "Hello world!", "cc": None, "campaign_id": "test_campaign_id", }, { "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": ["test_cc1@example.com", "test_cc2@example.com"], "campaign_id": "test_campaign_id", }, ] expected_result = [ { "test1@example.com": "test_id", }, { "test2@example.com": "test_id", "test3@example.com": "test_id", }, { "test1@example.com": "test_id", }, { "test2@example.com": "test_id", "test3@example.com": "test_id", }, { "test1@example.com": "test_id", }, ] with app.test_client(): analytic_url = url_for( "analytics.link_click", _external=True, ) result = services.schedule_emails_batch(test_data) assert result == expected_result assert mocked_task.call_args_list == [ mocker.call( kwargs={ "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": "test_cc1@example.com", "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": "some title", "to": "test2@example.com", "body": "Hello world!", "cc": "test_cc1@example.com", "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": "some title", "to": "test3@example.com", "body": "Hello world!", "cc": "test_cc1@example.com", "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": None, "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": "some title", "to": "test2@example.com", "body": "Hello world!", "cc": None, "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": "some title", "to": "test3@example.com", "body": "Hello world!", "cc": None, "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "to": "test1@example.com", "subject": "some title", "body": "Hello world!", "cc": ["test_cc1@example.com", "test_cc2@example.com"], "analytics": { "subject": "some title", "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), ] assert mocked_analytics_log.call_args_list == [ mocker.call( SystemEvents.sending_email, id="test_id", to=to, subject="some title", user_id=authorized_app.id, user_name=authorized_app.name, campaign_id="test_campaign_id", ) for to in ( "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", ) ] def test_schedule_emails_batch_template( app, mocker, db_session, authorized_app, faker ): mocked_task = mocker.patch( "core_notifications.tasks.send_email_task.apply_async" ) mocked_task.return_value.task_id = "test_id" mocked_analytics_log = mocker.patch( "core_notifications.analytics.emails_events.log" ) test_template = TemplateFactory.build() db_session.add(test_template) db_session.commit() expected_subject = RenderLiquidService.execute( test_template.subject, test_template.context ) expected_body = RenderLiquidService.execute( test_template.source, test_template.context ) test_data = [ { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_template.context, }, "cc": "test_cc1@example.com", "campaign_id": "test_campaign_id", }, { "to": ["test2@example.com", "test3@example.com"], "template": { "id": test_template.id, "context": test_template.context, }, "cc": "test_cc1@example.com", "campaign_id": "test_campaign_id", }, { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_template.context, }, "cc": None, "campaign_id": "test_campaign_id", }, { "to": ["test2@example.com", "test3@example.com"], "template": { "id": test_template.id, "context": test_template.context, }, "cc": None, "campaign_id": "test_campaign_id", }, { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_template.context, }, "cc": ["test_cc1@example.com", "test_cc2@example.com"], "campaign_id": "test_campaign_id", }, ] expected_result = [ { "test1@example.com": "test_id", }, { "test2@example.com": "test_id", "test3@example.com": "test_id", }, { "test1@example.com": "test_id", }, { "test2@example.com": "test_id", "test3@example.com": "test_id", }, { "test1@example.com": "test_id", }, ] with app.test_client(): analytic_url = url_for( "analytics.link_click", _external=True, ) result = services.schedule_emails_batch(test_data) assert result == expected_result assert mocked_task.call_args_list == [ mocker.call( kwargs={ "to": "test1@example.com", "subject": expected_subject, "body": expected_body, "cc": "test_cc1@example.com", "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": expected_subject, "to": "test2@example.com", "body": expected_body, "cc": "test_cc1@example.com", "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": expected_subject, "to": "test3@example.com", "body": expected_body, "cc": "test_cc1@example.com", "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "to": "test1@example.com", "subject": expected_subject, "body": expected_body, "cc": None, "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": expected_subject, "to": "test2@example.com", "body": expected_body, "cc": None, "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "subject": expected_subject, "to": "test3@example.com", "body": expected_body, "cc": None, "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), mocker.call( kwargs={ "to": "test1@example.com", "subject": expected_subject, "body": expected_body, "cc": ["test_cc1@example.com", "test_cc2@example.com"], "analytics": { "subject": expected_subject, "template_id": test_template.id, "template_name": test_template.name, "user_id": authorized_app.id, "user_name": authorized_app.name, "campaign_id": "test_campaign_id", }, "analytic_url": analytic_url, } ), ] assert mocked_analytics_log.call_args_list == [ mocker.call( SystemEvents.sending_email, id="test_id", template_id=test_template.id, template_name=test_template.name, to=to, subject=expected_subject, user_id=authorized_app.id, user_name=authorized_app.name, campaign_id="test_campaign_id", ) for to in ( "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", ) ] def test_get_sending_status(mocker): mocked_result = mocker.patch( "core_notifications.services.AsyncResult" ).return_value mocked_result.state = "SUCCESS" actual_status = services.get_sending_status("test_id") assert mocked_result.get.call_args_list == [ mocker.call(timeout=Settings.TASK_GET_RESULT_TIMEOUT) ] assert actual_status == SendingTaskStatus.SUCCESS