"""get_users: Proxmox users with their level, and the node's login accounts (#19).""" from __future__ import annotations from unittest.mock import patch PASSWD = """\ root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin alice:x:1000:1000:Alice:/home/alice:/bin/bash nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin """ # GET /access/acl: who holds which role where. ACL = [ {"path": "/", "roleid": "Administrator", "type": "user", "ugid": "admin@pve", "propagate": 1}, {"path": "/", "roleid": "Administrator", "type": "group", "ugid": "ops", "propagate": 1}, {"path": "/vms/100", "roleid": "Administrator", "type": "user", "ugid": "vm@pve"}, {"path": "/", "roleid": "PVEAuditor", "type": "user", "ugid": "audit@pve", "propagate": 1}, ] USERS = [ {"userid": "root@pam", "groups": ""}, {"userid": "admin@pve", "groups": ""}, {"userid": "bob@pve", "groups": "ops,dev"}, {"userid": "vm@pve", "groups": ""}, {"userid": "audit@pve", "groups": ""}, ] def _users(driver, passwd=PASSWD): driver._api.access.users.get.return_value = USERS driver._api.access.acl.get.return_value = ACL with patch.object(driver, "_exec_ssh_command", return_value=passwd): return driver.get_users() class TestLocalAccounts: def test_root_and_real_users_not_system_accounts(self, driver): result = _users(driver) assert result["root"]["level"] == 15 assert "alice" in result for system in ("daemon", "bin", "www-data", "nobody"): assert system not in result class TestProxmoxUsers: def test_administrator_on_the_whole_datacenter_is_level_15(self, driver): result = _users(driver) assert result["admin@pve"]["level"] == 15 def test_administrator_through_a_group_is_level_15(self, driver): assert _users(driver)["bob@pve"]["level"] == 15 def test_root_at_pam_is_always_level_15(self, driver): assert _users(driver)["root@pam"]["level"] == 15 def test_another_role_or_a_narrower_path_is_level_1(self, driver): result = _users(driver) assert result["audit@pve"]["level"] == 1 assert result["vm@pve"]["level"] == 1