DRY-up email revalidation check

Previously this was duplicated between the "two_factor" and the
"webauthn" views, and required more test setup. This DRYs up the
check and tests it once, using mocks to simplify the view tests.

As part of DRYing up the check into a util module, I've also moved
the "is_less_than_days_ago" function it uses.
This commit is contained in:
Ben Thorner
2021-06-14 12:40:12 +01:00
parent bf2e6802bf
commit c17d438de8
12 changed files with 97 additions and 75 deletions

View File

@@ -30,11 +30,11 @@ from app.utils import (
DELIVERED_STATUSES,
FAILURE_STATUSES,
REQUESTED_STATUSES,
get_current_financial_year,
service_has_permission,
)
from app.utils.csv import Spreadsheet
from app.utils.pagination import generate_next_dict, generate_previous_dict
from app.utils.time import get_current_financial_year
from app.utils.user import user_has_permissions

View File

@@ -16,8 +16,8 @@ from app import user_api_client
from app.main import main
from app.main.forms import TwoFactorForm
from app.models.user import User
from app.utils import is_less_than_days_ago
from app.utils.login import (
email_needs_revalidating,
log_in_user,
redirect_to_sign_in,
redirect_when_logged_in,
@@ -79,11 +79,11 @@ def two_factor_sms():
redirect_url = request.args.get('next')
if form.validate_on_submit():
if is_less_than_days_ago(user.email_access_validated_at, 90):
return log_in_user(user_id)
else:
if email_needs_revalidating(user):
user_api_client.send_verify_code(user.id, 'email', None, redirect_url)
return redirect(url_for('.revalidate_email_sent', next=redirect_url))
else:
return log_in_user(user_id)
return render_template('views/two-factor-sms.html', form=form, redirect_url=redirect_url)

View File

@@ -9,8 +9,11 @@ from app.main import main
from app.models.user import User
from app.models.webauthn_credential import RegistrationError, WebAuthnCredential
from app.notify_client.user_api_client import user_api_client
from app.utils import is_less_than_days_ago
from app.utils.login import log_in_user, redirect_to_sign_in
from app.utils.login import (
email_needs_revalidating,
log_in_user,
redirect_to_sign_in,
)
from app.utils.user import user_is_platform_admin
@@ -168,7 +171,7 @@ def _complete_webauthn_login_attempt(user):
# user account is locked as too many failed logins
abort(403)
if not is_less_than_days_ago(user.email_access_validated_at, 90):
if email_needs_revalidating(user):
user_api_client.send_verify_code(user.id, 'email', None, redirect_url)
return redirect(url_for('.revalidate_email_sent', next=redirect_url))