コード例 #1
0
def test_delayed_notifications(
        loan_validated_martigny, item2_lib_martigny,
        mailbox, patron_martigny, lib_martigny):
    """Test availability notification created from a loan."""
    mailbox.clear()
    loan = loan_validated_martigny
    # ensure an availability notification exists (possibly not yet sent)
    notification = get_notification(loan, NotificationType.AVAILABILITY)
    assert notification
    assert notification.loan_pid == loan_validated_martigny.get('pid')
    assert notification.item_pid == item2_lib_martigny.pid
    assert notification.patron_pid == patron_martigny.pid

    # ensure an at_desk notification exists (possibly not yet sent)
    notification = get_notification(loan, NotificationType.AT_DESK)
    assert notification
    assert notification.loan_pid == loan_validated_martigny.get('pid')
    assert notification.item_pid == item2_lib_martigny.pid
    flush_index(NotificationsSearch.Meta.index)

    assert not get_notification(loan, NotificationType.RECALL)
    for notification_type in NotificationType.ALL_NOTIFICATIONS:
        process_notifications(notification_type)
    assert loan.is_notified(notification_type=NotificationType.AVAILABILITY)
    assert loan.is_notified(notification_type=NotificationType.AT_DESK)

    # One notification will be sent : AVAILABILITY (sent to patron).
    # Get the last message from mailbox and check it.
    availability_msg = mailbox[-1]
    assert availability_msg.reply_to == lib_martigny.get('email')
    mailbox.clear()
コード例 #2
0
def test_availability_notification(
        loan_validated_martigny, item2_lib_martigny,
        mailbox, patron_martigny, lib_martigny):
    """Test availability notification created from a loan."""
    mailbox.clear()
    loan = loan_validated_martigny
    notification = get_notification(loan, NotificationType.AVAILABILITY)
    assert notification  # ensure a notification exists (possibly not yet sent)
    assert notification.loan_pid == loan_validated_martigny.get('pid')
    assert notification.item_pid == item2_lib_martigny.pid
    assert notification.patron_pid == patron_martigny.pid

    assert not get_notification(loan, NotificationType.RECALL)
    for notification_type in NotificationType.ALL_NOTIFICATIONS:
        process_notifications(notification_type)
    assert len(mailbox)
    assert loan.is_notified(notification_type=NotificationType.AVAILABILITY)

    message = mailbox[-1]
    assert message.reply_to == lib_martigny.get('email')
    mailbox.clear()
コード例 #3
0
def test_cancel_notifications(
    client, patron_martigny, lib_martigny, item_lib_martigny,
    librarian_martigny, loc_public_martigny, circulation_policies, mailbox
):
    """Test cancel notifications."""
    login_user_via_session(client, librarian_martigny.user)
    # CREATE and VALIDATE a request ...
    res, data = postdata(
        client,
        'api_item.librarian_request',
        dict(
            item_pid=item_lib_martigny.pid,
            pickup_location_pid=loc_public_martigny.pid,
            patron_pid=patron_martigny.pid,
            transaction_library_pid=lib_martigny.pid,
            transaction_user_pid=librarian_martigny.pid
        )
    )
    assert res.status_code == 200
    request_loan_pid = data.get(
        'action_applied')[LoanAction.REQUEST].get('pid')

    flush_index(NotificationsSearch.Meta.index)
    res, data = postdata(
        client,
        'api_item.validate_request',
        dict(
            pid=request_loan_pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid
        )
    )
    assert res.status_code == 200
    # At this time, an AVAILABILITY notification should be create but not yet
    # dispatched
    loan = Loan.get_record_by_pid(request_loan_pid)
    notification = get_notification(loan, NotificationType.AVAILABILITY)
    assert notification \
           and notification['status'] == NotificationStatus.CREATED

    # BORROW the requested item
    res, data = postdata(
        client,
        'api_item.checkout',
        dict(
            item_pid=item_lib_martigny.pid,
            patron_pid=patron_martigny.pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid,
        )
    )
    assert res.status_code == 200
    loan_pid = data.get(
        'action_applied')[LoanAction.CHECKOUT].get('pid')
    loan = Loan.get_record_by_pid(loan_pid)

    # Try to dispatch pending availability notifications.
    # As the item is now checkout, then the availability notification is not
    # yet relevant.
    mailbox.clear()
    process_notifications(NotificationType.AVAILABILITY)

    notification = get_notification(loan, NotificationType.AVAILABILITY)
    assert notification and \
           notification['status'] == NotificationStatus.CANCELLED
    assert len(mailbox) == 0

    # restore to initial state
    res, data = postdata(
        client,
        'api_item.checkin',
        dict(
            item_pid=item_lib_martigny.pid,
            # patron_pid=patron_martigny.pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid,
        )
    )
    assert res.status_code == 200
    mailbox.clear()

    # Test REMINDERS notifications.
    #   reminders notification check about the end_date. As the loan end_date
    #   is not yet over, the notification could be cancelled.

    notification = loan.create_notification(_type=NotificationType.DUE_SOON)[0]
    can_cancel, _ = notification.can_be_cancelled()
    assert not can_cancel
    process_notifications(NotificationType.DUE_SOON)
    notification = Notification.get_record_by_pid(notification.pid)
    assert notification['status'] == NotificationStatus.DONE
    flush_index(NotificationsSearch.Meta.index)

    # try to create a new DUE_SOON notification for the same loan
    record = {
        'creation_date': datetime.now(timezone.utc).isoformat(),
        'notification_type': NotificationType.DUE_SOON,
        'context': {
            'loan': {'$ref': get_ref_for_pid('loans', loan.pid)},
            'reminder_counter': 0
        }
    }
    notification = Notification.create(record)
    can_cancel, _ = notification.can_be_cancelled()
    assert can_cancel
    Dispatcher.dispatch_notifications([notification.pid])
    notification = Notification.get_record_by_pid(notification.pid)
    assert notification['status'] == NotificationStatus.CANCELLED
