Example #1
0
def test_create_payment_record_rollback_on_paybc_connection_error(
        session, public_user_mock):
    """Assert that the payment records are not created."""
    # Create a payment account
    factory_payment_account()

    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectionError('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == ServiceUnavailableException

    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=ConnectTimeout('mocked error')):
        with pytest.raises(ServiceUnavailableException) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == ServiceUnavailableException

    with patch('pay_api.services.oauth_service.requests.post',
               side_effect=HTTPError('mocked error')) as post_mock:
        post_mock.status_Code = 503
        with pytest.raises(HTTPError) as excinfo:
            PaymentService.create_invoice(get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == HTTPError
def test_create_payment_record_with_service_charge(session, public_user_mock):
    """Assert that the payment records are created."""
    # Create a payment request for corp type BC
    payment_response = PaymentService.create_invoice(get_payment_request_with_service_fees(),
                                                     get_auth_basic_user())
    account_model = PaymentAccount.find_by_auth_account_id(get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    assert payment_response.get('service_fees') == 1.50
def test_create_payment_record(session, public_user_mock):
    """Assert that the payment records are created."""
    payment_response = PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
    account_model = PaymentAccount.find_by_auth_account_id(get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    # Create another payment with same request, the account should be the same
    PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
    account_model = PaymentAccount.find_by_auth_account_id(get_auth_basic_user().get('account').get('id'))

    assert account_id == account_model.id
Example #4
0
def test_create_payment_record_with_service_charge(session, public_user_mock):
    """Assert that the payment records are created."""
    # Create a payment request for corp type BC
    payment_response = PaymentService.create_payment(get_payment_request(corp_type='BC', second_filing_type='OTFDR'),
                                                     get_auth_basic_user())
    account_model = CreditPaymentAccount. \
        find_by_corp_number_and_corp_type_and_auth_account_id('CP0001234', 'BC',
                                                              get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    assert payment_response.get('invoices')[0].get('service_fees') == 1.50
def test_create_payment_record_with_direct_pay(session, public_user_mock):
    """Assert that the payment records are created."""
    current_app.config['DIRECT_PAY_ENABLED'] = True
    payment_response = PaymentService.create_invoice(
        get_payment_request(), get_auth_basic_user(PaymentMethod.DIRECT_PAY.value))
    account_model = PaymentAccount.find_by_auth_account_id(get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    # Create another payment with same request, the account should be the same
    PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
    account_model = PaymentAccount.find_by_auth_account_id(get_auth_basic_user().get('account').get('id'))

    assert account_id == account_model.id
Example #6
0
def test_account_invalid_lookup(session):
    """Invalid account test."""
    business_info: Dict = {'businessIdentifier': '1234', 'corpType': 'CP'}

    p = PaymentAccountService.find_account(business_info,
                                           get_auth_basic_user(), 'PAYBC')

    assert p is not None
    assert p.id is None
    import pytest
    from pay_api.exceptions import BusinessException
    from pay_api.utils.errors import Error
    with pytest.raises(BusinessException) as excinfo:
        PaymentAccountService.find_account({}, get_auth_basic_user(), 'PAYBC')
    assert excinfo.value.status == Error.PAY004.status
Example #7
0
def test_create_payment_record(session, public_user_mock):
    """Assert that the payment records are created."""
    payment_response = PaymentService.create_payment(get_payment_request(), get_auth_basic_user())
    account_model = CreditPaymentAccount. \
        find_by_corp_number_and_corp_type_and_auth_account_id('CP0001234', 'CP',
                                                              get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    # Create another payment with same request, the account should be the same
    PaymentService.create_payment(get_payment_request(), get_auth_basic_user())
    account_model = CreditPaymentAccount. \
        find_by_corp_number_and_corp_type_and_auth_account_id('CP0001234', 'CP',
                                                              get_auth_basic_user().get('account').get('id'))
    assert account_id == account_model.id
Example #8
0
def test_create_receipt_without_invoice(session, public_user_mock):
    """Try creating a receipt without invoice number."""
    payment_account = factory_payment_account()
    payment = factory_payment()
    payment_account.save()
    payment.save()
    invoice = factory_invoice(payment, payment_account)
    invoice.save()
    factory_invoice_reference(invoice.id).save()
    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')
    line = factory_payment_line_item(
        invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id)
    line.save()
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    PaymentService.update_payment(payment.id, get_payment_request(),
                                  get_auth_basic_user())
    input_data = {
        'corpName': 'Pennsular Coop ',
        'filingDateTime': '1999',
        'fileName': 'coopser'
    }
    response = ReceiptService.create_receipt(payment.id,
                                             '',
                                             input_data,
                                             skip_auth_check=True)
    assert response is not None
Example #9
0
def test_create_receipt_with_no_receipt(session, public_user_mock):
    """Try creating a receipt with invoice number."""
    payment_account = factory_payment_account()
    payment = factory_payment()
    payment_account.save()
    payment.save()
    invoice = factory_invoice(payment, payment_account)
    invoice.save()
    factory_invoice_reference(invoice.id).save()
    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')
    line = factory_payment_line_item(
        invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id)
    line.save()

    PaymentService.update_payment(payment.id, get_payment_request(),
                                  get_auth_basic_user())
    input_data = {
        'corpName': 'Pennsular Coop ',
        'filingDateTime': '1999',
        'fileName': 'coopser'
    }

    with pytest.raises(BusinessException) as excinfo:
        ReceiptService.create_receipt(payment.id,
                                      '',
                                      input_data,
                                      skip_auth_check=True)
    assert excinfo.type == BusinessException
def test_create_bcol_payment_for_basic_user(session, public_user_mock):
    """Assert that the payment records are created."""
    with pytest.raises(Exception) as excinfo:
        PaymentService.create_payment(
            get_payment_request_with_payment_method(payment_method='DRAWDOWN', business_identifier='CP0002000'),
            get_auth_basic_user())
    assert excinfo.type == BusinessException
Example #11
0
def test_create_payment_record_rollback(session, public_user_mock):
    """Assert that the payment records are created."""
    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.invoice.Invoice.flush', side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_payment(get_payment_request(), get_auth_basic_user())
        assert excinfo.type == Exception

    with patch('pay_api.services.payment.Payment.create', side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_payment(get_payment_request(), get_auth_basic_user())
        assert excinfo.type == Exception
    with patch('pay_api.services.paybc_service.PaybcService.create_invoice', side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_payment(get_payment_request(), get_auth_basic_user())
        assert excinfo.type == Exception
def test_direct_pay_account_saved_from_new(session):
    """Assert that the payment is saved to the table."""
    payment_account = factory_payment_account(payment_method_code=PaymentMethod.DIRECT_PAY.value)
    payment_account.save()

    pa = PaymentAccountService.find_account(get_auth_basic_user())

    assert pa is not None
    assert pa.id is not None
Example #13
0
def test_account_saved_from_new(session):
    """Assert that the payment is saved to the table."""
    payment_account = factory_payment_account()
    payment_account.save()

    pa = PaymentAccountService.find_account(get_auth_basic_user())

    assert pa is not None
    assert pa.id is not None
Example #14
0
def test_create_zero_dollar_payment_record(session, public_user_mock):
    """Assert that the payment records are created and completed."""
    payment_response = PaymentService.create_payment(
        get_zero_dollar_payment_request(), get_auth_basic_user())
    account_model = InternalPaymentAccount.find_by_corp_number_and_corp_type_and_account_id(
        'CP0001234', 'CP',
        get_auth_basic_user().get('account').get('id'))
    account_id = account_model.id
    assert account_id is not None
    assert payment_response.get('id') is not None
    assert payment_response.get('status_code') == 'COMPLETED'
    # Create another payment with same request, the account should be the same
    PaymentService.create_payment(get_zero_dollar_payment_request(),
                                  get_auth_basic_user())
    account_model = InternalPaymentAccount.find_by_corp_number_and_corp_type_and_account_id(
        'CP0001234', 'CP',
        get_auth_basic_user().get('account').get('id'))
    assert account_id == account_model.id
    assert payment_response.get('status_code') == 'COMPLETED'
def test_create_payment_record_rollback(session, public_user_mock):
    """Assert that the payment records are created."""
    # Mock here that the invoice update fails here to test the rollback scenario
    with patch('pay_api.services.invoice.Invoice.flush', side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
        assert excinfo.type == Exception

    # with patch('pay_api.services.invoice.InvoiceReference.create', side_effect=Exception('mocked error')):
    #     with pytest.raises(Exception) as excinfo:
    #         PaymentService.create_invoice(get_payment_request(), get_auth_basic_user())
    #     assert excinfo.type == Exception
    with patch('pay_api.services.direct_pay_service.DirectPayService.create_invoice',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.create_invoice(
                get_payment_request_with_payment_method(payment_method=PaymentMethod.DIRECT_PAY.value),
                get_auth_basic_user())
        assert excinfo.type == Exception
def test_create_payment_record_with_rs(session, public_user_mock):
    """Assert that the payment records are created and completed."""
    payment_account = factory_payment_account()
    payment_account.save()
    rs = factory_routing_slip(payment_account_id=payment_account.id, total=1000, remaining_amount=1000)
    rs.save()
    cfs_response = Mock(spec=Response)
    cfs_response.json.return_value = {'invoice_number': 'abcde', }
    cfs_response.status_code = 200

    request = get_payment_request()
    request['accountInfo'] = {'routingSlip': rs.number}
    with patch.object(CFSService, 'create_account_invoice', return_value=cfs_response) as mock_post:
        PaymentService.create_invoice(request, get_auth_basic_user())
        mock_post.assert_called()

    request = get_zero_dollar_payment_request()
    request['accountInfo'] = {'routingSlip': rs.number}
    with patch.object(CFSService, 'create_account_invoice', return_value=cfs_response) as mock_post:
        PaymentService.create_invoice(request, get_auth_basic_user())
        mock_post.assert_not_called()
Example #17
0
def test_direct_pay_account_saved_from_new(session):
    """Assert that the payment is saved to the table."""
    payment_account = factory_payment_account(payment_method_code=PaymentMethod.DIRECT_PAY.value)
    payment_account.save()
    business_info: Dict = {
        'businessIdentifier': payment_account.corp_number,
        'corpType': payment_account.corp_type_code
    }

    pa = PaymentAccountService.find_account(business_info, get_auth_basic_user(), 'PAYBC')

    assert pa is not None
    assert pa.id is not None
    assert pa.corp_number is not None
    assert pa.corp_type_code is not None
Example #18
0
def test_update_payment_record(session, public_user_mock):
    """Assert that the payment records are updated."""
    payment_account = factory_payment_account()
    payment = factory_payment()
    payment_account.save()
    payment.save()
    invoice = factory_invoice(payment, payment_account)
    invoice.save()
    factory_invoice_reference(invoice.id).save()
    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')
    line = factory_payment_line_item(
        invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id)
    line.save()
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    payment_response = PaymentService.update_payment(payment.id,
                                                     get_payment_request(),
                                                     get_auth_basic_user())
    assert payment_response.get('id') is not None
Example #19
0
def test_update_payment_deleted_invalid(session, public_user_mock):
    """Assert that the payment records are not updated."""
    payment_account = factory_payment_account()
    payment = factory_payment()
    payment_account.save()
    payment.payment_status_code = PaymentStatus.DELETED.value
    payment.save()
    invoice = factory_invoice(payment, payment_account)
    invoice.save()
    factory_invoice_reference(invoice.id).save()

    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')
    line = factory_payment_line_item(invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id)
    line.save()
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    with pytest.raises(BusinessException) as excinfo:
        PaymentService.update_payment(payment.id, get_payment_request(), get_auth_basic_user())
    assert excinfo.type == BusinessException
Example #20
0
def test_update_payment_record_rollback(session, public_user_mock):
    """Assert that the payment records are updated."""
    payment_account = factory_payment_account()
    payment = factory_payment()
    payment_account.save()
    payment.save()
    invoice = factory_invoice(payment, payment_account)
    invoice.save()
    factory_invoice_reference(invoice.id).save()

    fee_schedule = FeeSchedule.find_by_filing_type_and_corp_type('CP', 'OTANN')
    line = factory_payment_line_item(
        invoice.id, fee_schedule_id=fee_schedule.fee_schedule_id)
    line.save()
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    # Mock here that the invoice update fails here to test the rollback scenario
    with patch(
            'pay_api.services.payment_transaction.PaymentTransaction.find_active_by_payment_id',
            side_effect=Exception('mocked error'),
    ):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    with patch(
            'pay_api.services.payment_transaction.PaymentTransaction.update_transaction',
            side_effect=Exception('mocked error'),
    ):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    with patch('pay_api.services.payment.Payment.find_by_id',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    with patch('pay_api.services.payment_line_item.PaymentLineItem.create',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    # reset transaction
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    with patch('pay_api.services.paybc_service.PaybcService.update_invoice',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    # reset transaction
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    with patch('pay_api.services.invoice.Invoice.find_by_id',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    # reset transaction
    transaction = factory_payment_transaction(payment.id)
    transaction.save()

    with patch('pay_api.services.invoice.Invoice.save',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception

    with patch('pay_api.services.payment.Payment.save',
               side_effect=Exception('mocked error')):
        with pytest.raises(Exception) as excinfo:
            PaymentService.update_payment(payment.id, get_payment_request(),
                                          get_auth_basic_user())
        assert excinfo.type == Exception