Example #1
0
def factory_entity_model():
    """Produce a templated entity model."""
    entity = EntityModel(business_identifier='CP1234567',
                         business_number='791861073BC0001',
                         name='Foobar, Inc.')
    entity.save()
    return entity
Example #2
0
def factory_entity_model(entity_info: dict = TestEntityInfo.entity1,
                         user_id=None):
    """Produce a templated entity model."""
    entity = EntityModel.create_from_dict(entity_info)
    entity.created_by_id = user_id
    entity.save()
    return entity
Example #3
0
def test_entity_find_by_business_id(session):
    """Assert that an Entity can be retrieved via business identifier."""
    entity = EntityModel(business_identifier='CP1234567')
    session.add(entity)
    session.commit()

    business_id = 'CP1234567'

    result_entity = EntityModel.find_by_business_identifier(business_identifier=business_id)

    assert result_entity.id is not None
def test_create_from_dict(session):  # pylint:disable=unused-argument
    """Assert that an Entity can be created from schema."""
    updated_entity_info = {
        'businessIdentifier': 'CP1234567',
        'businessNumber': '791861073BC0001',
        'passCode': '9898989',
        'name': 'Barfoo, Inc.',
        'corp_type_code': 'CP'
    }

    result_entity = EntityModel.create_from_dict(updated_entity_info)

    assert result_entity.id is not None
    assert result_entity.corp_type_code == 'CP'
Example #5
0
def test_entity_find_by_business_id(session):
    """Assert that an Entity can be retrieved via business identifier."""
    entity = EntityModel(business_identifier='CP1234567',
                         business_number='791861073BC0001',
                         name='Foobar, Inc.')
    session.add(entity)
    session.commit()

    business_id = 'CP1234567'

    result_entity = EntityModel.find_by_business_identifier(
        business_identifier=business_id)

    assert result_entity.id is not None
def test_create_from_dict_no_schema(session):  # pylint:disable=unused-argument
    """Assert that an Entity can not be created without schema."""
    result_entity = EntityModel.create_from_dict(None)

    assert result_entity is None
Example #7
0
async def process_name_events(event_message: Dict[str, any]):
    """Process name events.

    1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record.
    2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR.
    3. If an account is found, affiliate to that account.

    Args:
        event_message (object): cloud event message, sample below.
            {
                'specversion': '1.0.1',
                'type': 'bc.registry.names.events',
                'source': '/requests/6724165',
                'id': id,
                'time': '',
                'datacontenttype': 'application/json',
                'identifier': '781020202',
                'data': {
                    'request': {
                        'nrNum': 'NR 5659951',
                        'newState': 'APPROVED',
                        'previousState': 'DRAFT'
                    }
                }
            }
    """
    logger.debug('>>>>>>>process_name_events>>>>>')
    request_data = event_message.get('data').get('request')
    nr_number = request_data['nrNum']
    nr_status = request_data['newState']
    nr_entity = EntityModel.find_by_business_identifier(nr_number)
    if nr_entity is None:
        logger.info('Entity doesn' 't exist, creating a new entity.')
        nr_entity = EntityModel(business_identifier=nr_number,
                                corp_type_code=CorpType.NR.value)

    nr_entity.status = nr_status
    nr_entity.name = request_data.get(
        'name',
        '')  # its not part of event now, this is to handle if they include it.
    nr_entity.last_modified_by = None  # TODO not present in event message.
    nr_entity.last_modified = parser.parse(event_message.get('time'))

    if nr_status == 'DRAFT' and AffiliationModel.find_affiliations_by_business_identifier(
            nr_number) is None:
        logger.info('Status is DRAFT, getting invoices for account')
        # Find account details for the NR.
        invoices = RestService.get(
            f'{APP_CONFIG.PAY_API_URL}/payment-requests?businessIdentifier={nr_number}',
            token=RestService.get_service_account_token()).json()

        # Ideally there should be only one or two (priority fees) payment request for the NR.
        if invoices and (auth_account_id := invoices['invoices'][0].get('paymentAccount').get('accountId')) \
                and str(auth_account_id).isnumeric():
            logger.info('Account ID received : %s', auth_account_id)
            # Auth account id can be service account value too, so doing a query lookup than find_by_id
            org: OrgModel = db.session.query(OrgModel).filter(
                OrgModel.id == auth_account_id).one_or_none()
            if org:
                nr_entity.pass_code_claimed = True
                # Create an affiliation.
                logger.info(
                    'Creating affiliation between Entity : %s and Org : %s',
                    nr_entity, org)
                affiliation: AffiliationModel = AffiliationModel(
                    entity=nr_entity, org=org)
                affiliation.flush()
