def test_my_mail_util(app, sqlalchemy_datastore): from flask_security import MailUtil class MyMailUtil(MailUtil): def send_mail(self, template, subject, recipient, sender, body, html, user, **kwargs): assert template == "reset_instructions" assert subject == app.config[ "SECURITY_EMAIL_SUBJECT_PASSWORD_RESET"] assert recipient == "*****@*****.**" assert user.email == "*****@*****.**" assert sender == "no-reply@localhost" assert isinstance(sender, str) init_app_with_options(app, sqlalchemy_datastore, **{"security_args": { "mail_util_cls": MyMailUtil }}) client = app.test_client() client.post("/reset", data=dict(email="*****@*****.**"))
def test_post_security_with_application_root_and_views(app, sqlalchemy_datastore): init_app_with_options( app, sqlalchemy_datastore, **{ "APPLICATION_ROOT": "/root", "SECURITY_POST_LOGIN_VIEW": "/post_login", "SECURITY_POST_LOGOUT_VIEW": "/post_logout", } ) client = app.test_client() response = client.post( "/login", data=dict(email="*****@*****.**", password="******") ) assert response.status_code == 302 assert response.headers["Location"] == "http://localhost/post_login" response = client.get("/logout") assert response.status_code == 302 assert response.headers["Location"] == "http://localhost/post_logout"
def test_myxlation(app, sqlalchemy_datastore, pytestconfig): # Test changing a single MSG and having an additional translation dir 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_modify_permissions_unsupported(app, datastore): from tests.conftest import PonyUserDatastore ds = datastore if hasattr(datastore.role_model, "permissions"): # already tested this return if isinstance(datastore, PonyUserDatastore): # sigh - Pony doesn't use RoleMixin. return init_app_with_options(app, ds) with app.app_context(): ds.create_role(name="test3") ds.commit() t3 = ds.find_role("test3") with raises(NotImplementedError): t3.add_permissions("whatever") with raises(NotImplementedError): t3.remove_permissions("whatever")
def test_verify_password_single_hash_list(app, sqlalchemy_datastore): init_app_with_options( app, sqlalchemy_datastore, **{ "SECURITY_PASSWORD_HASH": "bcrypt", "SECURITY_PASSWORD_SALT": "salty", "SECURITY_PASSWORD_SINGLE_HASH": ["django_pbkdf2_sha256", "plaintext"], "SECURITY_PASSWORD_SCHEMES": [ "bcrypt", "pbkdf2_sha256", "django_pbkdf2_sha256", "plaintext", ], } ) with app.app_context(): # double hash assert verify_password("pass", hash_password("pass")) assert verify_password("pass", pbkdf2_sha256.hash(get_hmac("pass"))) # single hash assert verify_password("pass", django_pbkdf2_sha256.hash("pass")) assert verify_password("pass", plaintext.hash("pass"))
def test_invalid_hash_scheme(app, sqlalchemy_datastore, get_message): with pytest.raises(ValueError): init_app_with_options( app, sqlalchemy_datastore, **{"SECURITY_PASSWORD_HASH": "bogus"} )
def test_flash_messages_off(app, sqlalchemy_datastore, get_message): init_app_with_options(app, sqlalchemy_datastore, **{"SECURITY_FLASH_MESSAGES": False}) client = app.test_client() response = client.get("/profile") assert get_message("LOGIN") not in response.data