def test_email_action(): if settings.EMAIL_BACKEND != 'django.core.mail.backends.locmem.EmailBackend': pytest.skip("Need locmem email backend") mail.outbox = [] # Clear the Django testing mail outbox event = get_initialized_test_event() ctx = Context.from_event(event, shop=factories.get_default_shop()) ctx.set( "name", "Luke Warm" ) # This variable isn't published by the event, but it's used by the template se = SendEmail({ "template_data": TEST_TEMPLATE_DATA, "from_email": { "constant": "*****@*****.**" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "ja" }, "send_identifier": { "constant": "hello, hello, hello" } }) se.execute(ctx) # Once, se.execute(ctx) # Twice! assert len( mail.outbox) == 1 # 'send_identifier' should ensure this is true msg = mail.outbox[0] assert msg.to == ['*****@*****.**'] assert msg.from_email == '*****@*****.**' assert ctx.get("name").upper( ) in msg.subject # The Japanese template upper-cases the name
def test_email_action_with_template_body(): with override_settings(LANGUAGES=(("en", "en"))): SUPER_TEST_TEMPLATE_DATA = { "en": { # English "subject": "Hello, {{ name }}!", "body_template": "<html><style>.dog-color { color: red; }</style><body>%html_body%</body></html>", "body": "Hi, {{ name }}. This is a test.", "content_type": "plain" } } if settings.EMAIL_BACKEND != 'django.core.mail.backends.locmem.EmailBackend': pytest.skip("Need locmem email backend") mail.outbox = [] # Clear the Django testing mail outbox event = get_initialized_test_event() ctx = Context.from_event(event, shop=factories.get_default_shop()) ctx.set("name", "Luke J. Warm") # This variable isn't published by the event, but it's used by the template se = SendEmail({ "template_data": SUPER_TEST_TEMPLATE_DATA, "from_email": {"constant": "*****@*****.**"}, "recipient": {"constant": "*****@*****.**"}, "language": {"constant": "ja"}, }) se.execute(ctx) # Once assert len(mail.outbox) == 1 # 'send_identifier' should ensure this is true msg = mail.outbox[0] assert msg.to == ['*****@*****.**'] assert msg.from_email == '*****@*****.**' assert ".dog-color { color: red; }" in msg.body assert "Luke J. Warm" in msg.body
def test_notify_item_admin_form(): event_class = ATestEvent script_item = SendEmail({ "send_identifier": { "constant": "hello" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "en" }, }) form = ScriptItemEditForm(event_class=event_class, script_item=script_item, data={ "b_recipient_c": "*****@*****.**", "b_language_c": "en", "b_send_identifier_c": "hello", }) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert form.is_valid() form.save() assert script_item.data["recipient"] == { "constant": "*****@*****.**" }
def test_notify_item_admin_form(rf, admin_user): event_class = ATestEvent script_item = SendEmail({ "send_identifier": { "constant": "hello" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "en" }, }) send_data = { "b_recipient_c": "*****@*****.**", "b_language_c": "en", "b_message_c": "Message", "b_send_identifier_c": "hello", "t_en_subject": "Welcome!", "t_ja_subject": "Konnichiwa!", "t_ja_body": "Bye", "t_en_content_type": "html", } form = ScriptItemEditForm(event_class=event_class, script_item=script_item, data=send_data) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert not form.is_valid() # Missing template body for default language with pytest.raises(forms.ValidationError): form.save() send_data.update({"t_en_body": "ok now this should pass"}) form = ScriptItemEditForm(event_class=event_class, script_item=script_item, data=send_data) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert form.is_valid() form.save() assert script_item.data["template_data"]["en"]["subject"] == "Welcome!" assert script_item.data["template_data"]["ja"]["subject"] == "Konnichiwa!" assert script_item.data["recipient"] == { "constant": "*****@*****.**" } send_data["b_recipient_c"] = admin_user.pk send_data["init_data"] = json.dumps({ "eventIdentifier": "order_received", "itemType": "action", "data": { "identifier": "add_notification" } }) view = script_item_editor request = apply_request_middleware(rf.post("/", data=send_data), user=admin_user) response = view(request) assert response.status_code == 200 # Assert no errors have occurred
def test_email_action(): if settings.EMAIL_BACKEND != 'django.core.mail.backends.locmem.EmailBackend': pytest.skip("Need locmem email backend") mail.outbox = [] # Clear the Django testing mail outbox event = get_initialized_test_event() ctx = Context.from_event(event) ctx.set("name", "Luke Warm") # This variable isn't published by the event, but it's used by the template se = SendEmail({ "template_data": TEST_TEMPLATE_DATA, "recipient": {"constant": "*****@*****.**"}, "language": {"constant": "ja"}, "send_identifier": {"constant": "hello, hello, hello"} }) se.execute(ctx) # Once, se.execute(ctx) # Twice! assert len(mail.outbox) == 1 # 'send_identifier' should ensure this is true msg = mail.outbox[0] assert msg.to == ['*****@*****.**'] assert ctx.get("name").upper() in msg.subject # The Japanese template upper-cases the name
def test_notify_item_admin_form(rf, admin_user): event_class = ATestEvent script_item = SendEmail({ "send_identifier": { "constant": "hello" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "en" }, }) send_data = { "b_recipient_c": "*****@*****.**", "b_language_c": "en", "b_message_c": "Message", "b_send_identifier_c": "hello", } form = ScriptItemEditForm(event_class=event_class, script_item=script_item, data=send_data) initial = form.get_initial() assert initial["b_send_identifier_c"] == "hello" assert form.is_valid() form.save() assert script_item.data["recipient"] == { "constant": "*****@*****.**" } send_data["b_recipient_c"] = admin_user.pk send_data['init_data'] = json.dumps({ "eventIdentifier": "order_received", "itemType": "action", "data": { "identifier": "add_notification" } }) view = script_item_editor request = apply_request_middleware(rf.post("/", data=send_data), user=admin_user) response = view(request) assert response.status_code == 200 # Assert no errors have occurred
def test_email_action_with_template_body(): with override_settings(LANGUAGES=(("en", "en"))): email_template = EmailTemplate.objects.create( name="template 1", template= "<html><style>.dog-color { color: red; }</style><body>%html_body%</body></html>" ) SUPER_TEST_TEMPLATE_DATA = { "en": { # English "subject": "Hello, {{ name }}!", "email_template": str(email_template.pk), "body": "Hi, {{ name }}. This is a test & it works.", "content_type": "plain", } } if settings.EMAIL_BACKEND != "django.core.mail.backends.locmem.EmailBackend": pytest.skip("Need locmem email backend") mail.outbox = [] # Clear the Django testing mail outbox with mock.patch.object(notification_email_before_send, "send") as mocked_method_1: event = get_initialized_test_event() ctx = Context.from_event(event, shop=factories.get_default_shop()) ctx.set( "name", "John Smith" ) # This variable isn't published by the event, but it's used by the template se = SendEmail({ "template_data": SUPER_TEST_TEMPLATE_DATA, "from_email": { "constant": "*****@*****.**" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "ja" }, }) assert ctx.event_identifier == "test_event" se.execute(ctx) mail.outbox[0].body == "Hi, John Smith. This is a test & it works." mocked_method_1.assert_called() mail.outbox = [] # Clear the Django testing mail outbox with mock.patch.object(notification_email_sent, "send") as mocked_method_2: event = get_initialized_test_event() ctx = Context.from_event(event, shop=factories.get_default_shop()) ctx.set( "name", "Luke J. Warm" ) # This variable isn't published by the event, but it's used by the template se = SendEmail({ "template_data": SUPER_TEST_TEMPLATE_DATA, "from_email": { "constant": "*****@*****.**" }, "recipient": { "constant": "*****@*****.**" }, "language": { "constant": "ja" }, }) se.execute(ctx) # Once assert len(mail.outbox ) == 1 # 'send_identifier' should ensure this is true msg = mail.outbox[0] assert msg.to == ["*****@*****.**"] assert msg.from_email == "*****@*****.**" assert ".dog-color { color: red; }" in msg.body assert "Luke J. Warm" in msg.body mocked_method_2.assert_called()