Exemple #1
0
def test_notification(admin_user, specific_user):
    AddNotification(make_bind_data(
        variables={"priority": "priority"},
        constants={
            "message": "Hi {{ name }}!",
            "message_identifier": "hi mom",
            "url": "http://burymewithmymoney.com/",
            "recipient_type": (RecipientType.SPECIFIC_USER if specific_user else RecipientType.ADMINS),
            "recipient": (admin_user if specific_user else None),
            "priority": Priority.CRITICAL
        }
    )).execute(Context.from_variables(name="Justin Case"))
    notif = Notification.objects.last()
    assert isinstance(notif, Notification)
    if specific_user:
        assert notif.recipient == admin_user
        assert Notification.objects.unread_for_user(admin_user).get(pk=notif.pk)
    assert notif.identifier == "hi mom"
    assert notif.message == "Hi Justin Case!"
    assert notif.priority == Priority.CRITICAL
    assert notif.url == "http://burymewithmymoney.com/"
    with pytest.raises(ValueError):
        notif.url = "http://www.theuselessweb.com/"

    assert not notif.is_read
    notif.mark_read(admin_user)  # Once, for setting
    notif.mark_read(admin_user)  # Twice, for branch checking
    assert notif.marked_read_by == admin_user
    assert very_recently(notif.marked_read_on)
Exemple #2
0
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
Exemple #3
0
def test_notification(admin_user, specific_user):
    AddNotification(make_bind_data(
        variables={"priority": "priority"},
        constants={
            "message": "Hi {{ name }}!",
            "message_identifier": "hi mom",
            "url": "http://burymewithmymoney.com/",
            "recipient_type": (RecipientType.SPECIFIC_USER if specific_user else RecipientType.ADMINS),
            "recipient": (admin_user if specific_user else None),
            "priority": Priority.CRITICAL
        }
    )).execute(Context.from_variables(name="Justin Case", shop=factories.get_default_shop()))
    notif = Notification.objects.last()
    assert isinstance(notif, Notification)
    if specific_user:
        assert notif.recipient == admin_user
        assert Notification.objects.unread_for_user(admin_user).get(pk=notif.pk)
    assert notif.identifier == "hi mom"
    assert notif.message == "Hi Justin Case!"
    assert notif.priority == Priority.CRITICAL
    assert notif.url == "http://burymewithmymoney.com/"
    with pytest.raises(ValueError):
        notif.url = "http://www.theuselessweb.com/"

    assert not notif.is_read
    notif.mark_read(admin_user)  # Once, for setting
    notif.mark_read(admin_user)  # Twice, for branch checking
    assert notif.marked_read_by == admin_user
    assert very_recently(notif.marked_read_on)
