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_bcol_account_not_exists(session): # pylint:disable=unused-argument """Assert that the BCOL account is not exists.""" factory_org_service(bcol_info=TestBCOLInfo.bcol1) check_result = OrgService.bcol_account_link_check( TestBCOLInfo.bcol2['bcol_account_id']) assert not check_result
def test_patch_org_status(session, monkeypatch, auth_mock): # pylint:disable=unused-argument """Assert that an Org status can be updated.""" org = factory_org_service() user = factory_user_model_with_contact() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value) patch_token_info(token_info, monkeypatch) # Validate and update org status patch_info = { 'action': PatchActions.UPDATE_STATUS.value, 'statusCode': OrgStatus.SUSPENDED.value, } with pytest.raises(BusinessException) as exception: org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) assert exception.value.code == Error.INVALID_INPUT.name patch_info['suspensionReasonCode'] = SuspensionReasonCode.OWNER_CHANGE.name with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: updated_org = org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) mock_alp.assert_called_with(Activity(action=ActivityAction.ACCOUNT_SUSPENSION.value, org_id=ANY, name=ANY, id=ANY, value=SuspensionReasonCode.OWNER_CHANGE.value)) assert updated_org['status_code'] == OrgStatus.SUSPENDED.value patch_info = { 'action': PatchActions.UPDATE_STATUS.value, 'statusCode': OrgStatus.ACTIVE.value, } updated_org = org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) assert updated_org['status_code'] == OrgStatus.ACTIVE.value with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: OrgService.update_login_option(org._model.id, 'BCROS') mock_alp.assert_called_with(Activity(action=ActivityAction.AUTHENTICATION_METHOD_CHANGE.value, org_id=ANY, name=ANY, id=ANY, value='BCROS'))
def test_update_org(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org can be updated.""" org = factory_org_service() org.update_org(TestOrgInfo.update_org_with_business_type) dictionary = org.as_dict() assert dictionary['business_type'] == TestOrgInfo.update_org_with_business_type['businessType']
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 test_update_org_name(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org name cannot be updated.""" org = factory_org_service() with pytest.raises(BusinessException) as exception: org.update_org(TestOrgInfo.org2) assert exception.value.code == Error.INVALID_INPUT.name
def test_update_contact_no_contact(session): # pylint:disable=unused-argument """Assert that a contact for a non-existent contact cannot be updated.""" org = factory_org_service() with pytest.raises(BusinessException) as exception: org.update_contact(TestContactInfo.contact2) assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_delete_contact_org_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if it's still being used by an entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = entity._model # pylint:disable=protected-access contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() OrgService.delete_contact(org_id=org_id) org = OrgService.find_by_org_id(org_id) response = OrgService.get_contacts(org_id) assert len(response['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert not exist_contact_link
def test_add_contact(session): # pylint:disable=unused-argument """Assert that a contact can be added to an org.""" org = factory_org_service() org_dictionary = org.as_dict() contact = OrgService.add_contact(org_dictionary['id'], TestContactInfo.contact1) dictionary = contact.as_dict() assert dictionary['email'] == TestContactInfo.contact1['email']
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_as_dict(session): # pylint:disable=unused-argument """Assert that the Org is exported correctly as a dictinoary.""" org = factory_org_service() dictionary = org.as_dict() assert dictionary assert dictionary['name'] == TestOrgInfo.org1['name']
def test_delete_contact_entity_link(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted without entity.""" entity_model = factory_entity_model() entity = EntityService(entity_model) org = factory_org_service() org_dictionary = org.as_dict() org_id = org_dictionary['id'] contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.entity = entity._model # pylint:disable=protected-access contact_link.org = org._model # pylint:disable=protected-access contact_link.commit() updated_entity = entity.delete_contact() dictionary = None dictionary = updated_entity.as_dict() assert len(dictionary['contacts']) == 0 delete_contact_link = ContactLinkModel.find_by_entity_id(entity.identifier) assert not delete_contact_link exist_contact_link = ContactLinkModel.find_by_org_id(org_id) assert exist_contact_link
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_org_with_duplicate_name_bcol(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org linking to bcol can be created when the name is duplicated.""" org = factory_org_service() factory_org_model({'name': 'BC ONLINE TECHNICAL TEAM DEVL'}, org_type_info=TestOrgTypeInfo.implicit, org_status_info=None, payment_type_info=None) bcol_response = Mock(spec=Response) bcol_response.json.return_value = { 'userId': 'PB25020', 'accountNumber': '180670', 'orgName': 'BC ONLINE TECHNICAL TEAM DEVL' } bcol_response.status_code = 200 pay_api_response = Mock(spec=Response) pay_api_response.status_code = 201 with patch.object(RestService, 'post', side_effect=[bcol_response, pay_api_response]): user = factory_user_model() org = OrgService.create_org(TestOrgInfo.bcol_linked(), user_id=user.id) assert org
def test_create_org_with_duplicate_name_bcol(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org linking to bcol retrun exception if there's duplicated names.""" org = factory_org_service() factory_org_model({'name': 'BC ONLINE TECHNICAL TEAM DEVL'}, org_type_info=TestOrgTypeInfo.implicit, org_status_info=None, payment_type_info=None) bcol_response = Mock(spec=Response) bcol_response.json.return_value = { 'userId': 'PB25020', 'accountNumber': '180670', 'orgName': 'BC ONLINE TECHNICAL TEAM DEVL' } bcol_response.status_code = 200 pay_api_response = Mock(spec=Response) pay_api_response.status_code = 201 with patch.object(RestService, 'post', side_effect=[bcol_response, pay_api_response]): user = factory_user_model() with pytest.raises(BusinessException) as exception: org.create_org(TestOrgInfo.bcol_linked(), user_id=user.id) assert exception.value.code == Error.DATA_CONFLICT.name
def test_add_contact_duplicate(session): # pylint:disable=unused-argument """Assert that a contact cannot be added to an Org if that Org already has a contact.""" org = factory_org_service() org.add_contact(TestContactInfo.contact1) with pytest.raises(BusinessException) as exception: org.add_contact(TestContactInfo.contact2) assert exception.value.code == Error.DATA_ALREADY_EXISTS.name
def test_add_contact(session): # pylint:disable=unused-argument """Assert that a contact can be added to an org.""" org = factory_org_service() org.add_contact(TestContactInfo.contact1) dictionary = org.as_dict() assert dictionary['contacts'] assert len(dictionary['contacts']) == 1 assert dictionary['contacts'][0]['email'] == TestContactInfo.contact1['email']
def test_update_org(session): # pylint:disable=unused-argument """Assert that an Org can be updated.""" org = factory_org_service() org.update_org(TestOrgInfo.org2) dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org2['name']
def test_change_org_access_type(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org can be updated.""" org = factory_org_service() user = factory_user_model_with_contact() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value) patch_token_info(token_info, monkeypatch) updated_org = org.change_org_access_type(AccessType.GOVN.value) assert updated_org.as_dict()['access_type'] == AccessType.GOVN.value
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_create_org_with_similar_name(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org with similar name can be created.""" user = factory_user_model() org = factory_org_service() new_org = org.create_org({'name': 'My Test'}, user_id=user.id) dictionary = new_org.as_dict() assert dictionary['name'] == 'My Test'
def test_raise_error_if_duplicate_name(session, auth_mock): # pylint:disable=unused-argument """Assert that when org name is duplicate, DATA_CONFILICT error will be raised.""" org = factory_org_service() dictionary = org.as_dict() new_org_name = dictionary['name'] with pytest.raises(BusinessException) as exception: OrgService.raise_error_if_duplicate_name(new_org_name) assert exception.value.code == Error.DATA_CONFLICT.name
def test_find_org_by_id(session, auth_mock): # pylint:disable=unused-argument """Assert that an org can be retrieved by its id.""" org = factory_org_service() dictionary = org.as_dict() org_id = dictionary['id'] found_org = OrgService.find_by_org_id(org_id) assert found_org dictionary = found_org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name']
def test_find_org_by_name(session, auth_mock): # pylint:disable=unused-argument """Assert that an org can be retrieved by its name.""" org = factory_org_service() dictionary = org.as_dict() org_name = dictionary['name'] found_org = OrgService.find_by_org_name(org_name) assert found_org assert found_org.get('orgs')[0].get('name') == org_name
def test_update__duplicate_org(session): # pylint:disable=unused-argument """Assert that an Org cannot be updated.""" org = factory_org_service() factory_org_model(org_info=TestOrgInfo.org2, org_type_info=TestOrgTypeInfo.implicit, org_status_info=None, payment_type_info=None) with pytest.raises(BusinessException) as exception: org.update_org(TestOrgInfo.org2) assert exception.value.code == Error.DATA_CONFLICT.name
def test_delete_contact_no_org(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted without org.""" org = factory_org_service() org.add_contact(TestContactInfo.contact1) updated_org = org.delete_contact() with pytest.raises(BusinessException) as exception: updated_org.delete_contact() assert exception.value.code == Error.DATA_NOT_FOUND.name
def test_create_org_with_duplicate_name(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org with duplicate name cannot be created.""" user = factory_user_model() org = factory_org_service() factory_org_model(org_info=TestOrgInfo.org2, org_type_info=TestOrgTypeInfo.implicit) with pytest.raises(BusinessException) as exception: patch_token_info({'sub': user.keycloak_guid}, monkeypatch) org.create_org(TestOrgInfo.org2, user_id=user.id) assert exception.value.code == Error.DATA_CONFLICT.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 test_create_org_with_duplicate_name(session): # pylint:disable=unused-argument """Assert that an Org with duplicate name cannot be created.""" user = factory_user_model() org = factory_org_service() factory_org_model(org_info=TestOrgInfo.org2, org_type_info=TestOrgTypeInfo.implicit, org_status_info=None, payment_type_info=None) with pytest.raises(BusinessException) as exception: org.create_org(TestOrgInfo.org2, user_id=user.id) assert exception.value.code == Error.DATA_CONFLICT.name
def test_delete_contact_no_org(session, auth_mock): # pylint:disable=unused-argument """Assert that a contact can not be deleted if it doesn't exist.""" org = factory_org_service() org_dictionary = org.as_dict() OrgService.add_contact(org_dictionary['id'], TestContactInfo.contact1) OrgService.delete_contact(org_dictionary['id']) with pytest.raises(BusinessException) as exception: OrgService.delete_contact(org_dictionary['id']) assert exception.value.code == Error.DATA_NOT_FOUND.name