Fix bugs due to the modification of python_moondb 79/53779/5
authorThomas Duval <thomas.duval@orange.com>
Wed, 14 Mar 2018 10:25:06 +0000 (11:25 +0100)
committerThomas Duval <thomas.duval@orange.com>
Thu, 15 Mar 2018 09:47:03 +0000 (10:47 +0100)
Change-Id: Id3855a6e8da9f8ef942c7a34a3e8da4f87be4c9e

moon_manager/moon_manager/api/json_export.py
moon_manager/moon_manager/http_server.py
moon_manager/tests/unit_python/api/import_export_utilities.py
moon_manager/tests/unit_python/api/test_data.py
moon_manager/tests/unit_python/api/test_export.py
moon_manager/tests/unit_python/api/test_import.py
python_moondb/Changelog
python_moondb/MANIFEST.in
python_moondb/python_moondb/__init__.py
python_moondb/python_moondb/api/policy.py
python_moondb/python_moondb/backends/sql.py

index feb4fde..9ddcfc8 100644 (file)
@@ -1,3 +1,8 @@
+# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
 import logging
 from flask_restful import Resource
 from python_moonutilities.security_functions import check_auth
@@ -95,8 +100,8 @@ class JsonExport(Resource):
                         JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "name", str)
                         JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "description", str)
                     else:
-                        JsonUtils.copy_field_if_exists(data_group["data"][data_key]["value"], data_dict, "name", str)
-                        JsonUtils.copy_field_if_exists(data_group["data"][data_key]["value"], data_dict, "description", str)
+                        JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "name", str)
+                        JsonUtils.copy_field_if_exists(data_group["data"][data_key], data_dict, "description", str)
 
                     JsonUtils.convert_id_to_name(policy_id, data_dict, "policy", "policy", PolicyManager, self._user_id)
                     JsonUtils.convert_id_to_name(category_id, data_dict, "category", type_element + "_category", ModelManager, self._user_id, policy_key)
index 28d77ea..76d0459 100644 (file)
@@ -2,12 +2,10 @@
 # This software is distributed under the terms and conditions of the 'Apache-2.0'
 # license which can be found in the file 'LICENSE' in this package distribution
 # or at 'http://www.apache.org/licenses/LICENSE-2.0'.
-from werkzeug.exceptions import HTTPException
 from flask import Flask, jsonify, Response, make_response
 from flask_cors import CORS, cross_origin
 from json import dumps
 from flask_restful import Resource, Api
-import flask_restful
 import logging
 import sqlalchemy.exc
 import time
@@ -24,7 +22,6 @@ from moon_manager.api.data import SubjectData, ObjectData, ActionData
 from moon_manager.api.assignments import SubjectAssignments, ObjectAssignments, ActionAssignments
 from moon_manager.api.rules import Rules
 from moon_manager.api.json_import import JsonImport
-from moon_manager.api.base_exception import BaseException
 from moon_manager.api.json_export import JsonExport
 from python_moonutilities import configuration
 from python_moondb.core import PDPManager
@@ -112,17 +109,19 @@ class Root(Resource):
 
 class CustomApi(Api):
 
-    def handle_error(self, e):
+    @staticmethod
+    def handle_error(e):
         try:
-            error_message = dumps({'message': str(e)})
+            error_message = dumps({'message': str(e), "code": getattr(e, "code", 500)})
             logger.error(error_message)
-            return make_response(error_message, e.code)
-        except Exception as e2: # unhandled exception in the api...
+            return make_response(error_message, getattr(e, "code", 500))
+        except Exception as e2:  # unhandled exception in the api...
             logger.error(str(e2))
             return make_response(error_message, 500)
 
 
 class HTTPServer(Server):
+
     def __init__(self, host="localhost", port=80, **kwargs):
         super(HTTPServer, self).__init__(host=host, port=port, **kwargs)
         self.app = Flask(__name__)
@@ -135,26 +134,6 @@ class HTTPServer(Server):
         CORS(self.app)
         self.api = CustomApi(self.app)
         self.__set_route()
