From 767b33136def5b42d049610e1e29026efd5d321e Mon Sep 17 00:00:00 2001 From: hammem Date: Tue, 5 Mar 2024 17:16:31 -0500 Subject: [PATCH 1/2] Improve input handling on interactive_login() and login() --- monarchmoney/monarchmoney.py | 13 ++++++++++++- tests/test_monarchmoney.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/monarchmoney/monarchmoney.py b/monarchmoney/monarchmoney.py index 2a78472..53866ec 100644 --- a/monarchmoney/monarchmoney.py +++ b/monarchmoney/monarchmoney.py @@ -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: @@ -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." ) diff --git a/tests/test_monarchmoney.py b/tests/test_monarchmoney.py index c0bed65..8f5546a 100644 --- a/tests/test_monarchmoney.py +++ b/tests/test_monarchmoney.py @@ -6,6 +6,7 @@ import json from gql import Client from monarchmoney import MonarchMoney +from monarchmoney.monarchmoney import LoginFailedException class TestMonarchMoney(unittest.IsolatedAsyncioTestCase): @@ -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}" From caed232d744ae929bc305c3706bf02fa1e791f74 Mon Sep 17 00:00:00 2001 From: hammem Date: Sun, 14 Apr 2024 20:08:17 +0000 Subject: [PATCH 2/2] Fix broken unit test --- tests/test_monarchmoney.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_monarchmoney.py b/tests/test_monarchmoney.py index ca675d3..d746f47 100644 --- a/tests/test_monarchmoney.py +++ b/tests/test_monarchmoney.py @@ -212,7 +212,8 @@ async def test_login(self): ) @patch("builtins.input", return_value="") - async def test_interactive_login(self, _): + @patch("getpass.getpass", return_value="") + async def test_interactive_login(self, _input_mock, _getpass_mock): """ Test the interactive_login method with empty values for email and password. """