Files
notifications-admin/tests/app/main/test_two_factor_form.py
Chris Hill-Scott 929dc45224 Normalize whitespace in test arguments
We have a bunch of different styles of handling when function
definitions span multiple lines, which they almost always do with tests.

Here’s why an argument per line, single indent is best:
- cleaner diffs when you change the name of a method (one line change
  instead of multiple lines)
- works better on narrow screens, eg Github’s diff view, or with two
  terminals side by side on a laptop screen
- works with any editor’s indenting shortcuts, no need for an IDE

Also, trailing comma in the list of arguments is good because adding a
new argument to a method becomes a one line, not two line diff.
2017-02-06 10:44:37 +00:00

75 lines
2.6 KiB
Python

from datetime import datetime, timedelta
from app.main.forms import TwoFactorForm
from app import user_api_client
def test_form_is_valid_returns_no_errors(
app_,
mock_check_verify_code,
):
with app_.test_request_context(method='POST',
data={'sms_code': '12345'}) as req:
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
form = TwoFactorForm(_check_code)
assert form.validate() is True
assert len(form.errors) == 0
def test_returns_errors_when_code_is_too_short(
app_,
mock_check_verify_code,
):
with app_.test_request_context(method='POST',
data={'sms_code': '145'}) as req:
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert len(form.errors) == 1
assert set(form.errors) == set({'sms_code': ['Code must be 5 digits', 'Code does not match']})
def test_returns_errors_when_code_is_missing(
app_,
mock_check_verify_code,
):
with app_.test_request_context(method='POST',
data={}) as req:
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert len(form.errors) == 1
assert set(form.errors) == set({'sms_code': ['Code must not be empty']})
def test_returns_errors_when_code_contains_letters(
app_,
mock_check_verify_code,
):
with app_.test_request_context(method='POST',
data={'sms_code': 'asdfg'}) as req:
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
form = TwoFactorForm(_check_code)
assert form.validate() is False
assert len(form.errors) == 1
assert set(form.errors) == set({'sms_code': ['Code must be 5 digits', 'Code does not match']})
def test_should_return_errors_when_code_is_expired(
app_,
mock_check_verify_code_code_expired,
):
with app_.test_request_context(method='POST',
data={'sms_code': '23456'}) as req:
def _check_code(code):
return user_api_client.check_verify_code('1', code, "sms")
form = TwoFactorForm(_check_code)
assert form.validate() is False
errors = form.errors
assert len(errors) == 1
assert errors == {'sms_code': ['Code has expired']}