Skip to content

Commit

Permalink
Improve input handling on interactive_login() and login()
Browse files Browse the repository at this point in the history
  • Loading branch information
hammem committed Mar 5, 2024
1 parent 9f2607f commit 767b331
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
13 changes: 12 additions & 1 deletion monarchmoney/monarchmoney.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ async def interactive_login(
"""Performs an interactive login for iPython and similar environments."""
email = input("Email: ")
passwd = input("Password: ")

empty_values = [None, ""]
if (
(email in empty_values)
and (passwd in empty_values)
and (not use_saved_session)
):
raise LoginFailedException(
"Email and password are required to login when not using a saved session."
)

try:
await self.login(email, passwd, use_saved_session, save_session)
except RequireMFAException:
Expand All @@ -112,7 +123,7 @@ async def login(
self.load_session(self._session_file)
return

if email is None or password is None:
if (email is None) or (password is None) or (email == "") or (password == ""):
raise LoginFailedException(
"Email and password are required to login when not using a saved session."
)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_monarchmoney.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from gql import Client
from monarchmoney import MonarchMoney
from monarchmoney.monarchmoney import LoginFailedException


class TestMonarchMoney(unittest.IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -173,6 +174,25 @@ async def test_get_account_holdings(self, mock_execute_async):
"Expected third holding name to be 'U S Dollar'",
)

async def test_login(self):
"""
Test the login method with empty values for email and password.
"""
with self.assertRaises(LoginFailedException):
await self.monarch_money.login(use_saved_session=False)
with self.assertRaises(LoginFailedException):
await self.monarch_money.login(
email="", password="", use_saved_session=False
)

@patch("builtins.input", return_value="")
async def test_interactive_login(self, _):
"""
Test the interactive_login method with empty values for email and password.
"""
with self.assertRaises(LoginFailedException):
await self.monarch_money.interactive_login(use_saved_session=False)

@classmethod
def loadTestData(cls, filename) -> dict:
filename = f"{os.path.dirname(os.path.realpath(__file__))}/{filename}"
Expand Down

0 comments on commit 767b331

Please sign in to comment.