def test_delete_org_with_affiliation(session, auth_mock, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an org cannot be deleted.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_info=user_with_token) patch_token_info({'sub': user.keycloak_guid}, monkeypatch) org = OrgService.create_org(TestOrgInfo.org1, user.id) org_id = org.as_dict()['id'] entity_service = factory_entity_service( entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] AffiliationService.create_affiliation( org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) patch_token_info(TestJwtClaims.public_user_role, monkeypatch) patch_pay_account_delete(monkeypatch) OrgService.delete_org(org_id) assert len( AffiliationService.find_visible_affiliations_by_org_id(org_id)) == 0
def test_create_affiliation_exists(session, auth_mock): # pylint:disable=unused-argument """Assert that multiple affiliation is allowed.""" entity_service1 = factory_entity_service( entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary1 = entity_service1.as_dict() business_identifier1 = entity_dictionary1['business_identifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] org_service_2 = factory_org_service(org_info=TestOrgInfo.org2) org_dictionary_2 = org_service_2.as_dict() org_id_2 = org_dictionary_2['id'] pass_code = TestEntityInfo.entity_lear_mock['passCode'] # create first row in affiliation table AffiliationService.create_affiliation(org_id, business_identifier1, pass_code) affiliation = AffiliationService.create_affiliation( org_id_2, business_identifier1, pass_code) assert affiliation
def test_delete_affiliation_reset_passcode(session, auth_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an affiliation can be deleted.""" entity_service = factory_entity_service(TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] patch_token_info(TestJwtClaims.public_account_holder_user, monkeypatch) org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] affiliation = AffiliationService.create_affiliation( org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) AffiliationService.delete_affiliation( org_id=org_id, business_identifier=business_identifier, email_addresses=None, reset_passcode=True) found_affiliation = AffiliationModel.query.filter_by( id=affiliation.identifier).first() assert found_affiliation is None
def test_delete_org_with_affiliation_fail(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that an org cannot be deleted.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_info=user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user.id) org_id = org.as_dict()['id'] entity_service = factory_entity_service(entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] AffiliationService.create_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode'], {}) with pytest.raises(BusinessException) as exception: OrgService.delete_org(org_id, TestJwtClaims.public_user_role) assert exception.value.code == Error.ORG_CANNOT_BE_DISSOLVED.name AffiliationService.delete_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) OrgService.delete_org(org.as_dict()['id'], TestJwtClaims.public_user_role) org_inactive = OrgService.find_by_org_id(org.as_dict()['id']) assert org_inactive.as_dict()['org_status'] == 'INACTIVE'
def test_find_affiliated_entities_by_org_id(session): # pylint:disable=unused-argument """Assert that an Affiliation can be created.""" entity_service1 = factory_entity_service(business_identifier='CP555') entity_dictionary1 = entity_service1.as_dict() business_identifier1 = entity_dictionary1['businessIdentifier'] entity_service2 = factory_entity_service(business_identifier='CP556') entity_dictionary2 = entity_service2.as_dict() business_identifier2 = entity_dictionary2['businessIdentifier'] org_service = factory_org_service(name='My Test Org') org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] # create first row in affiliation table AffiliationService.create_affiliation(org_id, business_identifier1) # 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) == 2 assert affiliated_entities[0]['businessIdentifier'] == entity_dictionary1[ 'businessIdentifier']
def test_find_affiliated_entities_by_org_id(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can be created.""" entity_service1 = factory_entity_service( entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary1 = entity_service1.as_dict() business_identifier1 = entity_dictionary1['business_identifier'] entity_service2 = factory_entity_service( entity_info=TestEntityInfo.entity_lear_mock2) 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 first row in affiliation table AffiliationService.create_affiliation( org_id, business_identifier1, TestEntityInfo.entity_lear_mock['passCode']) # create second row in affiliation table AffiliationService.create_affiliation( org_id, business_identifier2, TestEntityInfo.entity_lear_mock2['passCode']) affiliated_entities = AffiliationService.find_visible_affiliations_by_org_id( org_id) assert affiliated_entities assert len(affiliated_entities) == 2 assert affiliated_entities[0]['business_identifier'] == entity_dictionary2[ 'business_identifier']
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_affiliated_entities_by_org_id_no_org(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be find without org id or org id not exists.""" with pytest.raises(BusinessException) as exception: AffiliationService.find_visible_affiliations_by_org_id(None) assert exception.value.code == Error.DATA_NOT_FOUND.name with pytest.raises(BusinessException) as exception: AffiliationService.find_visible_affiliations_by_org_id(999999) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_create_affiliation_no_org(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be created without org.""" entity_service = factory_entity_service() entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(None, business_identifier) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_create_affiliation_no_entity(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be created without entity.""" org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(org_id, None) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_find_affiliated_entities_by_org_id_no_affiliation(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be find without affiliation.""" org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] with patch.object(AffiliationModel, 'find_affiliations_by_org_id', return_value=None): with pytest.raises(BusinessException) as exception: AffiliationService.find_affiliated_entities_by_org_id(org_id) assert exception.value.code == Error.DATA_NOT_FOUND.name
def delete(org_id, business_identifier): """Delete an affiliation between an org and an entity.""" try: AffiliationService.delete_affiliation(org_id, business_identifier) response, status = {}, http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, \ exception.status_code return response, status
def test_create_affiliation_with_passcode_no_passcode_input(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be created with a passcode entity and no passcode input parameter.""" entity_service = factory_entity_service(entity_info=TestEntityInfo.entity_passcode) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(org_id, business_identifier) assert exception.value.code == Error.INVALID_USER_CREDENTIALS.name
def delete(org_id, business_identifier): """Delete an affiliation between an org and an entity.""" request_json = request.get_json(silent=True) email_addresses = request_json.get('passcodeResetEmail') if request_json else None reset_passcode = request_json.get('resetPasscode') if request_json else False try: AffiliationService.delete_affiliation(org_id, business_identifier, email_addresses, reset_passcode) response, status = {}, http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, \ exception.status_code return response, status
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') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: response, status = AffiliationService.create_affiliation( org_id, request_json.get('businessIdentifier'), request_json.get('passCode'), token_info=g.jwt_oidc_token_info).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_create_affiliation_exists(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be created affiliation exists.""" entity_service1 = factory_entity_service(entity_info=TestEntityInfo.entity_lear_mock) entity_dictionary1 = entity_service1.as_dict() business_identifier1 = entity_dictionary1['businessIdentifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] pass_code = TestEntityInfo.entity_lear_mock['passCode'] # create first row in affiliation table AffiliationService.create_affiliation(org_id, business_identifier1, pass_code, {}) with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(org_id, business_identifier1, pass_code, {}) assert exception.value.code == Error.ALREADY_CLAIMED_PASSCODE.name
def test_create_affiliation_firms_party_not_valid(session, auth_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an Affiliation can be created.""" patch_get_firms_parties(monkeypatch) entity_service = factory_entity_service( entity_info=TestEntityInfo.entity_lear_mock3) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(org_id, business_identifier, 'test user') assert exception.value.code == Error.INVALID_USER_CREDENTIALS.name
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
def get(org_id): """Get all affiliated entities for the given org.""" try: response, status = jsonify({ 'entities': AffiliationService.find_visible_affiliations_by_org_id(org_id)}), http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_delete_affiliation_no_entity(session, auth_mock): # pylint:disable=unused-argument """Assert that an affiliation can not be deleted without entity.""" entity_service = factory_entity_service(TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] AffiliationService.create_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode'], {}) with pytest.raises(BusinessException) as exception: AffiliationService.delete_affiliation(org_id=org_id, business_identifier=None) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_delete_affiliation_implicit(session, auth_mock): # pylint:disable=unused-argument """Assert that an affiliation can be deleted.""" entity_service = factory_entity_service(TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] org_service = factory_org_service(org_type_info=TestOrgTypeInfo.implicit) org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] affiliation = AffiliationService.create_affiliation(org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode'], {}) AffiliationService.delete_affiliation(org_id=org_id, business_identifier=business_identifier) found_affiliation = AffiliationModel.query.filter_by(id=affiliation.identifier).first() assert found_affiliation is None
def test_create_affiliation_implicit(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be created when org is BASIC.""" entity_service1 = factory_entity_service() entity_dictionary1 = entity_service1.as_dict() business_identifier1 = entity_dictionary1['businessIdentifier'] org_service = factory_org_service(org_type_info=TestOrgTypeInfo.implicit) org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] pass_code = '111111111' with pytest.raises(BusinessException) as exception: AffiliationService.create_affiliation(org_id, business_identifier1, pass_code, {}) found_org = OrgModel.query.filter_by(id=org_id).first() assert found_org is None assert exception.value.code == Error.INVALID_USER_CREDENTIALS.name
def test_delete_affiliation(session): # pylint:disable=unused-argument """Assert that an affiliation can be deleted.""" entity_service = factory_entity_service() entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['businessIdentifier'] org_service = factory_org_service(name='My Test Org') org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] affiliation = AffiliationService.create_affiliation( org_id, business_identifier) AffiliationService.delete_affiliation( org_id=org_id, business_identifier=business_identifier) found_affiliation = AffiliationModel.query.filter_by( id=affiliation.identifier).first() assert found_affiliation is None
def test_find_affiliated_entities_by_org_id_no_affiliation(session, auth_mock): # pylint:disable=unused-argument """Assert that an Affiliation can not be find without affiliation.""" org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] with patch.object(AffiliationModel, 'find_affiliations_by_org_id', return_value=[]): affiliations = AffiliationService.find_visible_affiliations_by_org_id( org_id) assert not affiliations
def test_delete_affiliation_no_affiliation(session, auth_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an affiliation can not be deleted without affiliation.""" entity_service = factory_entity_service(TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] patch_token_info(TestJwtClaims.user_test, monkeypatch) org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] AffiliationService.create_affiliation( org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) AffiliationService.delete_affiliation( org_id=org_id, business_identifier=business_identifier, email_addresses=None) with pytest.raises(BusinessException) as exception: AffiliationService.delete_affiliation( org_id=org_id, business_identifier=business_identifier, email_addresses=None) assert exception.value.code == Error.DATA_NOT_FOUND.name
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_delete_affiliation(session, auth_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an affiliation can be deleted.""" entity_service = factory_entity_service(TestEntityInfo.entity_lear_mock) entity_dictionary = entity_service.as_dict() business_identifier = entity_dictionary['business_identifier'] org_service = factory_org_service() org_dictionary = org_service.as_dict() org_id = org_dictionary['id'] patch_token_info(TestJwtClaims.user_test, monkeypatch) with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: affiliation = AffiliationService.create_affiliation( org_id, business_identifier, TestEntityInfo.entity_lear_mock['passCode']) mock_alp.assert_called_with( Activity(action=ActivityAction.CREATE_AFFILIATION.value, org_id=ANY, name=ANY, id=ANY)) with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: AffiliationService.delete_affiliation( org_id=org_id, business_identifier=business_identifier, email_addresses=None) mock_alp.assert_called_with( Activity(action=ActivityAction.REMOVE_AFFILIATION.value, org_id=ANY, name=ANY, id=ANY)) found_affiliation = AffiliationModel.query.filter_by( id=affiliation.identifier).first() assert found_affiliation is None
def get(org_id): """Get all affiliated entities for the given org.""" try: response, status = jsonify( AffiliationService.find_affiliated_entities_by_org_id(org_id, g.jwt_oidc_token_info)), \ http_status.HTTP_200_OK 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(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