def send_email_after_loan_change(_, initial_loan, loan, trigger):
    """Send email notification when the loan changes."""
    send_loan_mail(
        action=trigger,
        loan=loan,
        message_ctx=dict(initial_loan=initial_loan),
    )
def test_send_only_to_test_recipients(app, users, testdata, mocker):
    """Tests that send only to test recipients works."""
    class TestMessage(BlockTemplatedMessage):
        def __init__(self, *args, **kwargs):
            template = "tests/subject_body_html.html"
            kwargs.pop("trigger", None)
            kwargs.pop("message_ctx", {})
            kwargs.setdefault("sender", app.config["MAIL_NOTIFY_SENDER"])
            super().__init__(template, **kwargs)

    app.config.update(
        dict(CELERY_TASK_ALWAYS_EAGER=True,
             ILS_MAIL_LOAN_MSG_LOADER=TestMessage,
             ILS_MAIL_ENABLE_TEST_RECIPIENTS=True))
    patron = Patron(users["patron1"].id)
    mocker.patch(
        "invenio_app_ils.records.api.Patron.get_patron",
        return_value=patron,
    )
    loan_data = testdata["loans"][-1]
    loan = Loan.get_record_by_pid(loan_data["pid"])
    fake_recipients = app.config["ILS_MAIL_NOTIFY_TEST_RECIPIENTS"]
    with app.extensions["mail"].record_messages() as outbox:
        assert len(outbox) == 0
        send_loan_mail("trigger", loan, subject="Test", body="Test")
        assert len(outbox) == 1
        assert outbox[0].recipients == fake_recipients
def test_log_successful_mail_task(app, testdata, mocker, users):
    """Test that a successfully sent email is logged."""
    app.config.update(CELERY_TASK_ALWAYS_EAGER=True)
    mocker.patch(
        "invenio_app_ils.records.api.Patron.get_patron",
        return_value=Patron(users["patron1"].id),
    )
    succ = mocker.patch("invenio_app_ils.mail.tasks.log_successful_mail")
    loan = testdata["loans"][0]

    assert not succ.s.called
    send_loan_mail("extend", loan)
    assert succ.s.called
def test_log_error_mail_task(app, testdata, mocker, users):
    """Test that an error is logged on email send error."""
    prev_sender = app.config["MAIL_NOTIFY_SENDER"]
    app.config.update(
        dict(CELERY_TASK_ALWAYS_EAGER=True, MAIL_NOTIFY_SENDER=[]))
    mocker.patch(
        "invenio_app_ils.records.api.Patron.get_patron",
        return_value=Patron(users["patron1"].id),
    )
    err = mocker.patch("invenio_app_ils.mail.tasks.log_error_mail")
    loan = testdata["loans"][0]

    assert not err.s.called
    send_loan_mail("extend", loan)
    assert err.s.called

    # restore
    app.config.update(MAIL_NOTIFY_SENDER=prev_sender)
Exemple #5
0
def send_email_after_loan_change(_, prev_loan, loan, trigger):
    """Send email notification when the loan changes."""
    send_loan_mail(trigger, loan, message_ctx=dict(prev_loan=prev_loan))