コード例 #4
0
def test_recall_notification_with_patron_additional_email_only(
        client, patron_sion_with_additional_email, lib_martigny,
        json_header, patron2_martigny,
        item3_lib_martigny, librarian_martigny,
        circulation_policies, loc_public_martigny,
        mailbox):
    """Test recall notification."""
    mailbox.clear()
    login_user_via_session(client, librarian_martigny.user)
    res, data = postdata(
        client,
        'api_item.checkout',
        dict(
            item_pid=item3_lib_martigny.pid,
            patron_pid=patron_sion_with_additional_email.pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid,
        )
    )
    assert res.status_code == 200
    loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')
    loan = Loan.get_record_by_pid(loan_pid)

    assert not get_notification(loan, NotificationType.RECALL)
    # test notification
    res, data = postdata(
        client,
        'api_item.librarian_request',
        dict(
            item_pid=item3_lib_martigny.pid,
            pickup_location_pid=loc_public_martigny.pid,
            patron_pid=patron2_martigny.pid,
            transaction_library_pid=lib_martigny.pid,
            transaction_user_pid=librarian_martigny.pid
        )
    )
    assert res.status_code == 200
    flush_index(NotificationsSearch.Meta.index)

    request_loan_pid = data.get(
        'action_applied')[LoanAction.REQUEST].get('pid')

    for notification_type in NotificationType.ALL_NOTIFICATIONS:
        process_notifications(notification_type)
    # one new email for the librarian
    assert mailbox[0].recipients == \
        [patron_sion_with_additional_email[
            'patron']['additional_communication_email']]
    mailbox.clear()
    params = {
        'transaction_location_pid': loc_public_martigny.pid,
        'transaction_user_pid': librarian_martigny.pid
    }
    # cancel request
    res, _ = postdata(
        client,
        'api_item.cancel_item_request',
        dict(
            item_pid=item3_lib_martigny.pid,
            pid=request_loan_pid,
            transaction_user_pid=librarian_martigny.pid,
            transaction_library_pid=lib_martigny.pid
        )
    )
    assert res.status_code == 200
    _, actions = item3_lib_martigny.checkin(**params)
