Пример #1
0
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']
Пример #2
0
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'
Пример #3
0
async def run(loop, application: Flask = None):
    """Run the methods for applying future effective filings."""
    if application is None:
        application = create_app()

    queue_service = ServiceWorker(loop=loop,
                                  nats_connection_options=default_nats_options,
                                  stan_connection_options=default_stan_options,
                                  config=config.get_named_config('production'))

    await queue_service.connect()

    with application.app_context():
        try:
            filings = get_filings(app=application)
            if not filings:
                application.logger.debug(f'No PAID filings found to apply.')
            for filing in filings:
                filing_id = filing['filing']['header']['filingId']
                effective_date = filing['filing']['header']['effectiveDate']
                # NB: effective_date and now are both UTC
                now = datetime.utcnow().replace(tzinfo=timezone.utc)
                valid = effective_date and parse(effective_date) <= now
                if valid:
                    msg = {'filing': {'id': filing_id}}
                    await queue_service.publish(subject, msg)
                    application.logger.debug(
                        f'Successfully put filing {filing_id} on the queue.')
        except Exception as err:
            application.logger.error(err)
Пример #4
0
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'
Пример #5
0
async def test_is_ready(test_name, mock_return, expected):
    """Assert that the readiness property works as expected."""
    service = ServiceWorker(loop=None,
                            cb_handler=None,
                            config=config.get_named_config())

    class NC():
        def __init__(self, mock_return):
            self.mock_return = mock_return

        @property
        def is_connected(self):
            return self.mock_return

    if mock_return is not None:
        service.nc = NC(mock_return)

    assert expected == await service.is_ready
Пример #6
0
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
Пример #7
0
async def test_service_connect(stan_server, event_loop):
    """Assert that the service connects to NATS, via the readiness check."""
    # setup
    service = ServiceWorker(loop=event_loop,
                            cb_handler=None,
                            config=config.get_named_config())
    assert not await service.is_ready
    try:
        await service.connect()
    except Exception as err:
        print(err)

    # test
    assert await service.is_ready

    # teardown
    await service.close()
Пример #8
0
async def run(loop, application: Flask = None):
    application = create_app()
    queue_service = ServiceWorker(loop=loop,
                                  nats_connection_options=default_nats_options,
                                  stan_connection_options=default_stan_options,
                                  config=config.get_named_config('production'))

    await queue_service.connect()

    with application.app_context():
        try:
            # get updater-job token
            creds = {
                'username': application.config['USERNAME'],
                'password': application.config['PASSWORD']
            }
            auth = requests.post(application.config['AUTH_URL'],
                                 json=creds,
                                 headers={'Content-Type': 'application/json'})
            if auth.status_code != 200:
                application.logger.error(
                    f'file_future_effective failed to authenticate {auth.json()} {auth.status_code}'
                )
                raise Exception
            # TODO token = dict(auth.json())['access_token']

            filings = get_filings(app=application)
            if not filings:
                application.logger.debug(
                    f'No completed filings to send to colin.')
            for filing in filings:
                filing_id = filing["filing"]["header"]["filingId"]
                payment_id = filing["filing"]["header"]["paymentToken"]
                effective_date = filing["filing"]["header"]["effectiveDate"]
                # TODO Use UTC time?
                now = datetime.utcnow().replace(tzinfo=timezone.utc)
                valid = effective_date and parse(effective_date) <= now
                if valid:
                    msg = format_message(filing_id, payment_id, 'COMPLETE')
                    await queue_service.publish(subject, msg)
                    #await queue.async_publish_filing(filing_id, 'COMPLETED', payment_id)
                    application.logger.error(
                        f'Successfully updated filing {filing_id}')
        except Exception as err:
            application.logger.error(err)
Пример #9
0
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
Пример #10
0
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