Ejemplo n.º 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", 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)
def test_notify_on_company_created(regular_user, allow_company_registration):
    if "shuup.front.apps.customer_information" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.front.apps.customer_information required in installed apps")
    if "shuup.notify" not in settings.INSTALLED_APPS:
        pytest.skip("shuup.notify required in installed apps")

    configuration.set(None, "allow_company_registration", allow_company_registration)
    step = Step(
        cond_op=StepConditionOperator.NONE,
        actions=[
            AddNotification(
                {
                    "message": {"constant": "It Works. {{ customer_email }}"},
                    "message_identifier": {"constant": "company_created"},
                }
            )
        ],
        next=StepNext.STOP,
    )
    script = Script(
        event_identifier=CompanyAccountCreated.identifier, name="Test Script", enabled=True, shop=get_default_shop()
    )
    script.set_steps([step])
    script.save()

    assert not Notification.objects.filter(identifier="company_created").exists()

    assert get_person_contact(regular_user)
    assert not get_company_contact(regular_user)

    client = SmartClient()
    client.login(username=REGULAR_USER_USERNAME, password=REGULAR_USER_PASSWORD)
    company_edit_url = reverse("shuup:company_edit")

    if allow_company_registration:
        client.soup(company_edit_url)

        data = _default_company_data()
        data.update(_default_address_data("billing"))
        data.update(_default_address_data("shipping"))

        response, soup = client.response_and_soup(company_edit_url, data, "post")

        assert response.status_code == 302
        assert get_company_contact(regular_user)
        assert Notification.objects.filter(identifier="company_created").count() == 1
        notification = Notification.objects.filter(identifier="company_created").first()
        assert notification
        assert data["contact-email"] in notification.message

        # New save should not add new notifications
        response, soup = client.response_and_soup(company_edit_url, data, "post")
        assert response.status_code == 302
        assert Notification.objects.filter(identifier="company_created").count() == 1
        script.delete()
    else:
        response = client.get(company_edit_url)
        assert reverse("shuup:customer_edit") in response.url
        assert Notification.objects.filter(identifier="company_created").count() == 0
Ejemplo n.º 3
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