async def test_events_listener_queue(app, session, stan_server, event_loop,
                                     client_id, events_stan, future,
                                     monkeypatch):
    """Assert that events can be retrieved and decoded from the Queue."""
    # Call back for the subscription
    from business_events_listener.worker import cb_nr_subscription_handler

    # 1. Create an Org
    # 2. Mock the rest service to return the invoices with the org created.
    # 3. Publish NR event and assert it's affiliated to the org.
    org = OrgModel(name='Test',
                   org_type=OrgTypeModel.get_default_type(),
                   org_status=OrgStatusModel.get_default_status()).save()
    org_id = org.id

    events_subject = 'test_subject'
    events_queue = 'test_queue'
    events_durable_name = 'test_durable'

    nr_number = 'NR 1234'
    nr_state = 'DRAFT'

    # register the handler to test it
    await subscribe_to_queue(events_stan, events_subject, events_queue,
                             events_durable_name, cb_nr_subscription_handler)

    # Mock the rest service response to return the org just created.
    def get_invoices_mock(nr_number, token):
        response_content = json.dumps({
            'invoices': [{
                'businessIdentifier': nr_number,
                'paymentAccount': {
                    'accountId': org_id
                }
            }]
        })

        response = Response()
        response.status_code = 200
        response._content = str.encode(response_content)
        return response

    monkeypatch.setattr('auth_api.services.rest_service.RestService.get',
                        get_invoices_mock)
    monkeypatch.setattr(
        'auth_api.services.rest_service.RestService.get_service_account_token',
        lambda *args, **kwargs: None)

    # add an event to queue
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')

    # Query the affiliations and assert the org has affiliation for the NR.
    entity: EntityModel = EntityModel.find_by_business_identifier(nr_number)
    assert entity
    assert entity.pass_code_claimed
    affiliations: [AffiliationModel
                   ] = AffiliationModel.find_affiliations_by_org_id(org_id)
    assert len(affiliations) == 1
    assert affiliations[0].entity_id == entity.id
    assert affiliations[0].org_id == org_id

    # Publish message again and assert it doesn't create duplicate affiliation.
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')
    affiliations: [AffiliationModel
                   ] = AffiliationModel.find_affiliations_by_org_id(org_id)
    assert len(affiliations) == 1

    # Publish message for an NR which was done using a service account or staff with no user account.
    def get_invoices_mock(nr_number, token):
        response_content = json.dumps({
            'invoices': [{
                'businessIdentifier': nr_number,
                'paymentAccount': {
                    'accountId': 'SERVICE-ACCOUNT-NAME-REQUEST-SERVICE-ACCOUNT'
                }
            }]
        })

        response = Response()
        response.status_code = 200
        response._content = str.encode(response_content)
        return response

    monkeypatch.setattr('auth_api.services.rest_service.RestService.get',
                        get_invoices_mock)
    # add an event to queue
    nr_number = 'NR 000001'
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')

    # Query the entity and assert the entity is not affiliated.
    entity: EntityModel = EntityModel.find_by_business_identifier(nr_number)
    assert entity
    assert not entity.pass_code_claimed