Browse Source

[2595] Update __try_login in bindcmd and add test

Jelte Jansen 12 years ago
parent
commit
55b05119c7
2 changed files with 42 additions and 5 deletions
  1. 14 5
      src/bin/bindctl/bindcmd.py
  2. 28 0
      src/bin/bindctl/tests/bindctl_test.py

+ 14 - 5
src/bin/bindctl/bindcmd.py

@@ -209,9 +209,19 @@ WARNING: Python readline module isn't available, so the command line editor
         return True
         return True
 
 
     def __try_login(self, username, password):
     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}
         param = {'username': username, 'password' : password}
         try:
         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:
         except socket.error as err:
             print("Socket error while sending login information: ", err)
             print("Socket error while sending login information: ", err)
             if err.errno == errno.ECONNRESET:
             if err.errno == errno.ECONNRESET:
@@ -230,8 +240,8 @@ WARNING: Python readline module isn't available, so the command line editor
         # Look at existing username/password combinations and try to log in
         # Look at existing username/password combinations and try to log in
         users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
         users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
         for row in users:
         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:
             if response.status == http.client.OK:
                 # Is interactive?
                 # Is interactive?
                 if sys.stdin.isatty():
                 if sys.stdin.isatty():
@@ -252,8 +262,7 @@ WARNING: Python readline module isn't available, so the command line editor
             username = input("Username: ")
             username = input("Username: ")
             passwd = getpass.getpass()
             passwd = getpass.getpass()
 
 
-            response = self.__try_login(username, passwd)
-            data = response.read().decode()
+            response, data = self.__try_login(username, passwd)
             print(data)
             print(data)
 
 
             if response.status == http.client.OK:
             if response.status == http.client.OK:

+ 28 - 0
src/bin/bindctl/tests/bindctl_test.py

@@ -347,6 +347,34 @@ class TestConfigCommands(unittest.TestCase):
         precmd('EOF')
         precmd('EOF')
         self.assertRaises(socket.error, precmd, 'continue')
         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 test_run(self):
         def login_to_cmdctl():
         def login_to_cmdctl():
             return True
             return True