From 1a4d265ad7a92e58be67b877c9db5e8f1e0eed5c Mon Sep 17 00:00:00 2001 From: Nicola Tarocco Date: Fri, 20 Oct 2023 14:54:27 +0200 Subject: [PATCH] email: force lowercase --- invenio_accounts/models.py | 12 +++++++++++- tests/conftest.py | 4 ++-- tests/test_hash.py | 2 +- tests/test_utils.py | 6 +++--- tests/test_views_rest.py | 4 ++-- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/invenio_accounts/models.py b/invenio_accounts/models.py index 30b698e1..db81bf71 100644 --- a/invenio_accounts/models.py +++ b/invenio_accounts/models.py @@ -100,7 +100,7 @@ class User(db.Model, Timestamp, UserMixin): _displayname = db.Column("displayname", db.String(255), nullable=True) """Case-preserving version of the username.""" - email = db.Column(db.String(255), unique=True) + _email = db.Column("email", db.String(255), unique=True) """User email.""" password = db.Column(db.String(255)) @@ -203,6 +203,16 @@ def username(self, username): self._displayname = username self._username = username.lower() + @hybrid_property + def email(self): + """Get email.""" + return self._email + + @email.setter + def email(self, email): + """Set lowercase email.""" + self._email = email.lower() + @hybrid_property def user_profile(self): """Get the user profile.""" diff --git a/tests/conftest.py b/tests/conftest.py index 931a5510..0bee29e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -284,8 +284,8 @@ def admin_view(app): @pytest.fixture() def users(app): """Create users.""" - user1 = create_test_user(email="info@inveniosoftware.org", password="tester") - user2 = create_test_user(email="info2@inveniosoftware.org", password="tester2") + user1 = create_test_user(email="INFO@inveniosoftware.org", password="tester") + user2 = create_test_user(email="info2@invenioSOFTWARE.org", password="tester2") return [ { diff --git a/tests/test_hash.py b/tests/test_hash.py index f724feb3..250b1c96 100644 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -101,7 +101,7 @@ def test_invenio_aes_encrypted_email(app): def test_user_login(app): """Test users' high-level login process.""" with app.app_context(): - user = create_test_user("test@test.org") + user = create_test_user("test@TEST.org") with app.test_client() as client: login_user_via_view(client, user.email, user.password_plaintext) assert client_authenticated(client) diff --git a/tests/test_utils.py b/tests/test_utils.py index d279aef5..36a51f4a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,7 +29,7 @@ def test_client_authenticated(app): authenticated/logged in. """ ds = app.extensions["security"].datastore - email = "test@test.org" + email = "test@TEST.org" password = "123456" with app.app_context(): @@ -110,7 +110,7 @@ def test_create_test_user(app): def test_create_test_user_defaults(app): """Test the default values for testutils.py:create_test_user.""" with app.app_context(): - user = testutils.create_test_user("test@test.org") + user = testutils.create_test_user("test@TEST.org") with app.test_client() as client: testutils.login_user_via_view(client, user.email, user.password_plaintext) assert testutils.client_authenticated(client) @@ -118,7 +118,7 @@ def test_create_test_user_defaults(app): def test_login_user_via_view(app): """Test the login-via-view function/hack.""" - email = "test@test.org" + email = "TEST@test.org" password = "1234" with app.app_context(): diff --git a/tests/test_views_rest.py b/tests/test_views_rest.py index 39446a46..abb5d9fe 100644 --- a/tests/test_views_rest.py +++ b/tests/test_views_rest.py @@ -57,13 +57,13 @@ def _mock_send_confirmation_mail(subject, recipient, template, **context): ) -def _login_user(client, user, email="normal@test.com", password="123456"): +def _login_user(client, user, email="normal@TEST.com", password="123456"): url = url_for("invenio_accounts_rest_auth.login") res = client.post(url, data=dict(email=email, password=password)) payload = get_json(res) assert res.status_code == 200 assert payload["id"] == user.id - assert payload["email"] == user.email + assert payload["email"].lower() == user.email.lower() session_cookie = next(c for c in client.cookie_jar if c.name == "session") assert session_cookie is not None assert session_cookie.value