Skip to content

Commit

Permalink
WIP: Better error reporting and exception handling in rbac precompile…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
talhadaar committed Nov 24, 2023
1 parent e3305ee commit 921e793
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions tests/bridge_rbac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tools.peaq_eth_utils import call_eth_transfer_a_lot, get_contract, generate_random_hex
from tools.peaq_eth_utils import TX_SUCCESS_STATUS
from web3 import Web3
import enum

import unittest

Expand Down Expand Up @@ -35,6 +36,24 @@ def generate_random_tuple():
name = f'NAME{id[:4]}'.encode("utf-8")
return (id, name)

##############################################################################
# RbacErrorType as Enum for convenience
##############################################################################
class RbacErrorType(enum.Enum):
# Returned if the Entity already exists
EntityAlreadyExist = "EntityAlreadyExist"
# Returned if the Entity does not exists
EntityDoesNotExist = "EntityDoesNotExist"
# Returned if the Entity does not belong to the caller
EntityAuthorizationFailed = "EntityAuthorizationFailed"
# Returned if the Entity is not enabled
EntityDisabled = "EntityDisabled"
# Returned if an assignment does already exist
AssignmentAlreadyExist = "AssignmentAlreadyExist"
# Returned if an assignment does not exist
AssignmentDoesNotExist = "AssignmentDoesNotExist"
# Exceeds max characters
NameExceedMaxChar = "NameExceedMaxChar"

##############################################################################
# Helper functions for submitting transactions
Expand Down Expand Up @@ -252,6 +271,8 @@ def _verify_update_role(self, tx, role_id, name):

return tx

# fetch_role will return an exception if the role is disabled
# exception is caught and verified
def _verify_disable_role(self, tx, role_id):
self.assertEqual(tx['status'], TX_SUCCESS_STATUS)

Expand All @@ -261,9 +282,10 @@ def _verify_disable_role(self, tx, role_id):
self._verify_role_disabled_event(events, self._eth_kp_src.ss58_address, role_id)

# fetch role and verify
data = self._contract.functions.fetch_role(self._account, role_id).call()
self.assertEqual(data[0], role_id)
self.assertEqual(data[2], False)
with self.assertRaises(ValueError) as tx_info:
self._contract.functions.fetch_role(self._account, role_id).call()

self.assertIn(RbacErrorType.EntityDisabled.value, tx_info.exception.args[0]['message'])

return tx

Expand Down Expand Up @@ -494,52 +516,52 @@ def test_rbac_bridge(self):
self._add_role(*roles[1])
self._add_role(*roles[2])

self._verify_add_permission(self._add_permission(*permissions[0]), *permissions[0])
self._add_permission(*permissions[1])
self._add_permission(*permissions[2])
# self._verify_add_permission(self._add_permission(*permissions[0]), *permissions[0])
# self._add_permission(*permissions[1])
# self._add_permission(*permissions[2])

self._verify_add_group(self._add_group(*groups[0]), *groups[0])
self._add_group(*groups[1])
self._add_group(*groups[2])
# self._verify_add_group(self._add_group(*groups[0]), *groups[0])
# self._add_group(*groups[1])
# self._add_group(*groups[2])

# update roles, permissions and groups - TODO replace updated values with newer values and check
self._verify_update_role(self._update_role(*roles[1]), *roles[1])
self._verify_update_permission(self._update_permission(*permissions[1]), *permissions[1])
self._verify_update_group(self._update_group(*groups[1]), *groups[1])
# # update roles, permissions and groups - TODO replace updated values with newer values and check
# self._verify_update_role(self._update_role(*roles[1]), *roles[1])
# self._verify_update_permission(self._update_permission(*permissions[1]), *permissions[1])
# self._verify_update_group(self._update_group(*groups[1]), *groups[1])

