| 1 |
""" |
| 2 |
test_registerable |
| 3 |
~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Registerable tests |
| 6 |
""" |
| 7 |
|
| 8 |
import pytest |
| 9 |
import re |
| 10 |
from flask import Flask |
| 11 |
from tests.conftest import v2_param |
| 12 |
from tests.test_utils import ( |
| 13 |
authenticate, |
| 14 |
get_form_input_value, |
| 15 |
get_form_input, |
| 16 |
init_app_with_options, |
| 17 |
json_authenticate, |
| 18 |
logout, |
| 19 |
is_authenticated, |
| 20 |
) |
| 21 |
|
| 22 |
from flask_security import Security, UserMixin, user_registered, user_not_registered |
| 23 |
from flask_security.forms import ( |
| 24 |
ConfirmRegisterForm, |
| 25 |
RegisterForm, |
| 26 |
RegisterFormV2, |
| 27 |
StringField, |
| 28 |
_default_field_labels, |
| 29 |
) |
| 30 |
from flask_security.utils import localize_callback |
| 31 |
|
| 32 |
pytestmark = pytest.mark.registerable() |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 36 |
@pytest.mark.settings(post_register_view="/post_register") |
| 37 |
def test_registerable_flag(clients, app, get_message, outbox): |
| 38 |
recorded = [] |
| 39 |
|
| 40 |
# Test the register view |
| 41 |
response = clients.get("/register") |
| 42 |
assert b"<h1>Register</h1>" in response.data |
| 43 |
assert re.search(b'<input[^>]*type="email"[^>]*>', response.data) |
| 44 |
assert get_form_input_value(response, "email") is not None |
| 45 |
assert not get_form_input_value(response, "username") |
| 46 |
|
| 47 |
# Test registering is successful, sends email, and fires signal |
| 48 |
@user_registered.connect_via(app) |
| 49 |
def on_user_registered(app, **kwargs): |
| 50 |
assert isinstance(app, Flask) |
| 51 |
assert isinstance(kwargs["user"], UserMixin) |
| 52 |
assert kwargs["confirm_token"] is None |
| 53 |
assert len(kwargs["form_data"].keys()) > 0 |
| 54 |
|
| 55 |
recorded.append(kwargs["user"]) |
| 56 |
|
| 57 |
data = dict( |
| 58 |
email="[email protected]", |
| 59 |
password="battery staple", |
| 60 |
password_confirm="battery staple", |
| 61 |
next="", |
| 62 |
) |
| 63 |
response = clients.post("/register", data=data, follow_redirects=True) |
| 64 |
|
| 65 |
assert len(recorded) == 1 |
| 66 |
assert len(outbox) == 1 |
| 67 |
assert b"Post Register" in response.data |
| 68 |
|
| 69 |
logout(clients) |
| 70 |
|
| 71 |
# Test user can login after registering |
| 72 |
response = authenticate(clients, email="[email protected]", password="battery staple") |
| 73 |
assert response.status_code in [302, 303] |
| 74 |
|
| 75 |
logout(clients) |
| 76 |
|
| 77 |
# Test registering with an existing email |
| 78 |
data = dict( |
| 79 |
email="[email protected]", password="password", password_confirm="password", next="" |
| 80 |
) |
| 81 |
response = clients.post("/register", data=data, follow_redirects=True) |
| 82 |
assert get_message("EMAIL_ALREADY_ASSOCIATED", email="[email protected]") in response.data |
| 83 |
|
| 84 |
# Test registering with an existing email but case insensitive |
| 85 |
data = dict( |
| 86 |
email="[email protected]", password="password", password_confirm="password", next="" |
| 87 |
) |
| 88 |
response = clients.post("/register", data=data, follow_redirects=True) |
| 89 |
assert get_message("EMAIL_ALREADY_ASSOCIATED", email="[email protected]") in response.data |
| 90 |
|
| 91 |
# Test registering with JSON |
| 92 |
data = dict( |
| 93 |
email="[email protected]", password="horse battery", password_confirm="horse battery" |
| 94 |
) |
| 95 |
response = clients.post( |
| 96 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 97 |
) |
| 98 |
|
| 99 |
assert response.headers["content-type"] == "application/json" |
| 100 |
assert response.json["meta"]["code"] == 200 |
| 101 |
assert len(response.json["response"]) == 3 |
| 102 |
assert all( |
| 103 |
k in response.json["response"] for k in ["csrf_token", "user", "tf_required"] |
| 104 |
) |
| 105 |
|
| 106 |
logout(clients) |
| 107 |
|
| 108 |
# Test registering with invalid JSON |
| 109 |
data = dict(email="bogus", password="password", password_confirm="password") |
| 110 |
response = clients.post( |
| 111 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 112 |
) |
| 113 |
assert response.headers["content-type"] == "application/json" |
| 114 |
assert response.json["meta"]["code"] == 400 |
| 115 |
|
| 116 |
logout(clients) |
| 117 |
|
| 118 |
# Test ?next param |
| 119 |
data = dict( |
| 120 |
email="[email protected]", |
| 121 |
password="horse staple", |
| 122 |
password_confirm="horse staple", |
| 123 |
next="", |
| 124 |
) |
| 125 |
|
| 126 |
response = clients.post("/register?next=/page1", data=data, follow_redirects=True) |
| 127 |
assert b"Page 1" in response.data |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.csrf(csrfprotect=True) |
| 131 |
def test_form_csrf(app, client): |
| 132 |
# Test that CSRFprotect config catches CSRF requirement at unauth_csrf() decorator. |
| 133 |
# Note that in this case - 400 is returned - though if CSRFprotect isn't enabled |
| 134 |
# then CSRF errors are caught at form.submit time and a 200 with error values |
| 135 |
# is returned. |
| 136 |
response = client.post( |
| 137 |
"/register", |
| 138 |
data=dict( |
| 139 |
email="[email protected]", |
| 140 |
password="mypassword", |
| 141 |
password_confirm="mypassword", |
| 142 |
), |
| 143 |
) |
| 144 |
assert response.status_code == 400 |
| 145 |
assert b"The CSRF token is missing" in response.data |
| 146 |
|
| 147 |
response = client.get("/register") |
| 148 |
csrf_token = get_form_input_value(response, "csrf_token") |
| 149 |
response = client.post( |
| 150 |
"/register", |
| 151 |
data=dict( |
| 152 |
email="[email protected]", |
| 153 |
password="mypassword", |
| 154 |
password_confirm="mypassword", |
| 155 |
csrf_token=csrf_token, |
| 156 |
), |
| 157 |
follow_redirects=False, |
| 158 |
) |
| 159 |
assert response.status_code in [302, 303] |
| 160 |
assert response.location == "/" |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.confirmable() |
| 164 |
@pytest.mark.babel(babel_default_locale="cic_US", test_xlations=True) |
| 165 |
def test_xlation(app, client, get_message_local, outbox): |
| 166 |
# Test form and email translation |
| 167 |
confirmation_token = [] |
| 168 |
|
| 169 |
@user_registered.connect_via(app) |
| 170 |
def on_user_registered(app, **kwargs): |
| 171 |
confirmation_token.append(kwargs["confirmation_token"]) |
| 172 |
|
| 173 |
response = client.get("/register", follow_redirects=True) |
| 174 |
with app.test_request_context(): |
| 175 |
# Check header |
| 176 |
header = localize_callback("Register") |
| 177 |
assert "OKAY!" in header # ensures actual translation |
| 178 |
assert f"<h1>{header}</h1>".encode() in response.data |
| 179 |
submit = localize_callback(_default_field_labels["register"]) |
| 180 |
assert "OKAY!" in submit |
| 181 |
assert f'value="{submit}"'.encode() in response.data |
| 182 |
|
| 183 |
response = client.post( |
| 184 |
"/register", |
| 185 |
data={ |
| 186 |
"email": "[email protected]", |
| 187 |
"password": "new strong password", |
| 188 |
"password_confirm": "new strong password", |
| 189 |
}, |
| 190 |
follow_redirects=True, |
| 191 |
) |
| 192 |
|
| 193 |
with app.test_request_context(): |
| 194 |
flash = get_message_local("CONFIRM_REGISTRATION", email="[email protected]") |
| 195 |
assert "OKAY!" in flash |
| 196 |
assert flash.encode() in response.data |
| 197 |
assert b"Home Page" in response.data |
| 198 |
assert len(outbox) == 1 |
| 199 |
subj = localize_callback(app.config["SECURITY_EMAIL_SUBJECT_REGISTER"]) |
| 200 |
assert "OKAY!" in subj |
| 201 |
assert subj in outbox[0].subject |
| 202 |
|
| 203 |
lc = localize_callback( |
| 204 |
'Use <a href="%(confirmation_link)s">this link</a> to confirm your email' |
| 205 |
" address.", |
| 206 |
confirmation_link=f"http://localhost/confirm/{confirmation_token[0]}", |
| 207 |
) |
| 208 |
assert "OKAY!" in lc |
| 209 |
assert lc in outbox[0].alts["html"] |
| 210 |
|
| 211 |
|
| 212 |
@pytest.mark.confirmable() |
| 213 |
@pytest.mark.settings(use_register_v2=False) |
| 214 |
def test_required_password(client, get_message): |
| 215 |
# when confirm required - should not require confirm_password - but should |
| 216 |
# require a password |
| 217 |
data = dict(email="[email protected]", password="") |
| 218 |
response = client.post("/register", data=data, follow_redirects=True) |
| 219 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 220 |
|
| 221 |
data = dict(email="[email protected]", password="battery staple") |
| 222 |
response = client.post("/register", data=data, follow_redirects=True) |
| 223 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 224 |
|
| 225 |
|
| 226 |
def test_register_non_string_password(client): |
| 227 |
# ConfirmRegisterForm and RegisterFormV2 have special validation code for |
| 228 |
# OWASP (user enumeration). So they should have a separate test case from |
| 229 |
# general IsString tests. |
| 230 |
response = client.post( |
| 231 |
"/register", |
| 232 |
json=dict(email="[email protected]", password={"not": "a string"}), |
| 233 |
) |
| 234 |
assert response.status_code == 400 |
| 235 |
|
| 236 |
|
| 237 |
@pytest.mark.settings(use_register_v2=False) |
| 238 |
def test_required_password_confirm(client, get_message): |
| 239 |
response = client.post( |
| 240 |
"/register", |
| 241 |
data={ |
| 242 |
"email": "[email protected]", |
| 243 |
"password": "password", |
| 244 |
"password_confirm": "notpassword", |
| 245 |
}, |
| 246 |
follow_redirects=True, |
| 247 |
) |
| 248 |
assert get_message("RETYPE_PASSWORD_MISMATCH") in response.data |
| 249 |
|
| 250 |
response = client.post( |
| 251 |
"/register", |
| 252 |
data={"email": "[email protected]", "password": "password", "password_confirm": ""}, |
| 253 |
follow_redirects=True, |
| 254 |
) |
| 255 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 256 |
|
| 257 |
|
| 258 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 259 |
@pytest.mark.unified_signin() |
| 260 |
@pytest.mark.confirmable() |
| 261 |
@pytest.mark.settings(password_required=False) |
| 262 |
def test_allow_null_password(app, client, get_message): |
| 263 |
# Test password not required with either register form |
| 264 |
response = client.get("/register") |
| 265 |
data = dict(email="[email protected]") |
| 266 |
pw = get_form_input(response, "password") |
| 267 |
assert "required" not in pw |
| 268 |
pwc = get_form_input(response, "password_confirm") |
| 269 |
assert not pwc or "required" not in pwc |
| 270 |
|
| 271 |
response = client.post("/register", data=data, follow_redirects=True) |
| 272 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 273 |
logout(client) |
| 274 |
|
| 275 |
# should be able to register with password and password_confirm (with RegisterFormV2 |
| 276 |
if app.config["SECURITY_USE_REGISTER_V2"]: |
| 277 |
data = dict( |
| 278 |
email="[email protected]", |
| 279 |
password="battery staple", |
| 280 |
password_confirm="battery staples", |
| 281 |
) |
| 282 |
response = client.post("/register", data=data, follow_redirects=True) |
| 283 |
assert get_message("RETYPE_PASSWORD_MISMATCH") in response.data |
| 284 |
|
| 285 |
data = dict( |
| 286 |
email="[email protected]", |
| 287 |
password="battery staple", |
| 288 |
password_confirm="battery staple", |
| 289 |
) |
| 290 |
response = client.post("/register", data=data, follow_redirects=True) |
| 291 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 292 |
|
| 293 |
|
| 294 |
@pytest.mark.unified_signin() |
| 295 |
@pytest.mark.settings(password_required=False, use_register_v2=False) |
| 296 |
def test_allow_null_password_nologin(client, get_message): |
| 297 |
# If unified sign in is enabled - should be able to register w/o password |
| 298 |
# With confirmable false - should be logged in automatically upon register. |
| 299 |
# But shouldn't be able to perform normal login again |
| 300 |
data = dict(email="[email protected]", password="") |
| 301 |
response = client.post("/register", data=data, follow_redirects=True) |
| 302 |
assert b"Welcome [email protected]" in response.data |
| 303 |
logout(client) |
| 304 |
|
| 305 |
# Make sure can't log in |
| 306 |
response = authenticate(client, email="[email protected]", password="") |
| 307 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 308 |
|
| 309 |
response = authenticate(client, email="[email protected]", password="NoPassword") |
| 310 |
assert get_message("INVALID_PASSWORD") in response.data |
| 311 |
|
| 312 |
|
| 313 |
@pytest.mark.settings( |
| 314 |
register_url="/custom_register", post_register_view="/post_register" |
| 315 |
) |
| 316 |
def test_custom_register_url(client): |
| 317 |
response = client.get("/custom_register") |
| 318 |
assert b"<h1>Register</h1>" in response.data |
| 319 |
|
| 320 |
data = dict( |
| 321 |
email="[email protected]", |
| 322 |
password="battery staple", |
| 323 |
password_confirm="battery staple", |
| 324 |
next="", |
| 325 |
) |
| 326 |
|
| 327 |
response = client.post("/custom_register", data=data, follow_redirects=True) |
| 328 |
assert b"Post Register" in response.data |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.settings(register_user_template="custom_security/register_user.html") |
| 332 |
def test_custom_register_template(client): |
| 333 |
response = client.get("/register") |
| 334 |
assert b"CUSTOM REGISTER USER" in response.data |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.settings(send_register_email=False) |
| 338 |
def test_disable_register_emails(client, app, outbox): |
| 339 |
data = dict( |
| 340 |
email="[email protected]", password="password", password_confirm="password", next="" |
| 341 |
) |
| 342 |
client.post("/register", data=data, follow_redirects=True) |
| 343 |
assert len(outbox) == 0 |
| 344 |
|
| 345 |
|
| 346 |
@pytest.mark.two_factor() |
| 347 |
@pytest.mark.settings(two_factor_required=True) |
| 348 |
def test_two_factor(app, client): |
| 349 |
"""If two-factor is enabled, the register shouldn't login, but start the |
| 350 |
2-factor setup. |
| 351 |
""" |
| 352 |
data = dict(email="[email protected]", password="password", password_confirm="password") |
| 353 |
response = client.post("/register", data=data, follow_redirects=False) |
| 354 |
assert "tf-setup" in response.location |
| 355 |
|
| 356 |
# make sure not logged in |
| 357 |
response = client.get("/profile") |
| 358 |
assert response.status_code in [302, 303] |
| 359 |
assert response.location == "/login?next=/profile" |
| 360 |
|
| 361 |
|
| 362 |
@pytest.mark.two_factor() |
| 363 |
@pytest.mark.settings(two_factor_required=True) |
| 364 |
def test_two_factor_json(app, client, get_message): |
| 365 |
data = dict(email="[email protected]", password="password", password_confirm="password") |
| 366 |
response = client.post("/register", content_type="application/json", json=data) |
| 367 |
assert response.status_code == 200 |
| 368 |
assert response.json["response"]["tf_required"] |
| 369 |
assert response.json["response"]["tf_state"] == "setup_from_login" |
| 370 |
|
| 371 |
# make sure not logged in |
| 372 |
response = client.get("/profile", headers={"accept": "application/json"}) |
| 373 |
assert response.status_code == 401 |
| 374 |
assert response.json["response"]["errors"][0].encode("utf-8") == get_message( |
| 375 |
"UNAUTHENTICATED" |
| 376 |
) |
| 377 |
|
| 378 |
|
| 379 |
def test_form_data_is_passed_to_user_registered_signal(app, sqlalchemy_datastore): |
| 380 |
class MyRegisterForm(RegisterFormV2): |
| 381 |
additional_field = StringField("additional_field") |
| 382 |
|
| 383 |
app.security = Security( |
| 384 |
app, datastore=sqlalchemy_datastore, register_form=MyRegisterForm |
| 385 |
) |
| 386 |
|
| 387 |
recorded = [] |
| 388 |
|
| 389 |
@user_registered.connect_via(app) |
| 390 |
def on_user_registered(app, **kwargs): |
| 391 |
assert isinstance(app, Flask) |
| 392 |
assert isinstance(kwargs["user"], UserMixin) |
| 393 |
assert kwargs["confirm_token"] is None |
| 394 |
assert kwargs["confirmation_token"] is None |
| 395 |
assert kwargs["form_data"]["additional_field"] == "additional_data" |
| 396 |
|
| 397 |
recorded.append(kwargs["user"]) |
| 398 |
|
| 399 |
client = app.test_client() |
| 400 |
|
| 401 |
data = dict( |
| 402 |
email="[email protected]", |
| 403 |
password="password", |
| 404 |
password_confirm="password", |
| 405 |
additional_field="additional_data", |
| 406 |
) |
| 407 |
response = client.post("/register", data=data, follow_redirects=True) |
| 408 |
|
| 409 |
assert response.status_code == 200 |
| 410 |
assert len(recorded) == 1 |
| 411 |
|
| 412 |
|
| 413 |
@pytest.mark.settings(password_complexity_checker="zxcvbn") |
| 414 |
@pytest.mark.filterwarnings("ignore:.*The ConfirmRegisterForm.*:DeprecationWarning") |
| 415 |
def test_easy_password(app, sqlalchemy_datastore): |
| 416 |
class MyRegisterForm(ConfirmRegisterForm): |
| 417 |
username = StringField("Username") |
| 418 |
|
| 419 |
app.config["SECURITY_CONFIRM_REGISTER_FORM"] = MyRegisterForm |
| 420 |
security = Security(datastore=sqlalchemy_datastore) |
| 421 |
security.init_app(app) |
| 422 |
|
| 423 |
client = app.test_client() |
| 424 |
|
| 425 |
# With zxcvbn |
| 426 |
data = dict( |
| 427 |
email="[email protected]", |
| 428 |
username="dude", |
| 429 |
password="mattmatt2", |
| 430 |
password_confirm="mattmatt2", |
| 431 |
) |
| 432 |
response = client.post( |
| 433 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 434 |
) |
| 435 |
assert response.headers["Content-Type"] == "application/json" |
| 436 |
assert response.status_code == 400 |
| 437 |
# Response from zxcvbn |
| 438 |
assert "Repeats like" in response.json["response"]["errors"][0] |
| 439 |
|
| 440 |
# Test that username affects password selection |
| 441 |
data = dict( |
| 442 |
email="[email protected]", |
| 443 |
username="Joe", |
| 444 |
password="JoeTheDude", |
| 445 |
password_confirm="JoeTheDude", |
| 446 |
) |
| 447 |
response = client.post( |
| 448 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 449 |
) |
| 450 |
assert response.headers["Content-Type"] == "application/json" |
| 451 |
assert response.status_code == 400 |
| 452 |
# Response from zxcvbn |
| 453 |
assert ( |
| 454 |
"Password not complex enough" |
| 455 |
in response.json["response"]["field_errors"]["password"][0] |
| 456 |
) |
| 457 |
|
| 458 |
|
| 459 |
@pytest.mark.settings(username_enable=True, password_confirm_required=False) |
| 460 |
def test_nullable_username(app, client): |
| 461 |
# sqlalchemy datastore uses fsqlav2 which has username as unique and nullable |
| 462 |
# make sure can register multiple users with no username |
| 463 |
# Note that current WTForms (2.2.1) has a bug where StringFields can never be |
| 464 |
# None - it changes them to an empty string. DBs don't like that if you have |
| 465 |
# your column be 'nullable'. |
| 466 |
data = dict(email="[email protected]", password="password") |
| 467 |
response = client.post( |
| 468 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 469 |
) |
| 470 |
assert response.status_code == 200 |
| 471 |
logout(client) |
| 472 |
|
| 473 |
data = dict(email="[email protected]", password="password") |
| 474 |
response = client.post( |
| 475 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 476 |
) |
| 477 |
assert response.status_code == 200 |
| 478 |
|
| 479 |
|
| 480 |
def test_email_normalization(app, client): |
| 481 |
# should be able to login either as LP.com or lp.com or canonical unicode form |
| 482 |
data = dict( |
| 483 |
email="Imnumber\N{OHM SIGN}@LP.com", |
| 484 |
password="battery staple", |
| 485 |
password_confirm="battery staple", |
| 486 |
) |
| 487 |
|
| 488 |
response = client.post("/register", data=data, follow_redirects=True) |
| 489 |
assert b"Home Page" in response.data |
| 490 |
logout(client) |
| 491 |
|
| 492 |
# Test user can login after registering |
| 493 |
response = authenticate( |
| 494 |
client, email="Imnumber\N{OHM SIGN}@lp.com", password="battery staple" |
| 495 |
) |
| 496 |
assert response.status_code in [302, 303] |
| 497 |
|
| 498 |
logout(client) |
| 499 |
# Test user can login after registering using original non-canonical email |
| 500 |
response = authenticate( |
| 501 |
client, email="Imnumber\N{OHM SIGN}@LP.com", password="battery staple" |
| 502 |
) |
| 503 |
assert response.status_code in [302, 303] |
| 504 |
|
| 505 |
logout(client) |
| 506 |
# Test user can login after registering using original non-canonical email |
| 507 |
response = authenticate( |
| 508 |
client, |
| 509 |
email="Imnumber\N{GREEK CAPITAL LETTER OMEGA}@LP.com", |
| 510 |
password="battery staple", |
| 511 |
) |
| 512 |
assert response.status_code in [302, 303] |
| 513 |
|
| 514 |
|
| 515 |
def test_email_normalization_options(app, client, get_message): |
| 516 |
# verify can set options for email_validator |
| 517 |
data = dict( |
| 518 |
email="\N{BLACK SCISSORS}@LP.com", |
| 519 |
password="battery staple", |
| 520 |
password_confirm="battery staple", |
| 521 |
) |
| 522 |
|
| 523 |
response = client.post("/register", data=data, follow_redirects=True) |
| 524 |
assert b"Home Page" in response.data |
| 525 |
logout(client) |
| 526 |
|
| 527 |
# turn off allowing 'local' part unicode. |
| 528 |
app.config["SECURITY_EMAIL_VALIDATOR_ARGS"] = {"allow_smtputf8": False} |
| 529 |
data = dict( |
| 530 |
email="\N{WHITE SCISSORS}@LP.com", |
| 531 |
password="battery staple", |
| 532 |
password_confirm="battery staple", |
| 533 |
) |
| 534 |
|
| 535 |
response = client.post("/register", data=data, follow_redirects=True) |
| 536 |
assert response.status_code == 200 |
| 537 |
assert get_message("INVALID_EMAIL_ADDRESS") in response.data |
| 538 |
|
| 539 |
|
| 540 |
@pytest.mark.babel() |
| 541 |
def test_form_error(app, client, get_message): |
| 542 |
# A few form validations use ValidatorMixin which provides a lazy string |
| 543 |
# Since CLI doesn't render_template it was seeing those lazy strings. |
| 544 |
# This test basically just illustrates all this. |
| 545 |
from babel.support import LazyProxy |
| 546 |
|
| 547 |
with app.test_request_context("/register"): |
| 548 |
rform = ConfirmRegisterForm() |
| 549 |
|
| 550 |
rform.validate() |
| 551 |
assert isinstance(rform.errors["email"][0], LazyProxy) |
| 552 |
assert str(rform.errors["email"][0]).encode("utf-8") == get_message( |
| 553 |
"EMAIL_NOT_PROVIDED" |
| 554 |
) |
| 555 |
|
| 556 |
# make sure rendered template has converted LocalProxy strings. |
| 557 |
rendered = app.security.render_template( |
| 558 |
app.config["SECURITY_REGISTER_USER_TEMPLATE"], |
| 559 |
register_user_form=rform, |
| 560 |
) |
| 561 |
assert get_message("EMAIL_NOT_PROVIDED") in rendered.encode("utf-8") |
| 562 |
|
| 563 |
|
| 564 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 565 |
@pytest.mark.settings(username_enable=True, password_confirm_required=False) |
| 566 |
@pytest.mark.unified_signin() |
| 567 |
def test_username(app, clients, get_message): |
| 568 |
client = clients |
| 569 |
data = dict( |
| 570 |
email="[email protected]", |
| 571 |
username="dude", |
| 572 |
password="awesome sunset", |
| 573 |
) |
| 574 |
response = client.post( |
| 575 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 576 |
) |
| 577 |
assert response.headers["Content-Type"] == "application/json" |
| 578 |
assert response.status_code == 200 |
| 579 |
logout(client) |
| 580 |
|
| 581 |
# login using historic - email field to hold a username - won't work since |
| 582 |
# it is an EmailField |
| 583 |
response = json_authenticate(client, email="dude", password="awesome sunset") |
| 584 |
assert response.status_code == 400 |
| 585 |
assert ( |
| 586 |
get_message("INVALID_EMAIL_ADDRESS", username="dude") |
| 587 |
== response.json["response"]["errors"][0].encode() |
| 588 |
) |
| 589 |
# login using username |
| 590 |
response = client.post( |
| 591 |
"/login", json=dict(username="dude", password="awesome sunset") |
| 592 |
) |
| 593 |
assert response.status_code == 200 |
| 594 |
assert is_authenticated(client, get_message) |
| 595 |
logout(client) |
| 596 |
|
| 597 |
# login with email |
| 598 |
response = client.post( |
| 599 |
"/login", json=dict(email="[email protected]", password="awesome sunset") |
| 600 |
) |
| 601 |
assert response.status_code == 200 |
| 602 |
assert is_authenticated(client, get_message) |
| 603 |
logout(client) |
| 604 |
|
| 605 |
response = client.post( |
| 606 |
"/login", json=dict(emails="dude", password="awesome sunset") |
| 607 |
) |
| 608 |
assert response.status_code == 400 |
| 609 |
assert ( |
| 610 |
get_message("USER_DOES_NOT_EXIST") |
| 611 |
== response.json["response"]["field_errors"][""][0].encode() |
| 612 |
) |
| 613 |
|
| 614 |
# login using us-signin |
| 615 |
response = client.post( |
| 616 |
"/us-signin", |
| 617 |
data=dict(identity="dude", passcode="awesome sunset"), |
| 618 |
follow_redirects=True, |
| 619 |
) |
| 620 |
assert b"Welcome [email protected]" in response.data |
| 621 |
|
| 622 |
# make sure username is unique |
| 623 |
logout(client) |
| 624 |
data = dict( |
| 625 |
email="[email protected]", |
| 626 |
username="dude", |
| 627 |
password="awesome sunset", |
| 628 |
) |
| 629 |
response = client.post( |
| 630 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 631 |
) |
| 632 |
assert response.status_code == 400 |
| 633 |
assert ( |
| 634 |
get_message("USERNAME_ALREADY_ASSOCIATED", username="dude") |
| 635 |
== response.json["response"]["field_errors"]["username"][0].encode() |
| 636 |
) |
| 637 |
|
| 638 |
|
| 639 |
@pytest.mark.settings(username_enable=True) |
| 640 |
def test_username_not_set(app, client, get_message): |
| 641 |
# login with null username - shouldn't match |
| 642 |
# note that this is caught at LoginForm - it doesn't force a DB call |
| 643 |
response = client.post( |
| 644 |
"/register", |
| 645 |
json=dict( |
| 646 |
email="[email protected]", |
| 647 |
username="", |
| 648 |
password="awesome sunset", |
| 649 |
password_confirm="awesome sunset", |
| 650 |
), |
| 651 |
) |
| 652 |
assert response.status_code == 200 |
| 653 |
client.post("/logout") |
| 654 |
response = client.post("/login", json=dict(username="", password="awesome sunset")) |
| 655 |
assert response.status_code == 400 |
| 656 |
assert ( |
| 657 |
get_message("USER_DOES_NOT_EXIST") |
| 658 |
== response.json["response"]["field_errors"][""][0].encode() |
| 659 |
) |
| 660 |
|
| 661 |
|
| 662 |
@pytest.mark.settings(username_enable=True) |
| 663 |
def test_username_template(app, client): |
| 664 |
# verify template displays username option |
| 665 |
response = client.get("/register") |
| 666 |
username_field = get_form_input_value(response, "username") |
| 667 |
assert username_field is not None |
| 668 |
|
| 669 |
|
| 670 |
@pytest.mark.settings(username_enable=True) |
| 671 |
@pytest.mark.unified_signin() |
| 672 |
def test_username_normalize(app, client, get_message): |
| 673 |
data = dict( |
| 674 |
email="[email protected]", |
| 675 |
username="Imnumber\N{ROMAN NUMERAL ONE}", |
| 676 |
password="awesome sunset", |
| 677 |
password_confirm="awesome sunset", |
| 678 |
) |
| 679 |
response = client.post( |
| 680 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 681 |
) |
| 682 |
assert response.status_code == 200 |
| 683 |
logout(client) |
| 684 |
|
| 685 |
response = client.post( |
| 686 |
"/us-signin", |
| 687 |
data=dict( |
| 688 |
identity="Imnumber\N{LATIN CAPITAL LETTER I}", passcode="awesome sunset" |
| 689 |
), |
| 690 |
follow_redirects=True, |
| 691 |
) |
| 692 |
assert b"Welcome [email protected]" in response.data |
| 693 |
|
| 694 |
|
| 695 |
@pytest.mark.settings(username_enable=True, username_required=True) |
| 696 |
def test_username_errors(app, client, get_message): |
| 697 |
data = dict( |
| 698 |
email="[email protected]", |
| 699 |
username="dud", |
| 700 |
password="awesome sunset", |
| 701 |
password_confirm="awesome sunset", |
| 702 |
) |
| 703 |
response = client.post( |
| 704 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 705 |
) |
| 706 |
assert response.status_code == 400 |
| 707 |
assert "Username must be at least" in response.json["response"]["errors"][0] |
| 708 |
|
| 709 |
data["username"] = "howlongcanIbebeforeyoucomplainIsupposereallyreallylong" |
| 710 |
response = client.post( |
| 711 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 712 |
) |
| 713 |
assert response.status_code == 400 |
| 714 |
assert "Username must be at least" in response.json["response"]["errors"][0] |
| 715 |
|
| 716 |
# try evil characters |
| 717 |
data["username"] = "hi <script>doing evil</script>" |
| 718 |
response = client.post( |
| 719 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 720 |
) |
| 721 |
assert response.status_code == 400 |
| 722 |
assert ( |
| 723 |
get_message("INVALID_INPUT") |
| 724 |
== response.json["response"]["field_errors"]["username"][0].encode() |
| 725 |
) |
| 726 |
|
| 727 |
# No spaces or punctuation |
| 728 |
data["username"] = "hi there?" |
| 729 |
response = client.post( |
| 730 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 731 |
) |
| 732 |
assert response.status_code == 400 |
| 733 |
assert ( |
| 734 |
get_message("USERNAME_DISALLOWED_CHARACTERS") |
| 735 |
in response.json["response"]["errors"][0].encode() |
| 736 |
) |
| 737 |
|
| 738 |
data["username"] = None |
| 739 |
response = client.post( |
| 740 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 741 |
) |
| 742 |
assert response.status_code == 400 |
| 743 |
assert ( |
| 744 |
get_message("USERNAME_NOT_PROVIDED") |
| 745 |
== response.json["response"]["errors"][0].encode() |
| 746 |
) |
| 747 |
|
| 748 |
|
| 749 |
@pytest.mark.settings( |
| 750 |
username_enable=True, username_required=True, username_allowed_chars=None |
| 751 |
) |
| 752 |
def test_username_anything_goes(app, client, get_message): |
| 753 |
# allow app to allow any characters in a username |
| 754 |
data = dict( |
| 755 |
email="[email protected]", |
| 756 |
username="hi there?", |
| 757 |
password="awesome sunset", |
| 758 |
password_confirm="awesome sunset", |
| 759 |
) |
| 760 |
response = client.post( |
| 761 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 762 |
) |
| 763 |
assert response.status_code == 200 |
| 764 |
|
| 765 |
|
| 766 |
def test_username_not_enabled(app, client, get_message): |
| 767 |
response = client.get("/register") |
| 768 |
assert b"username" not in response.data |
| 769 |
assert not hasattr(RegisterForm, "username") |
| 770 |
|
| 771 |
|
| 772 |
@pytest.mark.filterwarnings( |
| 773 |
"ignore:.*The RegisterForm is deprecated.*:DeprecationWarning" |
| 774 |
) |
| 775 |
def test_legacy_style_login(app, sqlalchemy_datastore, get_message): |
| 776 |
# Show how to setup LoginForm to mimic legacy behavior of |
| 777 |
# allowing any identity in the email field. |
| 778 |
# N.B. for simplicity we don't enable confirmable.... |
| 779 |
from flask_security import ( |
| 780 |
RegisterForm, |
| 781 |
LoginForm, |
| 782 |
Security, |
| 783 |
uia_username_mapper, |
| 784 |
unique_identity_attribute, |
| 785 |
) |
| 786 |
from flask_security.utils import lookup_identity |
| 787 |
from werkzeug.local import LocalProxy |
| 788 |
from wtforms import StringField, ValidationError, validators |
| 789 |
|
| 790 |
def username_validator(form, field): |
| 791 |
# Side-effect - field.data is updated to normalized value. |
| 792 |
# Use proxy to we can declare this prior to initializing Security. |
| 793 |
_security = LocalProxy(lambda: app.extensions["security"]) |
| 794 |
msg, field.data = _security.username_util.validate(field.data) |
| 795 |
if msg: |
| 796 |
raise ValidationError(msg) |
| 797 |
|
| 798 |
class MyRegisterForm(RegisterForm): |
| 799 |
# Note that unique_identity_attribute uses the defined field 'mapper' to |
| 800 |
# normalize. We validate before that to give better error messages and |
| 801 |
# to set the normalized value into the form for saving. |
| 802 |
username = StringField( |
| 803 |
"Username", |
| 804 |
validators=[ |
| 805 |
validators.data_required(), |
| 806 |
username_validator, |
| 807 |
unique_identity_attribute, |
| 808 |
], |
| 809 |
) |
| 810 |
|
| 811 |
class MyLoginForm(LoginForm): |
| 812 |
email = StringField("email", [validators.data_required()]) |
| 813 |
|
| 814 |
def validate(self, **kwargs): |
| 815 |
self.user = lookup_identity(self.email.data) |
| 816 |
self.ifield = self.email |
| 817 |
if not super().validate(**kwargs): |
| 818 |
return False |
| 819 |
return True |
| 820 |
|
| 821 |
app.config["SECURITY_USER_IDENTITY_ATTRIBUTES"] = [ |
| 822 |
{"username": {"mapper": uia_username_mapper}} |
| 823 |
] |
| 824 |
security = Security( |
| 825 |
datastore=sqlalchemy_datastore, |
| 826 |
register_form=MyRegisterForm, |
| 827 |
login_form=MyLoginForm, |
| 828 |
) |
| 829 |
security.init_app(app) |
| 830 |
|
| 831 |
client = app.test_client() |
| 832 |
|
| 833 |
data = dict( |
| 834 |
email="[email protected]", |
| 835 |
username="mary", |
| 836 |
password="awesome sunset", |
| 837 |
password_confirm="awesome sunset", |
| 838 |
) |
| 839 |
response = client.post("/register", data=data, follow_redirects=True) |
| 840 |
assert b"Welcome mary" in response.data |
| 841 |
logout(client) |
| 842 |
|
| 843 |
# log in with username |
| 844 |
response = client.post( |
| 845 |
"/login", |
| 846 |
data=dict(email="mary", password="awesome sunset"), |
| 847 |
follow_redirects=True, |
| 848 |
) |
| 849 |
# verify actually logged in |
| 850 |
response = client.get("/profile", follow_redirects=False) |
| 851 |
assert response.status_code == 200 |
| 852 |
|
| 853 |
|
| 854 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 855 |
@pytest.mark.confirmable() |
| 856 |
@pytest.mark.settings( |
| 857 |
return_generic_responses=True, username_enable=True, password_confirm_required=False |
| 858 |
) |
| 859 |
def test_generic_response(app, client, get_message, outbox): |
| 860 |
# Register should not expose whether email/username is already in system. |
| 861 |
recorded = [] |
| 862 |
|
| 863 |
@user_not_registered.connect_via(app) |
| 864 |
def on_user_not_registered(app, **kwargs): |
| 865 |
recorded.append(kwargs) |
| 866 |
|
| 867 |
# register new user |
| 868 |
data = dict( |
| 869 |
email="[email protected]", |
| 870 |
username="dude", |
| 871 |
password="awesome sunset", |
| 872 |
) |
| 873 |
response = client.post("/register", json=data) |
| 874 |
assert response.status_code == 200 |
| 875 |
assert len(outbox) == 1 |
| 876 |
assert len(recorded) == 0 |
| 877 |
|
| 878 |
# try again - should not get ANY error - but should get an email |
| 879 |
response = client.post("/register", json=data) |
| 880 |
assert response.status_code == 200 |
| 881 |
assert not any( |
| 882 |
e in response.json["response"].keys() for e in ["errors", "field_errors"] |
| 883 |
) |
| 884 |
# this is using the test template |
| 885 |
matcher = re.findall(r"\w+:.*", outbox[1].body, re.IGNORECASE) |
| 886 |
kv = {m.split(":")[0]: m.split(":", 1)[1] for m in matcher} |
| 887 |
assert kv["User"] == "dude" |
| 888 |
assert kv["Email"] == "[email protected]" |
| 889 |
assert kv["RegisterBlueprint"] == "True" |
| 890 |
assert "/confirm" in kv["ConfirmationLink"] |
| 891 |
assert kv["ConfirmationToken"] |
| 892 |
assert not kv["ResetLink"] |
| 893 |
assert not kv["ResetToken"] |
| 894 |
|
| 895 |
# verify that signal sent. |
| 896 |
assert len(recorded) == 1 |
| 897 |
nr = recorded[0] |
| 898 |
assert nr["existing_email"] |
| 899 |
assert nr["user"].email == "[email protected]" |
| 900 |
assert nr["form_data"]["email"] == "[email protected]" |
| 901 |
|
| 902 |
# Forms should get generic response - even though email already registered. |
| 903 |
response = client.post("/register", data=data, follow_redirects=True) |
| 904 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 905 |
assert len(outbox) == 3 |
| 906 |
assert len(recorded) == 2 |
| 907 |
|
| 908 |
# Try same email with different username |
| 909 |
response = client.post( |
| 910 |
"/register", |
| 911 |
data=dict(email="[email protected]", username="dude2", password="awesome sunset"), |
| 912 |
follow_redirects=True, |
| 913 |
) |
| 914 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 915 |
assert len(outbox) == 4 |
| 916 |
assert len(recorded) == 3 |
| 917 |
matcher = re.findall(r"\w+:.*", outbox[3].body, re.IGNORECASE) |
| 918 |
kv = {m.split(":")[0]: m.split(":", 1)[1] for m in matcher} |
| 919 |
assert kv["User"] == "dude" |
| 920 |
|
| 921 |
|
| 922 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 923 |
@pytest.mark.confirmable() |
| 924 |
@pytest.mark.settings( |
| 925 |
return_generic_responses=True, username_enable=True, password_confirm_required=False |
| 926 |
) |
| 927 |
def test_gr_existing_username(app, client, get_message, outbox): |
| 928 |
# Test a new email with an existing username |
| 929 |
# Should still return errors such as illegal password |
| 930 |
recorded = [] |
| 931 |
|
| 932 |
@user_not_registered.connect_via(app) |
| 933 |
def on_user_not_registered(app, **kwargs): |
| 934 |
recorded.append(kwargs) |
| 935 |
|
| 936 |
client.post( |
| 937 |
"/register", |
| 938 |
json=dict(email="[email protected]", username="dude", password="awesome sunset"), |
| 939 |
) |
| 940 |
response = client.post( |
| 941 |
"/register", |
| 942 |
data=dict(email="[email protected]", username="dude", password="awesome sunset"), |
| 943 |
follow_redirects=True, |
| 944 |
) |
| 945 |
assert get_message("CONFIRM_REGISTRATION", email="[email protected]") in response.data |
| 946 |
assert len(outbox) == 2 |
| 947 |
assert len(recorded) == 1 |
| 948 |
gr = outbox[1] |
| 949 |
assert 'You attempted to register with a username "dude" that' in gr.body |
| 950 |
# test that signal sent. |
| 951 |
nr = recorded[0] |
| 952 |
assert not nr["existing_email"] |
| 953 |
assert not nr["user"] |
| 954 |
assert nr["existing_username"] |
| 955 |
assert nr["form_data"]["username"] == "dude" |
| 956 |
|
| 957 |
# should still get detailed errors about e.g. bad password |
| 958 |
data = dict( |
| 959 |
email="[email protected]", |
| 960 |
username="dude", |
| 961 |
password="a", |
| 962 |
) |
| 963 |
response = client.post("/register", json=data) |
| 964 |
assert response.status_code == 400 |
| 965 |
assert response.json["response"]["errors"][0].encode() == get_message( |
| 966 |
"PASSWORD_INVALID_LENGTH", length=8 |
| 967 |
) |
| 968 |
data = dict( |
| 969 |
email="[email protected]", |
| 970 |
username="dd", |
| 971 |
password="awesome sunset", |
| 972 |
) |
| 973 |
response = client.post("/register", json=data) |
| 974 |
assert response.status_code == 400 |
| 975 |
assert response.json["response"]["errors"][0].encode() == get_message( |
| 976 |
"USERNAME_INVALID_LENGTH", min=4, max=32 |
| 977 |
) |
| 978 |
|
| 979 |
|
| 980 |
@pytest.mark.recoverable() |
| 981 |
@pytest.mark.confirmable() |
| 982 |
@pytest.mark.settings( |
| 983 |
return_generic_responses=True, |
| 984 |
username_enable=True, |
| 985 |
send_register_email_welcome_existing_template="welcome_existing", |
| 986 |
password_confirm_required=False, |
| 987 |
) |
| 988 |
def test_gr_extras(app, client, get_message, outbox): |
| 989 |
# If user tries to re-register - response email should contain reset password |
| 990 |
# link and (if applicable) a confirmation link |
| 991 |
data = dict( |
| 992 |
email="[email protected]", |
| 993 |
username="dude", |
| 994 |
password="awesome sunset", |
| 995 |
) |
| 996 |
response = client.post("/register", json=data) |
| 997 |
assert response.status_code == 200 |
| 998 |
assert len(outbox) == 1 |
| 999 |
|
| 1000 |
# now same email - same or different username |
| 1001 |
data = dict( |
| 1002 |
email="[email protected]", |
| 1003 |
username="dude2", |
| 1004 |
password="awesome sunset", |
| 1005 |
) |
| 1006 |
response = client.post("/register", json=data) |
| 1007 |
assert response.status_code == 200 |
| 1008 |
assert len(outbox) == 2 |
| 1009 |
matcher = re.findall(r"\w+:.*", outbox[1].body, re.IGNORECASE) |
| 1010 |
kv = {m.split(":")[0]: m.split(":", 1)[1] for m in matcher} |
| 1011 |
assert kv["User"] == "dude" |
| 1012 |
confirm_link = kv["ConfirmationLink"] |
| 1013 |
response = client.get(confirm_link) |
| 1014 |
assert response.status_code in [302, 303] |
| 1015 |
|
| 1016 |
# now confirmed - should not get confirmation link |
| 1017 |
response = client.post("/register", json=data) |
| 1018 |
assert response.status_code == 200 |
| 1019 |
assert len(outbox) == 3 |
| 1020 |
matcher = re.findall(r"\w+:.*", outbox[2].body, re.IGNORECASE) |
| 1021 |
kv = {m.split(":")[0]: m.split(":", 1)[1] for m in matcher} |
| 1022 |
assert not kv["ConfirmationLink"] |
| 1023 |
assert not kv["ConfirmationToken"] |
| 1024 |
|
| 1025 |
# but should still have reset link - test that |
| 1026 |
reset_link = kv["ResetLink"] |
| 1027 |
response = client.post( |
| 1028 |
reset_link, |
| 1029 |
json=dict(password="awesome password2", password_confirm="awesome password2"), |
| 1030 |
) |
| 1031 |
assert response.status_code == 200 |
| 1032 |
|
| 1033 |
response = client.post( |
| 1034 |
"/login", json=dict(username="dude", password="awesome password2") |
| 1035 |
) |
| 1036 |
assert response.status_code == 200 |
| 1037 |
|
| 1038 |
|
| 1039 |
@pytest.mark.parametrize("app", v2_param, indirect=True) |
| 1040 |
@pytest.mark.recoverable() |
| 1041 |
@pytest.mark.confirmable() |
| 1042 |
@pytest.mark.settings(return_generic_responses=True, password_confirm_required=False) |
| 1043 |
def test_gr_real_html_template(app, client, get_message, outbox): |
| 1044 |
# We have a test .txt template - but this will use the normal/real HTML template |
| 1045 |
data = dict( |
| 1046 |
email="[email protected]", |
| 1047 |
password="awesome sunset", |
| 1048 |
) |
| 1049 |
response = client.post("/register", json=data) |
| 1050 |
assert response.status_code == 200 |
| 1051 |
assert len(outbox) == 1 |
| 1052 |
|
| 1053 |
# now same email - same or different username |
| 1054 |
data = dict( |
| 1055 |
email="[email protected]", |
| 1056 |
password="awesome sunset", |
| 1057 |
) |
| 1058 |
response = client.post("/register", json=data) |
| 1059 |
assert response.status_code == 200 |
| 1060 |
assert len(outbox) == 2 |
| 1061 |
matcher = re.findall( |
| 1062 |
r'href="(\S*)"', |
| 1063 |
outbox[1].alts["html"], |
| 1064 |
re.IGNORECASE, |
| 1065 |
) |
| 1066 |
assert "/reset" in matcher[0] |
| 1067 |
assert "/confirm" in matcher[1] |
| 1068 |
|
| 1069 |
|
| 1070 |
def test_subclass(app, sqlalchemy_datastore): |
| 1071 |
# Test/show how to use multiple inheritance to override individual form fields. |
| 1072 |
from wtforms import PasswordField, ValidationError |
| 1073 |
from wtforms.validators import DataRequired |
| 1074 |
from flask_security.forms import _get_form_field_label |
| 1075 |
|
| 1076 |
def password_validator(form, field): |
| 1077 |
if field.data.startswith("PASS"): |
| 1078 |
raise ValidationError("Really - don't start a password with PASS") |
| 1079 |
|
| 1080 |
class NewPasswordFormMixinEx: |
| 1081 |
password = PasswordField( |
| 1082 |
_get_form_field_label("password"), |
| 1083 |
validators=[ |
| 1084 |
DataRequired(message="PASSWORD_NOT_PROVIDED"), |
| 1085 |
password_validator, |
| 1086 |
], |
| 1087 |
) |
| 1088 |
|
| 1089 |
class MyRegisterForm(NewPasswordFormMixinEx, RegisterFormV2): |
| 1090 |
pass |
| 1091 |
|
| 1092 |
app.config["SECURITY_REGISTER_FORM"] = MyRegisterForm |
| 1093 |
security = Security(datastore=sqlalchemy_datastore) |
| 1094 |
security.init_app(app) |
| 1095 |
|
| 1096 |
client = app.test_client() |
| 1097 |
|
| 1098 |
data = dict( |
| 1099 |
email="[email protected]", |
| 1100 |
password="PASSmattmatt", |
| 1101 |
password_confirm="PASSmattmatt", |
| 1102 |
) |
| 1103 |
response = client.post( |
| 1104 |
"/register", json=data, headers={"Content-Type": "application/json"} |
| 1105 |
) |
| 1106 |
assert response.status_code == 400 |
| 1107 |
assert "Really - don't start" in response.json["response"]["errors"][0] |
| 1108 |
|
| 1109 |
|
| 1110 |
@pytest.mark.confirmable() |
| 1111 |
@pytest.mark.settings(return_generic_responses=True) |
| 1112 |
def test_my_mail_util(app, sqlalchemy_datastore): |
| 1113 |
# Test that with generic_responses - we still can get syntax/validation errors. |
| 1114 |
from flask_security import MailUtil, EmailValidateException |
| 1115 |
|
| 1116 |
class MyMailUtil(MailUtil): |
| 1117 |
def validate(self, email): |
| 1118 |
if email.startswith("mike"): |
| 1119 |
raise EmailValidateException("No mikes allowed") |
| 1120 |
|
| 1121 |
init_app_with_options( |
| 1122 |
app, sqlalchemy_datastore, **{"security_args": {"mail_util_cls": MyMailUtil}} |
| 1123 |
) |
| 1124 |
|
| 1125 |
data = dict( |
| 1126 |
email="[email protected]", |
| 1127 |
password="awesome sunset", |
| 1128 |
) |
| 1129 |
client = app.test_client() |
| 1130 |
response = client.post("/register", data=data) |
| 1131 |
assert b"No mikes allowed" in response.data |
| 1132 |
|
| 1133 |
|
| 1134 |
@pytest.mark.settings( |
| 1135 |
register_form=RegisterFormV2, |
| 1136 |
post_register_view="/post_register", |
| 1137 |
) |
| 1138 |
def test_regv2(app, client, get_message): |
| 1139 |
# default config should require password, password_confirm and not allow username |
| 1140 |
response = client.get("/register") |
| 1141 |
assert b"<h1>Register</h1>" in response.data |
| 1142 |
email = get_form_input(response, "email") |
| 1143 |
assert all(s in email for s in ["required", 'type="email"']) |
| 1144 |
pw = get_form_input(response, "password") |
| 1145 |
assert all(s in pw for s in ["required", 'type="password"']) |
| 1146 |
pwc = get_form_input(response, "password_confirm") |
| 1147 |
assert all(s in pwc for s in ["required", 'type="password"']) |
| 1148 |
|
| 1149 |
assert not get_form_input(response, "username") |
| 1150 |
|
| 1151 |
# check password required |
| 1152 |
data = dict( |
| 1153 |
email="[email protected]", |
| 1154 |
password="", |
| 1155 |
password_confirm="battery staple", |
| 1156 |
next="", |
| 1157 |
) |
| 1158 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1159 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 1160 |
|
| 1161 |
# check confirm required |
| 1162 |
data = dict( |
| 1163 |
email="[email protected]", |
| 1164 |
password="battery staple", |
| 1165 |
password_confirm="", |
| 1166 |
next="", |
| 1167 |
) |
| 1168 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1169 |
assert get_message("PASSWORD_NOT_PROVIDED") in response.data |
| 1170 |
|
| 1171 |
# JSON also required password_confirm in V2 |
| 1172 |
response = client.post("/register", json=data) |
| 1173 |
assert ( |
| 1174 |
get_message("PASSWORD_NOT_PROVIDED") |
| 1175 |
== response.json["response"]["errors"][0].encode() |
| 1176 |
) |
| 1177 |
|
| 1178 |
# check confirm matches |
| 1179 |
data = dict( |
| 1180 |
email="[email protected]", |
| 1181 |
password="battery staple", |
| 1182 |
password_confirm="batery staple", |
| 1183 |
next="", |
| 1184 |
) |
| 1185 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1186 |
assert get_message("RETYPE_PASSWORD_MISMATCH") in response.data |
| 1187 |
|
| 1188 |
data = dict( |
| 1189 |
email="[email protected]", |
| 1190 |
password="battery staple", |
| 1191 |
password_confirm="battery staple", |
| 1192 |
next="", |
| 1193 |
) |
| 1194 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1195 |
assert b"Post Register" in response.data |
| 1196 |
|
| 1197 |
|
| 1198 |
@pytest.mark.settings( |
| 1199 |
register_form=RegisterFormV2, |
| 1200 |
post_register_view="/post_register", |
| 1201 |
password_confirm_required=False, |
| 1202 |
) |
| 1203 |
def test_regv2_no_confirm(app, client, get_message): |
| 1204 |
# Test password confirm not required |
| 1205 |
response = client.get("/register") |
| 1206 |
assert b"<h1>Register</h1>" in response.data |
| 1207 |
email = get_form_input(response, "email") |
| 1208 |
assert all(s in email for s in ["required", 'type="email"']) |
| 1209 |
pw = get_form_input(response, "password") |
| 1210 |
assert all(s in pw for s in ["required", 'type="password"']) |
| 1211 |
|
| 1212 |
assert not get_form_input(response, "password_confirm") |
| 1213 |
assert not get_form_input(response, "username") |
| 1214 |
|
| 1215 |
data = dict( |
| 1216 |
email="[email protected]", |
| 1217 |
password="abc", |
| 1218 |
) |
| 1219 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1220 |
assert get_message("PASSWORD_INVALID_LENGTH", length=8) in response.data |
| 1221 |
|
| 1222 |
data = dict( |
| 1223 |
email="[email protected]", |
| 1224 |
password="battery staple", |
| 1225 |
password_confirm="battery terminal", # Ignored |
| 1226 |
next="/my_next_idea", |
| 1227 |
) |
| 1228 |
response = client.post("/register", data=data, follow_redirects=False) |
| 1229 |
assert response.location == "/my_next_idea" |
| 1230 |
|
| 1231 |
|
| 1232 |
@pytest.mark.settings( |
| 1233 |
use_register_v2=True, |
| 1234 |
post_register_view="/post_register", |
| 1235 |
password_confirm_required=False, |
| 1236 |
username_enable=True, |
| 1237 |
username_required=True, |
| 1238 |
) |
| 1239 |
def test_regv2_no_confirm_username(app, client, get_message): |
| 1240 |
# Test password confirm not required but username is |
| 1241 |
response = client.get("/register") |
| 1242 |
assert b"<h1>Register</h1>" in response.data |
| 1243 |
email = get_form_input(response, "email") |
| 1244 |
assert all(s in email for s in ["required", 'type="email"']) |
| 1245 |
pw = get_form_input(response, "password") |
| 1246 |
assert all(s in pw for s in ["required", 'type="password"']) |
| 1247 |
|
| 1248 |
assert not get_form_input(response, "password_confirm") |
| 1249 |
|
| 1250 |
us = get_form_input(response, "username") |
| 1251 |
assert all(s in us for s in ["required", 'type="text"']) |
| 1252 |
|
| 1253 |
data = dict( |
| 1254 |
email="[email protected]", |
| 1255 |
password="battery staple", |
| 1256 |
) |
| 1257 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1258 |
assert get_message("USERNAME_NOT_PROVIDED") in response.data |
| 1259 |
|
| 1260 |
data = dict( |
| 1261 |
email="[email protected]", |
| 1262 |
password="battery staple", |
| 1263 |
username="dude", |
| 1264 |
) |
| 1265 |
response = client.post("/register", data=data, follow_redirects=True) |
| 1266 |
assert b"Post Register" in response.data |
| 1267 |
|
| 1268 |
|
| 1269 |
@pytest.mark.settings( |
| 1270 |
use_register_v2=True, |
| 1271 |
password_confirm_required=False, |
| 1272 |
) |
| 1273 |
def test_subclass_v2(app, sqlalchemy_datastore): |
| 1274 |
# Test that is create our own RegisterForm the USE_REGISTER_V2 config is ignored |
| 1275 |
class MyRegisterForm(RegisterFormV2): |
| 1276 |
from wtforms.validators import Length |
| 1277 |
|
| 1278 |
myfield = StringField(label="My field", validators=[Length(min=8)]) |
| 1279 |
|
| 1280 |
app.config["SECURITY_REGISTER_FORM"] = MyRegisterForm |
| 1281 |
security = Security(datastore=sqlalchemy_datastore) |
| 1282 |
security.init_app(app) |
| 1283 |
|
| 1284 |
client = app.test_client() |
| 1285 |
data = dict( |
| 1286 |
email="[email protected]", |
| 1287 |
password="battery staple", |
| 1288 |
myfield="short", |
| 1289 |
) |
| 1290 |
response = client.post("/register", json=data) |
| 1291 |
assert ( |
| 1292 |
response.json["response"]["errors"][0] |
| 1293 |
== "Field must be at least 8 characters long." |
| 1294 |
) |