Repository: pennersr/django-allauth
Pull request #3594 (2024-01-17 07:28:05)

fix(account/adapter): fix a typo in clean_password
# Submitting Pull Requests

## General

 - [x] Make sure you use [semantic commit messages](https://seesparkbox.com/foundry/semantic_commit_messages).
       Examples: `"fix(google): Fixed foobar bug"`, `"feat(accounts): Added foobar feature"`.
 - [ ] All Python code must formatted using Black, and clean from pep8 and isort issues.
 - [ ] JavaScript code should adhere to [StandardJS](https://standardjs.com).
 - [ ] If your changes are significant, please update `ChangeLog.rst`.
 - [ ] If your change is substantial, feel free to add yourself to `AUTHORS`.

 ## Provider Specifics

 In case you add a new provider:

- [ ] Make sure unit tests are available.
- [ ] Add an entry of your provider in `test_settings.py::INSTALLED_APPS` and `docs/installation.rst::INSTALLED_APPS`.
- [ ] Add documentation to `docs/providers/<provider name>.rst` and `docs/providers/index.rst` Provider Specifics toctree.
- [ ] Add an entry to the list of supported providers over at `docs/overview.rst`.



Patch:
diff --git a/allauth/account/adapter.py b/allauth/account/adapter.py
index 7ff297bf..d4d647c1 100644
--- a/allauth/account/adapter.py
+++ b/allauth/account/adapter.py
@@ -355,7 +355,7 @@ class DefaultAccountAdapter(object):
         min_length = app_settings.PASSWORD_MIN_LENGTH
         if min_length and len(password) < min_length:
             raise forms.ValidationError(
-                self.error_message["password_min_length"].format(min_length)
+                self.error_messages["password_min_length"].format(min_length)
             )
         validate_password(password, user)
         return password
diff --git a/allauth/usersessions/middleware.py b/allauth/usersessions/middleware.py
index 7dd3d5ba..ed0d14d4 100644
--- a/allauth/usersessions/middleware.py
+++ b/allauth/usersessions/middleware.py
@@ -7,7 +7,12 @@ class UserSessionsMiddleware:
         self.get_response = get_response
 
     def __call__(self, request):
-        if app_settings.TRACK_ACTIVITY:
+        if (
+            app_settings.TRACK_ACTIVITY
+            and hasattr(request, "session")
+            and hasattr(request, "user")
+            and request.user.is_authenticated
+        ):
             UserSession.objects.create_from_request(request)
         response = self.get_response(request)
         return response


Test patch:
diff --git a/allauth/usersessions/tests/test_middleware.py b/allauth/usersessions/tests/test_middleware.py
new file mode 100644
index 00000000..99ce7e5f
--- /dev/null
+++ b/allauth/usersessions/tests/test_middleware.py
@@ -0,0 +1,42 @@
+from unittest.mock import Mock
+
+from django.contrib.auth.models import AnonymousUser
+
+import pytest
+
+from allauth.usersessions.middleware import UserSessionsMiddleware
+from allauth.usersessions.models import UserSession
+
+
+def test_mw_without_request_user(rf, db, settings):
+    settings.USERSESSIONS_TRACK_ACTIVITY = True
+    mw = UserSessionsMiddleware(lambda request: None)
+    request = rf.get("/")
+    mw(request)
+    assert UserSession.objects.count() == 0
+
+
+@pytest.mark.parametrize("track_activity", [False, True])
+def test_mw_with_request_user(rf, db, settings, user, track_activity):
+    settings.USERSESSIONS_TRACK_ACTIVITY = track_activity
+    mw = UserSessionsMiddleware(lambda request: None)
+    request = rf.get("/")
+    request.user = user
+    request.session = Mock()
+    request.session.session_key = "sess-123"
+    mw(request)
+    assert (
+        UserSession.objects.filter(session_key="sess-123", user=user).exists()
+        is track_activity
+    )
+
+
+def test_mw_with_anonymous_request_user(rf, db, settings):
+    settings.USERSESSIONS_TRACK_ACTIVITY = True
+    mw = UserSessionsMiddleware(lambda request: None)
+    request = rf.get("/")
+    request.user = AnonymousUser()
+    request.session = Mock()
+    request.session.session_key = "sess-123"
+    mw(request)
+    assert not UserSession.objects.exists()