Add docs and tests

This commit is contained in:
Marek Wolan
2023-08-07 10:55:29 +01:00
parent ac9b83cc42
commit f0d7e03fd7
7 changed files with 147 additions and 25 deletions

View File

@@ -8,7 +8,13 @@ from primaite.simulator.domain.controller import AccountGroup, GroupMembershipVa
def test_group_action_validation() -> None:
"""Check that actions are denied when an unauthorised request is made."""
"""
Check that actions are denied when an unauthorised request is made.
This test checks the integration between SimComponent and the permissions validation system. First, we create a
basic node and folder class. We configure the node so that only admins can create a folder. Then, we try to create
a folder as both an admin user and a non-admin user.
"""
class Folder(SimComponent):
name: str
@@ -42,22 +48,28 @@ def test_group_action_validation() -> None:
def remove_folder(self, folder: Folder) -> None:
self.folders = [x for x in self.folders if x is not folder]
# check that the folder is created when a local admin tried to do it
permitted_context = {"request_source": {"agent": "BLUE", "account": "User1", "groups": ["local_admin"]}}
my_node = Node(uuid="0000-0000-1234", name="pc")
my_node.apply_action(["create_folder", "memes"], context=permitted_context)
assert len(my_node.folders) == 1
assert my_node.folders[0].name == "memes"
# check that the number of folders is still 1 even after attempting to create a second one without permissions
invalid_context = {"request_source": {"agent": "BLUE", "account": "User1", "groups": ["local_user", "domain_user"]}}
my_node.apply_action(["create_folder", "memes2"], context=invalid_context)
assert len(my_node.folders) == 1
assert my_node.folders[0].name == "memes"
def test_hierarchical_action_with_validation() -> None:
"""Check that validation works with sub-objects"""
"""
Check that validation works with sub-objects.
This test creates a parent object (Node) and a child object (Application) which both accept actions. The node allows
action passthrough to applications. The purpose of this test is to check that after an action is passed through to
a child object, that the permission system still works as intended.
"""
class Application(SimComponent):
name: str
@@ -100,19 +112,19 @@ def test_hierarchical_action_with_validation() -> None:
return super().describe_state()
def disable(self) -> None:
self.status = "disabled"
self.state = "disabled"
def enable(self) -> None:
if self.status == "disabled":
self.status = "off"
if self.state == "disabled":
self.state = "off"
def turn_on(self) -> None:
if self.status == "off":
self.status = "on"
if self.state == "off":
self.state = "on"
def turn_off(self) -> None:
if self.status == "on":
self.status = "off"
if self.state == "on":
self.state = "off"
class Node(SimComponent):
name: str
@@ -141,7 +153,7 @@ def test_hierarchical_action_with_validation() -> None:
def send_action_to_app(self, app_name: str, options: List[str], context: Dict):
for app in self.apps:
if app_name == app.name:
app.apply_action(options)
app.apply_action(options, context)
break
else:
msg = f"Node has no app with name {app_name}"
@@ -163,9 +175,16 @@ def test_hierarchical_action_with_validation() -> None:
}
}
# check that a non-admin can't disable this app
my_node.apply_action(["apps", "Chrome", "disable"], non_admin_context)
my_node.apply_action(["apps", "Firefox", "turn_on"], non_admin_context)
assert my_node.apps[0].name == "Chrome" # if failure occurs on this line, the test itself is broken
assert my_node.apps[0].state == "off"
assert my_node.apps[0].name == "Chrome"
assert my_node.apps[1].name == "Firefox"
assert my_node.apps[0].state == ... # TODO: finish
# check that a non-admin can turn this app on
my_node.apply_action(["apps", "Firefox", "turn_on"], non_admin_context)
assert my_node.apps[1].name == "Firefox" # if failure occurs on this line, the test itself is broken
assert my_node.apps[1].state == "on"
# check that an admin can disable this app
my_node.apply_action(["apps", "Chrome", "disable"], admin_context)
assert my_node.apps[0].state == "disabled"