From 02907afce1467f1708efef8066c3e200c63710cd Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 7 May 2018 22:57:18 +0100 Subject: [PATCH] Refactor `sms_code` functionality into the class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So it’s all in one place, not two. --- app/main/forms.py | 16 ++++++---------- tests/app/main/views/test_two_factor.py | 3 +++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 05114c362..883c84b8e 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -161,19 +161,15 @@ def password(label='Password'): class SMSCode(StringField): - def __call__(self, **kwargs): - return super().__call__( - type='tel', pattern='[0-9]*', **kwargs - ) - - -def sms_code(): - return SMSCode('Text message code', validators=[ + validators = [ DataRequired(message='Can’t be empty'), Regexp(regex='^\d+$', message='Numbers only'), Length(min=5, message='Not enough numbers'), Length(max=5, message='Too many numbers'), - ]) + ] + + def __call__(self, **kwargs): + return super().__call__(type='tel', pattern='[0-9]*', **kwargs) def organisation_type(): @@ -323,7 +319,7 @@ class TwoFactorForm(StripWhitespaceForm): self.validate_code_func = validate_code_func super(TwoFactorForm, self).__init__(*args, **kwargs) - sms_code = sms_code() + sms_code = SMSCode('Text message code') def validate(self): diff --git a/tests/app/main/views/test_two_factor.py b/tests/app/main/views/test_two_factor.py index e6498beff..b6788de47 100644 --- a/tests/app/main/views/test_two_factor.py +++ b/tests/app/main/views/test_two_factor.py @@ -21,6 +21,9 @@ def test_should_render_two_factor_page( assert page.select_one('main p').text.strip() == ( 'We’ve sent you a text message with a security code.' ) + assert page.select_one('label').text.strip( + 'Text message code' + ) assert page.select_one('input')['type'] == 'tel' assert page.select_one('input')['pattern'] == '[0-9]*'