Move date-checking logic to utils and unit test it

This commit is contained in:
Pea Tyczynska
2020-02-03 14:27:30 +00:00
parent caf77341b3
commit 7d460fe483
3 changed files with 18 additions and 5 deletions

View File

@@ -1,5 +1,4 @@
import json
from datetime import datetime
from flask import (
current_app,
@@ -17,7 +16,7 @@ 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 redirect_to_sign_in
from app.utils import is_less_than_90_days_ago, redirect_to_sign_in
@main.route('/two-factor-email-sent', methods=['GET'])
@@ -66,9 +65,7 @@ def two_factor():
form = TwoFactorForm(_check_code)
if form.validate_on_submit():
if (datetime.utcnow() - datetime.strptime(
user.email_access_validated_at, '%a, %d %b %Y %X %Z'
)).days < 90:
if is_less_than_90_days_ago(user.email_access_validated_at):
return log_in_user(user_id)
else:
user_api_client.send_verify_code(user.id, 'email', None, request.args.get('next'))

View File

@@ -676,3 +676,9 @@ def format_thousands(value):
if value is None:
return ''
return value
def is_less_than_90_days_ago(date_from_db):
return (datetime.utcnow() - datetime.strptime(
date_from_db, '%a, %d %b %Y %X %Z'
)).days < 90

View File

@@ -18,6 +18,7 @@ from app.utils import (
get_letter_printing_statement,
get_letter_validation_error,
get_logo_cdn_domain,
is_less_than_90_days_ago,
printing_today_or_tomorrow,
)
from tests.conftest import fake_uuid
@@ -506,3 +507,12 @@ def test_get_letter_validation_error_for_known_errors(
if summary.select_one('a'):
assert summary.select_one('a')['href'] == url_for('.letter_spec')
assert summary.select_one('a')['target'] == '_blank'
@pytest.mark.parametrize("date_from_db, expected_result", [
('Sun, 17 Nov 2019 11:28:25 GMT', True),
('Sun, 16 Nov 2019 11:28:25 GMT', False),
])
@freeze_time('2020-02-14T12:00:00')
def test_is_less_than_90_days_ago(date_from_db, expected_result):
assert is_less_than_90_days_ago(date_from_db) == expected_result