Skip to content

Commit

Permalink
email: force lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
ntarocco committed Oct 20, 2023
1 parent bb8277f commit 1a4d265
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
12 changes: 11 additions & 1 deletion invenio_accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -110,15 +110,15 @@ 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)


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():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_views_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a4d265

Please sign in to comment.