1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
|
"""
test_misc
~~~~~~~~~~~
Lots of tests
:copyright: (c) 2012 by Matt Wright.
:copyright: (c) 2019-2025 by J. Christopher Wagner (jwag).
:license: MIT, see LICENSE for more details.
"""
from datetime import timedelta
import hashlib
from unittest import mock
import re
import os.path
import time
import typing as t
import pytest
from itsdangerous import BadTimeSignature
from wtforms.validators import DataRequired, Length
from tests.test_utils import (
authenticate,
capture_flashes,
capture_reset_password_requests,
check_location,
check_xlation,
get_csrf_token,
init_app_with_options,
json_authenticate,
logout,
populate_data,
reset_fresh,
get_form_input,
)
from tests.test_webauthn import HackWebauthnUtil, reg_2_keys
from flask import Flask, abort, request, Response
from flask_security import Security
from flask_security.forms import (
ChangePasswordForm,
ConfirmRegisterForm,
EmailField,
EmailValidation,
ForgotPasswordForm,
LoginForm,
PasswordField,
PasswordlessLoginForm,
RegisterForm,
RegisterFormV2,
RequiredLocalize,
ResetPasswordForm,
SendConfirmationForm,
StringField,
email_required,
valid_user_email,
)
from flask_security import auth_required, roles_required
from flask_security.utils import (
base_render_json,
encode_string,
json_error_response,
get_request_attr,
hash_data,
send_mail,
uia_email_mapper,
uia_phone_mapper,
verify_hash,
)
from flask_security.core import _get_serializer
if t.TYPE_CHECKING: # pragma: no cover
from flask.testing import FlaskClient
@pytest.mark.recoverable()
def test_my_mail_util(app, sqlalchemy_datastore):
from flask_security import MailUtil
class MyMailUtil(MailUtil):
def send_mail(
self, template, subject, recipient, sender, body, html, user, **kwargs
):
assert template == "reset_instructions"
assert subject == app.config["SECURITY_EMAIL_SUBJECT_PASSWORD_RESET"]
assert recipient == "matt@lp.com"
assert user.email == "matt@lp.com"
assert sender == "no-reply@localhost"
assert isinstance(sender, str)
init_app_with_options(
app, sqlalchemy_datastore, **{"security_args": {"mail_util_cls": MyMailUtil}}
)
client = app.test_client()
client.post("/reset", data=dict(email="matt@lp.com"))
def test_register_blueprint_flag(app, sqlalchemy_datastore):
app.security = Security(
app, datastore=sqlalchemy_datastore, register_blueprint=False
)
client = app.test_client()
response = client.get("/login")
assert response.status_code == 404
@pytest.mark.registerable()
@pytest.mark.recoverable()
@pytest.mark.changeable()
@pytest.mark.settings(
USER_IDENTITY_ATTRIBUTES=[
{"email": {"mapper": uia_email_mapper}},
{"username": {"mapper": lambda x: x}},
]
)
@pytest.mark.filterwarnings(
"ignore:.*The RegisterForm is deprecated.*:DeprecationWarning"
)
def test_basic_custom_forms(app, sqlalchemy_datastore):
class MyLoginForm(LoginForm):
username = StringField("My Login Username Field")
class MyRegisterForm(RegisterForm):
email = EmailField("My Register Email Address Field")
class MyForgotPasswordForm(ForgotPasswordForm):
email = EmailField(
"My Forgot Email Address Field",
validators=[email_required, EmailValidation(verify=True), valid_user_email],
)
class MyResetPasswordForm(ResetPasswordForm):
password = StringField("My Reset Password Field")
class MyChangePasswordForm(ChangePasswordForm):
password = PasswordField("My Change Password Field")
app.security = Security(
app,
datastore=sqlalchemy_datastore,
login_form=MyLoginForm,
register_form=MyRegisterForm,
forgot_password_form=MyForgotPasswordForm,
reset_password_form=MyResetPasswordForm,
change_password_form=MyChangePasswordForm,
)
populate_data(app)
client = app.test_client()
response = client.get("/login")
assert b"My Login Username Field" in response.data
response = client.get("/register")
assert b"My Register Email Address Field" in response.data
response = client.get("/reset")
assert b"My Forgot Email Address Field" in response.data
with capture_reset_password_requests() as requests:
response = client.post("/reset", data=dict(email="matt@lp.com"))
token = requests[0]["token"]
response = client.get("/reset/" + token)
assert b"My Reset Password Field" in response.data
authenticate(client)
response = client.get("/change")
assert b"My Change Password Field" in response.data
@pytest.mark.registerable()
@pytest.mark.confirmable()
@pytest.mark.filterwarnings("ignore:.*The ConfirmRegisterForm.*:DeprecationWarning")
def test_confirmable_custom_form(app, sqlalchemy_datastore):
app.config["SECURITY_REGISTERABLE"] = True
app.config["SECURITY_CONFIRMABLE"] = True
class MyRegisterForm(ConfirmRegisterForm):
email = EmailField("My Register Email Address Field")
class MySendConfirmationForm(SendConfirmationForm):
email = EmailField("My Send Confirmation Email Address Field")
app.security = Security(
app,
datastore=sqlalchemy_datastore,
send_confirmation_form=MySendConfirmationForm,
confirm_register_form=MyRegisterForm,
)
client = app.test_client()
response = client.get("/register")
assert b"My Register Email Address Field" in response.data
response = client.get("/confirm")
assert b"My Send Confirmation Email Address Field" in response.data
def test_passwordless_custom_form(app, sqlalchemy_datastore):
app.config["SECURITY_PASSWORDLESS"] = True
class MyPasswordlessLoginForm(PasswordlessLoginForm):
email = EmailField("My Passwordless Email Address Field")
app.security = Security(
app,
datastore=sqlalchemy_datastore,
passwordless_login_form=MyPasswordlessLoginForm,
)
client = app.test_client()
response = client.get("/login")
assert b"My Passwordless Email Address Field" in response.data
@pytest.mark.parametrize("logout_methods", (["GET", "POST"], ["GET"], ["POST"]))
def test_logout_methods(app, sqlalchemy_datastore, logout_methods):
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_LOGOUT_METHODS": logout_methods}
)
client = app.test_client()
authenticate(client)
response = client.get("/logout", follow_redirects=True)
if "GET" in logout_methods:
assert response.status_code == 200
authenticate(client)
else:
assert response.status_code == 405 # method not allowed
response = client.post("/logout", follow_redirects=True)
if "POST" in logout_methods:
assert response.status_code == 200
else:
assert response.status_code == 405 # method not allowed
def test_logout_methods_none(app, sqlalchemy_datastore):
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_LOGOUT_METHODS": None}
)
client = app.test_client()
authenticate(client)
response = client.get("/logout", follow_redirects=True)
assert response.status_code == 404
response = client.post("/logout", follow_redirects=True)
assert response.status_code == 404
def test_passwordless_and_two_factor_configuration_mismatch(app, sqlalchemy_datastore):
with pytest.raises(ValueError):
init_app_with_options(
app,
sqlalchemy_datastore,
**{"SECURITY_TWO_FACTOR": True, "SECURITY_TWO_FACTOR_ENABLED_METHODS": []},
)
def test_flash_messages_off(app, sqlalchemy_datastore, get_message):
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_FLASH_MESSAGES": False}
)
client = app.test_client()
response = client.get("/profile")
assert get_message("LOGIN") not in response.data
def test_invalid_hash_scheme(app, sqlalchemy_datastore, get_message):
with pytest.raises(ValueError):
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_PASSWORD_HASH": "bogus"}
)
def test_change_hash_type(app, sqlalchemy_datastore):
init_app_with_options(
app,
sqlalchemy_datastore,
**{
"SECURITY_PASSWORD_HASH": "plaintext",
"SECURITY_PASSWORD_SALT": None,
"SECURITY_PASSWORD_SCHEMES": ["argon2", "plaintext"],
},
)
app.config["SECURITY_PASSWORD_HASH"] = "argon2"
app.config["SECURITY_PASSWORD_SALT"] = "salty"
app.security = Security(
app, datastore=sqlalchemy_datastore, register_blueprint=False
)
client = app.test_client()
response = client.post(
"/login", data=dict(email="matt@lp.com", password="password")
)
assert response.status_code == 302
response = client.get("/logout")
response = client.post(
"/login", data=dict(email="matt@lp.com", password="password")
)
assert response.status_code == 302
@pytest.mark.settings(hashing_schemes=["hex_md5"], deprecated_hashing_schemes=[])
@pytest.mark.parametrize("data", ["hellö", b"hello"])
def test_legacy_hash(in_app_context, data):
legacy_hash = hashlib.md5(encode_string(data)).hexdigest()
new_hash = hash_data(data)
assert legacy_hash == new_hash
def test_hash_data(in_app_context):
data = hash_data(b"hello")
assert isinstance(data, str)
data = hash_data("hellö")
assert isinstance(data, str)
def test_verify_hash(in_app_context):
data = hash_data("hellö")
assert verify_hash(data, "hellö") is True
assert verify_hash(data, "hello") is False
legacy_data = hashlib.md5(encode_string("hellö")).hexdigest()
assert verify_hash(legacy_data, "hellö") is True
assert verify_hash(legacy_data, "hello") is False
@pytest.mark.settings(
password_salt="öööööööööööööööööööööööööööööööööö", password_hash="bcrypt"
)
def test_password_unicode_password_salt(client):
response = authenticate(client)
assert response.status_code == 302
response = authenticate(client, follow_redirects=True)
assert b"Welcome matt@lp.com" in response.data
@pytest.mark.registerable()
def test_custom_forms_via_config(app, sqlalchemy_datastore):
class MyLoginForm(LoginForm):
email = StringField("My Login Email Address Field")
class MyRegisterForm(RegisterFormV2):
email = StringField("My Register Email Address Field")
app.config["SECURITY_LOGIN_FORM"] = MyLoginForm
app.config["SECURITY_REGISTER_FORM"] = MyRegisterForm
security = Security(datastore=sqlalchemy_datastore)
security.init_app(app)
client = app.test_client()
response = client.get("/login")
assert b"My Login Email Address Field" in response.data
response = client.get("/register")
assert b"My Register Email Address Field" in response.data
def test_custom_form_instantiator(app, client, get_message):
# Test application form instantiation.
# This is using the form factory pattern.
# Note in this case - Flask-Security doesn't even know the form class name.
from flask_security import FormInfo
class FormInstantiator:
def __init__(self, myservice):
self.myservice = myservice
def instantiator(self, form_name, form_cls, *args, **kwargs):
if form_name == "login_form":
return MyLoginForm(*args, service=self.myservice, **kwargs)
raise ValueError("Unknown Form")
class MyLoginForm(LoginForm):
def __init__(self, *args, service=None, **kwargs):
super().__init__(*args, **kwargs)
self.myservice = service
def validate(self, **kwargs: t.Any) -> bool:
if not super().validate(**kwargs): # pragma: no cover
return False
assert isinstance(self.email.errors, list)
if not self.myservice(self.email.data):
self.email.errors.append("Not happening")
return False
return True
def login_checker(email):
return True if email == "matt@lp.com" else False
fi = FormInstantiator(login_checker)
app.security.set_form_info("login_form", FormInfo(fi.instantiator))
response = authenticate(client, follow_redirects=True)
assert b"Welcome matt@lp.com" in response.data
logout(client)
# Try a normally legit user - but our service denies it
response = authenticate(client, email="joe@lp.com")
assert b"Not happening" in response.data
def test_custom_form_instantiator2(app, client, get_message):
# Test application form instantiation.
# This is using the form clone pattern.
# Note in this case - Flask-Security doesn't even know the form class name.
app.config["WTF_CSRF_ENABLED"] = True
from flask_security import FormInfo
class MyLoginForm(LoginForm):
def __init__(self, *args, service=None, **kwargs):
super().__init__(*args, **kwargs)
self.myservice = service
def instantiator(self, form_name, form_cls, *args, **kwargs):
return MyLoginForm(*args, service=self.myservice, **kwargs)
def validate(self, **kwargs: t.Any) -> bool:
if not super().validate(**kwargs): # pragma: no cover
return False
assert isinstance(self.email.errors, list)
if not self.myservice(self.email.data):
self.email.errors.append("Not happening")
return False
return True
def login_checker(email):
return True if email == "matt@lp.com" else False
with app.test_request_context():
fi = MyLoginForm(formdata=None, service=login_checker)
app.security.set_form_info("login_form", FormInfo(fi.instantiator))
csrf_token = get_csrf_token(client)
response = client.post(
"/login",
data=dict(email="matt@lp.com", password="password", csrf_token=csrf_token),
follow_redirects=True,
)
assert b"Welcome matt@lp.com" in response.data
logout(client)
# Try a normally legit user - but our service denies it
csrf_token = get_csrf_token(client)
response = client.post(
"/login",
data=dict(email="joe@lp.com", password="password", csrf_token=csrf_token),
)
assert b"Not happening" in response.data
def test_custom_form_setting(app, sqlalchemy_datastore):
from flask_security import FormInfo
security = Security(app=app, datastore=sqlalchemy_datastore)
with pytest.raises(ValueError) as vex:
security.set_form_info("mylogin_form", FormInfo())
assert "Unknown form name mylogin_form" == str(vex.value)
with pytest.raises(ValueError) as vex:
security.set_form_info("login_form", FormInfo())
assert "form class must be provided" in str(vex.value)
def test_form_required(app, sqlalchemy_datastore):
class MyLoginForm(LoginForm):
myfield = StringField("My Custom Field", validators=[RequiredLocalize()])
app.config["SECURITY_LOGIN_FORM"] = MyLoginForm
security = Security(datastore=sqlalchemy_datastore)
security.init_app(app)
client = app.test_client()
response = client.post("/login", content_type="application/json")
assert response.status_code == 400
assert b"myfield" in response.data
def test_form_required_local_message(app, sqlalchemy_datastore):
"""Test having a local message (not xlatable and not part of MSG_ config."""
msg = "hi! did you forget me?"
class MyLoginForm(LoginForm):
myfield = StringField(
"My Custom Field", validators=[RequiredLocalize(message=msg)]
)
app.config["SECURITY_LOGIN_FORM"] = MyLoginForm
security = Security(datastore=sqlalchemy_datastore)
security.init_app(app)
client = app.test_client()
response = client.post("/login", content_type="application/json")
assert response.status_code == 400
assert b"myfield" in response.data
assert msg.encode("utf-8") in response.data
# WTforms 2.x incorrectly catches ValueError and sets that as the form error.
# Our config_value routine raises ValueError for missing config items..
assert b"Key" not in response.data
def test_without_babel(app, client):
# Test if babel modules exist but we don't init babel - things still work
app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR"
response = client.get("/login")
assert response.status_code == 200
def test_no_email_sender(app, sqlalchemy_datastore):
"""Verify that if SECURITY_EMAIL_SENDER is default
(which is a local proxy) that send_mail picks up MAIL_DEFAULT_SENDER.
"""
app.config["MAIL_DEFAULT_SENDER"] = "test@testme.com"
class TestUser:
def __init__(self, email):
self.email = email
security = Security()
security.init_app(app, sqlalchemy_datastore)
with app.app_context():
user = TestUser("matt@lp.com")
send_mail("Test Default Sender", user.email, "welcome", user=user)
outbox = app.mail.outbox
assert 1 == len(outbox)
assert "test@testme.com" == outbox[0].from_email
def test_sender_tuple(app, sqlalchemy_datastore):
"""Verify that if sender is a (name, address) tuple,
in the received email sender is properly formatted as "name <address>"
Flask-Mail takes tuples - Flask-Mailman takes them - however the
local-mem backend doesn't format them correctly (SMTP backend doesn't work either?)
"""
app.config["MAIL_DEFAULT_SENDER"] = ("Test User", "test@testme.com")
class TestUser:
def __init__(self, email):
self.email = email
security = Security()
security.init_app(app, sqlalchemy_datastore)
with app.app_context():
user = TestUser("matt@lp.com")
send_mail("Test Tuple Sender", user.email, "welcome", user=user)
outbox = app.mail.outbox
assert 1 == len(outbox)
assert outbox[0].from_email == "Test User <test@testme.com>"
def test_send_mail_context(app, sqlalchemy_datastore):
"""Test full context sent to MailUtil/send_mail"""
app.config["MAIL_DEFAULT_SENDER"] = "test@testme.com"
app.security = Security()
app.security.init_app(app, sqlalchemy_datastore)
class TestUser:
def __init__(self, email):
self.email = email
@app.security.mail_context_processor
def mail():
return {"foo": "bar-mail"}
with app.app_context():
user = TestUser("matt@lp.com")
send_mail("Test Default Sender", user.email, "welcome", user=user)
outbox = app.mail.outbox
assert 1 == len(outbox)
assert "test@testme.com" == outbox[0].from_email
matcher = re.match(
r".*ExtraContext:(\S+).*", outbox[0].body, re.IGNORECASE | re.DOTALL
)
assert matcher.group(1) == "bar-mail"
@pytest.mark.babel()
@pytest.mark.app_settings(babel_default_locale="fr_FR")
def test_xlation(app, client):
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
response = client.get("/login")
assert b'<label for="password">Mot de passe</label>' in response.data
response = authenticate(client)
assert response.status_code == 302
response = authenticate(client, follow_redirects=True)
assert b"Bienvenue matt@lp.com" in response.data
@pytest.mark.babel()
@pytest.mark.app_settings(babel_default_locale="fr_FR")
def test_myxlation(app, sqlalchemy_datastore, pytestconfig):
# Test changing a single MSG and having an additional translation dir
# Flask-BabelEx doesn't support lists of directories..
pytest.importorskip("flask_babel")
i18n_dirname = [
"builtin",
os.path.join(pytestconfig.rootdir, "tests/translations"),
]
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_I18N_DIRNAME": i18n_dirname}
)
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
app.config["SECURITY_MSG_INVALID_PASSWORD"] = ("Password no-worky", "error")
client = app.test_client()
response = client.post("/login", data=dict(email="matt@lp.com", password="forgot"))
assert b"Passe - no-worky" in response.data
@pytest.mark.babel()
@pytest.mark.app_settings(babel_default_locale="fr_FR")
def test_myxlation_complete(app, sqlalchemy_datastore, pytestconfig):
# Test having own translations and not using builtin.
pytest.importorskip("flask_babel")
i18n_dirname = [
os.path.join(pytestconfig.rootdir, "tests/translations"),
]
init_app_with_options(
app, sqlalchemy_datastore, **{"SECURITY_I18N_DIRNAME": i18n_dirname}
)
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
app.config["SECURITY_MSG_INVALID_PASSWORD"] = ("Password no-worky", "error")
client = app.test_client()
response = client.post("/login", data=dict(email="matt@lp.com", password="forgot"))
assert b"Passe - no-worky" in response.data
@pytest.mark.babel()
@pytest.mark.app_settings(babel_default_locale="fr_FR")
def test_form_labels(app, sqlalchemy_datastore):
app.security = Security()
app.security.init_app(app, sqlalchemy_datastore)
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
with app.test_request_context():
rform = RegisterForm()
assert str(rform.password.label.text) == "Mot de passe"
assert str(rform.password_confirm.label.text) == "Confirmer le mot de passe"
assert str(rform.email.label.text) == "Adresse email"
assert str(rform.submit.label.text) == "Inscription"
form = LoginForm()
assert str(form.password.label.text) == "Mot de passe"
assert str(form.remember.label.text) == "Se souvenir de moi"
assert str(form.email.label.text) == "Adresse email"
assert str(form.submit.label.text) == "Connexion"
form = ChangePasswordForm()
assert str(form.password.label.text) == "Mot de passe"
assert str(form.new_password.label.text) == "Nouveau mot de passe"
assert str(form.new_password_confirm.label.text) == "Confirmer le mot de passe"
assert str(form.submit.label.text) == "Changer le mot de passe"
@pytest.mark.babel()
@pytest.mark.app_settings(babel_default_locale="fr_FR")
def test_wtform_xlation(app, sqlalchemy_datastore):
# Make sure wtform xlations work
class MyLoginForm(LoginForm):
fixed_length = StringField(
"FixedLength", validators=[DataRequired(), Length(3, 3)]
)
app.security = Security()
app.security.init_app(app, datastore=sqlalchemy_datastore, login_form=MyLoginForm)
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
client = app.test_client()
response = client.get("/login")
assert b'<label for="password">Mot de passe</label>' in response.data
data = dict(
email="matt@lp.com", password="", remember="y", fixed_length="waytoolong"
)
response = client.post(
"/login", json=data, headers={"Content-Type": "application/json"}
)
assert response.status_code == 400
flerror = response.json["response"]["field_errors"]["fixed_length"][0]
# This is completely dependent on WTforms translations....
assert (
flerror == "Le doit contenir exactement 3 caractères."
or flerror == "Le champ doit contenir exactement 3 caractères."
)
@pytest.mark.changeable()
@pytest.mark.babel()
def test_per_request_xlate(app, client):
from flask import request, session
babel = app.extensions["babel"]
def get_locale():
# For a given session - set lang based on first request.
# Honor explicit url request first
if "lang" not in session:
locale = request.args.get("lang", None)
if not locale:
locale = request.accept_languages.best
if locale:
session["lang"] = locale
return session.get("lang", None).replace("-", "_")
babel.locale_selector_func = get_locale
babel.locale_selector = get_locale # Flask-Babel >= 3.0.0
response = client.get("/login", headers=[("Accept-Language", "fr")])
assert b'<label for="password">Mot de passe</label>' in response.data
# make sure template contents get xlated (not just form).
assert b"<h1>Connexion</h1>" in response.data
data = dict(email="matt@lp.com", password="", remember="y")
response = client.post("/login", data=data, headers=[("Accept-Language", "fr")])
assert response.status_code == 200
# verify errors are xlated
assert b"Merci d'indiquer un mot de passe" in response.data
# log in correctly - this should set locale in session
data = dict(email="matt@lp.com", password="password", remember="y")
response = client.post(
"/login", data=data, headers=[("Accept-Language", "fr")], follow_redirects=True
)
assert response.status_code == 200
# make sure further requests always get correct xlation w/o sending header
response = client.get("/change", follow_redirects=True)
assert response.status_code == 200
assert b"Nouveau mot de passe" in response.data
assert b"<h1>Changer le mot de passe</h1>" in response.data
# try JSON
response = client.post(
"/change",
json=dict(email="matt@lp.com"),
headers={"Content-Type": "application/json"},
)
assert response.status_code == 400
assert response.json["response"]["field_errors"]["new_password"] == [
"Merci d'indiquer un mot de passe"
]
"""
This cant work yet due to zxcvbn usage of gettext
def test_zxcvbn_xlate(app):
class TestUser(object):
def __init__(self, email):
self.email = email
app.config["BABEL_DEFAULT_LOCALE"] = "fr_FR"
app.security = Security()
app.security.init_app(app)
assert check_xlation(app, "fr_FR"), "You must run python setup.py compile_catalog"
with app.test_request_context():
user = TestUser("jwag@notme.com")
pbad, pnorm = app.security.password_util.validate("simple", False, user=user)
print(pbad)
"""
@pytest.mark.settings(password_check_breached="strict")
def test_breached(app, sqlalchemy_datastore):
# partial response from: https://api.pwnedpasswords.com/range/07003
pwned_response = b"AF5A73CD3CBCFDCD12B0B68CB7930F3E888:2\r\n\
AFD8AA47E6FD782ADDC11D89744769F7354:2\r\n\
B04334E179537C975D0B3C72DA2E5B68E44:15\r\n\
B118F58C2373FDF97ACF93BD3339684D1EB:2\r\n\
B1ED5D27429EDF77EFD84F4EA9BDA5013FB:4\r\n\
B25C03CFBE4CBF19E0F4889711C9A488E5D:2\r\n\
B3902FD808DCA504AAAD30F3C14BD3ACE7C:10"
app.security = Security()
app.security.init_app(app, sqlalchemy_datastore)
with app.test_request_context():
with mock.patch("urllib.request.urlopen") as mock_urlopen:
mock_urlopen.return_value.__enter__.return_value.read.return_value = (
pwned_response
)
pbad, pnorm = app.security.password_util.validate("flaskflask", False)
assert len(pbad) == 1
assert app.config["SECURITY_MSG_PASSWORD_BREACHED"][0] in pbad[0]
@pytest.mark.settings(
password_check_breached="strict",
password_breached_count=16,
password_complexity_checker="zxcvbn",
)
def test_breached_cnt(app, sqlalchemy_datastore):
# partial response from: https://api.pwnedpasswords.com/range/07003
pwned_response = b"AF5A73CD3CBCFDCD12B0B68CB7930F3E888:2\r\n\
AFD8AA47E6FD782ADDC11D89744769F7354:2\r\n\
B04334E179537C975D0B3C72DA2E5B68E44:15\r\n\
B118F58C2373FDF97ACF93BD3339684D1EB:2\r\n\
B1ED5D27429EDF77EFD84F4EA9BDA5013FB:4\r\n\
B25C03CFBE4CBF19E0F4889711C9A488E5D:2\r\n\
B3902FD808DCA504AAAD30F3C14BD3ACE7C:10"
app.security = Security()
app.security.init_app(app, sqlalchemy_datastore)
with app.test_request_context():
with mock.patch("urllib.request.urlopen") as mock_urlopen:
mock_urlopen.return_value.__enter__.return_value.read.return_value = (
pwned_response
)
pbad, pnorm = app.security.password_util.validate("flaskflask", True)
# Still weak password, just not pwned enough. Should fail complexity
assert len(pbad) == 1
assert "Repeats like" in pbad[0]
@pytest.mark.skip
@pytest.mark.settings(password_check_breached="strict")
def test_breached_real(app, sqlalchemy_datastore):
"""Actually go out to internet.."""
app.security = Security()
app.security.init_app(app, sqlalchemy_datastore)
with app.test_request_context():
pbad, pnorm = app.security.password_util.validate("flaskflask", True)
assert len(pbad) == 1
assert app.config["SECURITY_MSG_PASSWORD_BREACHED"][0] in pbad[0]
def test_json_error_response_string():
"""Unit test for correct response when a string is given."""
error_msg = "This is an error!"
response = json_error_response(errors=error_msg)
assert "field_errors" not in response
assert response["errors"][0] == error_msg
def test_json_error_response_dict():
"""Unit test for correct response when a dict is given."""
error_msg = {
"e-mail": ["The e-mail address is already in the system."],
"name": ["The name is too long.", "Nice name"],
}
all_msgs = []
[all_msgs.extend(m) for m in error_msg.values()]
response = json_error_response(field_errors=error_msg)
assert "errors" in response
assert "field_errors" in response
assert all(m in response["errors"] for m in all_msgs)
def test_json_error_response_typeerror():
"""Unit test for checking for error raising."""
error_msg = ("tuple",)
with pytest.raises(TypeError):
json_error_response(errors=error_msg)
def test_json_form_errors(app, client):
"""Test wtforms form level errors are correctly sent via json"""
with app.test_request_context():
form = ChangePasswordForm()
form.validate()
form.form_errors.append("I am an error")
response = base_render_json(form)
error_list = response.json["response"]["errors"]
assert len(error_list) == 3
assert "I am an error" in error_list
def test_method_view(app, client):
# auth_required with flask method view
from flask.views import MethodView
from flask import render_template_string
class MyView(MethodView):
decorators = [auth_required("token", "session")]
def get(self):
return render_template_string("Hi view")
myview = MyView.as_view("myview")
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
response = client.get("/myview", follow_redirects=False)
# should require login
assert response.status_code == 302
assert "/login" in response.location
authenticate(client)
response = client.get("/myview")
assert response.status_code == 200
assert b"Hi view" in response.data
def test_phone_util_override(app, sqlalchemy_datastore):
from flask_security import phone_util
import warnings
warnings.simplefilter("error")
class MyPhoneUtil(phone_util.PhoneUtil):
def validate_phone_number(self, input_data):
return "call-me"
def get_canonical_form(self, input_data):
return "very-canonical"
app.security = Security(phone_util_cls=MyPhoneUtil)
app.security.init_app(app, sqlalchemy_datastore)
with app.app_context():
assert uia_phone_mapper("55") == "very-canonical"
# try init_app kwargs
app.config["SECURITY_BLUEPRINT_NAME"] = "security2"
app.security2 = Security()
with pytest.raises(DeprecationWarning):
app.security2.init_app(app, sqlalchemy_datastore, phone_util_cls=MyPhoneUtil)
with app.app_context():
assert uia_phone_mapper("55") == "very-canonical"
def test_authn_freshness(
app: "Flask", client: "FlaskClient", get_message: t.Callable[..., bytes]
) -> None:
"""Test freshness using default reauthn_handler"""
@auth_required(within=30, grace=0)
def myview():
return Response(status=200)
@auth_required(within=0.001, grace=0)
def myspecialview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
app.add_url_rule("/myspecialview", view_func=myspecialview, methods=["GET"])
authenticate(client)
# This should work and not be redirected
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 200
# This should require additional authn and redirect to verify
time.sleep(0.1)
with capture_flashes() as flashes:
response = client.get("/myspecialview", follow_redirects=False)
assert response.status_code == 302
assert response.location == "/verify?next=/myspecialview"
assert flashes[0]["category"] == "error"
assert flashes[0]["message"].encode("utf-8") == get_message(
"REAUTHENTICATION_REQUIRED"
)
# Test json error response
response = client.get("/myspecialview", headers={"accept": "application/json"})
assert response.status_code == 401
assert response.json and response.json["response"]["errors"][0].encode(
"utf-8"
) == get_message("REAUTHENTICATION_REQUIRED")
def test_authn_freshness_handler(app, client, get_message):
"""Test with our own handler"""
@app.security.reauthn_handler
def my_reauthn(within, grace, headers=None):
assert within == timedelta(minutes=30) or timedelta(minutes=0.001)
if app.security._want_json(request):
payload = json_error_response(errors="Oh No")
return app.security._render_json(payload, 401, headers, None)
abort(500)
@auth_required(within=30, grace=0)
def myview():
return Response(status=200)
@auth_required(within=0.001, grace=0)
def myspecialview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
app.add_url_rule("/myspecialview", view_func=myspecialview, methods=["GET"])
authenticate(client)
# This should work and not be redirected
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 200
# This should require additional authn
time.sleep(0.1)
response = client.get("/myspecialview", follow_redirects=False)
assert response.status_code == 500
# Test json error response
response = client.get("/myspecialview", headers={"accept": "application/json"})
assert response.status_code == 401
assert response.json["response"]["errors"][0] == "Oh No"
def test_authn_freshness_callable(app, client, get_message):
@auth_required(within=lambda: timedelta(minutes=30))
def myview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
authenticate(client)
# This should work and not be redirected
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 200
@pytest.mark.settings(url_prefix="/myprefix")
def test_default_authn_bp(app, client):
"""Test default reauthn handler with blueprint prefix"""
@auth_required(within=1, grace=0)
def myview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
authenticate(client, endpoint="/myprefix/login")
# This should require additional authn and redirect to verify
reset_fresh(client, within=timedelta(minutes=1))
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 302
assert response.location == "/myprefix/verify?next=/myview"
def test_authn_freshness_grace(app, client, get_message):
# Test that grace override within.
@auth_required(within=lambda: timedelta(minutes=30), grace=10)
def myview():
return Response(status=200)
@auth_required(within=0.001, grace=lambda: timedelta(minutes=10))
def myspecialview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
app.add_url_rule("/myspecialview", view_func=myspecialview, methods=["GET"])
authenticate(client)
# This should work and not be redirected
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 200
# This should NOT require additional authn
time.sleep(0.1)
response = client.get("/myspecialview", follow_redirects=False)
assert response.status_code == 200
def test_authn_freshness_nc(app, client_nc, get_message):
# By default, auth token carries the fs_paa time.
@auth_required(within=30)
def myview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
response = json_authenticate(client_nc)
token = response.json["response"]["user"]["authentication_token"]
h = {"Authentication-Token": token}
# This should fail - should be a redirect
response = client_nc.get("/myview", headers=h, follow_redirects=False)
assert response.status_code == 200
@pytest.mark.settings(freshness_allow_auth_token=False)
def test_authn_freshness_nc_no(app, client_nc, get_message):
# If don't send session cookie - then freshness always fails
@auth_required(within=30)
def myview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
response = json_authenticate(client_nc)
token = response.json["response"]["user"]["authentication_token"]
h = {"Authentication-Token": token}
# This should fail - should be a redirect
response = client_nc.get("/myview", headers=h, follow_redirects=False)
assert response.status_code == 302
assert response.location == "/verify?next=/myview"
@pytest.mark.two_factor()
@pytest.mark.settings(freshness=timedelta(minutes=-1), multi_factor_recovery_codes=True)
def test_authn_freshness_nc_no_fresh(app, client_nc, get_message):
# test disabling freshness when no session is sent.
response = json_authenticate(client_nc)
token = response.json["response"]["user"]["authentication_token"]
h = {"Authentication-Token": token}
# This should work
response = client_nc.get("/tf-setup", json={}, headers=h)
assert response.status_code == 200
assert not response.json["response"]["tf_required"]
response = client_nc.get("/mf-recovery-codes", json={}, headers=h)
assert response.status_code == 200
assert response.json["response"]["recovery_codes"] == []
def test_verify_fresh(app, client, get_message):
# Hit a fresh-required endpoint and walk through verify
authenticate(client)
reset_fresh(client, app.config["SECURITY_FRESHNESS"])
with capture_flashes() as flashes:
response = client.get("/fresh", follow_redirects=True)
assert b"Reauthenticate" in response.data
assert flashes[0]["category"] == "error"
assert flashes[0]["message"].encode("utf-8") == get_message(
"REAUTHENTICATION_REQUIRED"
)
form_response = response.data.decode("utf-8")
matcher = re.match(
r'.*action="([^"]*)".*', form_response, re.IGNORECASE | re.DOTALL
)
verify_url = matcher.group(1)
reset_fresh(client, app.config["SECURITY_FRESHNESS"])
response = client.get(verify_url)
assert b"Reauthenticate" in response.data
response = client.post(
verify_url, data=dict(password="not my password"), follow_redirects=False
)
assert b"Reauthenticate" in response.data
response = client.post(
verify_url, data=dict(password="password"), follow_redirects=False
)
assert check_location(app, response.location, "/fresh")
# should be fine now
response = client.get("/fresh", follow_redirects=True)
assert b"Fresh Only" in response.data
def test_verify_fresh_json(app, client, get_message):
# Hit a fresh-required endpoint and walk through verify
authenticate(client)
headers = {"Accept": "application/json", "Content-Type": "application/json"}
reset_fresh(client, app.config["SECURITY_FRESHNESS"])
response = client.get("/fresh", headers=headers)
assert response.status_code == 401
assert response.json["response"]["reauth_required"]
response = client.get("/verify")
assert b"Reauthenticate" in response.data
response = client.post(
"/verify", json=dict(password="not my password"), headers=headers
)
assert response.status_code == 400
response = client.post("/verify", json=dict(password="password"), headers=headers)
assert response.status_code == 200
# should be fine now
response = client.get("/fresh", headers=headers)
assert response.status_code == 200
assert response.json["title"] == "Fresh Only"
@pytest.mark.changeable()
def test_verify_pwd_json(app, client, get_message):
# Make sure verify accepts a normalized and original password.
authenticate(client)
headers = {"Accept": "application/json", "Content-Type": "application/json"}
data = dict(
password="password",
new_password="new strong password\N{ROMAN NUMERAL ONE}",
new_password_confirm="new strong password\N{ROMAN NUMERAL ONE}",
)
response = client.post(
"/change",
json=data,
headers={"Content-Type": "application/json"},
)
assert response.status_code == 200
response = client.post(
"/verify",
json=dict(password="new strong password\N{ROMAN NUMERAL ONE}"),
headers=headers,
)
assert response.status_code == 200
response = client.post(
"/verify",
json=dict(password="new strong password\N{LATIN CAPITAL LETTER I}"),
headers=headers,
)
assert response.status_code == 200
@pytest.mark.settings(verify_url="/auth/")
def test_verify_next(app, client, get_message):
authenticate(client)
response = client.post(
"/auth/?next=http://localhost/mynext",
data=dict(password="password"),
follow_redirects=False,
)
assert response.location == "http://localhost/mynext"
@pytest.mark.webauthn(webauthn_util_cls=HackWebauthnUtil)
def test_verify_wan(app, client, get_message):
# test get correct options when requiring a reauthentication and have wan keys
# setup.
headers = {"Accept": "application/json", "Content-Type": "application/json"}
reg_2_keys(client)
reset_fresh(client, app.config["SECURITY_FRESHNESS"])
response = client.get("/fresh", headers=headers)
assert response.status_code == 401
assert response.json["response"]["reauth_required"]
assert response.json["response"]["has_webauthn_verify_credential"]
# the verify form should have the webauthn verify form attached
response = client.get("verify")
assert b'action="/wan-verify"' in response.data
app.config["SECURITY_WAN_ALLOW_AS_VERIFY"] = None
response = client.get("/fresh", headers=headers)
assert response.status_code == 401
assert response.json["response"]["reauth_required"]
assert not response.json["response"]["has_webauthn_verify_credential"]
# the verify form should NOT have the webauthn verify form attached
response = client.get("verify")
assert b'action="/wan-verify"' not in response.data
def test_direct_decorator(app, client, get_message):
"""Test/show calling the auth_required decorator directly"""
headers = {"Accept": "application/json", "Content-Type": "application/json"}
def myview():
return roles_required("author")(domyview)()
def domyview():
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
authenticate(client)
response = client.get("/myview", headers=headers)
assert response.status_code == 403
logout(client)
authenticate(client, email="jill@lp.com")
response = client.get("/myview", headers=headers)
assert response.status_code == 200
def test_authn_via(app, client, get_message):
"""Test that we get correct fs_authn_via set in request"""
@auth_required(within=30, grace=0)
def myview():
assert get_request_attr("fs_authn_via") == "session"
return Response(status=200)
app.add_url_rule("/myview", view_func=myview, methods=["GET"])
authenticate(client)
# This should work and not be redirected
response = client.get("/myview", follow_redirects=False)
assert response.status_code == 200
def test_post_security_with_application_root(app, sqlalchemy_datastore):
init_app_with_options(app, sqlalchemy_datastore, **{"APPLICATION_ROOT": "/root"})
client = app.test_client()
response = client.post(
"/login", data=dict(email="matt@lp.com", password="password")
)
assert response.status_code == 302
assert "/root" in response.location
response = client.get("/logout")
assert response.status_code == 302
assert "/root" in response.location
def test_post_security_with_application_root_and_views(app, sqlalchemy_datastore):
init_app_with_options(
app,
sqlalchemy_datastore,
**{
"APPLICATION_ROOT": "/root",
"SECURITY_POST_LOGIN_VIEW": "/post_login",
"SECURITY_POST_LOGOUT_VIEW": "/post_logout",
},
)
client = app.test_client()
response = client.post(
"/login", data=dict(email="matt@lp.com", password="password")
)
assert response.status_code == 302
assert "/post_login" in response.location
response = client.get("/logout")
assert response.status_code == 302
assert "/post_logout" in response.location
def test_open_redirect(app, client, get_message):
"""
Test various possible URLs that urlsplit() shows as relative but
many browsers will interpret as absolute - and thus have a
open-redirect vulnerability.
"""
test_urls = [
("\\\\\\github.com", "%5C%5C%5Cgithub.com"),
(" //github.com", "%20//github.com"),
(r"/\github.com", "/%5Cgithub.com"),
(r"\/github.com", "%5C/github.com"),
("//github.com", ""),
("\t//github.com", "%09//github.com"),
]
for i, o in test_urls:
data = dict(email="matt@lp.com", password="password", next=i)
response = client.post("/login", data=data, follow_redirects=False)
if response.status_code == 302:
# this means it passed form validation but should have been quoted
assert check_location(app, response.location, o)
elif response.status_code == 200:
# should have failed form validation
assert get_message("INVALID_REDIRECT") in response.data
else:
raise AssertionError("Bad response code")
logout(client)
def test_kwargs():
import warnings
warnings.simplefilter("error")
with pytest.raises(DeprecationWarning):
Security(myownkwarg="hi")
def test_nodatastore(app):
with pytest.raises(ValueError):
s = Security(app)
s.init_app(app)
@pytest.mark.filterwarnings("ignore:.*Replacing login_manager.*:DeprecationWarning")
def test_reuse_security_object(sqlalchemy_datastore):
# See: https://github.com/pallets-eco/flask-security/issues/518
# Let folks re-use the Security object (mostly for testing).
security = Security(datastore=sqlalchemy_datastore)
app = Flask(__name__)
app.response_class = Response
app.debug = True
app.config["SECRET_KEY"] = "secret"
app.config["TESTING"] = True
security.init_app(app)
assert hasattr(app, "login_manager")
app = Flask(__name__)
app.response_class = Response
app.debug = True
app.config["SECRET_KEY"] = "secret"
app.config["TESTING"] = True
security.init_app(app)
assert hasattr(app, "login_manager")
@pytest.mark.settings(static_folder_url="/mystatic/fs")
def test_static_url(app, sqlalchemy_datastore):
from flask_security import url_for_security
from flask import url_for
init_app_with_options(app, sqlalchemy_datastore)
with app.test_request_context("http://localhost:5001/login"):
static_url = url_for_security("static", filename="js/webauthn.js")
assert static_url == "/mystatic/fs/js/webauthn.js"
static_url = url_for(".static", filename="js/webauthn.js")
assert static_url == "/mystatic/fs/js/webauthn.js"
def test_multi_app(app, sqlalchemy_datastore):
# test that 2 different app with 2 different FS
# with USERNAME_ENABLE which dynamically changes the class definition
app = Flask(__name__)
app.response_class = Response
app.debug = True
app.config["SECRET_KEY"] = "secret"
app.config["TESTING"] = True
app.config["SECURITY_USERNAME_ENABLE"] = True
security = Security(datastore=sqlalchemy_datastore)
security.init_app(app)
assert hasattr(security.forms["register_form"].cls, "username")
assert "username" in security.user_identity_attributes[1].keys()
app = Flask(__name__)
app.response_class = Response
app.debug = True
app.config["SECRET_KEY"] = "secret"
app.config["TESTING"] = True
app.config["SECURITY_USERNAME_ENABLE"] = True
security2 = Security(datastore=sqlalchemy_datastore)
security2.init_app(app)
assert hasattr(security2.forms["register_form"].cls, "username")
assert "username" in security2.user_identity_attributes[1].keys()
@pytest.mark.registerable()
def test_login_email_whatever(app, client, get_message):
# login, by default, shouldn't verify email address is deliverable..
# register etc can/should do that.
app.config["SECURITY_EMAIL_VALIDATOR_ARGS"] = {"check_deliverability": True}
# register should fail since non-deliverable TLD
data = dict(
email="dude@me.mytld",
password="awesome sunset",
)
response = client.post("/register", json=data)
assert response.status_code == 400
assert response.json["response"]["errors"][0].encode("utf-8") == get_message(
"INVALID_EMAIL_ADDRESS"
)
# login should work since we are just checking for identity
response = client.post(
"/login", data=dict(email="matt@lp.com", password="password")
)
assert response.status_code == 302
assert "/" in response.location
@pytest.mark.skip
def test_sqlalchemy_session_conn(request, app, tmpdir, realdburl):
# test harness for checking connection mgmt by logging all connections
from .conftest import sqlalchemy_session_setup
ds = sqlalchemy_session_setup(
request, app, tmpdir, realdburl, echo_pool="debug", echo="debug"
)
init_app_with_options(app, ds)
client = app.test_client()
client.post("/login", data=dict(email="matt@lp.com", password="password"))
client.post("/login", json=dict(noemail="matt@lp.com", password="password"))
time.sleep(5)
def test_login_required(app, client, get_message):
# Test that @login_required calls our default_unauthn_handler
from flask_login import login_required
@app.route("/loginreq")
@login_required
def login_req():
pass
response = client.get("/loginreq", follow_redirects=False)
assert response.location == "/login?next=/loginreq"
response = client.get("/loginreq", follow_redirects=True)
assert get_message("UNAUTHENTICATED") in response.data
def test_simplify_url():
from flask_security.utils import simplify_url
s = simplify_url("https://localhost/profile", "https://localhost/login")
assert s == "/login"
s = simplify_url("https:/myhost/profile", "https://localhost/login")
assert s == "https://localhost/login"
@pytest.mark.parametrize(
"verify_secret_key, verify_fallbacks, should_pass",
[
("new_secret", [], False), # Should fail - only new key
("new_secret", ["old_secret"], True), # Should pass - has fallback
("old_secret", [], True), # Should pass - using original key
("wrong_secret", ["also_wrong"], False), # Should fail - no valid keys
],
ids=["new-key-only", "with-fallback", "original-key", "wrong-keys"],
)
def test_secret_key_fallbacks(app, verify_secret_key, verify_fallbacks, should_pass):
# Create token with original key
app.config["SECRET_KEY"] = "old_secret"
serializer = _get_serializer(app, "CONFIRM")
token = serializer.dumps({"data": "test"})
# Attempt verification with different key configurations
app.config["SECRET_KEY"] = verify_secret_key
app.config["SECRET_KEY_FALLBACKS"] = verify_fallbacks
serializer = _get_serializer(app, "CONFIRM")
if should_pass:
data = serializer.loads(token)
assert data["data"] == "test"
else:
with pytest.raises(BadTimeSignature):
serializer.loads(token)
@pytest.mark.settings(username_enable=True)
def test_custom_login_form(app, sqlalchemy_datastore, get_message):
# Test custom login form that deletes email and uses username only
# Also test that if app leave 'email' in as a user identity attribute we
# will ignore it
class MyLoginForm(LoginForm):
email = None
app.security = Security(
app,
datastore=sqlalchemy_datastore,
login_form=MyLoginForm,
)
populate_data(app)
client = app.test_client()
response = client.get("/login", follow_redirects=False)
assert not get_form_input(response, "email")
response = client.post(
"/login", json=dict(email="jill@lp.com", password="password")
)
assert response.status_code == 400
assert (
get_message("USER_DOES_NOT_EXIST")
== response.json["response"]["field_errors"][""][0].encode()
)
response = client.post("/login", json=dict(username="jill", password="password"))
assert response.status_code == 200
@pytest.mark.settings(password_required=False)
def test_password_required_setting(app, sqlalchemy_datastore):
with pytest.raises(ValueError) as vex:
Security(app=app, datastore=sqlalchemy_datastore)
assert "SECURITY_PASSWORD_REQUIRED can only be" in str(vex.value)
|