-    #    self.__hook_errors()
-
-    #def __hook_errors(self):
-    #    def get_500_json(e):
-    #        logger.error("get_500_json")
-    #        return jsonify({"result": False, "code": 500, "description": str(e)}), 500
-    #    self.app.register_error_handler(JsonUtilsException, get_500_json)
-    #    self.app.register_error_handler(JsonImportException, get_500_json)
-    #    self.app.register_error_handler(UnknownName, get_500_json)
-
-    #    def get_404_json(e):
-    #        return jsonify({"result": False, "code": 404, "description": str(e)}), 404
-    #    self.app.register_error_handler(404, get_404_json)
-
-    #   def get_400_json(e):
-    #        return jsonify({"result": False, "code": 400, "description": str(e)}), 400
-
-    #    self.app.register_error_handler(500, lambda e: get_500_json)
-    #    self.app.register_error_handler(400, lambda e: get_400_json)
-    #    self.app.register_error_handler(403, exceptions.AuthException)
 
     def __set_route(self):
         self.api.add_resource(Root, '/')
@@ -179,4 +158,4 @@ class HTTPServer(Server):
 
     def run(self):
         self.__check_if_db_is_up()
-        self.app.run(debug=True, host=self._host, port=self._port)  # nosec
+        self.app.run(host=self._host, port=self._port)  # nosec
index 15c3e33..98586d0 100644 (file)
@@ -1,3 +1,8 @@
+# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
 import api.utilities as utilities
 import api.test_models as test_models
 import api.test_policies as test_policies
@@ -7,6 +12,10 @@ import api.test_data as test_data
 import api.meta_rules_test as test_meta_rules
 import api.test_assignemnt as test_assignments
 import api.test_rules as test_rules
+import logging
+
+logger = logging.getLogger("moon.manager.test.api." + __name__)
+
 
 def clean_models(client):
     req, models = test_models.get_models(client)
@@ -25,9 +34,11 @@ def clean_policies(client):
 
 def clean_subjects(client):
     subjects = test_perimeter.get_subjects(client)
+    logger.info("subjects {}".format(subjects))
     for key in subjects["subjects"]:
         subject = subjects["subjects"][key]
         policy_keys = subject["policy_list"]
+        logger.info("subjects policy_keys {}".format(policy_keys))
         for policy_key in policy_keys:
             client.delete("/policies/{}/subjects/{}".format(policy_key,key))
         client.delete("/subjects/{}".format(key))
@@ -36,9 +47,11 @@ def clean_subjects(client):
 
 def clean_objects(client):
     objects = test_perimeter.get_objects(client)
+    logger.info("objects {}".format(objects))
     for key in objects["objects"]:
         object_ = objects["objects"][key]
         policy_keys = object_["policy_list"]
+        logger.info("objects policy_keys {}".format(policy_keys))
         for policy_key in policy_keys:
             print("/policies/{}/objects/{}".format(policy_key, key))
             req = client.delete("/policies/{}/objects/{}".format(policy_key, key))
@@ -48,9 +61,11 @@ def clean_objects(client):
 
 def clean_actions(client):
     actions = test_perimeter.get_actions(client)
+    logger.info("objects {}".format(actions))
     for key in actions["actions"]:
         action = actions["actions"][key]
         policy_keys = action["policy_list"]
+        logger.info("action policy_keys {}".format(policy_keys))
         for policy_key in policy_keys:
             client.delete("/policies/{}/actions/{}".format(policy_key, key))
         client.delete("/actions/{}".format(key))
@@ -59,19 +74,21 @@ def clean_actions(client):
 
 def clean_subject_categories(client):
     req, categories = test_categories.get_subject_categories(client)
-    print(categories)
+    logger.info(categories)
     for key in categories["subject_categories"]:
         client.delete("/subject_categories/{}".format(key))
 
 
 def clean_object_categories(client):
     req, categories = test_categories.get_object_categories(client)
+    logger.info(categories)
     for key in categories["object_categories"]:
         client.delete("/object_categories/{}".format(key))
 
 
 def clean_action_categories(client):
     req, categories = test_categories.get_action_categories(client)
+    logger.info(categories)
     for key in categories["action_categories"]:
         client.delete("/action_categories/{}".format(key))
 
@@ -174,8 +191,9 @@ def clean_all(client):
     clean_object_data(client)
     clean_action_data(client)
 
-    clean_policies(client)
-    clean_models(client)
     clean_actions(client)
     clean_objects(client)
     clean_subjects(client)
+
+    clean_policies(client)
+    clean_models(client)
\ No newline at end of file
index f636aaa..724f919 100644 (file)
@@ -1,3 +1,8 @@
+# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
 import api.utilities as utilities
 import json
 
