Files
notifications-admin/tests/app/main/test_two_factor_form.py
Chris Hill-Scott 4d678aec93 Give better error messages for incorrect code
If we know the code won’t pass the validation on the API side, we might
as well tell the user before even passing it to the API.

So this commit:
- adds some more validators to the field
- rewrites the validation function on the form to actually call the
  field-level validators before hitting the API 🤦‍♂️
- refactors the tests to be parametrize, which means they can be
  shorter, easier to read, and more comprehensive
2018-05-08 10:53:22 +01:00

75 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pytest
from app import user_api_client
from app.main.forms import TwoFactorForm
from tests.conftest import (
mock_check_verify_code,
mock_check_verify_code_code_expired,
mock_check_verify_code_code_not_found,
)
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 '},
])
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 == {}
@pytest.mark.parametrize('mock, post_data, expected_error', (
(
mock_check_verify_code,
{'sms_code': '1234'},
'Not enough numbers',
),
(
mock_check_verify_code,
{'sms_code': '123456'},
'Too many numbers',
),
(
mock_check_verify_code,
{},
'Cant be empty',
),
(
mock_check_verify_code,
{'sms_code': '12E45'},
'Numbers only',
),
(
mock_check_verify_code_code_expired,
{'sms_code': '99999'},
'Code has expired',
),
(
mock_check_verify_code_code_not_found,
{'sms_code': '99999'},
'Code not found',
),
))
def test_returns_errors_when_code_is_too_short(
app_,
mocker,
mock,
post_data,
expected_error,
):
mock(mocker)
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]}