2023-07-20 16:43:28 +00:00
|
|
|
from canaille.core.account import build_hash
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_reset(testclient, user):
|
2022-11-01 11:25:21 +00:00
|
|
|
assert not user.check_password("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)
|
|
|
|
|
|
|
|
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
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2023-05-11 14:25:50 +00:00
|
|
|
user.reload()
|
2022-11-01 11:25:21 +00:00
|
|
|
assert user.check_password("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
|
|
|
|
|
|
|
|
2023-06-29 15:47:01 +00:00
|
|
|
def test_password_reset_multiple_emails(testclient, user):
|
|
|
|
user.emails = ["foo@bar.com", "foo@baz.com"]
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
assert not user.check_password("foobarbaz")[0]
|
2023-11-28 14:51:14 +00:00
|
|
|
hash = build_hash("user", "foo@baz.com", 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
|
|
|
|
|
|
|
|
user.reload()
|
|
|
|
assert user.check_password("foobarbaz")[0]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_reset_bad_password(testclient, user):
|
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)
|
|
|
|
|
2022-11-01 11:25:21 +00:00
|
|
|
assert user.check_password("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)
|
|
|
|
|
|
|
|
del testclient.app.config["SMTP"]
|
|
|
|
|
|
|
|
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)
|