def test_form_passwords_have_to_be_the_same(self): form = SignupForm( data={ 'email': '*****@*****.**', 'password1': 'bla', 'password2': 'blabla', 'first_name': 'Test' }) self.assertFalse(form.is_valid())
def test_form_validation(self): form = SignupForm( data={ 'email': '*****@*****.**', 'password1': 'bla', 'password2': 'bla', 'first_name': 'Test' }) self.assertTrue(form.is_valid())
def test_form_has_error_message_if_passwords_donot_match(self): form = SignupForm( data={ 'email': '*****@*****.**', 'password1': 'bla', 'password2': 'blabla', 'first_name': 'Test' }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['password2'][0], "Passwords do not match")
def test_submitting_form_saves_user(self): form = SignupForm( data={ 'email': '*****@*****.**', 'password1': 'bla', 'password2': 'bla', 'first_name': 'Test' }) self.assertTrue(form.is_valid()) form.save() user = User.objects.first() self.assertEqual(user.email, '*****@*****.**')
def signup(): if not current_user.is_anonymous: return redirect(url_for('index')) form = SignupForm() title = "Sign Up Page" success = False message = "" if form.validate_on_submit(): try: if form.password.data != form.re_password.data: raise ValueError("Passwords do not match!") if User.query.filter_by(username=form.username.data).first(): raise ValueError("Username exists!") email = form.email.data if form.email.data.strip() != "" else None new_user = User(username=form.username.data, email=email) new_user.set_password(form.password.data) db.session.add(new_user) db.session.commit() success = True message += "Sign Up Successful, Please Log In" except ValueError as e: message += f'{e.args[0]}' except Exception as e: message += f" {e}" db.session.rollback() finally: flash(message) return redirect(url_for('login')) if success else redirect( url_for('signup')) else: for field, errors in form.errors.items(): for error in errors: flash(f"{field}: {error}") return render_template("signup.html", title=title, form=form)
def signup(request): if request.method == "POST": form = SignupForm(request.POST) if form.is_valid(): form.save() return redirect('signin') else: for error in form.errors: messages.error(request, form.errors[error]) context = { 'form': SignupForm(), } return render(request, 'chat/signup.html', context=context)
def test_form_has_placeholders(self): form = SignupForm() self.assertIn('email', form.as_p())