def test_form_invalid_email(self): form_data_invalid_email = { **self.form_data_valid, 'email': 'invalid#email.com', } form = forms.UserRegistrationForm(data=form_data_invalid_email) expected_error = 'Enter a valid email address.' assert form.is_valid() is False, \ 'Should return False - email field (required) is invalid' assert expected_error in form.errors['email']
def test_username_already_exist(self): form_data_invalid_username = { **self.form_data_valid, 'username': self.username, } form = forms.UserRegistrationForm(data=form_data_invalid_username) expected_error = 'A user with that username already exists.' assert expected_error in form.errors['username'] assert form.is_valid() is False, \ 'Should return False - username already exist'
def test_form_invalid_empty_username(self): form_data_invalid_username = { **self.form_data_valid, 'username': '', } form = forms.UserRegistrationForm(data=form_data_invalid_username) expected_error = 'This field is required.' assert expected_error in form.errors['username'] assert form.is_valid() is False, \ 'Should return False - username field (required) is empty'
def register_view(request): if request.method == 'POST': form = account_forms.UserRegistrationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') email = form.cleaned_data.get('email') first_name = form.cleaned_data.get('first_name') last_name = form.cleaned_data.get('last_name') password1 = form.cleaned_data.get('password1') password2 = form.cleaned_data.get('password2') user = authenticate(username=username, password=password1) login(request, user) messages.success(request, f"Welcome Mr.{username}.") return redirect('articles:index') else: form = account_forms.UserRegistrationForm() return render(request, 'account/register.html', {'form': form})
def test_form_invalid_password_too_short(self): form_data_invalid_password_too_short = { **self.form_data_valid, 'password': '******', 'password2': '12ab', } form = forms.UserRegistrationForm( data=form_data_invalid_password_too_short) assert form.is_valid() is False, \ 'Should return False - password to short' with self.assertRaises(django_form.ValidationError): form.clean()
def test_form_invalid_date_of_birth(self): form_data_invalid_date_of_birth = { **self.form_data_valid, 'date_of_birth': self.future_date(), } form = forms.UserRegistrationForm(data=form_data_invalid_date_of_birth) expected_error = 'Seriously are you coming from the future?' assert form.is_valid() is False, \ 'Should return False - date_of_birth from future' assert expected_error in form.non_field_errors() with self.assertRaises(django_form.ValidationError): form.clean()
def test_form_invalid_different_password(self): form_data_invalid_different_password = { **self.form_data_valid, 'password': '******', 'password2': 'abecadlo2', } form = forms.UserRegistrationForm( data=form_data_invalid_different_password) expected_error = "Passwords don't match." assert form.is_valid() is False, \ 'Should return False - password != password2' assert expected_error in form.non_field_errors().as_text() with self.assertRaises(django_form.ValidationError): form.clean()
def test_form_valid_data(self): form = forms.UserRegistrationForm(data=self.form_data_valid) assert form.is_valid() is True, 'Should return True - form is valid.'