@@ -106,8 +111,12 @@ def test_add_object_data():
     value = object_data["object_data"]['data']
     assert "object_data" in object_data
     id = list(value.keys())[0]
-    assert value[id]['value']['name'] == "testuser"
-    assert value[id]['value']['description'] == "description of {}".format("testuser")
+    print("-----------------------")
+    print(id)
+    print(value[id])
+    print("-----------------------")
+    assert value[id]['name'] == "testuser"
+    assert value[id]['description'] == "description of {}".format("testuser")
 
 
 def test_delete_object_data():
@@ -164,8 +173,8 @@ def test_add_action_data():
     value = action_data["action_data"]['data']
     assert "action_data" in action_data
     id = list(value.keys())[0]
-    assert value[id]['value']['name'] == "testuser"
-    assert value[id]['value']['description'] == "description of {}".format("testuser")
+    assert value[id]['name'] == "testuser"
+    assert value[id]['description'] == "description of {}".format("testuser")
 
 
 def test_delete_action_data():
index 0b9cd6a..122ab92 100644 (file)
@@ -1,3 +1,8 @@
+# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
 import json
 import api.utilities as utilities
 import api.import_export_utilities as import_export_utilities
@@ -169,7 +174,6 @@ def test_export_subject_object_action_categories():
     req = client.get("/export")
     assert req.status_code == 200
     data = utilities.get_json(req.data)
-    print(data)
     assert "content" in data
     type_elements = ["subject", "object", "action"]
     for type_element in type_elements:
index da7872d..4e970a0 100644 (file)
@@ -1,3 +1,8 @@
+# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
+# This software is distributed under the terms and conditions of the 'Apache-2.0'
+# license which can be found in the file 'LICENSE' in this package distribution
+# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
+
 import api.utilities as utilities
 import api.test_models as test_models
 import api.test_policies as test_policies
@@ -512,4 +517,6 @@ def test_import_subject_object_action_data():
 
 def test_clean():
     client = utilities.register_client()
-    import_export_utilities.clean_all(client)
\ No newline at end of file
+    import_export_utilities.clean_all(client)
+    #restore the database as previously
+    utilities.get_policy_id()
\ No newline at end of file
index b1e8e0c..44ae7fa 100644 (file)
@@ -61,3 +61,7 @@ CHANGES
 -----
 - Remove some code duplication in moon_db
 - handle the extra field for the perimeter
+
+1.2.7
+-----
+- Fix some bugs
index 82b4014..0265583 100644 (file)
@@ -3,7 +3,7 @@
 # license which can be found in the file 'LICENSE' in this package distribution
 # or at 'http://www.apache.org/licenses/LICENSE-2.0'.
 
-include README.rst
+include README.md
 include LICENSE
 include setup.py
 include requirements.txt
index 8de4de6..bebaca8 100644 (file)
@@ -3,5 +3,5 @@
 # license which can be found in the file 'LICENSE' in this package distribution
 # or at 'http://www.apache.org/licenses/LICENSE-2.0'.
 
-__version__ = "1.2.6"
+__version__ = "1.2.7"
 
index ca313f9..cde3ab7 100644 (file)
@@ -112,6 +112,7 @@ class PolicyManager(Managers):
 
     @enforce(("read", "write"), "perimeter")
     def add_action(self, user_id, policy_id, perimeter_id=None, value=None):
+        logger.info("add_action {}".format(policy_id))
         if not self.get_policies(user_id=user_id, policy_id=policy_id):
             raise exceptions.PolicyUnknown
         if not perimeter_id:
index a8e7740..c067095 100644 (file)
@@ -892,7 +892,7 @@ class ModelConnector(BaseConnector, ModelDriver):
         return self.__add_perimeter_category(ObjectCategory, name, description, uuid=uuid)
 
     def delete_object_category(self, category_id):
-        self.__delete_perimeter_category(SubjectCategory, category_id)
+        self.__delete_perimeter_category(ObjectCategory, category_id)
 
     def get_action_categories(self, category_id=None):
         return self.__get_perimeter_categories(ActionCategory, category_id=category_id)
@@ -901,7 +901,7 @@ class ModelConnector(BaseConnector, ModelDriver):
         return self.__add_perimeter_category(ActionCategory, name, description, uuid=uuid)
 
     def delete_action_category(self, category_id):
-        self.__delete_perimeter_category(SubjectCategory, category_id)
+        self.__delete_perimeter_category(ActionCategory, category_id)
 
     # Getter and Setter for subject_category