2023-12-25 23:23:47 +00:00
|
|
|
from canaille.core.endpoints.account import build_hash
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2024-04-07 18:12:13 +00:00
|
|
|
def test_password_reset(testclient, user, backend):
|
|
|
|
assert not backend.check_user_password(user, "foobarbaz")[0]
|
2023-11-28 14:51:14 +00:00
|
|
|
hash = build_hash("user", user.preferred_email, user.password)
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
res = testclient.get("/reset/user/" + hash, status=200)
|
|
|
|
|
2024-12-18 12:10:59 +00:00
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "foobar"
|
|
|
|
res = res.form.submit()
|
|
|
|
res.mustcontain("Password and confirmation do not match.")
|
|
|
|
res.mustcontain('data-percent="50"')
|
|
|
|
|
|
|
|
res.form["password"] = "123"
|
|
|
|
res.form["confirmation"] = "123"
|
|
|
|
res = res.form.submit()
|
|
|
|
res.mustcontain("Field must be at least 8 characters long.")
|
|
|
|
|
2021-01-01 14:20:26 +00:00
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "foobarbaz"
|
2023-01-28 18:02:00 +00:00
|
|
|
res = res.form.submit()
|
2023-05-30 07:44:11 +00:00
|
|
|
assert ("success", "Your password has been updated successfully") in res.flashes
|
2023-12-15 15:12:33 +00:00
|
|
|
assert res.location == "/profile/user"
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2024-04-14 20:51:58 +00:00
|
|
|
backend.reload(user)
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "foobarbaz")[0]
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
res = testclient.get("/reset/user/" + hash)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"error",
|
|
|
|
"The password reset link that brought you here was invalid.",
|
|
|
|
) in res.flashes
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2024-04-07 18:12:13 +00:00
|
|
|
def test_password_reset_multiple_emails(testclient, user, backend):
|
2024-11-20 22:30:44 +00:00
|
|
|
user.emails = ["foo@bar.test", "foo@baz.test"]
|
2024-04-14 18:31:43 +00:00
|
|
|
backend.save(user)
|
2023-06-29 15:47:01 +00:00
|
|
|
|
2024-04-07 18:12:13 +00:00
|
|
|
assert not backend.check_user_password(user, "foobarbaz")[0]
|
2024-11-20 22:30:44 +00:00
|
|
|
hash = build_hash("user", "foo@baz.test", user.password)
|
2023-06-29 15:47:01 +00:00
|
|
|
|
|
|
|
res = testclient.get("/reset/user/" + hash, status=200)
|
|
|
|
|
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "foobarbaz"
|
|
|
|
res = res.form.submit()
|
|
|
|
assert ("success", "Your password has been updated successfully") in res.flashes
|
|
|
|
|
2024-04-14 20:51:58 +00:00
|
|
|
backend.reload(user)
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "foobarbaz")[0]
|
2023-06-29 15:47:01 +00:00
|
|
|
|
|
|
|
res = testclient.get("/reset/user/" + hash)
|
|
|
|
assert (
|
|
|
|
"error",
|
|
|
|
"The password reset link that brought you here was invalid.",
|
|
|
|
) in res.flashes
|
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_reset_bad_link(testclient, user):
|
2021-01-01 14:20:26 +00:00
|
|
|
res = testclient.get("/reset/user/foobarbaz")
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"error",
|
|
|
|
"The password reset link that brought you here was invalid.",
|
|
|
|
) in res.flashes
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2024-04-07 18:12:13 +00:00
|
|
|
def test_password_reset_bad_password(testclient, user, backend):
|
2023-11-28 14:51:14 +00:00
|
|
|
hash = build_hash("user", user.preferred_email, user.password)
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
res = testclient.get("/reset/user/" + hash, status=200)
|
|
|
|
|
|
|
|
res.form["password"] = "foobarbaz"
|
|
|
|
res.form["confirmation"] = "typo"
|
|
|
|
res = res.form.submit(status=200)
|
|
|
|
|
2024-04-07 18:12:13 +00:00
|
|
|
assert backend.check_user_password(user, "correct horse battery staple")[0]
|
2021-12-07 15:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unavailable_if_no_smtp(testclient, user):
|
|
|
|
res = testclient.get("/login")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("Forgotten password")
|
2021-12-07 15:39:18 +00:00
|
|
|
|
2023-05-11 13:33:34 +00:00
|
|
|
res.form["login"] = "user"
|
2021-12-07 15:39:18 +00:00
|
|
|
res = res.form.submit()
|
|
|
|
res = res.follow()
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain("Forgotten password")
|
2021-12-07 15:39:18 +00:00
|
|
|
|
|
|
|
testclient.get("/reset", status=200)
|
|
|
|
|
2024-05-14 21:04:32 +00:00
|
|
|
testclient.app.config["CANAILLE"]["SMTP"] = None
|
2021-12-07 15:39:18 +00:00
|
|
|
|
|
|
|
res = testclient.get("/login")
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain(no="Forgotten password")
|
2021-12-07 15:39:18 +00:00
|
|
|
|
2023-05-11 13:33:34 +00:00
|
|
|
res.form["login"] = "user"
|
2021-12-07 15:39:18 +00:00
|
|
|
res = res.form.submit()
|
|
|
|
res = res.follow()
|
2023-03-16 15:25:14 +00:00
|
|
|
res.mustcontain(no="Forgotten password")
|
2021-12-07 15:39:18 +00:00
|
|
|
|
|
|
|
testclient.get("/reset", status=500, expect_errors=True)
|