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
async def test_process_payment( app, session, mocker, test_name, action_code, start_request_state, start_priority, start_datetime, start_payment_state, start_payment_date, ): from namex.models import Request, State, Payment from namex_pay.worker import process_payment, FLASK_APP nest_asyncio.apply() # 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() # setup mock and patch msg = None def catch(qsm, cloud_event_msg): nonlocal msg msg = cloud_event_msg mocker.patch('namex_pay.worker.publish_email_message', side_effect=catch) # Test pay_msg = { "paymentToken": { "id": PAYMENT_TOKEN, "statusCode": "COMPLETED", "filingIdentifier": None } } await process_payment(pay_msg, FLASK_APP) print(msg) # Verify message that would be sent to the email server assert msg['type'] == 'bc.registry.names.request' assert msg['source'] == '/requests/NR B000001' assert msg['datacontenttype'] == 'application/json' assert msg['identifier'] == 'NR B000001' assert msg['data']['request']['header']['nrNum'] == NR_NUMBER assert msg['data']['request']['paymentToken'] == PAYMENT_TOKEN assert msg['data']['request']['statusCode'] == 'DRAFT'
async def test_furnish_receipt_message(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that events are placed on the email queue and the payment is marked furnished.""" from queue_common.messages import create_cloud_event_msg from queue_common.service import ServiceWorker from queue_common.service_utils import subscribe_to_queue from namex_pay.worker import APP_CONFIG, furnish_receipt_message, qsm from namex.models import Request, State, Payment print('test vars') print(app, session, stan_server, event_loop, client_id, entity_stan, future) # setup PAYMENT_TOKEN = 'dog' NR_NUMBER = 'NR B000001' name_request = Request() name_request.nrNum = NR_NUMBER name_request.stateCd = State.DRAFT name_request._source = 'NRO' name_request.save_to_db() payment = Payment() payment.nrId = name_request.id payment._payment_token = PAYMENT_TOKEN payment._payment_status_code = 'COMPLETED' payment.furnished = False payment.save_to_db() # file handler callback msgs = [] s = ServiceWorker() s.sc = entity_stan qsm.service = s async def cb_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) print('call back recvd') if len(msgs) == 1: future.set_result(True) file_handler_subject = APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'] print(f'file_handler_subject:{file_handler_subject}') await subscribe_to_queue(entity_stan, file_handler_subject, f'entity_queue.{file_handler_subject}', f'entity_durable_name.{file_handler_subject}', cb_handler) print(payment.as_dict()) # sanity check assert name_request.id assert payment.nrId == name_request.id # test await furnish_receipt_message(qsm, payment) try: await asyncio.wait_for(future, 1, loop=event_loop) except Exception as err: print(err) # results processed_payment = Payment.find_by_payment_token(PAYMENT_TOKEN) # verify assert processed_payment.furnished assert len(msgs) == 1 cloud_event = json.loads(msgs[0].data.decode('utf-8')) assert cloud_event['identifier'] == NR_NUMBER