Skip to content

Commit

Permalink
Fix datetime_factory as attribute. (#960)
Browse files Browse the repository at this point in the history
datetime_factory should still be set as an attribute - that got lost. Also - trackable didn't use the attribute - it directly referenced the config value - fix that.

Add a test.

close #959
  • Loading branch information
jwag956 authored Mar 21, 2024
1 parent 3c9c44d commit 140d097
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Fixes
- (:issue:`950`) Regression - some templates no longer getting correct config (thanks pete7863).
- (:issue:`954`) CSRF not properly ignored for application forms using SECURITY_CSRF_PROTECT_MECHANISMS.
- (:pr:`957`) Improve jp translations (e-goto)
- (:issue:`959`) Regression - datetime_factory should still be an attribute (thanks TimotheeJeannin)

Version 5.4.2
-------------
Expand Down
1 change: 1 addition & 0 deletions flask_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ def init_app(
"render_template",
"totp_cls",
"webauthn_util_cls",
"datetime_factory",
]
for attr in attr_names:
if ov := kwargs.get(attr, cv(attr.upper(), app, strict=False)):
Expand Down
2 changes: 1 addition & 1 deletion flask_security/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def login_user(

old_current_login, new_current_login = (
user.current_login_at,
config_value("DATETIME_FACTORY")(),
_security.datetime_factory(),
)
old_current_ip, new_current_ip = user.current_login_ip, remote_addr

Expand Down
13 changes: 13 additions & 0 deletions tests/test_trackable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Trackable tests
"""

import datetime
import pytest
from flask import after_this_request, redirect
from werkzeug.middleware.proxy_fix import ProxyFix
Expand Down Expand Up @@ -92,3 +93,15 @@ def save_user(response):
assert user.last_login_ip == _client_ip(client)
assert user.current_login_ip == "127.0.0.1"
assert user.login_count == 2


@pytest.mark.settings(datetime_factory=lambda: datetime.datetime(2024, 3, 24))
def test_trackable_datetime(app, client):
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)
e = "matt@lp.com"
authenticate(client, email=e)

with app.app_context():
user = app.security.datastore.find_user(email=e)
assert user.current_login_at == datetime.datetime(2024, 3, 24)
assert user.login_count == 1

0 comments on commit 140d097

Please sign in to comment.