2022-11-01 11:25:21 +00:00
|
|
|
import datetime
|
2024-12-17 13:45:10 +00:00
|
|
|
from unittest import mock
|
2022-12-21 20:52:01 +00:00
|
|
|
|
2024-12-17 13:45:10 +00:00
|
|
|
import pytest
|
|
|
|
import time_machine
|
2023-08-13 20:08:28 +00:00
|
|
|
from flask import g
|
2020-10-22 15:37:01 +00:00
|
|
|
|
2024-03-15 18:58:06 +00:00
|
|
|
from canaille.app import models
|
|
|
|
|
2020-10-22 15:37:01 +00:00
|
|
|
|
2024-04-14 20:51:58 +00:00
|
|
|
def test_index(testclient, user, backend):
|
2022-12-04 11:57:59 +00:00
|
|
|
res = testclient.get("/", status=302)
|
|
|
|
assert res.location == "/login"
|
|
|
|
|
2023-08-13 20:08:28 +00:00
|
|
|
g.user = user
|
2022-12-04 11:57:59 +00:00
|
|
|
res = testclient.get("/", status=302)
|
|
|
|
assert res.location == "/profile/user"
|
|
|
|
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE"]["ACL"]["DEFAULT"]["PERMISSIONS"] = []
|
2024-04-14 20:51:58 +00:00
|
|
|
backend.reload(g.user)
|
2022-12-04 11:57:59 +00:00
|
|
|
res = testclient.get("/", status=302)
|
|
|
|
assert res.location == "/about"
|
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_user_deleted_in_session(testclient, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
u = models.User(
|
2023-02-05 17:57:18 +00:00
|
|
|
formatted_name="Jake Doe",
|
|
|
|
family_name="Jake",
|
|
|
|
user_name="jake",
|
2024-11-20 22:30:44 +00:00
|
|
|
emails=["jake@doe.test"],
|
2023-04-10 19:42:14 +00:00
|
|
|
password="correct horse battery staple",
|
2020-11-25 16:41:03 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(u)
|
2020-12-11 10:52:37 +00:00
|
|
|
testclient.get("/profile/jake", status=403)
|
|
|
|
|
|
|
|
with testclient.session_transaction() as session:
|
2023-02-05 18:08:25 +00:00
|
|
|
session["user_id"] = [u.id]
|
2020-11-25 16:41:03 +00:00
|
|
|
|
2024-04-14 18:37:52 +00:00
|
|
|
backend.delete(u)
|
2020-11-25 16:41:03 +00:00
|
|
|
|
2023-06-28 15:56:49 +00:00
|
|
|
testclient.get("/profile/jake", status=404)
|
2020-12-11 10:52:37 +00:00
|
|
|
with testclient.session_transaction() as session:
|
2022-12-29 00:10:07 +00:00
|
|
|
assert not session.get("user_id")
|
2020-12-11 10:52:37 +00:00
|
|
|
|
|
|
|
|
2022-05-19 10:36:39 +00:00
|
|
|
def test_impersonate(testclient, logged_admin, user):
|
2023-06-30 16:10:16 +00:00
|
|
|
res = testclient.get("/", status=302).follow(status=200).click("Account settings")
|
2023-02-05 17:57:18 +00:00
|
|
|
assert "admin" == res.form["user_name"].value
|
2020-12-11 10:52:37 +00:00
|
|
|
|
|
|
|
res = (
|
|
|
|
testclient.get("/impersonate/user", status=302)
|
|
|
|
.follow(status=302)
|
|
|
|
.follow(status=200)
|
2023-06-30 16:10:16 +00:00
|
|
|
.click("Account settings")
|
2020-12-11 10:52:37 +00:00
|
|
|
)
|
2023-02-05 17:57:18 +00:00
|
|
|
assert "user" == res.form["user_name"].value
|
2020-12-11 10:52:37 +00:00
|
|
|
|
|
|
|
testclient.get("/logout", status=302).follow(status=302).follow(status=200)
|
|
|
|
|
2023-06-30 16:10:16 +00:00
|
|
|
res = testclient.get("/", status=302).follow(status=200).click("Account settings")
|
2023-02-05 17:57:18 +00:00
|
|
|
assert "admin" == res.form["user_name"].value
|
2020-12-31 18:55:30 +00:00
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_admin_self_deletion(testclient, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
admin = models.User(
|
2023-02-05 17:57:18 +00:00
|
|
|
formatted_name="Temp admin",
|
|
|
|
family_name="admin",
|
|
|
|
user_name="temp",
|
2024-11-20 22:30:44 +00:00
|
|
|
emails=["temp@temp.test"],
|
2023-04-10 19:42:14 +00:00
|
|
|
password="admin",
|
2021-01-01 15:42:13 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(admin)
|
2021-01-01 15:42:13 +00:00
|
|
|
with testclient.session_transaction() as sess:
|
2023-02-05 18:08:25 +00:00
|
|
|
sess["user_id"] = [admin.id]
|
2021-01-01 15:42:13 +00:00
|
|
|
|
2023-03-16 17:39:28 +00:00
|
|
|
res = testclient.get("/profile/temp/settings")
|
2023-07-06 16:43:37 +00:00
|
|
|
res = res.form.submit(name="action", value="confirm-delete")
|
2021-01-01 15:42:13 +00:00
|
|
|
res = (
|
|
|
|
res.form.submit(name="action", value="delete", status=302)
|
|
|
|
.follow(status=302)
|
|
|
|
.follow(status=200)
|
|
|
|
)
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
assert backend.get(models.User, user_name="temp") is None
|
2021-01-01 15:42:13 +00:00
|
|
|
|
|
|
|
with testclient.session_transaction() as sess:
|
2022-12-29 00:10:07 +00:00
|
|
|
assert not sess.get("user_id")
|
2021-01-01 15:42:13 +00:00
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def test_user_self_deletion(testclient, backend):
|
2023-04-09 09:37:04 +00:00
|
|
|
user = models.User(
|
2023-02-05 17:57:18 +00:00
|
|
|
formatted_name="Temp user",
|
|
|
|
family_name="user",
|
|
|
|
user_name="temp",
|
2024-11-20 22:30:44 +00:00
|
|
|
emails=["temp@temp.test"],
|
2023-04-10 19:42:14 +00:00
|
|
|
password="correct horse battery staple",
|
2021-01-01 15:42:13 +00:00
|
|
|
)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2021-01-01 15:42:13 +00:00
|
|
|
with testclient.session_transaction() as sess:
|
2023-02-05 18:08:25 +00:00
|
|
|
sess["user_id"] = [user.id]
|
2021-01-01 15:42:13 +00:00
|
|
|
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE"]["ACL"]["DEFAULT"]["PERMISSIONS"] = ["edit_self"]
|
2023-03-16 17:39:28 +00:00
|
|
|
res = testclient.get("/profile/temp/settings")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain(no="Delete my account")
|
2021-01-01 15:42:13 +00:00
|
|
|
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE"]["ACL"]["DEFAULT"]["PERMISSIONS"] = [
|
2022-04-05 15:16:09 +00:00
|
|
|
"edit_self",
|
|
|
|
"delete_account",
|
|
|
|
]
|
2023-11-24 11:10:17 +00:00
|
|
|
# Simulate an app restart
|
2024-04-14 20:51:58 +00:00
|
|
|
backend.reload(user)
|
2023-11-24 11:10:17 +00:00
|
|
|
|
2023-03-16 17:39:28 +00:00
|
|
|
res = testclient.get("/profile/temp/settings")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("Delete my account")
|
2023-07-06 16:43:37 +00:00
|
|
|
res = res.form.submit(name="action", value="confirm-delete")
|
2021-01-01 15:42:13 +00:00
|
|
|
res = (
|
|
|
|
res.form.submit(name="action", value="delete", status=302)
|
|
|
|
.follow(status=302)
|
|
|
|
.follow(status=200)
|
|
|
|
)
|
|
|
|
|
2024-04-14 15:30:59 +00:00
|
|
|
assert backend.get(models.User, user_name="temp") is None
|
2021-01-01 15:42:13 +00:00
|
|
|
|
|
|
|
with testclient.session_transaction() as sess:
|
2022-12-29 00:10:07 +00:00
|
|
|
assert not sess.get("user_id")
|
2021-01-01 15:42:13 +00:00
|
|
|
|
2023-12-18 17:06:03 +00:00
|
|
|
testclient.app.config["CANAILLE"]["ACL"]["DEFAULT"]["PERMISSIONS"] = []
|
2022-11-01 11:25:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_account_locking(user, backend):
|
|
|
|
assert not user.locked
|
|
|
|
assert not user.lock_date
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
2023-05-26 15:44:15 +00:00
|
|
|
user.lock_date = datetime.datetime.now(datetime.timezone.utc)
|
2023-11-24 11:10:17 +00:00
|
|
|
assert user.locked
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2022-11-01 11:25:21 +00:00
|
|
|
assert user.locked
|
2024-04-14 15:30:59 +00:00
|
|
|
assert backend.get(models.User, id=user.id).locked
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
2022-11-01 11:25:21 +00:00
|
|
|
False,
|
|
|
|
"Your account has been locked.",
|
|
|
|
)
|
|
|
|
|
2023-11-21 13:57:28 +00:00
|
|
|
user.lock_date = None
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2022-11-01 11:25:21 +00:00
|
|
|
assert not user.locked
|
2024-04-14 15:30:59 +00:00
|
|
|
assert not backend.get(models.User, id=user.id).locked
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_account_locking_past_date(user, backend):
|
|
|
|
assert not user.locked
|
|
|
|
assert not user.lock_date
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
2023-05-26 15:44:15 +00:00
|
|
|
user.lock_date = datetime.datetime.now(datetime.timezone.utc).replace(
|
2022-11-01 11:25:21 +00:00
|
|
|
microsecond=0
|
|
|
|
) - datetime.timedelta(days=30)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2022-11-01 11:25:21 +00:00
|
|
|
assert user.locked
|
2024-04-14 15:30:59 +00:00
|
|
|
assert backend.get(models.User, id=user.id).locked
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
2022-11-01 11:25:21 +00:00
|
|
|
False,
|
|
|
|
"Your account has been locked.",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_account_locking_future_date(user, backend):
|
|
|
|
assert not user.locked
|
|
|
|
assert not user.lock_date
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
2023-05-26 15:44:15 +00:00
|
|
|
user.lock_date = datetime.datetime.now(datetime.timezone.utc).replace(
|
2022-11-01 11:25:21 +00:00
|
|
|
microsecond=0
|
|
|
|
) + datetime.timedelta(days=365 * 4)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2022-11-01 11:25:21 +00:00
|
|
|
assert not user.locked
|
2024-04-14 15:30:59 +00:00
|
|
|
assert not backend.get(models.User, id=user.id).locked
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple") == (
|
|
|
|
True,
|
|
|
|
None,
|
|
|
|
)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
|
|
|
|
2024-04-14 18:31:43 +00:00
|
|
|
def test_account_locked_during_session(testclient, logged_user, backend):
|
2023-05-26 15:44:15 +00:00
|
|
|
logged_user.lock_date = datetime.datetime.now(datetime.timezone.utc)
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(logged_user)
|
2022-11-01 11:25:21 +00:00
|
|
|
testclient.get("/profile/user/settings", status=403)
|
2024-12-17 13:45:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_expired_password_redirection_and_register_new_password_for_memory_and_sql(
|
|
|
|
testclient,
|
|
|
|
logged_user,
|
|
|
|
user,
|
|
|
|
backend,
|
|
|
|
admin,
|
|
|
|
):
|
|
|
|
"""time_machine does not work with ldap."""
|
|
|
|
if "ldap" in backend.__class__.__module__:
|
|
|
|
pytest.skip()
|
|
|
|
|
|
|
|
testclient.app.config["WTF_CSRF_ENABLED"] = False
|
|
|
|
backend.reload(logged_user)
|
|
|
|
res = testclient.get("/profile/user/settings", status=200)
|
|
|
|
res.form["password1"] = "123456789"
|
|
|
|
res.form["password2"] = "123456789"
|
|
|
|
|
|
|
|
with time_machine.travel("2020-01-01 01:00:00+00:00", tick=False) as traveller:
|
|
|
|
res = res.form.submit(name="action", value="edit-settings")
|
|
|
|
|
|
|
|
testclient.app.config["CANAILLE"]["PASSWORD_LIFETIME"] = "P5D"
|
|
|
|
|
|
|
|
traveller.shift(datetime.timedelta(days=5, minutes=1))
|
|
|
|
|
|
|
|
backend.reload(g.user)
|
|
|
|
|
|
|
|
res = testclient.get("/profile/user/settings")
|
|
|
|
|
|
|
|
testclient.get("/reset/admin", status=403)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
"info",
|
|
|
|
"Your password has expired, please choose a new password.",
|
|
|
|
) in res.flashes
|
|
|
|
assert res.location == "/reset/user"
|
|
|
|
|
|
|
|
backend.reload(logged_user)
|
|
|
|
res = testclient.get("/reset/user")
|
|
|
|
|
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "foobarbaz"
|
|
|
|
res = res.form.submit()
|
|
|
|
assert ("success", "Your password has been updated successfully") in res.flashes
|
|
|
|
|
|
|
|
|
|
|
|
@mock.patch("canaille.core.models.User.has_expired_password")
|
|
|
|
def test_expired_password_redirection_and_register_new_password_for_ldap_sql_and_memory(
|
|
|
|
has_expired,
|
|
|
|
testclient,
|
|
|
|
logged_user,
|
|
|
|
user,
|
|
|
|
backend,
|
|
|
|
admin,
|
|
|
|
):
|
|
|
|
"""time_machine does not work with ldap."""
|
|
|
|
has_expired.return_value = False
|
|
|
|
assert user.password_last_update is None
|
|
|
|
res = testclient.get("/profile/user/settings", status=200)
|
|
|
|
res.form["password1"] = "123456789"
|
|
|
|
res.form["password2"] = "123456789"
|
|
|
|
res = res.form.submit(name="action", value="edit-settings")
|
|
|
|
backend.reload(logged_user)
|
|
|
|
assert user.password_last_update is not None
|
|
|
|
|
|
|
|
has_expired.return_value = True
|
|
|
|
res = testclient.get("/profile/user/settings")
|
|
|
|
testclient.get("/reset/admin", status=403)
|
|
|
|
assert (
|
|
|
|
"info",
|
|
|
|
"Your password has expired, please choose a new password.",
|
|
|
|
) in res.flashes
|
|
|
|
assert res.location == "/reset/user"
|
|
|
|
backend.reload(logged_user)
|
|
|
|
backend.reload(g.user)
|
|
|
|
backend.reload(user)
|
|
|
|
|
|
|
|
res = testclient.get("/reset/user")
|
|
|
|
|
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "foobarbaz"
|
|
|
|
res = res.form.submit()
|
|
|
|
assert ("success", "Your password has been updated successfully") in res.flashes
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_expired_password_or_wrong_user_redirection(
|
|
|
|
testclient, logged_user, user, backend, admin
|
|
|
|
):
|
|
|
|
assert user.password_last_update is None
|
|
|
|
res = testclient.get("/profile/user/settings", status=200)
|
|
|
|
res.form["password1"] = "123456789"
|
|
|
|
res.form["password2"] = "123456789"
|
|
|
|
res = res.form.submit(name="action", value="edit-settings")
|
|
|
|
backend.reload(logged_user)
|
|
|
|
assert user.password_last_update is not None
|
|
|
|
|
|
|
|
def test_two_redirections(password_lifetime):
|
|
|
|
testclient.app.config["CANAILLE"]["PASSWORD_LIFETIME"] = password_lifetime
|
|
|
|
testclient.get("/reset/user", status=403)
|
|
|
|
testclient.get("/reset/admin", status=403)
|
|
|
|
|
|
|
|
test_two_redirections(None)
|
|
|
|
|
|
|
|
testclient.app.config["CANAILLE"]["PASSWORD_LIFETIME"] = "PT0S"
|
|
|
|
res = testclient.get("/profile/user/settings")
|
|
|
|
assert (
|
|
|
|
"info",
|
|
|
|
"Your password has expired, please choose a new password.",
|
|
|
|
) in res.flashes
|
|
|
|
assert res.location == "/reset/user"
|
|
|
|
|
|
|
|
testclient.app.config["CANAILLE"]["PASSWORD_LIFETIME"] = "P1D"
|
|
|
|
res = testclient.get("/profile/user/settings")
|
|
|
|
res.form["password1"] = "123456789"
|
|
|
|
res.form["password2"] = "123456789"
|
|
|
|
res = res.form.submit(name="action", value="edit-settings")
|
|
|
|
|
|
|
|
test_two_redirections("P1D")
|
|
|
|
|
|
|
|
|
|
|
|
def test_expired_password_needed_without_current_user(testclient, user):
|
|
|
|
testclient.get("/reset/user", status=403)
|