Example #1
0
def test_find_affiliations_for_new_business(session, auth_mock, nr_mock,
                                            monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Affiliation can be created."""
    # Create 2 entities - 1 with type NR and another one TMP
    # Affiliate to an org
    # Get should return only 1 - TMP
    # Then delete one affiliation - TMP
    # Get should return only 1 - NR
    patch_token_info(TestJwtClaims.public_account_holder_user, monkeypatch)

    entity_service1 = factory_entity_service(
        entity_info=TestEntityInfo.name_request)
    entity_dictionary1 = entity_service1.as_dict()
    business_identifier1 = entity_dictionary1['business_identifier']
    name1 = entity_dictionary1['name']

    entity_service2 = factory_entity_service(
        entity_info=TestEntityInfo.tenp_business)
    entity_dictionary2 = entity_service2.as_dict()
    business_identifier2 = entity_dictionary2['business_identifier']

    org_service = factory_org_service()
    org_dictionary = org_service.as_dict()
    org_id = org_dictionary['id']

    # create NR affiliation
    AffiliationService.create_new_business_affiliation(
        org_id, business_identifier=business_identifier1, phone='1112223333')
    # create second row in affiliation table
    AffiliationService.create_affiliation(org_id, business_identifier2)

    affiliated_entities = AffiliationService.find_visible_affiliations_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == business_identifier2
    assert affiliated_entities[0]['nr_number'] == business_identifier1
    assert affiliated_entities[0]['name'] == name1

    AffiliationService.delete_affiliation(
        org_id=org_id,
        business_identifier=business_identifier2,
        email_addresses=None)

    affiliated_entities = AffiliationService.find_visible_affiliations_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == business_identifier1
Example #2
0
    def post(org_id):
        """Post a new Affiliation for an org using the request body."""
        request_json = request.get_json()
        valid_format, errors = schema_utils.validate(request_json,
                                                     'affiliation')
        bearer_token = request.headers['Authorization'].replace('Bearer ', '')
        is_new_business = request.args.get('newBusiness',
                                           'false').lower() == 'true'
        if not valid_format:
            return {
                'message': schema_utils.serialize(errors)
            }, http_status.HTTP_400_BAD_REQUEST

        try:
            if is_new_business:
                response, status = AffiliationService.create_new_business_affiliation(
                    org_id,
                    request_json.get('businessIdentifier'),
                    request_json.get('email'),
                    request_json.get('phone'),
                    bearer_token=bearer_token).as_dict(
                    ), http_status.HTTP_201_CREATED
            else:
                response, status = AffiliationService.create_affiliation(
                    org_id, request_json.get('businessIdentifier'), request_json.get('passCode'), bearer_token).\
                                       as_dict(), http_status.HTTP_201_CREATED

        except BusinessException as exception:
            response, status = {
                'code': exception.code,
                'message': exception.message
            }, exception.status_code

        return response, status
def test_find_affiliations_for_new_business(session, auth_mock, nr_mock):  # pylint:disable=unused-argument
    """Assert that an Affiliation can be created."""
    # Create 2 entities - 1 with type NR and another one TMP
    # Affiliate to an org
    # Get should return only 1 - TMP
    # Then delete one affiliation - TMP
    # Get should return only 1 - NR

    entity_service1 = factory_entity_service(
        entity_info=TestEntityInfo.name_request)
    entity_dictionary1 = entity_service1.as_dict()
    business_identifier1 = entity_dictionary1['business_identifier']

    entity_service2 = factory_entity_service(
        entity_info=TestEntityInfo.tenp_business)
    entity_dictionary2 = entity_service2.as_dict()
    business_identifier2 = entity_dictionary2['business_identifier']

    org_service = factory_org_service()
    org_dictionary = org_service.as_dict()
    org_id = org_dictionary['id']

    # create NR affiliation
    AffiliationService.create_new_business_affiliation(
        org_id, business_identifier=business_identifier1, phone='1112223333')
    # create second row in affiliation table
    AffiliationService.create_affiliation(org_id, business_identifier2, {})

    affiliated_entities = AffiliationService.find_affiliated_entities_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == business_identifier2

    AffiliationService.delete_affiliation(
        org_id=org_id, business_identifier=business_identifier2)

    affiliated_entities = AffiliationService.find_affiliated_entities_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == business_identifier1
def test_create_new_business_invalid_contact(session, auth_mock, nr_mock):  # pylint:disable=unused-argument
    """Assert that an new business can be created."""
    org_service = factory_org_service()
    org_dictionary = org_service.as_dict()
    org_id = org_dictionary['id']
    business_identifier = 'NR 1234567'

    with pytest.raises(BusinessException) as exception:
        AffiliationService.create_new_business_affiliation(
            org_id,
            business_identifier=business_identifier,
            phone='0000000000')

    assert exception.value.code == Error.NR_INVALID_CONTACT.name

    with pytest.raises(BusinessException) as exception:
        AffiliationService.create_new_business_affiliation(
            org_id,
            business_identifier=business_identifier,
            email='*****@*****.**')

    assert exception.value.code == Error.NR_INVALID_CONTACT.name
def test_create_new_business(session, auth_mock, nr_mock):  # pylint:disable=unused-argument
    """Assert that an new business can be created."""
    org_service = factory_org_service()
    org_dictionary = org_service.as_dict()
    org_id = org_dictionary['id']
    business_identifier = 'NR 1234567'

    affiliation = AffiliationService.create_new_business_affiliation(
        org_id,
        business_identifier=business_identifier,
        email='*****@*****.**',
        phone='1112223333')
    assert affiliation
    assert affiliation.as_dict(
    )['business']['business_identifier'] == business_identifier
def test_find_affiliations_for_new_business_incorporation_complete(
        session, auth_mock, nr_mock):  # pylint:disable=unused-argument
    """Assert that an Affiliation can be created."""
    # Create 2 entities - 1 with type NR and another one TMP
    # Affiliate to an org
    # Get should return only 1 - TMP
    # Delete NR affiliation
    # Then delete one affiliation - TMP
    # Re-add the TMP affiliation with name as the BC...

    nr_entity = factory_entity_service(entity_info=TestEntityInfo.name_request)
    entity_dictionary1 = nr_entity.as_dict()
    nr_business_identifier = entity_dictionary1['business_identifier']

    tmp_entity = factory_entity_service(
        entity_info=TestEntityInfo.tenp_business)
    entity_dictionary2 = tmp_entity.as_dict()
    tmp_business_identifier = entity_dictionary2['business_identifier']

    org_service = factory_org_service()
    org_dictionary = org_service.as_dict()
    org_id = org_dictionary['id']

    # create NR affiliation
    AffiliationService.create_new_business_affiliation(
        org_id, business_identifier=nr_business_identifier, phone='1112223333')
    # create second row in affiliation table
    AffiliationService.create_affiliation(org_id, tmp_business_identifier)

    affiliated_entities = AffiliationService.find_visible_affiliations_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == tmp_business_identifier

    # Delete the NR And TEMP IA affiliation and entities
    AffiliationService.delete_affiliation(
        org_id=org_id, business_identifier=tmp_business_identifier)
    tmp_entity.delete()
    AffiliationService.delete_affiliation(
        org_id=org_id, business_identifier=nr_business_identifier)
    nr_entity.delete()

    # Create entities for a TEMP with name as BC... number and incorporated entity
    tmp_inc_entity = factory_entity_service(
        entity_info=TestEntityInfo.temp_business_incoporated)
    entity_dictionary1 = tmp_inc_entity.as_dict()
    tmp_business_incorporated_identifier = entity_dictionary1[
        'business_identifier']
    AffiliationService.create_affiliation(
        org_id, business_identifier=tmp_business_incorporated_identifier)

    inc_entity = factory_entity_service(
        entity_info=TestEntityInfo.business_incoporated)
    entity_dictionary1 = inc_entity.as_dict()
    business_incorporated_identifier = entity_dictionary1[
        'business_identifier']
    AffiliationService.create_affiliation(
        org_id, business_identifier=business_incorporated_identifier)

    affiliated_entities = AffiliationService.find_visible_affiliations_by_org_id(
        org_id)

    assert affiliated_entities
    assert len(affiliated_entities) == 1
    assert affiliated_entities[0][
        'business_identifier'] == business_incorporated_identifier