class PasswordResetForm(forms.Form): ''' Allows users to enter their phone number so they can be texted a code that will allow them to reset their password. ''' phone_number = USPhoneNumberField()
class OnboardingStep4Form(OptionalSetPasswordForm, forms.ModelForm): class Meta: model = OnboardingInfo fields = ('can_we_sms', 'signup_intent') phone_number = USPhoneNumberField() agree_to_terms = forms.BooleanField(required=True) def clean_phone_number(self): phone_number = self.cleaned_data['phone_number'] if JustfixUser.objects.filter(phone_number=phone_number).exists(): # TODO: Are we leaking valuable PII here? raise ValidationError( 'A user with that phone number already exists.') return phone_number
class BaseOnboardingStep4Form(SetPasswordForm, forms.ModelForm): class Meta: model = OnboardingInfo fields = ("can_we_sms", "signup_intent") phone_number = USPhoneNumberField() agree_to_terms = forms.BooleanField(required=True) def clean_phone_number(self): phone_number = self.cleaned_data["phone_number"] if JustfixUser.objects.filter(phone_number=phone_number).exists(): # TODO: Are we leaking valuable PII here? raise ValidationError( "A user with that phone number already exists.", code="PHONE_NUMBER_TAKEN" ) return phone_number
class RhForm(AddressAndBoroughFormMixin, forms.ModelForm): class Meta: model = RentalHistoryRequest fields = ( "first_name", "last_name", "apartment_number", ) phone_number = USPhoneNumberField() to_scaffolding_keys = { **AddressAndBoroughFormMixin.to_scaffolding_keys, "apartment_number": "apt_number", } from_scaffolding_keys = flip_dict(to_scaffolding_keys)
class LoginForm(forms.Form): phone_number = USPhoneNumberField() password = forms.CharField() # This will be set to a valid user once is_valid() returns True. authenticated_user: Optional[JustfixUser] = None def clean(self): cleaned_data = super().clean() phone_number = cleaned_data.get('phone_number') password = cleaned_data.get('password') if phone_number and password: user = authenticate(phone_number=phone_number, password=password) if user is None: raise ValidationError('Invalid phone number or password.', code='authenticate_failed') self.authenticated_user = user
def test_phone_number_field_raises_error_on_bad_area_code(): with pytest.raises(ValidationError, match="area code"): USPhoneNumberField().clean("1912311234")
def test_phone_number_field_raises_errors(bad_phone_number): with pytest.raises(ValidationError) as exc_info: USPhoneNumberField().clean(bad_phone_number) assert "This does not look like a U.S. phone number." in str(exc_info.value)
def test_phone_number_field_errors_on_really_long_input(): with pytest.raises(ValidationError) as exc_info: USPhoneNumberField().clean("5551234567" * 30) assert "Ensure this value has at most " in str(exc_info.value)
def test_phone_number_field_works(phone_number): assert USPhoneNumberField().clean(phone_number) == "5551234567"
class PhoneNumberForm(forms.Form): phone_number = USPhoneNumberField()
class PhoneNumberForm(forms.ModelForm): class Meta: model = JustfixUser fields = ["phone_number"] phone_number = USPhoneNumberField()