Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_group_shouldnt_exist(self):
bundle = MagicMock()
group = groups.Group(
bundle,
"bundlewrap",
{'delete': True},
)
status = ItemStatus(correct=False, info={'exists': True})
self.assertEqual(
group.ask(status),
"'bundlewrap' found in /etc/group. Will be deleted."
)
bundle = MagicMock()
user = users.User(
bundle,
"bundlewrap",
{
'home': "/home/bundlewrap",
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group1", "group2"],
'password_hash': "topsecret",
'shell': "/bin/bash",
'uid': 1123,
'use_shadow': False,
},
)
status = ItemStatus(correct=False)
status.info = {
'exists': True,
'full_name': "BundleWrap",
'gid': 2357,
'groups': ["group1", "group2"],
'home': "/home/blkwrt",
'passwd_hash': "secret",
'shell': "/bin/bsh",
'uid': 1113,
'needs_fixing': ['home', 'full_name', 'gid', 'password', 'shell', 'uid'],
}
self.assertEqual(
user.ask(status),
"home dir /home/blkwrt → /home/bundlewrap\n"
"full name BundleWrap → Blöck Wart\n"
def test_shadow_not_found(self):
bundle = MagicMock()
user = users.User(
bundle,
"bundlewrap",
{
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group1", "group2"],
'home': "/home/bundlewrap",
'password_hash': "secret",
'shell': "/bin/bash",
'uid': 1123,
},
)
status = ItemStatus(correct=False)
status.info = {
'exists': True,
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group1", "group2"],
'home': "/home/bundlewrap",
'shadow_hash': None,
'shell': "/bin/bash",
'uid': 1123,
'needs_fixing': ['password'],
}
self.assertEqual(
user.ask(status),
"password hash not found in /etc/shadow\n"
)
def test_user_doesnt_exist(self):
bundle = MagicMock()
user = users.User(
bundle,
"bundlewrap",
{
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group1", "group2"],
'home': "/home/bundlewrap",
'password_hash': "secret",
'shell': "/bin/bash",
'uid': 1123,
},
)
status = ItemStatus(correct=False, info={'exists': False})
self.assertEqual(
user.ask(status),
"'bundlewrap' not found in /etc/passwd"
)
def test_groups(self):
bundle = MagicMock()
user = users.User(
bundle,
"bundlewrap",
{
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group1", "group2", "group3"],
'home': "/home/bundlewrap",
'password_hash': "secret",
'shell': "/bin/bash",
'uid': 1123,
},
)
status = ItemStatus(correct=False)
status.info = {
'exists': True,
'full_name': "Blöck Wart",
'gid': 2345,
'groups': ["group3", "group2", "group4", "group5"],
'home': "/home/bundlewrap",
'shadow_hash': "secret",
'shell': "/bin/bash",
'uid': 1123,
'needs_fixing': ['groups'],
}
self.assertEqual(
user.ask(status),
"missing groups group1\n" +
"extra groups group4, group5\n"
)
def test_group_doesnt_exist(self):
bundle = MagicMock()
group = groups.Group(
bundle,
"bundlewrap",
{ 'gid': 2345 },
)
status = ItemStatus(correct=False, info={'exists': False})
self.assertEqual(
group.ask(status),
"'bundlewrap' not found in /etc/group"
)
def test_combined(self, fix_type, fix_owner, fix_mode, fix_content):
f = files.File(MagicMock(), "/foo", {})
pinfo = MagicMock()
pinfo.exists = False
status = ItemStatus(correct=False, info={
'path_info': pinfo,
'needs_fixing': ['owner', 'mode'],
})
f.fix(status)
self.assertFalse(fix_type.called)
fix_owner.assert_called_once_with(status)
fix_mode.assert_called_once_with(status)
self.assertFalse(fix_content.called)
def test_owner(self, fix_type, fix_owner, fix_mode):
f = directories.Directory(MagicMock(), "/", {})
status = ItemStatus(correct=False, info={
'needs_fixing': ['owner'],
})
f.fix(status)
self.assertFalse(fix_type.called)
fix_owner.assert_called_once_with(status)
self.assertFalse(fix_mode.called)
def test_create(self):
bundle = MagicMock()
role = postgres_roles.PostgresRole(
bundle,
"bundlewrap",
{},
)
status = ItemStatus(correct=False, info={
'can_login': False,
'exists': False,
'needs_fixing': ['existence'],
'password_hash': "foo",
'superuser': False,
})
role.fix(status)
self.assertEqual(
bundle.node.run.call_args_list,
[
call("echo \"CREATE ROLE bundlewrap WITH LOGIN NOSUPERUSER\" | sudo -u postgres psql -nqw"),
],
def test_change_superuser(self):
bundle = MagicMock()
role = postgres_roles.PostgresRole(
bundle,
"bundlewrap",
{'superuser': True},
)
status = ItemStatus(correct=False, info={
'can_login': True,
'exists': True,
'needs_fixing': ['superuser'],
'password_hash': "foo",
'superuser': False,
})
role.fix(status)
self.assertEqual(
bundle.node.run.call_args_list,
[
call("echo \"ALTER ROLE bundlewrap WITH LOGIN SUPERUSER\""
" | sudo -u postgres psql -nqw"),