def root(): ipa = maybe_ipa_session(current_app, session) username = session.get('noggin_username') if ipa and username: return redirect(url_for('.user', username=username)) # Kick any non-authed user back to the login form. activetab = request.args.get("tab", "login") register_form = RegisterUserForm(prefix="register") login_form = LoginUserForm(prefix="login") if login_form.validate_on_submit(): with handle_form_errors(login_form): return handle_login_form(login_form) if register_form.validate_on_submit(): with handle_form_errors(register_form): return handle_register_form(register_form) return render_template( 'index.html', register_form=register_form, login_form=login_form, activetab=activetab, )
def test_formerror(client): with current_app.test_request_context('/'): form = LoginUserForm(data={"username": "******", "password": "******"}) form.validate() error = FormError("username", "error message") error.populate_form(form) assert form.errors == {"username": ["error message"]}
def test_formerror_unknown_field_append(client): with current_app.test_request_context('/'): form = LoginUserForm() form.validate() FormError("non_field_errors", "error message 1").populate_form(form) FormError("non_field_errors", "error message 2").populate_form(form) assert form.errors["non_field_errors"] == ["error message 1", "error message 2"]
def root(): ipa = maybe_ipa_session(current_app, session) username = session.get('noggin_username') if ipa and username: return redirect(url_for('.user', username=username)) # Kick any non-authed user back to the login form. activetab = request.args.get("tab", "login") register_form = RegisterUserForm(prefix="register") login_form = LoginUserForm(prefix="login") if login_form.validate_on_submit(): with handle_form_errors(login_form): return handle_login_form(login_form) if register_form.validate_on_submit(): if not current_app.config["REGISTRATION_OPEN"]: flash(_("Registration is closed at the moment."), "warning") return redirect(url_for('.root')) with handle_form_errors(register_form): return handle_register_form(register_form) return render_template( 'index.html', register_form=register_form, login_form=login_form, activetab=activetab, )
def test_formerror_existing_field(client): with current_app.test_request_context('/'): form = LoginUserForm() form.validate() error = FormError("username", "error message") error.populate_form(form) assert form.errors["username"] == [ "You must provide a user name", "error message", ]
def test_handle_form_errors(client): with current_app.test_request_context('/'): form = LoginUserForm() error = FormError("username", "error_message") with mock.patch.object(error, "populate_form") as populate_form: with handle_form_errors(form): raise error populate_form.assert_called_once_with(form)