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_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)
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_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_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
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_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"]
def test_template_in_action(): ac = TestTemplateUsingAction(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 = TestUnilingualTemplateUsingAction(data={"template_data": TEST_UNI_TEMPLATE_DATA}) assert name in ac.get_template_values(context)["subject"]
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_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_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)
def get_test_template(): ctx = Context.from_variables(name=u"Sir Test") template = Template(ctx, TEST_TEMPLATE_DATA) return template
def test_templated_binding_syntax_errors_swallowed(): tb = TemplatedBinding("z", constant_use=ConstantUse.CONSTANT_ONLY) assert tb.get_value(Context(), {"constant": "{{"}) == "{{"