async def test_publish_event(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that filing event is placed on the queue.""" # Call back for the subscription from entity_queue_common.service import ServiceWorker from entity_filer.worker import APP_CONFIG, publish_event, qsm from legal_api.models import Business, Filing # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) event_handler_subject = APP_CONFIG.ENTITY_EVENT_PUBLISH_OPTIONS['subject'] await entity_stan.subscribe( subject=event_handler_subject, queue=f'entity_queue.{event_handler_subject}', durable_name=f'entity_durable_name.{event_handler_subject}', cb=cb_file_handler) s = ServiceWorker() s.sc = entity_stan qsm.service = s # Setup filing = Filing() filing.id = 101 filing.effective_date = datetime.utcnow().replace(tzinfo=timezone.utc) filing.filing_json = ANNUAL_REPORT business = Business() business.identifier = 'CP1234567' business.legal_name = 'CP1234567 - Legal Name' # Test await publish_event(business, filing) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: print(err) # check it out assert len(msgs) == 1 event_msg = json.loads(msgs[0].data.decode('utf-8')) assert event_msg['filing']['filingId'] == 101 assert event_msg['filing']['identifier'] == 'CP1234567' assert event_msg['filing']['legalFilings'] == ['annualReport']
async def test_publish_email_message(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that payment tokens can be retrieved and decoded from the Queue.""" # Call back for the subscription from entity_queue_common.service import ServiceWorker from entity_pay.worker import APP_CONFIG, publish_email_message, qsm from legal_api.models import Filing # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) file_handler_subject = APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'] await subscribe_to_queue(entity_stan, file_handler_subject, f'entity_queue.{file_handler_subject}', f'entity_durable_name.{file_handler_subject}', cb_file_handler) s = ServiceWorker() s.sc = entity_stan qsm.service = s # Test filing = Filing() filing.id = 101 filing.filing_type = 'incorporationApplication' filing_date = datetime.datetime.utcnow() filing.filing_date = filing_date filing.effective_date = filing_date await publish_email_message(filing) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: print(err) # check it out assert len(msgs) == 1 assert get_data_from_msg(msgs[0], 'id') == filing.id assert get_data_from_msg(msgs[0], 'type') == filing.filing_type assert get_data_from_msg(msgs[0], 'option') == 'filed'
async def test_publish_event(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that email event is placed on the queue.""" # Call back for the subscription from entity_queue_common.service import ServiceWorker from entity_emailer.worker import APP_CONFIG, publish_event, qsm # email handler callback msgs = [] async def cb_email_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) event_handler_subject = APP_CONFIG.ENTITY_EVENT_PUBLISH_OPTIONS['subject'] await entity_stan.subscribe( subject=event_handler_subject, queue=f'entity_queue.{event_handler_subject}', durable_name=f'entity_durable_name.{event_handler_subject}', cb=cb_email_handler) s = ServiceWorker() s.sc = entity_stan qsm.service = s # Setup # Test await publish_event({'email': {'type': 'bn'}}) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: # noqa B902 print(err) # check it out assert len(msgs) == 1 event_msg = json.loads(msgs[0].data.decode('utf-8')) assert event_msg['email']['type'] == 'bn'
async def test_publish_filing(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that payment tokens can be retrieved and decoded from the Queue.""" # Call back for the subscription from entity_queue_common.service import ServiceWorker from entity_pay.worker import APP_CONFIG, publish_filing, qsm from legal_api.models import Filing # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) file_handler_subject = APP_CONFIG.FILER_PUBLISH_OPTIONS['subject'] await subscribe_to_queue(entity_stan, file_handler_subject, f'entity_queue.{file_handler_subject}', f'entity_durable_name.{file_handler_subject}', cb_file_handler) s = ServiceWorker() s.sc = entity_stan qsm.service = s # Test filing = Filing() filing.id = 101 await publish_filing(filing) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: print(err) # check it out assert len(msgs) == 1 assert get_filing_id_from_msg(msgs[0]) == filing.id
async def test_cb_subscription_handler(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that payment tokens can be retrieved and decoded from the Queue.""" # Call back for the subscription from entity_queue_common.service import ServiceWorker from entity_pay.worker import APP_CONFIG, cb_subscription_handler, get_filing_by_payment_id, qsm from legal_api.models import Business, Filing from tests.unit import create_filing, create_business # vars uuid = str(random.SystemRandom().getrandbits(0x58)) payment_id = uuid identifier = 'CP1234567' # entity_subject = os.getenv('LEGAL_FILING_STAN_SUBJECT') # entity_queue = os.getenv('LEGAL_FILING_STAN_QUEUE') # entity_durable_name = os.getenv('LEGAL_FILING_STAN_DURABLE_NAME') entity_subject = f'test_subject.{uuid}' entity_queue = f'test_queue.{uuid}' entity_durable_name = f'test_durable.{uuid}' # setup business = create_business(identifier) business_id = business.id # create_filing(payment_id, AR_FILING, business.id) create_filing(payment_id, None, business.id) # register the handler to test it entity_subject = await subscribe_to_queue(entity_stan, entity_subject, entity_queue, entity_durable_name, cb_subscription_handler) # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) file_handler_subject = APP_CONFIG.FILER_PUBLISH_OPTIONS['subject'] await subscribe_to_queue(entity_stan, file_handler_subject, f'entity_queue.{file_handler_subject}', f'entity_durable_name.{file_handler_subject}', cb_file_handler) s = ServiceWorker() s.sc = entity_stan qsm.service = s # add payment tokens to queue await helper_add_payment_to_queue(entity_stan, entity_subject, payment_id=payment_id, status_code='COMPLETED') try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: print(err) # Get modified data filing = get_filing_by_payment_id(payment_id) business = Business.find_by_internal_id(business_id) # check it out # assert filing.transaction_id assert filing.business_id == business_id assert filing.status == Filing.Status.PAID.value assert len(msgs) == 1 assert get_filing_id_from_msg(msgs[0]) == filing.id
async def test_cb_subscription_handler(session, # pylint: disable=too-many-arguments, too-many-locals sendmail_mock, stan_server, event_loop, # pylint: disable=unused-argument notification_stan, future): """Assert that notification id can be retrieved and decoded from the Queue.""" from entity_queue_common.service import ServiceWorker # pylint: disable=import-outside-toplevel # vars uuid = str(random.SystemRandom().randint(0, 999999)) notification_id = uuid notification_subject = f'test_subject.{uuid}' notification_queue = f'test_queue.{uuid}' notification_durable_name = f'test_durable.{uuid}' notification = NotificationModel(**NOTIFICATION_DATA[0]) notification.id = notification_id session.add(notification) session.commit() notification = session.merge(notification) content = NotificationContentsModel(**CONTENT_DATA[0], notification_id=notification.id) session.add(content) session.commit() content = session.merge(content) # register the handler to test it notification_subject = await subscribe_to_queue(notification_stan, notification_subject, notification_queue, notification_durable_name, cb_subscription_handler) # file handler callback msgs = [] async def cb_file_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) if len(msgs) == 1: future.set_result(True) file_handler_subject = app_config.FILER_PUBLISH_OPTIONS['subject'] await subscribe_to_queue(notification_stan, file_handler_subject, f'notification_queue.{file_handler_subject}', f'notification_durable_name.{file_handler_subject}', cb_file_handler) service_worker = ServiceWorker() service_worker.sc = notification_stan qsm.service = service_worker # add notification id to queue await helper_add_notification_to_queue(notification_stan, notification_subject, notification_id=notification_id) try: await asyncio.wait_for(future, 2, loop=event_loop) except Exception as err: # pylint: disable=broad-except print(err) notification_update: NotificationModel = await NotifyService.find_notification(session, notification_id) assert notification_update is not None assert notification_update.id == notification.id assert notification_update.status_code == NotificationStatusEnum.DELIVERED