forked from Github-Mirrors/canaille
refacto form test: separating htmx request and form submit
This commit is contained in:
parent
d9f01fef6e
commit
bfff1bcdc1
3 changed files with 15 additions and 33 deletions
|
@ -98,11 +98,14 @@ def compromised_password_validator(form, field):
|
||||||
api_url = (
|
api_url = (
|
||||||
current_app.config["CANAILLE"]["API_URL_HIBP"] + hashed_password_prefix
|
current_app.config["CANAILLE"]["API_URL_HIBP"] + hashed_password_prefix
|
||||||
)
|
)
|
||||||
print(api_url)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.api.get(api_url, timeout=10)
|
response = requests.api.get(api_url, timeout=10)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
if not request_is_htmx():
|
||||||
|
current_app.logger.exception(
|
||||||
|
"Password compromise investigation failed on HIBP API."
|
||||||
|
)
|
||||||
check_if_send_mail_to_admins(form, api_url, hashed_password_suffix)
|
check_if_send_mail_to_admins(form, api_url, hashed_password_suffix)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,9 @@ from flask import flash
|
||||||
from canaille.app.i18n import gettext as _
|
from canaille.app.i18n import gettext as _
|
||||||
from canaille.core.mails import send_compromised_password_check_failure_mail
|
from canaille.core.mails import send_compromised_password_check_failure_mail
|
||||||
|
|
||||||
from .flask import request_is_htmx
|
|
||||||
|
|
||||||
|
|
||||||
def check_if_send_mail_to_admins(form, api_url, hashed_password_suffix):
|
def check_if_send_mail_to_admins(form, api_url, hashed_password_suffix):
|
||||||
if current_app.features.has_smtp and not request_is_htmx():
|
if current_app.features.has_smtp:
|
||||||
current_app.logger.exception(
|
|
||||||
"Password compromise investigation failed on HIBP API."
|
|
||||||
)
|
|
||||||
|
|
||||||
flash(
|
flash(
|
||||||
_(
|
_(
|
||||||
"Password compromise investigation failed. "
|
"Password compromise investigation failed. "
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import logging
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -370,30 +371,8 @@ def test_compromised_password_validator(api_get, testclient):
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("requests.api.get")
|
@mock.patch("requests.api.get")
|
||||||
def test_compromised_password_validator_with_failure_of_api_request_and_no_SMTP_in_config(
|
def test_compromised_password_validator_with_failure_of_api_request_without_form_validation(
|
||||||
api_get, testclient, logged_user
|
api_get, testclient, logged_user, caplog
|
||||||
):
|
|
||||||
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
|
|
||||||
api_get.side_effect = mock.Mock(side_effect=Exception())
|
|
||||||
current_app.config["CANAILLE"]["SMTP"] = None
|
|
||||||
|
|
||||||
class Field:
|
|
||||||
def __init__(self, data):
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
compromised_password_validator(None, Field("i'm a little pea"))
|
|
||||||
compromised_password_validator(None, Field("i'm a little chickpea"))
|
|
||||||
compromised_password_validator(None, Field("i'm singing in the rain"))
|
|
||||||
compromised_password_validator(None, Field("password"))
|
|
||||||
compromised_password_validator(None, Field("987654321"))
|
|
||||||
compromised_password_validator(None, Field("correct horse battery staple"))
|
|
||||||
compromised_password_validator(None, Field("zxcvbn123"))
|
|
||||||
compromised_password_validator(None, Field("azertyuiop123"))
|
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("requests.api.get")
|
|
||||||
def test_compromised_password_validator_with_failure_of_api_request_and_only_with_htmx(
|
|
||||||
api_get, testclient, logged_user
|
|
||||||
):
|
):
|
||||||
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
|
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
|
||||||
api_get.side_effect = mock.Mock(side_effect=Exception())
|
api_get.side_effect = mock.Mock(side_effect=Exception())
|
||||||
|
@ -412,3 +391,9 @@ def test_compromised_password_validator_with_failure_of_api_request_and_only_wit
|
||||||
)
|
)
|
||||||
|
|
||||||
res.mustcontain('data-percent="100"')
|
res.mustcontain('data-percent="100"')
|
||||||
|
|
||||||
|
assert (
|
||||||
|
"canaille",
|
||||||
|
logging.ERROR,
|
||||||
|
"Password compromise investigation failed on HIBP API.",
|
||||||
|
) not in caplog.record_tuples
|
||||||
|
|
Loading…
Reference in a new issue