def test_get_beneficiary_import_for_beneficiary(self): """Create 'BeneficiaryImport's with different statuses and check that the last one created is the one returned. """ user = users_factories.UserFactory() source = BeneficiaryImportSources.demarches_simplifiees.value for rejected_import in users_factories.BeneficiaryImportFactory.create_batch( 3, beneficiary=user, source=source): users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=rejected_import, status=ImportStatus.REJECTED) # The created status is set to a random datetime in the past (yesterday at most) created_import = users_factories.BeneficiaryImportFactory( beneficiary=user) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=created_import, status=ImportStatus.CREATED) latest_created_import = users_factories.BeneficiaryImportFactory( beneficiary=user) latest_created_import.setStatus(ImportStatus.CREATED) beneficiary_import = subscription_repository.get_beneficiary_import_for_beneficiary( user) assert beneficiary_import.id == latest_created_import.id
def test_already_processed_application_ids_right_procedure(self): created = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=123, status=ImportStatus.CREATED, ) another_procedure = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=456, status=ImportStatus.CREATED, ) application_ids = get_already_processed_applications_ids(123) assert created.beneficiaryImport.applicationId in application_ids assert another_procedure.beneficiaryImport.applicationId not in application_ids
def test_archive_applications_only_archive_beneficiary( self, dms_archive, dms_applications, caplog): caplog.set_level(logging.INFO) application_id = 42 user_to_archive = users_factories.BeneficiaryGrant18Factory() beneficiary_import = users_factories.BeneficiaryImportFactory( sourceId=self.PROCEDURE_ID, applicationId=application_id, beneficiary=user_to_archive) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=ImportStatus.CREATED) user_to_not_archive = users_factories.UserFactory() application_to_archive = make_graphql_application( application_id, "closed", email=user_to_archive.email) dms_applications.return_value = [ make_graphql_application(20, "closed", email=user_to_not_archive.email), application_to_archive, ] archive_dms_applications.archive_applications(self.PROCEDURE_ID, dry_run=False) assert dms_archive.call_count == 1 assert dms_archive.call_args == [(application_to_archive["id"], "SomeInstructorId")] assert ( caplog.messages[0] == f"Archiving application {application_id} on procedure {self.PROCEDURE_ID} for user_id {user_to_archive.id}" ) assert caplog.messages[ 1] == "script ran : total applications : 2 to archive applications : 1"
def test_returns_min_year_if_no_beneficiary_import_exist_for_given_source_id( self, app): # given old_source_id = 1 new_source_id = 2 now = datetime.utcnow() yesterday = now - timedelta(days=1) user = users_factories.BeneficiaryGrant18Factory( dateCreated=yesterday, email="*****@*****.**") beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user, applicationId=3, sourceId=old_source_id, source=BeneficiaryImportSources.demarches_simplifiees.value, ) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=ImportStatus.CREATED, date=yesterday) # when most_recent_creation_date = find_most_recent_beneficiary_creation_date_for_source( BeneficiaryImportSources.demarches_simplifiees, new_source_id) # then assert most_recent_creation_date == datetime(MINYEAR, 1, 1)
def test_update_beneficiary_id_piece_number_from_dms_data_with_existing_beneficiary_import( self, client): user = users_factories.UserFactory() fraud_check = fraud_factories.BeneficiaryFraudCheckFactory( user=user, type=fraud_models.FraudCheckType.DMS) fraud_factories.BeneficiaryFraudResultFactory( user=user, status=fraud_models.FraudStatus.SUSPICIOUS) dms_data = fraud_check.source_data() beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user, applicationId=dms_data.application_id, sourceId=dms_data.procedure_id, source=pcapi.models.BeneficiaryImportSources.demarches_simplifiees. value, ) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import) client.with_session_auth(self.jouve_admin.email) id_piece_number = "123123123123" response = client.post( f"/pc/back-office/support_beneficiary/update/beneficiary/id_piece_number/{user.id}", form={"id_piece_number": id_piece_number}, ) assert response.status_code == 302 assert user.beneficiaryFraudResult.status == fraud_models.FraudStatus.OK assert fraud_check.resultContent["id_piece_number"] == id_piece_number assert user.idPieceNumber == id_piece_number assert user.has_beneficiary_role assert len(user.beneficiaryImports) == 1 assert user.beneficiaryImports[ 0].currentStatus == pcapi.models.ImportStatus.CREATED
def test_returns_an_empty_list_if_no_retry_imports_exist(self, app): # given beneficiary = users_factories.BeneficiaryGrant18Factory() users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=456, status=ImportStatus.DUPLICATE) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=123, status=ImportStatus.CREATED) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=789, beneficiaryImport__beneficiary=beneficiary, status=ImportStatus.CREATED, ) # when ids = find_applications_ids_to_retry() # then assert not ids
def test_returns_applications_ids_with_current_status_retry(self, app): # given users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=456, status=ImportStatus.RETRY) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=123, status=ImportStatus.RETRY) beneficiary = users_factories.BeneficiaryGrant18Factory() users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__beneficiary=beneficiary, beneficiaryImport__applicationId=789, status=ImportStatus.CREATED, ) # when ids = find_applications_ids_to_retry() # then assert ids == [123, 456]
def test_returns_created_at_date_of_most_recent_beneficiary_import_with_created_status_for_one_procedure( self, app): # given source_id = 1 now = datetime.utcnow() yesterday = now - timedelta(days=1) two_days_ago = now - timedelta(days=2) three_days_ago = now - timedelta(days=3) users_factories.BeneficiaryGrant18Factory(dateCreated=yesterday, email="*****@*****.**") user2 = users_factories.BeneficiaryGrant18Factory( dateCreated=two_days_ago, email="*****@*****.**") user3 = users_factories.BeneficiaryGrant18Factory( dateCreated=three_days_ago, email="*****@*****.**") beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user2, applicationId=1, sourceId=source_id, source=BeneficiaryImportSources.demarches_simplifiees.value, ) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=ImportStatus.ERROR, date=two_days_ago) beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user3, applicationId=3, sourceId=source_id, source=BeneficiaryImportSources.demarches_simplifiees.value, ) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=ImportStatus.CREATED, date=three_days_ago) # when most_recent_creation_date = find_most_recent_beneficiary_creation_date_for_source( BeneficiaryImportSources.demarches_simplifiees, source_id) # then assert most_recent_creation_date == three_days_ago
def test_already_processed_application_ids(self): procedure_id = 123 draft = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.DRAFT, ) ongoing = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.ONGOING, ) duplicate = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.DUPLICATE, ) error = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.ERROR, ) created = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.CREATED, ) rejected = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.REJECTED, ) retry = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.RETRY, ) without_continuation = users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__sourceId=procedure_id, status=ImportStatus.WITHOUT_CONTINUATION, ) application_ids = get_already_processed_applications_ids(procedure_id) assert draft.beneficiaryImport.applicationId not in application_ids assert ongoing.beneficiaryImport.applicationId not in application_ids assert without_continuation.beneficiaryImport.applicationId not in application_ids assert retry.beneficiaryImport.applicationId not in application_ids assert rejected.beneficiaryImport.applicationId in application_ids assert error.beneficiaryImport.applicationId in application_ids assert created.beneficiaryImport.applicationId in application_ids assert duplicate.beneficiaryImport.applicationId in application_ids
def test_returns_true_when_a_beneficiary_import_exist_with_imported_status( self, valid_status): # given now = datetime.utcnow() beneficiary = users_factories.BeneficiaryGrant18Factory( dateCreated=now) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__applicationId=123, beneficiaryImport__beneficiary=beneficiary, status=valid_status) # when result = is_already_imported(123) # then assert result is True
def create_beneficiary_imports( beneficiary_user: users_models.User) -> list[BeneficiaryImport]: beneficiary_imports = [] index_of_beneficiary_imports = 1 for status in ImportStatus: user = beneficiary_user if status == ImportStatus.CREATED else None beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user, applicationId=index_of_beneficiary_imports) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=status) index_of_beneficiary_imports += 1 repository.save(*beneficiary_imports) logger.info("created %i beneficiary imports and status", len(beneficiary_imports)) return beneficiary_imports
def test_returns_false_when_no_beneficiary_import_exist_for_this_id( self, app): # given now = datetime.utcnow() beneficiary = users_factories.BeneficiaryGrant18Factory( dateCreated=now) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport__beneficiary=beneficiary, beneficiaryImport__applicationId=123, status=ImportStatus.CREATED, ) # when result = is_already_imported(456) # then assert result is False
def test_archive_applications(self, dms_archive, dms_applications): application_id = 42 user = users_factories.BeneficiaryGrant18Factory() beneficiary_import = users_factories.BeneficiaryImportFactory( sourceId=self.PROCEDURE_ID, applicationId=application_id, beneficiary=user) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import, status=ImportStatus.CREATED) dms_applications.return_value = [ make_graphql_application(application_id, "closed", email=user.email) ] archive_dms_applications.archive_applications(self.PROCEDURE_ID, dry_run=False) assert dms_archive.call_count == 1
def test_user_application_already_have_another_dms_application( self, import_status): user = users_factories.UserFactory() application_id = 42 procedure_id = 21 beneficiary_import = users_factories.BeneficiaryImportFactory( beneficiary=user, source=BeneficiaryImportSources.demarches_simplifiees.value, applicationId=143, sourceId=procedure_id, ) users_factories.BeneficiaryImportStatusFactory( beneficiaryImport=beneficiary_import) subscription_api.attach_beneficiary_import_details( user, application_id, procedure_id, BeneficiaryImportSources.demarches_simplifiees, "random details", import_status, ) assert BeneficiaryImport.query.count() == 2