def test_application_for_native_app_user(app): # Given users_api.create_account( email=JOUVE_CONTENT["email"], password="******", birthdate=AGE18_ELIGIBLE_BIRTH_DATE, is_email_validated=True, send_activation_mail=False, marketing_email_subscription=False, phone_number="0607080900", ) push_testing.reset_requests() # When create_beneficiary_from_application.execute(APPLICATION_ID) # Then beneficiary = User.query.one() # the fake Jouve backend returns a default phone number. Since a User # alredy exists, the phone number should not be updated during the import process assert beneficiary.phoneNumber == "0607080900" deposit = Deposit.query.one() assert deposit.amount == 300 assert deposit.source == "dossier jouve [35]" assert deposit.userId == beneficiary.id beneficiary_import = BeneficiaryImport.query.one() assert beneficiary_import.currentStatus == ImportStatus.CREATED assert beneficiary_import.applicationId == APPLICATION_ID assert beneficiary_import.beneficiary == beneficiary assert beneficiary.notificationSubscriptions == {"marketing_push": True, "marketing_email": False} assert len(push_testing.requests) == 1
def test_application_for_native_app_user(mocked_send_accepted_as_beneficiary_email, app): # Given application_id = 35 users_api.create_account( email=PRE_SUBSCRIPTION_BASE_DATA["email"], password="******", birthdate=PRE_SUBSCRIPTION_BASE_DATA["date_of_birth"], is_email_validated=True, send_activation_mail=False, ) # When create_beneficiary_from_application.execute(application_id) # Then mocked_send_accepted_as_beneficiary_email.assert_called_once() beneficiary = User.query.one() deposit = Deposit.query.one() assert deposit.amount == 500 assert deposit.source == "dossier jouve [35]" assert deposit.userId == beneficiary.id beneficiary_import = BeneficiaryImport.query.one() assert beneficiary_import.currentStatus == ImportStatus.CREATED assert beneficiary_import.applicationId == application_id assert beneficiary_import.beneficiary == beneficiary
def create_or_update_users(rows: Iterable[dict]) -> list[User]: # The purpose of this function is to recreate test users on # staging after the staging database is reset. It's not meant to # be used anywhere else, and certainly not on production. if settings.IS_PROD: raise ValueError( "This function is not supposed to be run on production") users = [] for row in rows: user = find_user_by_email(row["Mail"]) birthdate = datetime.strptime(row["Date de naissance"], "%Y-%m-%d") if user: user.dateOfBirth = birthdate user.setPassword(settings.STAGING_TEST_USER_PASSWORD) else: user = users_api.create_account( email=sanitize_email(row["Mail"]), password=settings.STAGING_TEST_USER_PASSWORD, birthdate=birthdate, is_email_validated=True, send_activation_mail=False, remote_updates=False, ) deposit = payments_api.create_deposit(user, "import_users (csv)") repository.save(deposit) user.add_beneficiary_role() user.lastName = row["Nom"] user.firstName = row["Prénom"] user.publicName = f"{user.firstName} {user.lastName}" user.phoneNumber = row["Téléphone"] user.departementCode = row["Département"] user.postalCode = row["Code postal"] repository.save(user) users.append(user) logger.info("Created or updated user=%s from CSV import", user.id) admin = find_user_by_email("*****@*****.**") if not admin: admin = users_api.create_account( email="*****@*****.**", password=settings.STAGING_TEST_USER_PASSWORD, birthdate=datetime(1946, 12, 24), is_email_validated=True, send_activation_mail=False, remote_updates=False, ) admin.setPassword(settings.STAGING_TEST_USER_PASSWORD) admin.remove_beneficiary_role() admin.add_admin_role() admin.firstName = "Jeanne" admin.lastName = "Admin" admin.publicName = f"{user.firstName} {user.lastName}" repository.save(admin) logger.info("Created or updated admin user=%s", admin.id) return users
def test_import_native_app_user(self): # given user = users_api.create_account( email=self.EMAIL, password="******", birthdate=self.BENEFICIARY_BIRTH_DATE, is_email_validated=True, send_activation_mail=False, ) # when remote_import.run( ONE_WEEK_AGO, get_details=self._get_details, get_all_applications_ids=self._get_all_applications_ids, ) # then assert User.query.count() == 1 user = User.query.first() assert user.firstName == "john" assert user.postalCode == "93450" assert BeneficiaryImport.query.count() == 1 beneficiary_import = BeneficiaryImport.query.first() assert beneficiary_import.source == "demarches_simplifiees" assert beneficiary_import.applicationId == 123 assert beneficiary_import.beneficiary == user assert beneficiary_import.currentStatus == ImportStatus.CREATED assert len(mails_testing.outbox) == 1 assert mails_testing.outbox[0].sent_data["Mj-TemplateID"] == 2016025
def create_account(body: serializers.AccountRequest) -> None: if settings.NATIVE_ACCOUNT_CREATION_REQUIRES_RECAPTCHA: try: check_recaptcha_token_is_valid(body.token, "submit", settings.RECAPTCHA_RESET_PASSWORD_MINIMAL_SCORE) except ReCaptchaException: raise ApiErrors({"token": "The given token is not invalid"}) try: api.create_account( email=body.email, password=body.password, birthdate=body.birthdate, has_allowed_recommendations=body.has_allowed_recommendations, is_email_validated=False, ) except UserAlreadyExistsException: user = find_user_by_email(body.email) api.request_password_reset(user)
def create_account(body: serializers.AccountRequest) -> None: if feature_queries.is_active(FeatureToggle.ENABLE_NATIVE_APP_RECAPTCHA): try: api_recaptcha.check_native_app_recaptcha_token(body.token) except api_recaptcha.ReCaptchaException: raise ApiErrors({"token": "The given token is not invalid"}) try: api.create_account( email=body.email, password=body.password, birthdate=body.birthdate, has_allowed_recommendations=body.has_allowed_recommendations, is_email_validated=False, ) except exceptions.UserAlreadyExistsException: user = find_user_by_email(body.email) api.request_password_reset(user) except exceptions.UnderAgeUserException: raise ApiErrors({"dateOfBirth": "The birthdate is invalid"})
def test_updates_existing_user(self): # given beneficiary_information = fraud_models.DMSContent( department="67", last_name="Doe", first_name="Jane", activity="Lycéen", civility="Mme", birth_date=datetime.date(2000, 5, 1), email="*****@*****.**", phone="0612345678", postal_code="67200", address="11 Rue du Test", application_id=123, procedure_id=98012, ) user = users_api.create_account( email=beneficiary_information.email, password="******", birthdate=beneficiary_information.birth_date, send_activation_mail=False, ) db.session.add(user) db.session.flush() # when beneficiary = create_beneficiary_from_application( beneficiary_information, user=user) db.session.add(beneficiary) db.session.flush() # Then assert users_models.User.query.count() == 1 assert beneficiary.lastName == "Doe" assert beneficiary.firstName == "Jane" assert beneficiary.publicName == "Jane Doe" assert beneficiary.email == "*****@*****.**" assert beneficiary.phoneNumber == "0612345678" assert beneficiary.departementCode == "67" assert beneficiary.postalCode == "67200" assert beneficiary.address == "11 Rue du Test" assert beneficiary.dateOfBirth == datetime.datetime(2000, 5, 1) assert not beneficiary.has_beneficiary_role assert not beneficiary.isAdmin assert beneficiary.password is not None assert beneficiary.activity == "Lycéen" assert beneficiary.civility == "Mme" assert not beneficiary.hasSeenTutorials assert not beneficiary.deposits
def create_account(body: serializers.AccountRequest) -> None: if FeatureToggle.ENABLE_NATIVE_APP_RECAPTCHA.is_active(): try: api_recaptcha.check_native_app_recaptcha_token(body.token) except api_recaptcha.ReCaptchaException: raise ApiErrors({"token": "The given token is not invalid"}) try: api.create_account( email=body.email, password=body.password, birthdate=body.birthdate, marketing_email_subscription=body.marketing_email_subscription, is_email_validated=False, postal_code=body.postal_code, apps_flyer_user_id=body.apps_flyer_user_id, apps_flyer_platform=body.apps_flyer_platform, ) except exceptions.UserAlreadyExistsException: user = find_user_by_email(body.email) if not user.isEmailValidated: try: api.initialize_account( user, body.password, apps_flyer_user_id=body.apps_flyer_user_id, apps_flyer_platform=body.apps_flyer_platform, ) except exceptions.EmailNotSent: raise ApiErrors({"email": ["L'email n'a pas pu être envoyé"]}) else: try: api.request_password_reset(user) except exceptions.EmailNotSent: raise ApiErrors({"email": ["L'email n'a pas pu être envoyé"]}) except exceptions.UnderAgeUserException: raise ApiErrors({"dateOfBirth": "The birthdate is invalid"})
def test_updates_existing_user(self): # given THIRTY_DAYS_FROM_NOW = (datetime.utcnow() + timedelta(days=30)).date() beneficiary_information = { "department": "67", "last_name": "Doe", "first_name": "Jane", "activity": "Lycéen", "civility": "Mme", "birth_date": datetime(2000, 5, 1), "email": "*****@*****.**", "phone": "0612345678", "postal_code": "67200", "application_id": 123, } user = users_api.create_account( email=beneficiary_information["email"], password="******", birthdate=beneficiary_information["birth_date"], ) db.session.add(user) db.session.flush() # when beneficiary = create_beneficiary_from_application( beneficiary_information, user=user) db.session.add(beneficiary) db.session.flush() # Then assert users_models.User.query.count() == 1 assert beneficiary.lastName == "Doe" assert beneficiary.firstName == "Jane" assert beneficiary.publicName == "Jane Doe" assert beneficiary.email == "*****@*****.**" assert beneficiary.phoneNumber == "0612345678" assert beneficiary.departementCode == "67" assert beneficiary.postalCode == "67200" assert beneficiary.dateOfBirth == datetime(2000, 5, 1) assert beneficiary.isBeneficiary == True assert beneficiary.isAdmin == False assert beneficiary.password is not None assert beneficiary.resetPasswordToken is not None assert beneficiary.resetPasswordTokenValidityLimit.date( ) == THIRTY_DAYS_FROM_NOW assert beneficiary.activity == "Lycéen" assert beneficiary.civility == "Mme" assert beneficiary.hasSeenTutorials == False