def test_form_labels(app): app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" app.security = Security() app.security.init_app(app) assert check_xlation( app, "fr_FR"), "You must run python setup.py compile_catalog" with app.test_request_context(): rform = RegisterForm() assert str(rform.password.label.text) == "Mot de passe" assert str( rform.password_confirm.label.text) == "Confirmer le mot de passe" assert str(rform.email.label.text) == "Adresse email" assert str(rform.submit.label.text) == "Inscription" form = LoginForm() assert str(form.password.label.text) == "Mot de passe" assert str(form.remember.label.text) == "Se souvenir de moi" assert str(form.email.label.text) == "Adresse email" assert str(form.submit.label.text) == "Connexion" form = ChangePasswordForm() assert str(form.password.label.text) == "Mot de passe" assert str(form.new_password.label.text) == "Nouveau mot de passe" assert str(form.new_password_confirm.label.text ) == "Confirmer le mot de passe" assert str(form.submit.label.text) == "Changer le mot de passe"
def test_wtform_xlation(app, sqlalchemy_datastore): # Make sure wtform xlations work class MyLoginForm(LoginForm): fixed_length = StringField( "FixedLength", validators=[DataRequired(), Length(3, 3)] ) app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" app.security = Security() app.security.init_app(app, datastore=sqlalchemy_datastore, login_form=MyLoginForm) assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog" client = app.test_client() response = client.get("/login") assert b'<label for="password">Mot de passe</label>' in response.data data = dict( email="*****@*****.**", password="", remember="y", fixed_length="waytoolong" ) response = client.post( "/login", json=data, headers={"Content-Type": "application/json"} ) assert response.status_code == 400 flerror = response.json["response"]["errors"]["fixed_length"][0] # This is completely dependent on WTforms translations.... assert ( flerror == "Le doit contenir exactement 3 caractères." or flerror == "Le champ doit contenir exactement 3 caractères." )
def test_myxlation(app, sqlalchemy_datastore, pytestconfig): # Test changing a single MSG and having an additional translation dir # Flask-BabelEx doesn't support lists of directories.. try: import flask_babelex # noqa: F401 pytest.skip("Flask-BabelEx doesn't support lists of translations") except ImportError: pass i18n_dirname = [ pkg_resources.resource_filename("flask_security", "translations"), os.path.join(pytestconfig.rootdir, "tests/translations"), ] init_app_with_options( app, sqlalchemy_datastore, **{"SECURITY_I18N_DIRNAME": i18n_dirname} ) app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog" app.config["SECURITY_MSG_INVALID_PASSWORD"] = ("Password no-worky", "error") client = app.test_client() response = client.post("/login", data=dict(email="*****@*****.**", password="******")) assert b"Passe - no-worky" in response.data
def test_xlation(app, client): app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog" response = client.get("/login") assert b'<label for="password">Mot de passe</label>' in response.data response = authenticate(client) assert response.status_code == 302 response = authenticate(client, follow_redirects=True) assert b"Bienvenue [email protected]" in response.data
def test_xlation(app, client, get_message_local): # Test form and email translation app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog" response = client.get("/register", follow_redirects=True) with app.app_context(): # Check header assert ( f'<h1>{localize_callback("Register")}</h1>'.encode("utf-8") in response.data ) submit = localize_callback(_default_field_labels["register"]) assert f'value="{submit}"'.encode("utf-8") in response.data with app.mail.record_messages() as outbox: response = client.post( "/register", data={ "email": "*****@*****.**", "password": "******", "password_confirm": "new strong password", }, follow_redirects=True, ) with app.app_context(): assert ( get_message_local("CONFIRM_REGISTRATION", email="*****@*****.**").encode("utf-8") in response.data ) assert b"Home Page" in response.data assert len(outbox) == 1 assert ( localize_callback(app.config["SECURITY_EMAIL_SUBJECT_REGISTER"]) in outbox[0].subject ) assert ( str( jinja2.escape( localize_callback( "You can confirm your email through the link below:" ) ) ) in outbox[0].html ) assert ( localize_callback("You can confirm your email through the link below:") in outbox[0].body )
def test_xlation(app, client, get_message_local): # Test form and email translation app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR" assert check_xlation( app, "fr_FR"), "You must run python setup.py compile_catalog" authenticate(client) response = client.get("/change", follow_redirects=True) with app.app_context(): # Check header assert ( f'<h1>{localize_callback("Change password")}</h1>'.encode("utf-8") in response.data) submit = localize_callback(_default_field_labels["change_password"]) assert f'value="{submit}"'.encode("utf-8") in response.data with app.mail.record_messages() as outbox: response = client.post( "/change", data={ "password": "******", "new_password": "******", "new_password_confirm": "new strong password", }, follow_redirects=True, ) with app.app_context(): assert get_message_local("PASSWORD_CHANGE").encode( "utf-8") in response.data assert b"Home Page" in response.data assert len(outbox) == 1 assert (localize_callback( app.config["SECURITY_EMAIL_SUBJECT_PASSWORD_CHANGE_NOTICE"]) in outbox[0].subject) assert (str( markupsafe.escape( localize_callback("Your password has been changed."))) in outbox[0].html) assert localize_callback( "Your password has been changed") in outbox[0].body