| 1 |
""" |
| 2 |
test_trackable |
| 3 |
~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Trackable tests |
| 6 |
""" |
| 7 |
|
| 8 |
import datetime |
| 9 |
import pytest |
| 10 |
from flask import after_this_request, redirect |
| 11 |
from werkzeug.middleware.proxy_fix import ProxyFix |
| 12 |
|
| 13 |
from flask_security import login_user |
| 14 |
|
| 15 |
from tests.test_utils import authenticate, logout |
| 16 |
|
| 17 |
pytestmark = pytest.mark.trackable() |
| 18 |
|
| 19 |
|
| 20 |
def _client_ip(client): |
| 21 |
"""Compatibility layer for Flask<0.12.""" |
| 22 |
return getattr(client, "environ_base", {}).get("REMOTE_ADDR") |
| 23 |
|
| 24 |
|
| 25 |
def test_trackable_flag(app, client): |
| 26 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1) |
| 27 |
e = "[email protected]" |
| 28 |
authenticate(client, email=e) |
| 29 |
logout(client) |
| 30 |
authenticate(client, email=e, headers={"X-Forwarded-For": "127.0.0.1"}) |
| 31 |
|
| 32 |
with app.app_context(): |
| 33 |
user = app.security.datastore.find_user(email=e) |
| 34 |
assert user.last_login_at is not None |
| 35 |
assert user.current_login_at is not None |
| 36 |
assert user.last_login_ip == _client_ip(client) |
| 37 |
assert user.current_login_ip == "127.0.0.1" |
| 38 |
assert user.login_count == 2 |
| 39 |
|
| 40 |
|
| 41 |
def test_trackable_with_multiple_ips_in_headers(app, client): |
| 42 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2) |
| 43 |
|
| 44 |
e = "[email protected]" |
| 45 |
authenticate(client, email=e) |
| 46 |
logout(client) |
| 47 |
authenticate( |
| 48 |
client, |
| 49 |
email=e, |
| 50 |
headers={"X-Forwarded-For": "99.99.99.99, 88.88.88.88, 77.77.77.77"}, |
| 51 |
) |
| 52 |
|
| 53 |
with app.app_context(): |
| 54 |
user = app.security.datastore.find_user(email=e) |
| 55 |
assert user.last_login_at is not None |
| 56 |
assert user.current_login_at is not None |
| 57 |
assert user.last_login_ip == _client_ip(client) |
| 58 |
assert user.current_login_ip == "88.88.88.88" |
| 59 |
assert user.login_count == 2 |
| 60 |
|
| 61 |
|
| 62 |
def test_trackable_using_login_user(app, client): |
| 63 |
""" |
| 64 |
This tests is only to serve as an example of how one needs to call |
| 65 |
datastore.commit() after logging a user in to make sure the trackable |
| 66 |
fields are saved to the datastore. |
| 67 |
""" |
| 68 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1) |
| 69 |
|
| 70 |
@app.route("/login_custom", methods=["POST"]) |
| 71 |
def login_custom(): |
| 72 |
user = app.security.datastore.find_user(email=e) |
| 73 |
login_user(user) |
| 74 |
|
| 75 |
@after_this_request |
| 76 |
def save_user(response): |
| 77 |
app.security.datastore.commit() |
| 78 |
return response |
| 79 |
|
| 80 |
return redirect("/") |
| 81 |
|
| 82 |
e = "[email protected]" |
| 83 |
authenticate(client, email=e) |
| 84 |
logout(client) |
| 85 |
|
| 86 |
data = dict(email=e, password="password", remember="y") |
| 87 |
client.post("/login_custom", data=data, headers={"X-Forwarded-For": "127.0.0.1"}) |
| 88 |
|
| 89 |
with app.app_context(): |
| 90 |
user = app.security.datastore.find_user(email=e) |
| 91 |
assert user.last_login_at is not None |
| 92 |
assert user.current_login_at is not None |
| 93 |
assert user.last_login_ip == _client_ip(client) |
| 94 |
assert user.current_login_ip == "127.0.0.1" |
| 95 |
assert user.login_count == 2 |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.settings(datetime_factory=lambda: datetime.datetime(2024, 3, 24)) |
| 99 |
def test_trackable_datetime(app, client): |
| 100 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1) |
| 101 |
e = "[email protected]" |
| 102 |
authenticate(client, email=e) |
| 103 |
|
| 104 |
with app.app_context(): |
| 105 |
user = app.security.datastore.find_user(email=e) |
| 106 |
assert user.current_login_at == datetime.datetime(2024, 3, 24) |
| 107 |
assert user.login_count == 1 |