Exemplo n.º 1
0
async def test_update_payment_record(
        app, session, test_name, action_code, start_request_state,
        end_request_state, start_priority, end_priority, start_datetime,
        days_after_start_datetime, start_payment_state, end_payment_state,
        start_payment_date, end_payment_has_value, error):
    """Assert that the update_payment_record works as expected."""
    from namex.models import Request, State, Payment
    from namex_pay.worker import update_payment_record

    print(test_name)

    now = datetime.utcnow()

    with freeze_time(now):
        # setup
        PAYMENT_TOKEN = 'dog'
        NR_NUMBER = 'NR B000001'
        name_request = Request()
        name_request.nrNum = NR_NUMBER
        name_request.stateCd = start_request_state
        name_request._source = 'NRO'
        name_request.expirationDate = start_datetime
        name_request.priorityCd = start_priority
        name_request.save_to_db()

        payment = Payment()
        payment.nrId = name_request.id
        payment._payment_token = PAYMENT_TOKEN
        payment._payment_status_code = start_payment_state
        payment.payment_action = action_code
        payment.furnished = False
        payment._payment_completion_date = start_payment_date
        payment.save_to_db()

        # run test
        if error:  # expecting it to raise an error
            with pytest.raises(error):
                await update_payment_record(payment)
        else:
            # else it was processable
            if not (payment_final := await update_payment_record(payment)):
                payment_final = payment

            nr_final = Request.find_by_nr(NR_NUMBER)

            assert nr_final.stateCd == end_request_state
            assert nr_final.priorityCd == end_priority
            assert nr_final.expirationDate == (
                start_datetime
                or now) + timedelta(days=days_after_start_datetime)
            assert eval(
                f'payment_final.payment_completion_date {end_payment_has_value} None'
            )
            assert payment_final.payment_status_code == end_payment_state
Exemplo n.º 2
0
     Payment.PaymentActions.CREATE.
     value,  # payment action [CREATE|UPGRADE|REAPPLY|RESUBMIT]
     State.PENDING_PAYMENT,  # start state of NR
     State.DRAFT,  # end state of NR
     'N',  # start state of Priority
     'N',  # end state of Priority
     None,  # start of frozen time
     NAME_REQUEST_LIFESPAN_DAYS,  # days after frozen time
     None,  # start_payment_completion_state
     'COMPLETED',  # end_payment_completion_state
     None,  # start_payment_date
     'is not',  # end_payment_has_value
     None  # error
 ),
 ('already draft', Payment.PaymentActions.CREATE.value, State.DRAFT,
  State.DRAFT, 'N', 'N', datetime.utcnow(), 0, 'COMPLETED', 'COMPLETED',
  None, 'is not', None),
 ('resubmit', Payment.PaymentActions.RESUBMIT.value,
  State.PENDING_PAYMENT, State.DRAFT, 'N', 'N', datetime.utcnow(), 0,
  None, 'COMPLETED', None, 'is not', None),
 ('resubmit draft', Payment.PaymentActions.RESUBMIT.value, State.DRAFT,
  State.DRAFT, 'N', 'N', datetime.utcnow(), 0, 'COMPLETED', 'COMPLETED',
  None, 'is not', None),
 ('upgrade', Payment.PaymentActions.UPGRADE.value, State.DRAFT,
  State.DRAFT, 'N', 'Y', datetime.utcnow(), 0, 'PENDING_PAYMENT',
  'COMPLETED', None, 'is not', None),
 ('re-upgrade', Payment.PaymentActions.UPGRADE.value, State.DRAFT,
  State.DRAFT, 'Y', 'Y', datetime.utcnow(), 0, 'PENDING_PAYMENT',
  'COMPLETED', None, 'is not', None),
 ('extend ', Payment.PaymentActions.REAPPLY.value, State.DRAFT,
  State.DRAFT, 'N', 'N', datetime.utcnow() + timedelta(days=3),
Exemplo n.º 3
0
async def update_payment_record(payment: Payment) -> Optional[Payment]:
    """Update the payment record in the database.

    Alter the NR state as required based on the payment action.
    Payment NR Action - One of [COMPLETE, UPGRADE, REAPPLY]
    COMPLETE - set NR to DRAFT IFF nr.state == PENDING_PAYMENT
    UPGRADE - set the nr.priority to True/'Y'
    REAPPLY - add REAPPLY_EXTENSION to expiry date of NR IFF it hasn't expired
    """
    if payment.payment_completion_date:
        msg = f'Queue Issue: Duplicate, payment already processed for payment.id={payment.id}'
        logger.debug(msg)
        capture_message(msg)
        return None

    payment_action = payment.payment_action
    nr = RequestDAO.find_by_id(payment.nrId)
    if payment_action == Payment.PaymentActions.CREATE.value:  # pylint: disable=R1705
        if nr.stateCd == State.PENDING_PAYMENT:
            nr.stateCd = State.DRAFT
            payment.payment_completion_date = datetime.utcnow()
            payment.payment_status_code = State.COMPLETED

            nr.save_to_db()
            payment.save_to_db()
        return payment

    elif payment_action == Payment.PaymentActions.UPGRADE.value:
        if nr.stateCd == State.PENDING_PAYMENT:
            msg = f'Queue Issue: Upgrading a non-DRAFT NR for payment.id={payment.id}'
            logger.debug(msg)
            capture_message(msg)
            raise QueueException(msg)

        nr.priorityCd = 'Y'
        nr.priorityDate = datetime.utcnow()
        payment.payment_completion_date = datetime.utcnow()
        payment.payment_status_code = State.COMPLETED

        nr.save_to_db()
        payment.save_to_db()
        return payment

    elif payment_action == Payment.PaymentActions.REAPPLY.value:
        if nr.stateCd != State.APPROVED \
                and nr.expirationDate + timedelta(hours=NAME_REQUEST_EXTENSION_PAD_HOURS) < datetime.utcnow():
            msg = f'Queue Issue: Failed attempt to extend NR for payment.id={payment.id} '\
                'nr.state{nr.stateCd}, nr.expires:{nr.expirationDate}'
            logger.debug(msg)
            capture_message(msg)
            raise QueueException(msg)
        nr.expirationDate = nr.expirationDate + timedelta(days=NAME_REQUEST_LIFESPAN_DAYS)
        payment.payment_completion_date = datetime.utcnow()
        payment.payment_status_code = State.COMPLETED

        nr.save_to_db()
        payment.save_to_db()
        return payment

    msg = f'Queue Issue: Unknown action:{payment_action} for payment.id={payment.id}'
    logger.debug(msg)
    capture_message(msg)
    raise QueueException(f'Unknown action:{payment_action} for payment.id={payment.id}')