2022-12-22 16:02:07 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_forgotten_disabled(smtpd, testclient, user):
|
2022-04-05 07:49:45 +00:00
|
|
|
testclient.app.config["ENABLE_PASSWORD_RECOVERY"] = False
|
|
|
|
|
|
|
|
testclient.get("/reset", status=404)
|
|
|
|
testclient.get("/reset/uid/hash", status=404)
|
|
|
|
|
|
|
|
res = testclient.get("/login")
|
|
|
|
assert "Forgotten password" not in res.text
|
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_forgotten(smtpd, testclient, user):
|
2021-01-01 14:20:26 +00:00
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "user"
|
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) in res.flashes
|
2021-01-01 14:29:55 +00:00
|
|
|
assert "Send again" in res.text
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2021-10-12 18:36:31 +00:00
|
|
|
assert len(smtpd.messages) == 1
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_forgotten_invalid_form(smtpd, testclient, user):
|
2021-01-01 14:20:26 +00:00
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = ""
|
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert ("error", "Could not send the password reset link.") in res.flashes
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2021-10-12 18:36:31 +00:00
|
|
|
assert len(smtpd.messages) == 0
|
2021-01-01 14:20:26 +00:00
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_forgotten_invalid(smtpd, testclient, user):
|
2022-04-06 15:32:11 +00:00
|
|
|
testclient.app.config["HIDE_INVALID_LOGINS"] = True
|
2021-01-01 14:20:26 +00:00
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "i-dont-really-exist"
|
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) in res.flashes
|
2021-12-06 13:24:47 +00:00
|
|
|
assert "The login 'i-dont-really-exist' does not exist" not in res.text
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2022-04-06 15:32:11 +00:00
|
|
|
testclient.app.config["HIDE_INVALID_LOGINS"] = False
|
2021-01-01 14:20:26 +00:00
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "i-dont-really-exist"
|
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) not in res.flashes
|
2021-12-06 13:24:47 +00:00
|
|
|
assert "The login 'i-dont-really-exist' does not exist" in res.text
|
2021-01-01 14:20:26 +00:00
|
|
|
|
2021-10-12 18:36:31 +00:00
|
|
|
assert len(smtpd.messages) == 0
|
2022-04-05 15:16:09 +00:00
|
|
|
|
|
|
|
|
2022-05-08 14:31:17 +00:00
|
|
|
def test_password_forgotten_invalid_when_user_cannot_self_edit(smtpd, testclient, user):
|
2022-04-05 15:16:09 +00:00
|
|
|
testclient.app.config["ACL"]["DEFAULT"]["PERMISSIONS"] = []
|
|
|
|
|
|
|
|
testclient.app.config["HIDE_INVALID_LOGINS"] = False
|
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "user"
|
|
|
|
res = res.form.submit(status=200)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) not in res.flashes
|
2022-04-05 15:16:09 +00:00
|
|
|
assert (
|
2023-03-09 17:10:01 +00:00
|
|
|
"error",
|
|
|
|
"The user 'John (johnny) Doe' does not have permissions to update their password. We cannot send a password reset email.",
|
|
|
|
) in res.flashes
|
2022-04-05 15:16:09 +00:00
|
|
|
|
|
|
|
testclient.app.config["HIDE_INVALID_LOGINS"] = True
|
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "user"
|
|
|
|
res = res.form.submit(status=200)
|
|
|
|
assert (
|
2023-03-09 17:10:01 +00:00
|
|
|
"error",
|
|
|
|
"The user 'John (johnny) Doe' does not have permissions to update their password. We cannot send a password reset email.",
|
|
|
|
) not in res.flashes
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) in res.flashes
|
2022-04-05 15:16:09 +00:00
|
|
|
|
|
|
|
assert len(smtpd.messages) == 0
|
2022-12-22 16:02:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch("smtplib.SMTP")
|
|
|
|
def test_password_forgotten_mail_error(SMTP, smtpd, testclient, user):
|
|
|
|
SMTP.side_effect = mock.Mock(side_effect=OSError("unit test mail error"))
|
|
|
|
res = testclient.get("/reset", status=200)
|
|
|
|
|
|
|
|
res.form["login"] = "user"
|
|
|
|
res = res.form.submit(status=200, expect_errors=True)
|
2023-01-28 18:02:00 +00:00
|
|
|
assert (
|
|
|
|
"success",
|
|
|
|
"A password reset link has been sent at your email address. You should receive it within a few minutes.",
|
|
|
|
) not in res.flashes
|
|
|
|
assert (
|
|
|
|
"error",
|
|
|
|
"We encountered an issue while we sent the password recovery email.",
|
|
|
|
) in res.flashes
|
2022-12-22 16:02:07 +00:00
|
|
|
assert "Send again" in res.text
|
|
|
|
|
|
|
|
assert len(smtpd.messages) == 0
|