Browse Source

add test into settingsloader to verify the rest api configuration

nico 8 years ago
parent
commit
da36b9d282
2 changed files with 53 additions and 22 deletions
  1. 49 18
      core/ConfigurationManager/SettingLoader.py
  2. 4 4
      test.py

+ 49 - 18
core/ConfigurationManager/SettingLoader.py

@@ -13,6 +13,10 @@ logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
 
+class SettingInvalidException(Exception):
+    pass
+
+
 class NullSettingException(Exception):
     pass
 
@@ -70,8 +74,8 @@ class SettingLoader(object):
                 raise NullSettingException("Attribute default_speech_to_text is null")
             logger.debug("Default STT: %s" % default_speech_to_text)
             return default_speech_to_text
-        except KeyError:
-            raise SettingNotFound("Attribute default_speech_to_text not found in settings")
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
     @staticmethod
     def _get_default_text_to_speech(settings):
@@ -81,8 +85,8 @@ class SettingLoader(object):
                 raise NullSettingException("Attribute default_text_to_speech is null")
             logger.debug("Default TTS: %s" % default_text_to_speech)
             return default_text_to_speech
-        except KeyError:
-            raise SettingNotFound("Attribute default_text_to_speech not found in settings")
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
     @staticmethod
     def _get_default_trigger(settings):
@@ -92,8 +96,8 @@ class SettingLoader(object):
                 raise NullSettingException("Attribute default_trigger is null")
             logger.debug("Default Trigger name: %s" % default_trigger)
             return default_trigger
-        except KeyError:
-            raise SettingNotFound("Attribute default_trigger not found in settings")
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
     @classmethod
     def _get_stts(cls, settings):
@@ -131,8 +135,8 @@ class SettingLoader(object):
         """
         try:
             text_to_speech_list = settings["text_to_speech"]
-        except KeyError:
-            raise SettingNotFound("text_to_speech settings not found")
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
         ttss = list()
         for text_to_speech_el in text_to_speech_list:
@@ -158,8 +162,8 @@ class SettingLoader(object):
         """
         try:
             triggers_list = settings["triggers"]
-        except KeyError:
-            raise SettingNotFound("text_to_speech settings not found")
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
         triggers = list()
         for trigger_el in triggers_list:
@@ -218,15 +222,42 @@ class SettingLoader(object):
     def _get_rest_api(cls, settings):
         try:
             rest_api = settings["rest_api"]
-        except KeyError:
-            return None
+        except KeyError, e:
+            raise SettingNotFound("%s setting not found" % e)
 
         if rest_api is not None:
-            password_protected = rest_api["password_protected"]
-            login = rest_api["login"]
-            password = rest_api["password"]
-            active = rest_api["active"]
-            port = rest_api["port"]
+            try:
+                password_protected = rest_api["password_protected"]
+                if password_protected is None:
+                    raise NullSettingException("password_protected setting cannot be null")
+                login = rest_api["login"]
+                password = rest_api["password"]
+                if password_protected:
+                    if login is None:
+                        raise NullSettingException("login setting cannot be null if password_protected is True")
+                    if login is None:
+                        raise NullSettingException("password setting cannot be null if password_protected is True")
+                active = rest_api["active"]
+                if active is None:
+                    raise NullSettingException("active setting cannot be null")
+                port = rest_api["port"]
+                if port is None:
+                    raise NullSettingException("port setting cannot be null")
+                # check that the port in an integer
+                try:
+                    port = int(port)
+                except ValueError:
+                    raise SettingInvalidException("port must be an integer")
+                # check the port is a valid port number
+                if not 1024 <= port <= 65535:
+                    raise SettingInvalidException("port must be in range 1024-65535")
+
+            except KeyError, e:
+                print e
+                raise SettingNotFound("%s settings not found" % e)
+
+            # config ok, we can return the rest api object
             rest_api_obj = RestAPI(password_protected=password_protected, login=login, password=password, active=active, port=port)
-
             return rest_api_obj
+        else:
+            raise NullSettingException("rest_api settings cannot be null")

+ 4 - 4
test.py

@@ -21,12 +21,12 @@ logger.setLevel(logging.DEBUG)
 # oa = OrderAnalyser(order=order)
 # oa.start()
 
+SettingLoader.get_settings()
 
 
-
-app = Flask(__name__)
-flask_api = FlaskAPI(app)
-flask_api.start()
+# app = Flask(__name__)
+# flask_api = FlaskAPI(app)
+# flask_api.start()