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_run(): event = get_initialized_test_event() step = Step(actions=[ AddOrderLogEntry({ "order": { "variable": "order" }, "message": { "constant": "It Works." }, "message_identifier": { "constant": "test_run" }, }) ], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script") script.set_steps([step]) script.save() event.run() # The script is disabled by default, of course it won't run assert not event.variable_values["order"].log_entries.filter( identifier="test_run").exists() # Let's try that again. script.enabled = True script.save() event.run() assert event.variable_values["order"].log_entries.filter( identifier="test_run").exists() script.delete()
def test_run_multishop(): shop1 = factories.get_default_shop() shop2 = factories.get_shop(identifier="shop2") event = get_initialized_test_event() step = Step(actions=[ AddOrderLogEntry({ "order": { "variable": "order" }, "message": { "constant": "It Works." }, "message_identifier": { "constant": "test_run" }, }) ], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script", shop=shop2, enabled=True) script.set_steps([step]) script.save() # runs for shop1 - no script exists event.run(shop1) assert not event.variable_values["order"].log_entries.filter( identifier="test_run").exists() # run for shop2 - ok event.run(shop2) assert event.variable_values["order"].log_entries.filter( identifier="test_run").exists() script.delete()
def test_log_entry_on_unloggable_object(target_obj): event = get_initialized_test_event() event.variable_values["order"] = target_obj # invalidate log target _before_ creating context ctx = Context.from_event(event) n_log_entries = ctx.log_entry_queryset.count() ctx.add_log_entry_on_log_target("blap", "blorr") assert ctx.log_entry_queryset.count() == n_log_entries # couldn't add :(
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_log_entries(): event = get_initialized_test_event() ctx = Context.from_event(event) order = ctx.get("order") n_log_entries = ctx.log_entry_queryset.count() ctx.add_log_entry_on_log_target("blap", "blorr") order.add_log_entry("blep") assert ctx.log_entry_queryset.count() == n_log_entries + 2 # they got added assert order.log_entries.last().message == "blep" # it's what we added assert ctx.log_entry_queryset.last().message == "blep" # from this perspective too
def test_run(): event = get_initialized_test_event() step = Step(actions=[AddOrderLogEntry({ "order": {"variable": "order"}, "message": {"constant": "It Works."}, "message_identifier": {"constant": "test_run"}, })], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script", shop=factories.get_default_shop()) script.set_steps([step]) script.save() event.run(factories.get_default_shop()) # The script is disabled by default, of course it won't run assert not event.variable_values["order"].log_entries.filter(identifier="test_run").exists() # Let's try that again. script.enabled = True script.save() event.run(factories.get_default_shop()) assert event.variable_values["order"].log_entries.filter(identifier="test_run").exists() script.delete()
def test_run_multishop(): shop1 = factories.get_default_shop() shop2 = factories.get_shop(identifier="shop2") event = get_initialized_test_event() step = Step(actions=[AddOrderLogEntry({ "order": {"variable": "order"}, "message": {"constant": "It Works."}, "message_identifier": {"constant": "test_run"}, })], next=StepNext.STOP) script = Script(event_identifier=event.identifier, name="Test Script", shop=shop2, enabled=True) script.set_steps([step]) script.save() # runs for shop1 - no script exists event.run(shop1) assert not event.variable_values["order"].log_entries.filter(identifier="test_run").exists() # run for shop2 - ok event.run(shop2) assert event.variable_values["order"].log_entries.filter(identifier="test_run").exists() script.delete()
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_event_init(): assert get_initialized_test_event().variable_values
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()