コード例 #5
0
def test_recall_notification_without_email(
        client, patron_sion_without_email1, lib_martigny,
        json_header, patron2_martigny,
        item3_lib_martigny, librarian_martigny,
        circulation_policies, loc_public_martigny,
        mailbox):
    """Test recall notification."""
    mailbox.clear()
    login_user_via_session(client, librarian_martigny.user)
    res, data = postdata(
        client,
        'api_item.checkout',
        dict(
            item_pid=item3_lib_martigny.pid,
            patron_pid=patron_sion_without_email1.pid,
            transaction_location_pid=loc_public_martigny.pid,
            transaction_user_pid=librarian_martigny.pid,
        )
    )
    assert res.status_code == 200
    loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')
    loan = Loan.get_record_by_pid(loan_pid)

    assert not get_notification(loan, NotificationType.RECALL)
    # test notification
    res, data = postdata(
        client,
        'api_item.librarian_request',
        dict(
            item_pid=item3_lib_martigny.pid,
            pickup_location_pid=loc_public_martigny.pid,
            patron_pid=patron2_martigny.pid,
            transaction_library_pid=lib_martigny.pid,
            transaction_user_pid=librarian_martigny.pid
        )
    )
    request_loan_pid = data.get(
        'action_applied')[LoanAction.REQUEST].get('pid')
    assert res.status_code == 200
    flush_index(NotificationsSearch.Meta.index)

    notification = get_notification(loan, NotificationType.RECALL)
    assert notification and notification.loan_pid == loan.pid
    assert not get_notification(loan, NotificationType.AVAILABILITY)

    for notification_type in NotificationType.ALL_NOTIFICATIONS:
        process_notifications(notification_type)
    # one new email for the librarian
    recipient = lib_martigny.get_email(notification['notification_type'])
    assert recipient
    assert mailbox[0].recipients == [recipient]
    # check the address block
    assert patron2_martigny.dumps()['street'] in mailbox[0].body
    mailbox.clear()
    params = {
        'transaction_location_pid': loc_public_martigny.pid,
        'transaction_user_pid': librarian_martigny.pid
    }
    # cancel request
    res, _ = postdata(
        client,
        'api_item.cancel_item_request',
        dict(
            item_pid=item3_lib_martigny.pid,
            pid=request_loan_pid,
            transaction_user_pid=librarian_martigny.pid,
            transaction_library_pid=lib_martigny.pid
        )
    )
    assert res.status_code == 200
    _, actions = item3_lib_martigny.checkin(**params)
コード例 #6
0
def test_recall_notification(client, patron_sion, lib_sion,
                             json_header, patron2_martigny,
                             item_lib_sion, librarian_sion,
                             circulation_policies, loc_public_sion,
                             mailbox):
    """Test recall notification."""
    mailbox.clear()
    login_user_via_session(client, librarian_sion.user)
    res, data = postdata(
        client,
        'api_item.checkout',
        dict(
            item_pid=item_lib_sion.pid,
            patron_pid=patron_sion.pid,
            transaction_location_pid=loc_public_sion.pid,
            transaction_user_pid=librarian_sion.pid,
        )
    )
    assert res.status_code == 200
    loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')
    loan = Loan.get_record_by_pid(loan_pid)

    assert not get_notification(loan, NotificationType.RECALL)
    # test notification permissions
    res, data = postdata(
        client,
        'api_item.librarian_request',
        dict(
            item_pid=item_lib_sion.pid,
            pickup_location_pid=loc_public_sion.pid,
            patron_pid=patron2_martigny.pid,
            transaction_library_pid=lib_sion.pid,
            transaction_user_pid=librarian_sion.pid
        )
    )
    assert res.status_code == 200

    request_loan_pid = data.get(
        'action_applied')[LoanAction.REQUEST].get('pid')
    request_loan = Loan.get_record_by_pid(request_loan_pid)
    flush_index(NotificationsSearch.Meta.index)

    notification = get_notification(loan, NotificationType.RECALL)
    assert notification and notification.loan_pid == loan.pid
    assert not get_notification(loan, NotificationType.AVAILABILITY)
    assert not get_notification(request_loan, NotificationType.REQUEST)
    assert not len(mailbox)

    for notification_type in NotificationType.ALL_NOTIFICATIONS:
        process_notifications(notification_type)
    # one new email for the patron
    assert mailbox[-1].recipients == [patron_sion.dumps()['email']]
    assert loan.is_notified(notification_type=NotificationType.RECALL)
    mailbox.clear()

    # cancel request
    res, _ = postdata(
        client,
        'api_item.cancel_item_request',
        dict(
            item_pid=item_lib_sion.pid,
            pid=request_loan_pid,
            transaction_user_pid=librarian_sion.pid,
            transaction_location_pid=loc_public_sion.pid
        )
    )
    assert res.status_code == 200

    # no new notification is send for the second time
    res, _ = postdata(
        client,
        'api_item.librarian_request',
        dict(
            item_pid=item_lib_sion.pid,
            pickup_location_pid=loc_public_sion.pid,
            patron_pid=patron2_martigny.pid,
            transaction_library_pid=lib_sion.pid,
            transaction_user_pid=librarian_sion.pid
        )
    )
    assert res.status_code == 200
    flush_index(NotificationsSearch.Meta.index)

    assert not loan.is_notified(
        notification_type=NotificationType.RECALL, counter=1)
    assert not loan.is_notified(
        notification_type=NotificationType.AVAILABILITY)
    assert not get_notification(loan, NotificationType.AVAILABILITY)
    assert not get_notification(request_loan, NotificationType.REQUEST)
    assert not request_loan.is_notified(
        notification_type=NotificationType.REQUEST)
    assert len(mailbox) == 0
