return True
def __try_login(self, username, password):
+ '''
+ Attempts to log in to bindctl by sending a POST with
+ the given username and password.
+ On success of the POST (mind, not the login, only the network
+ operation), returns a tuple (response, data).
+ On failure, raises a FailToLogin exception, and prints some
+ information on the failure.
+ '''
param = {'username': username, 'password' : password}
try:
- return self.send_POST('/login', param)
+ response = self.send_POST('/login', param)
+ data = response.read().decode()
+ return (response, data)
except socket.error as err:
print("Socket error while sending login information: ", err)
if err.errno == errno.ECONNRESET:
# Look at existing username/password combinations and try to log in
users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
for row in users:
- response = self.__try_login(row[0], row[1])
- data = response.read().decode()
+ response, data = self.__try_login(row[0], row[1])
+
if response.status == http.client.OK:
# Is interactive?
if sys.stdin.isatty():
username = input("Username: ")
passwd = getpass.getpass()
- response = self.__try_login(username, passwd)
- data = response.read().decode()
+ response, data = self.__try_login(username, passwd)
print(data)
if response.status == http.client.OK:
precmd('EOF')
self.assertRaises(socket.error, precmd, 'continue')
+ def test_try_login(self):
+ # Make sure __try_login raises the correct exception
+ # upon failure of either send_POST or the read() on the
+ # response
+
+ orig_send_POST = self.tool.send_POST
+ try:
+ def send_POST_raiseImmediately(self, params):
+ raise socket.error("test error")
+
+ self.tool.send_POST = send_POST_raiseImmediately
+ self.assertRaises(FailToLogin,
+ self.tool._BindCmdInterpreter__try_login,
+ "foo", "bar")
+
+ def send_POST_raiseOnRead(self, params):
+ class MyResponse:
+ def read(self):
+ raise socket.error("read error")
+ return MyResponse()
+
+ self.tool.send_POST = send_POST_raiseOnRead
+ self.assertRaises(FailToLogin,
+ self.tool._BindCmdInterpreter__try_login,
+ "foo", "bar")
+ finally:
+ self.tool.send_POST = orig_send_POST
+
def test_run(self):
def login_to_cmdctl():
return True