Skip to content

Commit d67031e

Browse files
authored
Simplify a few unit tests using the USER_INJECT model. (#1173)
This showed that we were flashing 'CODE HAS_BEEN_SENT' even on errors. Fixed that (unified signin).
1 parent 41ea63c commit d67031e

File tree

4 files changed

+23
-83
lines changed

4 files changed

+23
-83
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Released TBD
1010

1111
Features & Improvements
1212
+++++++++++++++++++++++
13-
- (:pr:`xx`) Add API :py:meth:`.UserMixin.check_tf_required` to allow applications to control which users
13+
- (:pr:`1170`) Add API :py:meth:`.UserMixin.check_tf_required` to allow applications to control which users
1414
require two-factor authentication.
1515

1616
Fixes

flask_security/unified_signin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,9 @@ def us_signin_send_code() -> ResponseValue:
451451
form, include_user=False, error_status_code=500 if msg else 200
452452
)
453453

454-
# Make sure same response as non-setup method below
455-
do_flash(*generic_message("CODE_HAS_BEEN_SENT", "GENERIC_US_SIGNIN"))
454+
if not msg:
455+
# Make sure same response as non-setup method below
456+
do_flash(*generic_message("CODE_HAS_BEEN_SENT", "GENERIC_US_SIGNIN"))
456457

457458
return _security.render_template(
458459
cv("US_SIGNIN_TEMPLATE"),

tests/test_two_factor.py

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,47 +1332,20 @@ def test_bad_sender(app, client, get_message):
13321332
)
13331333

13341334

1335-
def test_replace_send_code(app, get_message):
1336-
pytest.importorskip("sqlalchemy")
1337-
pytest.importorskip("flask_sqlalchemy")
1335+
cresponse = [None, "That didnt work out as we planned", "Failed Again"]
13381336

1339-
# replace tf_send_code - and have it return an error to check that.
1340-
from flask_sqlalchemy import SQLAlchemy
1341-
from flask_security.models import fsqla_v2 as fsqla
1342-
from flask_security import Security, hash_password
13431337

1344-
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
1345-
db = SQLAlchemy(app)
1338+
def _tf_send_code(self, method, totp_secret, phone_number):
1339+
return self.code_response.pop(0)
13461340

1347-
fsqla.FsModels.set_db_info(db)
13481341

1349-
class Role(db.Model, fsqla.FsRoleMixin):
1350-
pass
1351-
1352-
class User(db.Model, fsqla.FsUserMixin):
1353-
rv = [None, "That didnt work out as we planned", "Failed Again"]
1354-
1355-
def tf_send_security_token(self, method, **kwargs):
1356-
return User.rv.pop(0)
1357-
1358-
with app.app_context():
1359-
db.create_all()
1360-
1361-
ds = SQLAlchemyUserDatastore(db, User, Role)
1362-
app.security = Security(app, datastore=ds)
1363-
1364-
with app.app_context():
1365-
client = app.test_client()
1366-
1367-
ds.create_user(
1368-
email="trp@lp.com",
1369-
password=hash_password("password"),
1370-
tf_primary_method="sms",
1371-
tf_totp_secret=app.security._totp_factory.generate_totp_secret(),
1372-
)
1373-
ds.commit()
1374-
1375-
data = dict(email="trp@lp.com", password="password")
1342+
@pytest.mark.app_settings(
1343+
TESTING_USER_INJECT=dict(
1344+
tf_send_security_token=_tf_send_code, code_response=cresponse
1345+
)
1346+
)
1347+
def test_replace_send_code(app, client, get_message):
1348+
data = dict(email="gal@lp.com", password="password")
13761349
response = client.post("/login", data=data, follow_redirects=True)
13771350
assert b"Please enter your authentication code" in response.data
13781351
rescue_data = dict(help_setup="email")
@@ -1384,8 +1357,6 @@ def tf_send_security_token(self, method, **kwargs):
13841357
response = client.post("/tf-rescue", json=rescue_data, headers=headers)
13851358
assert response.status_code == 500
13861359
assert response.json["response"]["field_errors"]["help_setup"][0] == "Failed Again"
1387-
with app.app_context():
1388-
db.engine.dispose()
13891360

13901361

13911362
def test_propagate_next(app, client):

tests/test_unified_signin.py

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
from flask_security import (
4343
SmsSenderFactory,
44-
SQLAlchemyUserDatastore,
4544
UserMixin,
4645
uia_email_mapper,
4746
uia_phone_mapper,
@@ -1680,49 +1679,18 @@ def test_bad_sender(app, client, get_message):
16801679
) == get_message("FAILED_TO_SEND_CODE")
16811680

16821681

1683-
@pytest.mark.registerable()
1684-
def test_replace_send_code(app, get_message):
1685-
pytest.importorskip("sqlalchemy")
1686-
pytest.importorskip("flask_sqlalchemy")
1687-
1688-
from flask_sqlalchemy import SQLAlchemy
1689-
from flask_security.models import fsqla_v2 as fsqla
1690-
from flask_security import Security, us_send_security_token
1691-
1692-
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
1693-
db = SQLAlchemy(app)
1694-
1695-
fsqla.FsModels.set_db_info(db)
1696-
1697-
class Role(db.Model, fsqla.FsRoleMixin):
1698-
pass
1699-
1700-
class User(db.Model, fsqla.FsUserMixin):
1701-
def us_send_security_token(self, method, **kwargs):
1702-
assert method == "sms"
1703-
us_send_security_token(self, method, **kwargs)
1704-
1705-
with app.app_context():
1706-
db.create_all()
1707-
1708-
ds = SQLAlchemyUserDatastore(db, User, Role)
1709-
app.security = Security(app, datastore=ds)
1682+
def _send_code(self, method, **kwargs):
1683+
assert method == "sms"
1684+
return "NO SMS AVAILABLE"
17101685

1711-
client = app.test_client()
17121686

1713-
# since we don't use client fixture - have to add user
1714-
data = dict(email="trp@lp.com", password="password", password_confirm="password")
1715-
response = client.post("/register", data=data, follow_redirects=True)
1716-
assert b"Welcome trp@lp.com" in response.data
1717-
logout(client)
1718-
1719-
set_phone(app, email="trp@lp.com")
1720-
data = dict(identity="trp@lp.com", chosen_method="sms")
1687+
@pytest.mark.app_settings(TESTING_USER_INJECT=dict(us_send_security_token=_send_code))
1688+
@pytest.mark.registerable()
1689+
def test_replace_send_code(app, client, get_message):
1690+
set_phone(app, "gal@lp.com")
1691+
data = dict(identity="gal@lp.com", chosen_method="sms")
17211692
response = client.post("/us-signin/send-code", data=data, follow_redirects=True)
1722-
assert b"Code has been sent" in response.data
1723-
1724-
with app.app_context():
1725-
db.engine.dispose() # sqlite wants everything cleaned up
1693+
assert b"NO SMS AVAILABLE" in response.data
17261694

17271695

17281696
@pytest.mark.settings(us_enabled_methods=["password"])

0 commit comments

Comments
 (0)