コード例 #7
0
def test_reminder_notifications_after_extend(
    item_lib_martigny, patron_martigny, loc_public_martigny,
    librarian_martigny, circulation_policies, mailbox, client
):
    """Test any reminder notification could be resend after loan extension."""

    # STEP 1 - CREATE BASIC RESOURCES FOR THE TEST
    #   * Create a loan and update it to be considerate as "due soon".
    #   * Run the `notification-creation` task to create a DUE_SOON
    #     notification
    params = {
        'patron_pid': patron_martigny.pid,
        'transaction_location_pid': loc_public_martigny.pid,
        'transaction_user_pid': librarian_martigny.pid,
        'pickup_location_pid': loc_public_martigny.pid
    }
    item, loan = item_record_to_a_specific_loan_state(
        item=item_lib_martigny,
        loan_state=LoanState.ITEM_ON_LOAN,
        params=params, copy_item=True)

    # get the related cipo and check than an due_soon reminder exists
    cipo = get_circ_policy(loan)
    due_soon_reminder = cipo.get_reminder(DUE_SOON_REMINDER_TYPE)
    assert due_soon_reminder

    # Update the loan
    delay = due_soon_reminder.get('days_delay') - 1
    due_soon_date = datetime.now() - timedelta(days=delay)
    end_date = datetime.now() + timedelta(days=1)
    loan['due_soon_date'] = due_soon_date.astimezone(pytz.utc).isoformat()
    loan['end_date'] = end_date.astimezone(pytz.utc).isoformat()
    loan = loan.update(loan, dbcommit=True, reindex=True)
    assert loan.is_loan_due_soon()

    # run the create notification task and process notification.
    mailbox.clear()
    create_notifications(types=[NotificationType.DUE_SOON])
    process_notifications(NotificationType.DUE_SOON)

    first_notification = get_notification(loan, NotificationType.DUE_SOON)
    assert first_notification \
           and first_notification['status'] == NotificationStatus.DONE
    assert len(mailbox) == 1
    counter = NotificationsSearch()\
        .filter('term', context__loan__pid=loan.pid)\
        .filter('term', notification_type=NotificationType.DUE_SOON)\
        .count()
    assert counter == 1

    # STEP 2 - CHECK NOTIFICATIONS CREATION
    #   Run the `create_notification` task for DUE_SOON notification type.
    #   As a notification already exists, no new DUE_SOON#1 notifications
    #   should be created
    create_notifications(types=[NotificationType.DUE_SOON])
    query = NotificationsSearch() \
        .filter('term', context__loan__pid=loan.pid) \
        .filter('term', notification_type=NotificationType.DUE_SOON) \
        .source('pid').scan()
    notification_pids = [hit.pid for hit in query]
    assert len(notification_pids) == 1
    assert notification_pids[0] == first_notification.pid

    # STEP 3 - EXTEND THE LOAN
    #   * User has received the DUE_SOON message and extend the loan.
    #   * Get the new 'due_soon_date' it will be used later to create
    #     notifications
    login_user_via_session(client, librarian_martigny.user)
    params = dict(
        item_pid=item.pid,
        transaction_user_pid=librarian_martigny.pid,
        transaction_location_pid=loc_public_martigny.pid
    )
    res, _ = postdata(client, 'api_item.extend_loan', params)
    assert res.status_code == 200
    loan = Loan.get_record_by_pid(loan.pid)
    due_soon_date = ciso8601.parse_datetime(loan.get('due_soon_date'))

    # STEP 4 - CHECK NOTIFICATIONS CREATION
    #    Run again the `create_notification` task, again for DUE_SOON
    #    notification type. As the loan is extended, a new DUE_SOON
    #    notification should be created about this loan.
    #    Process the notification, check that this new notification isn't
    #    cancelled and well processed.
    process_date = due_soon_date + timedelta(days=1)
    create_notifications(
        types=[NotificationType.DUE_SOON],
        tstamp=process_date
    )
    counter = NotificationsSearch() \
        .filter('term', context__loan__pid=loan.pid) \
        .filter('term', notification_type=NotificationType.DUE_SOON) \
        .count()
    assert counter == 2
    process_notifications(NotificationType.DUE_SOON)
    assert len(mailbox) == 2
    second_notification = get_notification(loan, NotificationType.DUE_SOON)
    assert second_notification \
           and second_notification['status'] == NotificationStatus.DONE
    assert second_notification.pid != first_notification