From 69e1c3fd7b71a81d7beb7414242f2b8b2c9cd983 Mon Sep 17 00:00:00 2001 From: Jonathan Zhang Date: Thu, 14 Apr 2022 18:09:19 -0700 Subject: [PATCH 1/5] fix: handle expired passwords --- ocflib/account/utils.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ocflib/account/utils.py b/ocflib/account/utils.py index ca120211..946d045b 100644 --- a/ocflib/account/utils.py +++ b/ocflib/account/utils.py @@ -24,7 +24,19 @@ def password_matches(username, password): child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) child.sendline(password) - child.expect(pexpect.EOF) + try: + child.expect(pexpect.EOF) + catch pexpect.exceptions.TIMEOUT: + child = pexpect.spawn(cmd, timeout=10) + child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) + child.sendline(password) + child.expect(( + "expired\r\nYour password will expire at ... ... .. .." + ":..:.. ....\r\n\r\nChanging password\r\nNew password:" + )) + child.close() + raise AttributeError("User's password has expired") + child.close() return child.exitstatus == 0 From 17d75ac956fd0ce7bfc7db166b8c53edda8e8c8f Mon Sep 17 00:00:00 2001 From: Jonathan Zhang Date: Thu, 14 Apr 2022 18:12:17 -0700 Subject: [PATCH 2/5] fix: what was I doing --- ocflib/account/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ocflib/account/utils.py b/ocflib/account/utils.py index 946d045b..02f220ae 100644 --- a/ocflib/account/utils.py +++ b/ocflib/account/utils.py @@ -26,7 +26,7 @@ def password_matches(username, password): try: child.expect(pexpect.EOF) - catch pexpect.exceptions.TIMEOUT: + except pexpect.exceptions.TIMEOUT: child = pexpect.spawn(cmd, timeout=10) child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) child.sendline(password) From 302cd6d91224f0bf5318df3c25f84a3b70d65e84 Mon Sep 17 00:00:00 2001 From: Jonathan Zhang Date: Thu, 14 Apr 2022 20:30:41 -0700 Subject: [PATCH 3/5] fix: syntax --- ocflib/account/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ocflib/account/utils.py b/ocflib/account/utils.py index 02f220ae..4874baa4 100644 --- a/ocflib/account/utils.py +++ b/ocflib/account/utils.py @@ -31,9 +31,9 @@ def password_matches(username, password): child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) child.sendline(password) child.expect(( - "expired\r\nYour password will expire at ... ... .. .." - ":..:.. ....\r\n\r\nChanging password\r\nNew password:" - )) + 'expired\r\nYour password will expire at ... ... .. ..' + ':..:.. ....\r\n\r\nChanging password\r\nNew password:' + )) child.close() raise AttributeError("User's password has expired") From 8937dd49fc0f0d2e9843b91112d6214d038f1028 Mon Sep 17 00:00:00 2001 From: Jonathan Zhang Date: Thu, 14 Apr 2022 23:05:39 -0700 Subject: [PATCH 4/5] fix: proper use of expect --- ocflib/account/utils.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/ocflib/account/utils.py b/ocflib/account/utils.py index 4874baa4..b1ff6f15 100644 --- a/ocflib/account/utils.py +++ b/ocflib/account/utils.py @@ -24,18 +24,11 @@ def password_matches(username, password): child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) child.sendline(password) - try: - child.expect(pexpect.EOF) - except pexpect.exceptions.TIMEOUT: - child = pexpect.spawn(cmd, timeout=10) - child.expect("{}@OCF.BERKELEY.EDU's Password:".format(username)) - child.sendline(password) - child.expect(( - 'expired\r\nYour password will expire at ... ... .. ..' - ':..:.. ....\r\n\r\nChanging password\r\nNew password:' - )) + result = child.expect([pexpect.EOF, 'expired']) + + if (result == 1): child.close() - raise AttributeError("User's password has expired") + raise ValueError("User's password has expired") child.close() From 807e31d5d16508573f2617597ce01a4acc125cd9 Mon Sep 17 00:00:00 2001 From: Jonathan Zhang Date: Thu, 14 Apr 2022 23:15:02 -0700 Subject: [PATCH 5/5] fix: update tests --- tests/account/utils_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/account/utils_test.py b/tests/account/utils_test.py index 5d52a2d7..3ef2329e 100644 --- a/tests/account/utils_test.py +++ b/tests/account/utils_test.py @@ -53,7 +53,7 @@ def test_calls_pexpect_correctly(self, spawn, __): assert child.expect.mock_calls == [ mock.call.first('ckuehl@OCF.BERKELEY.EDU\'s Password:'), - mock.call.second(pexpect.EOF), + mock.call.second([pexpect.EOF, 'expired']), ] child.sendline.assert_called_with('hunter2') assert child.close.called