-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve /reset and /confirm w.r.t. GENERIC_RESPONSES and additional f…
…orm args. Title says it all - very confusing (UX) results when adding a form arg (e.g. captcha) so the above forms and having an error. closes #814
- Loading branch information
Showing
10 changed files
with
201 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% include "security/_messages.html" %} | ||
{% from "security/_macros.html" import render_field_with_errors, render_field, render_form_errors %} | ||
{{ render_field_with_errors(send_confirmation_form.email) }} | ||
{{ render_field_with_errors(send_confirmation_form.recaptcha) }} | ||
{{ render_form_errors(send_confirmation_form) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% include "security/_messages.html" %} | ||
{% from "security/_macros.html" import render_field_with_errors, render_field, render_form_errors %} | ||
{{ render_field_with_errors(forgot_password_form.email) }} | ||
{{ render_field_with_errors(forgot_password_form.recaptcha) }} | ||
{{ render_form_errors(forgot_password_form) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
from tests.test_utils import ( | ||
authenticate, | ||
capture_flashes, | ||
get_auth_token_version_3x, | ||
get_form_action, | ||
get_num_queries, | ||
|
@@ -218,12 +219,14 @@ def test_generic_response(app, client, get_message): | |
) | ||
|
||
# make sure don't get confirmation required | ||
response = client.post( | ||
"/login", | ||
data=dict(email="[email protected]", password="password"), | ||
follow_redirects=False, | ||
) | ||
assert response.status_code == 200 | ||
with capture_flashes() as flashes: | ||
response = client.post( | ||
"/login", | ||
data=dict(email="[email protected]", password="password"), | ||
follow_redirects=False, | ||
) | ||
assert response.status_code == 200 | ||
assert len(flashes) == 0 | ||
|
||
|
||
@pytest.mark.registerable() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,20 @@ | |
|
||
import pytest | ||
from flask import Flask | ||
from wtforms.fields import StringField | ||
from wtforms.validators import Length | ||
|
||
from flask_security.core import UserMixin | ||
from flask_security.core import Security, UserMixin | ||
from flask_security.confirmable import generate_confirmation_token | ||
from flask_security.signals import confirm_instructions_sent, user_confirmed | ||
from flask_security.forms import SendConfirmationForm | ||
|
||
from tests.test_utils import ( | ||
authenticate, | ||
capture_flashes, | ||
capture_registrations, | ||
logout, | ||
populate_data, | ||
) | ||
|
||
pytestmark = pytest.mark.confirmable() | ||
|
@@ -573,3 +577,64 @@ def on_instructions_sent(app, **kwargs): | |
response = client.post("/confirm", json=dict(email="[email protected]")) | ||
assert len(recorded_instructions_sent) == 2 | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_generic_with_extra(app, sqlalchemy_datastore): | ||
# If application adds a field, make sure we properly return errors | ||
# even if 'RETURN_GENERIC_RESPONSES' is set. | ||
class MySendConfirmationForm(SendConfirmationForm): | ||
recaptcha = StringField("Recaptcha", validators=[Length(min=5)]) | ||
|
||
app.config["SECURITY_RETURN_GENERIC_RESPONSES"] = True | ||
app.config["SECURITY_SEND_CONFIRMATION_TEMPLATE"] = "generic_confirm.html" | ||
app.security = Security( | ||
app, | ||
datastore=sqlalchemy_datastore, | ||
send_confirmation_form=MySendConfirmationForm, | ||
) | ||
|
||
populate_data(app) | ||
client = app.test_client() | ||
|
||
# Test valid user but invalid additional form field | ||
# We should get a form error for the extra (invalid) field, no flash | ||
bad_data = dict(email="[email protected]", recaptcha="1234") | ||
good_data = dict(email="[email protected]", recaptcha="123456") | ||
|
||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", data=bad_data) | ||
assert b"Field must be at least 5" in response.data | ||
assert len(flashes) == 0 | ||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", data=good_data) | ||
assert len(flashes) == 1 | ||
|
||
# JSON | ||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", json=bad_data) | ||
assert response.status_code == 400 | ||
assert ( | ||
"Field must be at least 5" | ||
in response.json["response"]["field_errors"]["recaptcha"][0] | ||
) | ||
assert len(flashes) == 0 | ||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", json=good_data) | ||
assert response.status_code == 200 | ||
assert len(flashes) == 0 | ||
|
||
# Try bad email AND bad recaptcha | ||
bad_data = dict(email="joe44-lp.com", recaptcha="1234") | ||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", data=bad_data) | ||
assert b"Field must be at least 5" in response.data | ||
assert len(flashes) == 0 | ||
with capture_flashes() as flashes: | ||
response = client.post("/confirm", json=bad_data) | ||
assert response.status_code == 400 | ||
assert ( | ||
"Field must be at least 5" | ||
in response.json["response"]["field_errors"]["recaptcha"][0] | ||
) | ||
assert len(response.json["response"]["errors"]) == 1 | ||
assert len(flashes) == 0 |
Oops, something went wrong.