Repository: baobabsoluciones/cornflow (language: python)

Issue:
Create unit test fot the cascade deletion of permissions
Modify the current test `test_permission_cascade_deletion` in `test_roles` to be equal to `test_user_role_cascade_deletion`.

This has to be done after issue #133 


Discussion / hints:

Fix patch:
diff --git a/cornflow/models/action.py b/cornflow/models/action.py
index e5472d1f..093ea818 100644
--- a/cornflow/models/action.py
+++ b/cornflow/models/action.py
@@ -21,9 +21,22 @@ class ActionModel(EmptyModel):
     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
     name = db.Column(db.String(128), unique=True, nullable=False)
 
+    permissions = db.relationship(
+        "PermissionViewRoleModel",
+        backref="actions",
+        lazy=True,
+        primaryjoin="and_(ActionModel.id==PermissionViewRoleModel.action_id, "
+        "PermissionViewRoleModel.deleted_at==None)",
+        cascade="all,delete",
+    )
+
     def __repr__(self):
         return str(self.id) + ": " + self.name
 
     @staticmethod
     def get_all_objects():
         return ActionModel.query.all()
+
+    @staticmethod
+    def get_one_object(idx):
+        return ActionModel.query.get(idx)
diff --git a/cornflow/models/apiview.py b/cornflow/models/apiview.py
index 8365b7c8..58f925da 100644
--- a/cornflow/models/apiview.py
+++ b/cornflow/models/apiview.py
@@ -20,6 +20,15 @@ class ApiViewModel(EmptyModel):
     url_rule = db.Column(db.String(128), nullable=False)
     description = db.Column(TEXT, nullable=True)
 
+    permissions = db.relationship(
+        "PermissionViewRoleModel",
+        backref="api_views",
+        lazy=True,
+        primaryjoin="and_(ApiViewModel.id==PermissionViewRoleModel.api_view_id, "
+        "PermissionViewRoleModel.deleted_at==None)",
+        cascade="all,delete",
+    )
+
     def __init__(self, data):
         super().__init__()
         self.name = data.get("name")
diff --git a/cornflow/models/permission.py b/cornflow/models/permission.py
index f51b3401..84be6fee 100644
--- a/cornflow/models/permission.py
+++ b/cornflow/models/permission.py
@@ -12,7 +12,7 @@ class PermissionViewRoleModel(TraceAttributes):
     action_id = db.Column(db.Integer, db.ForeignKey("actions.id"), nullable=False)
     action = db.relationship("ActionModel", viewonly=True)
 
-    api_view_id = db.Column(db.Integer, db.ForeignKey("api_view.id"), nullable=True)
+    api_view_id = db.Column(db.Integer, db.ForeignKey("api_view.id"), nullable=False)
     api_view = db.relationship("ApiViewModel", viewonly=True)
 
     role_id = db.Column(db.Integer, db.ForeignKey("roles.id"), nullable=False)
diff --git a/migrations/versions/c8a6c762e818_.py b/migrations/versions/c8a6c762e818_.py
index 86fa806d..32dcc5b1 100644
--- a/migrations/versions/c8a6c762e818_.py
+++ b/migrations/versions/c8a6c762e818_.py
@@ -52,7 +52,7 @@ def upgrade():
         sa.Column("deleted_at", sa.DateTime(), nullable=True),
         sa.Column("id", sa.Integer(), nullable=False),
         sa.Column("action_id", sa.Integer(), nullable=False),
-        sa.Column("api_view_id", sa.Integer(), nullable=True),
+        sa.Column("api_view_id", sa.Integer(), nullable=False),
         sa.Column("role_id", sa.Integer(), nullable=False),
         sa.ForeignKeyConstraint(
             ["action_id"],


Test patch:
diff --git a/cornflow/tests/unit/test_permissions.py b/cornflow/tests/unit/test_permissions.py
index 746f311a..f47a5dfa 100644
--- a/cornflow/tests/unit/test_permissions.py
+++ b/cornflow/tests/unit/test_permissions.py
@@ -11,9 +11,15 @@
     PermissionsViewRoleEndpoint,
     PermissionsViewRoleDetailEndpoint,
 )
-from cornflow.models import InstanceModel, PermissionViewRoleModel
-from cornflow.shared.const import ROLES_MAP
-from cornflow.tests.const import INSTANCE_PATH, INSTANCE_URL, PERMISSION_URL
+from cornflow.models import (
+    ActionModel,
+    ApiViewModel,
+    InstanceModel,
+    PermissionViewRoleModel,
+    RoleModel,
+)
+from cornflow.shared.const import GET_ACTION, ROLES_MAP, VIEWER_ROLE
+from cornflow.tests.const import INSTANCE_PATH, INSTANCE_URL, PERMISSION_URL, ROLES_URL
 from cornflow.tests.custom_test_case import CustomTestCase
 
 
@@ -209,3 +215,29 @@ def test_create_instance_no_permissions(self):
     def test_missing_schema(self):
         self.payload.pop("schema")
         self.create_new_row(self.url, self.model, self.payload, 400, False)
+
+
+class TestPermissionsViewModel(CustomTestCase):
+    def setUp(self):
+        super().setUp()
+
+    def test_permission_role_cascade_deletion(self):
+        before_permissions = PermissionViewRoleModel.get_all_objects()
+        role = RoleModel.get_one_object(VIEWER_ROLE)
+        role.delete()
+        after_permissions = PermissionViewRoleModel.get_all_objects()
+        self.assertNotEqual(before_permissions, after_permissions)
+
+    def test_permission_action_cascade_deletion(self):
+        before_permissions = PermissionViewRoleModel.get_all_objects()
+        action = ActionModel.get_one_object(GET_ACTION)
+        action.delete()
+        after_permissions = PermissionViewRoleModel.get_all_objects()
+        self.assertNotEqual(before_permissions, after_permissions)
+
+    def test_permission_api_view_cascade_deletion(self):
+        before_permissions = PermissionViewRoleModel.get_all_objects()
+        api_view = ApiViewModel.get_one_by_name("instance")
+        api_view.delete()
+        after_permissions = PermissionViewRoleModel.get_all_objects()
+        self.assertNotEqual(before_permissions, after_permissions)