Files
notifications-admin/tests/app/main/test_two_factor_form.py
Chris Hill-Scott b0cf36e1c7 Allow spaces and dashes in the two factor code
I noticed when using the dication software that saying ‘one two three
four five’ got dictated as `123 45`. This tripped the validation,
because the space character isn’t a digit.

So this commit normalises out spaces (and other spacing characters like
dashes and underscores) before validating the code and sending it to the
API.

I can also imagine that some people might like to space out the code to
make it easier to transcribe (like you might do with a credit card
number).
2020-04-17 16:16:52 +01:00

81 lines
2.1 KiB
Python

import pytest
from app import user_api_client
from app.main.forms import TwoFactorForm
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
@pytest.mark.parametrize('post_data', [
{'sms_code': '12345'},
{'sms_code': ' 12345 '},
{'sms_code': '12 34 5'},
{'sms_code': '1-23-45'},
])
def test_form_is_valid_returns_no_errors(
app_,
mock_check_verify_code,
post_data,
):
with app_.test_request_context(method='POST', data=post_data):
form = TwoFactorForm(_check_code)
assert form.validate() is True
assert form.errors == {}
mock_check_verify_code.assert_called_once_with('1', '12345', 'sms')
@pytest.mark.parametrize('post_data, expected_error', (
(
{'sms_code': '1234'},
'Not enough numbers',
),
(
{'sms_code': '123456'},
'Too many numbers',
),
(
{},
'Cannot be empty',
),
(
{'sms_code': '12E45'},
'Numbers only',
),
(
{'sms_code': ' ! 2 3 4 5'},
'Numbers only',
),
))
def test_check_verify_code_returns_errors(
app_,
post_data,
expected_error,
mock_check_verify_code,
):
with app_.test_request_context(method='POST', data=post_data):
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert form.errors == {'sms_code': [expected_error]}
def test_check_verify_code_returns_error_when_code_has_expired(
app_,
mock_check_verify_code_code_expired,
):
with app_.test_request_context(method='POST', data={'sms_code': '99999'}):
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert form.errors == {'sms_code': ['Code has expired']}
def test_check_verify_code_returns_error_when_code_was_not_found(
app_,
mock_check_verify_code_code_not_found,
):
with app_.test_request_context(method='POST', data={'sms_code': '99999'}):
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert form.errors == {'sms_code': ['Code not found']}