Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/add specific failed comment #1994

Merged
merged 7 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class Config(object):
ONE_OFF_MESSAGE_FILENAME = "Report"
MAX_VERIFY_CODE_COUNT = 10
JOBS_MAX_SCHEDULE_HOURS_AHEAD = 96
FAILED_LOGIN_LIMIT = os.getenv("FAILED_LOGIN_LIMIT", 10)

# be careful increasing this size without being sure that we won't see slowness in pysftp
MAX_LETTER_PDF_ZIP_FILESIZE = 40 * 1024 * 1024 # 40mb
Expand Down Expand Up @@ -689,6 +690,7 @@ class Test(Development):
FF_EMAIL_DAILY_LIMIT = False
CRM_GITHUB_PERSONAL_ACCESS_TOKEN = "test-token"
CRM_ORG_LIST_URL = "https://test-url.com"
FAILED_LOGIN_LIMIT = 0


class Production(Config):
Expand Down
9 changes: 8 additions & 1 deletion app/user/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ def verify_user_password(user_id):
return jsonify({}), 204
else:
increment_failed_login_count(user_to_verify)
message = "Incorrect password"
# We want to get the user again to check the failed login count
user_to_verify = get_user_by_id(user_id=user_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bother going back to the DB for the user again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cause we increment_failed_login_count in 232

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but I think the object is updated in place so I don't think we need to bother with the extra db call!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me look at it again, thanks for pointing it out

if user_to_verify.failed_login_count >= current_app.config["FAILED_LOGIN_LIMIT"]:
message = "Failed login: Incorrect password for user_id {user_id} failed_login {failed_login_count} times".format(
user_id=user_id, failed_login_count=user_to_verify.failed_login_count
)
else:
message = "Incorrect password for user_id {user_id}".format(user_id=user_id)
errors = {"password": [message]}
raise InvalidRequest(errors, status_code=400)

Expand Down
31 changes: 31 additions & 0 deletions tests/app/user/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,3 +1544,34 @@ def test_update_user_blocked(admin_request, sample_user, account_change_template

assert resp["data"]["id"] == str(sample_user.id)
assert resp["data"]["blocked"]


class TestFailedLogin:
def test_update_user_password_saves_correctly(self, client, sample_service, mocker):
sample_user = sample_service.users[0]
new_password = "tQETOgIO8yzDMyCsDjLZIEVZHAvkFArYfmSI1KTsJnlnPohI2tfIa8kfng7bxCm"
data = {"_password": new_password}
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
resp = client.post(
url_for("user.update_password", user_id=sample_user.id),
data=json.dumps(data),
headers=headers,
)
assert resp.status_code == 200

json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp["data"]["password_changed_at"] is not None
data = {"password": new_password}
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
# We force a the password to fail on login
mocker.patch("app.models.User.check_password", return_value=False)

resp = client.post(
url_for("user.verify_user_password", user_id=str(sample_user.id)),
data=json.dumps(data),
headers=headers,
)
assert resp.status_code == 400
assert "Incorrect password for user_id" in resp.json["message"]["password"][0]
Loading
Loading