Skip to content

Commit

Permalink
completed unit tests for login()
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Nov 18, 2024
1 parent 8c95082 commit 2cdbdd6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/controller/auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def login():

if user is None:
session.close()
flash("That email does not exist. Please try again.", "danger")
flash("That username does not exist. Please try again.", "danger")
elif not user_repo.check_password(password, user.password_hash):
session.close()
flash("Invalid password. Please try again.", "danger")
Expand Down
44 changes: 43 additions & 1 deletion tests/test_controller/test_auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,46 @@ def test_login_valid_credentails(client, app):
# Ensure only paths are compared. Ignore protocl and hostname differences.
response_path = urlparse(response.location).path
expected_path = url_for("tasks.view_tasks", user_id=1, _external=False)
assert response_path == expected_path
assert response_path == expected_path


def test_login_invalid_username(client, app):
with app.app_context():
response = client.post(
url_for("auth.login"),
data={"username": "Jazz", "password": "CrushinOnHillary"},
follow_redirects=True,
)

# Assert the login page is returned
assert response.status_code == 200
assert b"That username does not exist. Please try again." in response.data
assert b"LOGIN" in response.data


def test_login_invalid_password(client, app):
with app.app_context():
response = client.post(
url_for("auth.login"),
data={"username": "Will_Smith", "password": "wrong_password"},
follow_redirects=True,
)

print(response.data.decode())
# Assert the login page is returned
assert response.status_code == 200
assert b"Invalid password. Please try again." in response.data
assert b"LOGIN" in response.data


def test_login_form_validation(client, app):
with app.app_context():
response = client.post(
url_for("auth.login"),
data={"username": "", "password": ""},
follow_redirects=True,
)

# Assert that validation errors are displayed
assert response.status_code == 200
assert b"Because you're always on top of the mountain (or at least your to-do list)" in response.data

0 comments on commit 2cdbdd6

Please sign in to comment.