import json from flask import url_for from tests.core_notifications.factories import TemplateFactory def test_amplitude_integration(app, db_session, mocker, faker, authorized_app): test_template = TemplateFactory.build(is_active=True) db_session.add(test_template) db_session.commit() test_context = json.loads(test_template.context) mocked_task = mocker.patch( "core_notifications.tasks.send_email_task.apply_async" ) mocked_task.return_value.task_id = "test_id" mocked_send_event = mocker.patch( "core_notifications.analytics.amplitude.Amplitude.send_event" ) test_body = [ { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_context, }, }, { "to": ["test2@example.com", "test3@example.com"], "template": { "id": test_template.id, "context": test_context, }, }, { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_context, }, "cc": "test_cc1@example.com", }, { "to": ["test2@example.com", "test3@example.com"], "template": { "id": test_template.id, "context": test_context, }, "cc": "test_cc1@example.com", }, { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_context, }, "cc": None, }, { "to": ["test2@example.com", "test3@example.com"], "template": { "id": test_template.id, "context": test_context, }, }, { "to": "test1@example.com", "template": { "id": test_template.id, "context": test_context, }, "cc": ["test_cc1@example.com", "test_cc2@example.com"], }, { "to": "test_with_attachments@example.com", "template": { "id": test_template.id, "context": test_context, }, "attachments": [ { "filename": "some_name.csv", "filetype": "text/csv", "base64_data": "some_data", } ], }, ] url = url_for("api_v1.email_send") with app.test_client() as client: resp = client.post( url, data=json.dumps(test_body), content_type="application/json", ) assert resp.status_code == 200 assert mocked_send_event.call_args_list == [ mocker.call( { "event_type": "Sending email", "user_id": authorized_app.id, "event_properties": { "id": "test_id", "template_id": test_template.id, "template_name": test_template.name, "to": to, "subject": test_template.subject, "env": app.config.get("ENV"), "user_id": authorized_app.id, "user_name": authorized_app.name, }, } ) for to in ( "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", "test2@example.com", "test3@example.com", "test1@example.com", "test_with_attachments@example.com", ) ]