from liquid import Template from flask import url_for from core_notifications.consts import SystemEvents from core_notifications.templating.services import ( CreateTemplateService, UpdateTemplateService, RenderLiquidService, SendTestEmailService, EnableDisableTemplateService, ) from tests.core_notifications.factories import ( DNAAccountFactory, TemplateFactory, ) class TestCreateTemplateService: def test_success(self, app, db_session, faker): test_dna_account = DNAAccountFactory.build() test_name = faker.pystr() test_subject = "Test {{ variable }}" test_source = "{{ variable }}" test_context = '{"test_variable": "test_value"}' template = CreateTemplateService.execute( name=test_name, subject=test_subject, source=test_source, context=test_context, dna_account=test_dna_account, is_active=True, ) db_session.refresh(template) assert template.name == test_name assert template.subject == test_subject assert template.source == test_source assert template.context == test_context assert template.created_by == test_dna_account assert template.updated_by == test_dna_account assert template.is_active assert template.versions assert template.versions[0].name == test_name assert template.versions[0].subject == test_subject assert template.versions[0].source == test_source assert template.versions[0].context == test_context assert template.versions[0].created_by == test_dna_account class TestUpdateTemplateService: def test_success_new_version(self, app, db_session, faker): test_dna_account = DNAAccountFactory.build() template = TemplateFactory.build() initial_dna_account = template.created_by db_session.add(template) db_session.commit() test_name = faker.pystr() test_subject = "Test {{ variable }}" test_source = "{{ updated_variable }}" test_context = '{"updated_variable": "updated_test_value"}' template = UpdateTemplateService.execute( template=template, name=test_name, subject=test_subject, source=test_source, context=test_context, dna_account=test_dna_account, is_active=True, ) assert template.name == test_name assert template.subject == test_subject assert template.source == test_source assert template.context == test_context assert template.created_by == initial_dna_account assert template.updated_by == test_dna_account assert template.is_active assert template.versions assert template.versions[0].name == test_name assert template.versions[0].subject == test_subject assert template.versions[0].source == test_source assert template.versions[0].context == test_context assert template.versions[0].created_by == test_dna_account def test_success_same_version(self, app, db_session, faker): test_dna_account = DNAAccountFactory.build() template = TemplateFactory.build() initial_dna_account = template.created_by db_session.add(template) db_session.commit() test_name = faker.pystr() test_subject = "Test {{ variable }}" test_source = "{{ updated_variable }}" test_context = '{"updated_variable": "updated_test_value"}' template = UpdateTemplateService.execute( template=template, name=test_name, subject=test_subject, source=test_source, context=test_context, dna_account=test_dna_account, is_active=True, ) assert template.name == test_name assert template.subject == test_subject assert template.source == test_source assert template.context == test_context assert template.created_by == initial_dna_account assert template.updated_by == test_dna_account assert template.is_active assert template.versions assert template.versions[0].name == test_name assert template.versions[0].subject == test_subject assert template.versions[0].source == test_source assert template.versions[0].context == test_context assert template.versions[0].created_by == test_dna_account template = UpdateTemplateService.execute( template=template, name=test_name, subject=test_subject, source=test_source, context=test_context, dna_account=test_dna_account, is_active=True, ) assert len(template.versions) == 1 class TestRenderLiquidService: def test_correct_source_and_context(self, db_session): test_source = "{{ variable }}" test_context = {"test_variable": "test_value"} result = RenderLiquidService.execute( source=test_source, context=test_context ) expected_result = Template(test_source).render(test_context) assert result == expected_result def test_correct_source_and_incorrect_context(self, db_session): test_source = "{{ variable }}" test_context = None result = RenderLiquidService.execute( source=test_source, context=test_context ) expected_result = Template(test_source).render() assert result == expected_result def test_correct_incorrect_source_and_correct_context(self, db_session): test_source = "{{ variable " test_context = None result = RenderLiquidService.execute( source=test_source, context=test_context ) assert result == "" class TestSendTestEmailService: def test_success(self, db_session, faker, mocker, authorized_app, app): mocked_task = mocker.patch( "core_notifications.tasks.send_email_task.delay" ) mocked_analytics_log = mocker.patch( "core_notifications.analytics.emails_events.log" ) mocked_task.return_value.task_id = "test_id" mocked_task.return_value.task_name = "test_name" test_email = faker.email() template = TemplateFactory.build() db_session.add(template) db_session.commit() test_subject = RenderLiquidService.execute( template.subject, template.context ) test_body = RenderLiquidService.execute( template.source, template.context ) SendTestEmailService.execute(template, test_email) assert mocked_task.call_args_list == [ mocker.call( subject=test_subject, to=test_email, body=test_body, analytics={ "template_id": template.id, "template_name": template.name, "subject": test_subject, "user_id": app.config["ANALYTICS_USER_ID"], "user_name": app.config["ANALYTICS_USER_NAME"], }, analytic_url=url_for( "analytics.link_click", _external=True, ), track_clicks=True, ) ] assert mocked_analytics_log.call_args_list == [ mocker.call( SystemEvents.sending_email, id="test_id", template_id=template.id, template_name=template.name, to=test_email, subject=test_subject, user_id=app.config["ANALYTICS_USER_ID"], user_name=app.config["ANALYTICS_USER_NAME"], ) ] class TestEnableDisableTemplateService: def test_success(self, app, db_session, faker): test_dna_account = DNAAccountFactory.build() template = TemplateFactory.build() template = EnableDisableTemplateService.execute( template=template, is_active=True, dna_account=test_dna_account ) assert template.updated_by == test_dna_account assert template.is_active