Exemple #4
0
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, shop=factories.get_default_shop())
    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 :(
Exemple #5
0
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
Exemple #6
0
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 :(
Exemple #7
0
def test_basic_exec():
    script = get_test_script()

    # `en` is not in the conditions
    context = Context.from_variables(order_language="en")
    script.execute(context)
    assert not context.get("success")

    # `fi` is matched by the first condition and cond_op is 'or'
    context = Context.from_variables(order_language="fi")
    script.execute(context)
    assert context.get("success")

    # `ja` is matched by the other condition, and cond_op is 'or'
    context = Context.from_variables(order_language="ja")
    script.execute(context)
    assert context.get("success")
Exemple #8
0
def test_none_condop():
    step = Step(cond_op=StepConditionOperator.NONE, conditions=[
        NonEmpty({"v": {"variable": "a"}}),
        NonEmpty({"v": {"variable": "b"}}),
    ], actions=[SetDebugFlag({})])
    context = Context.from_variables(a=False, b=False)
    step.execute(context)
    assert context.get("debug")
Exemple #9
0
def test_basic_exec():
    script = get_test_script()

    # `en` is not in the conditions
    context = Context.from_variables(order_language="en")
    script.execute(context)
    assert not context.get("success")

    # `fi` is matched by the first condition and cond_op is 'or'
    context = Context.from_variables(order_language="fi")
    script.execute(context)
    assert context.get("success")

    # `ja` is matched by the other condition, and cond_op is 'or'
    context = Context.from_variables(order_language="ja")
    script.execute(context)
    assert context.get("success")
Exemple #10
0
def test_none_condop():
    step = Step(cond_op=StepConditionOperator.NONE, conditions=[
        NonEmpty({"v": {"variable": "a"}}),
        NonEmpty({"v": {"variable": "b"}}),
    ], actions=[SetDebugFlag({})])
    context = Context.from_variables(a=False, b=False)
    step.execute(context)
    assert context.get("debug")
Exemple #11
0
def test_disabled_steps():
    script = get_test_script()
    steps = script.get_steps()
    steps[0].enabled = False
    script.set_steps(steps)
    # Disabled steps don't run
    context = Context.from_variables()
    script.execute(context)
    assert not context.get("success")
Exemple #12
0
def test_misconfigured_add_notification_is_noop():
    n_notifs = Notification.objects.count()
    AddNotification(
        make_bind_data(
            constants={
                "recipient_type": RecipientType.SPECIFIC_USER,
                "message": "This'll never get delivered!",
            })).execute(Context())
    assert Notification.objects.count() == n_notifs
Exemple #13
0
def test_disabled_steps():
    script = get_test_script()
    steps = script.get_steps()
    steps[0].enabled = False
    script.set_steps(steps)
    # Disabled steps don't run
    context = Context.from_variables()
    script.execute(context)
    assert not context.get("success")
Exemple #14
0
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
Exemple #15
0
def test_log_entries():
    event = get_initialized_test_event()
    ctx = Context.from_event(event, shop=factories.get_default_shop())
    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
Exemple #16
0
def test_template_in_action():
    ac = ATestTemplateUsingAction(data={"template_data": TEST_TEMPLATE_DATA})
    context = Context.from_variables(name=u"Sir Test")
    template = ac.get_template(context)
    test_template_render(template)
    japanese_render = ac.get_template_values(context, ("ja",))
    name = template.context.get("name")
    assert name.upper() in japanese_render["body"]
    ac = ATestUnilingualTemplateUsingAction(data={"template_data": TEST_UNI_TEMPLATE_DATA})
    assert name in ac.get_template_values(context)["subject"]
Exemple #17
0
def test_template_in_action():
    ac = ATestTemplateUsingAction(data={"template_data": TEST_TEMPLATE_DATA})
    context = Context.from_variables(name=u"Sir Test")
    template = ac.get_template(context)
    test_template_render(template)
    japanese_render = ac.get_template_values(context, ("ja",))
    name = template.context.get("name")
    assert name.upper() in japanese_render["body"]
    ac = ATestUnilingualTemplateUsingAction(data={"template_data": TEST_UNI_TEMPLATE_DATA})
    assert name in ac.get_template_values(context)["subject"]
Exemple #18
0
def test_render_template():
    step = Step(
        conditions=(),
        actions=[Action.unserialize(action) for action in TEST_STEP_ACTIONS],
    )
    assert step

    execution_context = Context(variables={
        "customer_phone": "0594036495",
        "language": "fi",
        "customer_email": "*****@*****.**"
    })

    step.execute(context=execution_context)
Exemple #19
0
def test_condops(cond_op):
    step = Step(cond_op=cond_op, conditions=[
        NonEmpty({"v": {"variable": "a"}}),
        NonEmpty({"v": {"variable": "b"}}),
    ], actions=[SetDebugFlag({})])
    context = Context.from_variables(a=True, b=False)
    step.execute(context)
    if cond_op == StepConditionOperator.ALL:
        assert not context.get("debug")
    elif cond_op == StepConditionOperator.ANY:
        assert context.get("debug")
    elif cond_op == StepConditionOperator.NONE:
        assert not context.get("debug")
    else:
        raise ValueError("Unexpected condop %r" % cond_op)
Exemple #20
0
def test_condops(cond_op):
    step = Step(cond_op=cond_op, conditions=[
        NonEmpty({"v": {"variable": "a"}}),
        NonEmpty({"v": {"variable": "b"}}),
    ], actions=[SetDebugFlag({})])
    context = Context.from_variables(a=True, b=False)
    step.execute(context)
    if cond_op == StepConditionOperator.ALL:
        assert not context.get("debug")
    elif cond_op == StepConditionOperator.ANY:
        assert context.get("debug")
    elif cond_op == StepConditionOperator.NONE:
        assert not context.get("debug")
    else:
        raise ValueError("Unexpected condop %r" % cond_op)
Exemple #21
0
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
Exemple #22
0
def get_test_template():
    ctx = Context.from_variables(name=u"Sir Test")
    template = Template(ctx, TEST_TEMPLATE_DATA)
    return template
Exemple #23
0
def test_conditionless_step_executes():
    step = Step(actions=[SetDebugFlag({})])
    context = Context()
    step.execute(context)
    assert context.get("debug")
Exemple #24
0
def test_binding_fallthrough():
    ctx = Context.from_variables()
    b = Binding("x", default="foo")
    assert b.get_value(ctx, {"variable": "var"}) == "foo"
    assert b.get_value(ctx, {}) == "foo"
Exemple #25
0
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 &amp; 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()
Exemple #26
0
def test_empty():
    ie = Empty({"v": {"variable": "v"}})
    assert ie.test(Context.from_variables(v=False))
    assert ie.test(Context.from_variables(v=()))
    assert ie.test(Context.from_variables(v=0))
    assert not ie.test(Context.from_variables(v=6))
def get_test_template():
    ctx = Context.from_variables(name=u"Sir Test")
    template = Template(ctx, TEST_TEMPLATE_DATA)
    return template
Exemple #28
0
def test_boolean():
    ie = BooleanEqual({"v1": {"variable": "var1"}, "v2": {"variable": "var2"}})
    assert ie.test(Context.from_variables(var1=False, var2=False))
    assert ie.test(Context.from_variables(var1=False, var2=None))
    assert ie.test(Context.from_variables(var1=True, var2=True))
    assert not ie.test(Context.from_variables(var1=True, var2=False))
    assert not ie.test(Context.from_variables(var1=False, var2=True))

    ie = BooleanEqual({"v1": {"variable": "v"}, "v2": {"constant": None}})
    assert ie.test(Context.from_variables(v=False))
    assert ie.test(Context.from_variables(v=None))
    assert not ie.test(Context.from_variables(v=True))

    ie = BooleanEqual({"v1": {"variable": "v"}, "v2": {"constant": True}})
    assert not ie.test(Context.from_variables(v=False))
    assert not ie.test(Context.from_variables(v=None))
    assert ie.test(Context.from_variables(v=True))
Exemple #29
0
def test_integer_equals():
    ie = IntegerEqual({"v1": {"variable": "v"}, "v2": {"constant": 42}})
    assert ie.test(Context.from_variables(v=42))
    assert ie.test(Context.from_variables(v="42"))
    assert not ie.test(Context.from_variables(v="442"))
    assert not ie.test(Context.from_variables(v=True))
Exemple #30
0
def test_empty():
    ie = Empty({"v": {"variable": "v"}})
    assert ie.test(Context.from_variables(v=False))
    assert ie.test(Context.from_variables(v=()))
    assert ie.test(Context.from_variables(v=0))
    assert not ie.test(Context.from_variables(v=6))
Exemple #31
0
def test_non_empty():
    ie = NonEmpty({"v": {"variable": "v"}})
    assert ie.test(Context.from_variables(v=True))
    assert not ie.test(Context.from_variables(v=""))
    assert not ie.test(Context.from_variables(v=0))
Exemple #32
0
def test_text_equal():
    ie = TextEqual({"v1": {"variable": "v"}, "v2": {"constant": "   Foo   "}})
    assert ie.test(Context.from_variables(v="foo"))
    assert ie.test(Context.from_variables(v="Foo"))
    assert ie.test(Context.from_variables(v="Foo  "))
    assert not ie.test(Context.from_variables(v="faa"))
Exemple #33
0
def test_integer_equals():
    ie = IntegerEqual({"v1": {"variable": "v"}, "v2": {"constant": 42}})
    assert ie.test(Context.from_variables(v=42))
    assert ie.test(Context.from_variables(v="42"))
    assert not ie.test(Context.from_variables(v="442"))
    assert not ie.test(Context.from_variables(v=True))
Exemple #34
0
def test_non_empty():
    ie = NonEmpty({"v": {"variable": "v"}})
    assert ie.test(Context.from_variables(v=True))
    assert not ie.test(Context.from_variables(v=""))
    assert not ie.test(Context.from_variables(v=0))
Exemple #35
0
def test_text_equal():
    ie = TextEqual({"v1": {"variable": "v"}, "v2": {"constant": "   Foo   "}})
    assert ie.test(Context.from_variables(v="foo"))
    assert ie.test(Context.from_variables(v="Foo"))
    assert ie.test(Context.from_variables(v="Foo  "))
    assert not ie.test(Context.from_variables(v="faa"))
Exemple #36
0
def test_conditionless_step_executes():
    step = Step(actions=[SetDebugFlag({})])
    context = Context()
    step.execute(context)
    assert context.get("debug")
Exemple #37
0
def test_templated_binding_syntax_errors_swallowed():
    tb = TemplatedBinding("z", constant_use=ConstantUse.CONSTANT_ONLY)
    assert tb.get_value(Context(), {"constant": "{{"}) == "{{"
Exemple #38
0
def test_binding_fallthrough():
    ctx = Context.from_variables()
    b = Binding("x", default="foo")
    assert b.get_value(ctx, {"variable": "var"}) == "foo"
    assert b.get_value(ctx, {}) == "foo"