# disable roles, permissions and groups
self._verify_disable_role(self._disable_role(roles[2]), roles[2])
self._verify_disable_permission(self._disable_permission(permissions[2]), permissions[2])
self._verify_disable_group(self._disable_group(groups[2]), groups[2])
self._verify_disable_role(self._disable_role(roles[2][0]), roles[2][0])
# self._verify_disable_permission(self._disable_permission(permissions[2][0]), permissions[2][0])
# self._verify_disable_group(self._disable_group(groups[2][0]), groups[2][0])

# assign role to user
self._verify_assign_role_to_user(self._assign_role_to_user(roles[0][0], users[0][0]), roles[0][0], users[0][0])
self._assign_role_to_user(roles[1][0], users[0][0])
self._assign_role_to_user(roles[2][0], users[0][0])
# # assign role to user
# self._verify_assign_role_to_user(self._assign_role_to_user(roles[0][0], users[0][0]), roles[0][0], users[0][0])
# self._assign_role_to_user(roles[1][0], users[0][0])
# self._assign_role_to_user(roles[2][0], users[0][0])

# unassign role to user
self._verify_unassign_role_to_user(self._unassign_role_to_user(roles[0][0], users[0][0]),roles[0][0], users[0][0])
# # unassign role to user
# self._verify_unassign_role_to_user(self._unassign_role_to_user(roles[0][0], users[0][0]),roles[0][0], users[0][0])

# assign permission to role
self._verify_assign_permission_to_role(self._assign_permission_to_role(permissions[0][0], roles[0][0]), permissions[0][0], roles[0][0])
self._assign_permission_to_role(permissions[1][0], roles[0][0])
self._assign_permission_to_role(permissions[2][0], roles[0][0])
# # assign permission to role
# self._verify_assign_permission_to_role(self._assign_permission_to_role(permissions[0][0], roles[0][0]), permissions[0][0], roles[0][0])
# self._assign_permission_to_role(permissions[1][0], roles[0][0])
# self._assign_permission_to_role(permissions[2][0], roles[0][0])

# unassign permission to role
self._verify_unassign_permission_to_role(self._unassign_permission_to_role(permissions[0][0], roles[0][0]), permissions[0][0], roles[0][0])
# # unassign permission to role
# self._verify_unassign_permission_to_role(self._unassign_permission_to_role(permissions[0][0], roles[0][0]), permissions[0][0], roles[0][0])

# assign role to group
self._verify_assign_role_to_group(self._assign_role_to_group(roles[0][0], groups[0][0]), roles[0][0], groups[0][0])
self._assign_role_to_group(roles[1][0], groups[0][0])
self._assign_role_to_group(roles[2][0], groups[0][0])
# # assign role to group
# self._verify_assign_role_to_group(self._assign_role_to_group(roles[0][0], groups[0][0]), roles[0][0], groups[0][0])
# self._assign_role_to_group(roles[1][0], groups[0][0])
# self._assign_role_to_group(roles[2][0], groups[0][0])

# unassign role to group
self._verify_unassign_role_to_group(self._unassign_role_to_group(roles[0][0], groups[0][0]), roles[0][0], groups[0][0])
# # unassign role to group
# self._verify_unassign_role_to_group(self._unassign_role_to_group(roles[0][0], groups[0][0]), roles[0][0], groups[0][0])

# assign user to group
self._verify_assign_user_to_group(self._assign_user_to_group(users[0][0], groups[0][0]), users[0][0], groups[0][0])
self._assign_user_to_group(users[1][0], groups[0][0])
self._assign_user_to_group(users[2][0], groups[0][0])
# # assign user to group
# self._verify_assign_user_to_group(self._assign_user_to_group(users[0][0], groups[0][0]), users[0][0], groups[0][0])
# self._assign_user_to_group(users[1][0], groups[0][0])
# self._assign_user_to_group(users[2][0], groups[0][0])

# unassign user to group
self._verify_unassign_user_to_group(self._unassign_user_to_group(users[0][0], groups[0][0]), users[0][0], groups[0][0])
# # unassign user to group
# self._verify_unassign_user_to_group(self._unassign_user_to_group(users[0][0], groups[0][0]), users[0][0], groups[0][0])

0 comments on commit 921e793

Please sign in to comment.