Skip to content

Commit f33c4a1

Browse files
authored
Merge pull request #379 from Limmen/users
unit test for users_controller
2 parents e2b7076 + e7c72d1 commit f33c4a1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from unittest.mock import patch, MagicMock
2+
from csle_common.controllers.users_controller import UsersController
3+
4+
5+
class TestUsersControllerSuite:
6+
"""
7+
Test UsersController
8+
"""
9+
10+
@patch("csle_common.util.emulation_util.EmulationUtil.connect_admin")
11+
@patch("csle_common.util.emulation_util.EmulationUtil.disconnect_admin")
12+
@patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd")
13+
def test_create_users(
14+
self, mock_execute_ssh_cmd, mock_disconnect_admin, mock_connect_admin
15+
) -> None:
16+
"""
17+
Test method that creates users in an emulation environment according to a specified users-configuration
18+
19+
:param mock_execute_ssh_cmd: mock_execute_ssh_cmd
20+
:param mock_disconnect_admin: mock_disconnect_admin
21+
:param mock_connect_admin: mock_connect_admin
22+
23+
:return: None
24+
"""
25+
logger = MagicMock()
26+
user1 = MagicMock(username="user1", pw="password1", root=False)
27+
user2 = MagicMock(username="user2", pw="password2", root=True)
28+
users_conf = MagicMock(
29+
physical_host_ip="192.168.1.1",
30+
docker_gw_bridge_ip="192.168.1.2",
31+
users=[user1, user2],
32+
)
33+
34+
cred1 = MagicMock(username="cred1", pw="password1", root=False)
35+
cred2 = MagicMock(username="cred2", pw="password2", root=True)
36+
vuln_conf = MagicMock(
37+
physical_host_ip="192.168.1.1",
38+
docker_gw_bridge_ip="192.168.1.3",
39+
credentials=[cred1, cred2],
40+
)
41+
42+
emulation_env_config = MagicMock()
43+
emulation_env_config.users_config.users_configs = [users_conf]
44+
emulation_env_config.vuln_config.node_vulnerability_configs = [vuln_conf]
45+
emulation_env_config.connections = {
46+
"192.168.1.2": MagicMock(),
47+
"192.168.1.3": MagicMock(),
48+
}
49+
50+
physical_server_ip = "192.168.1.1"
51+
mock_execute_ssh_cmd.side_effect = [
52+
(b"user1\nuser2\n", "", 0), # output of ls /home
53+
("", "", 0), # output of deluser user1
54+
("", "", 0), # output of rm -rf /home/user1
55+
("", "", 0), # output of deluser user2
56+
("", "", 0), # output of rm -rf /home/user2
57+
("", "", 0), # output of useradd for user1
58+
("", "", 0), # output of useradd for user2
59+
("", "", 0), # output of useradd for cred1
60+
("", "", 0), # output of useradd for cred2
61+
]
62+
63+
UsersController.create_users(emulation_env_config, physical_server_ip, logger)
64+
mock_connect_admin.assert_any_call(
65+
emulation_env_config=emulation_env_config, ip="192.168.1.2"
66+
)
67+
mock_connect_admin.assert_any_call(
68+
emulation_env_config=emulation_env_config, ip="192.168.1.3"
69+
)
70+
mock_execute_ssh_cmd.assert_called()
71+
mock_disconnect_admin.assert_called()

0 commit comments

Comments
 (0)