pgbouncemgr/tests/test_drop_privileges.py

79 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
import unittest
from os import geteuid
from pgbouncemgr.drop_privileges import *
class DropPrivilegesTests(unittest.TestCase):
def test_givenKnownUsername_GetUid_ReturnsUid(self):
user, uid = get_uid('daemon')
self.assertEqual('daemon', user)
self.assertEqual(1, uid)
def test_givenUnknownUsername_GetUid_RaisesException(self):
with self.assertRaises(DropPrivilegesException) as context:
get_uid("nobodier_than_ever")
self.assertIn("name not found", str(context.exception))
self.assertIn("nobodier_than_ever", str(context.exception))
def test_givenKnownUserId_GetUid_ReturnsUserAndUid(self):
user, uid = get_uid(1)
self.assertEqual('daemon', user)
self.assertEqual(1, uid)
def test_givenUnknownUserId_GetUid_RaisesException(self):
with self.assertRaises(DropPrivilegesException) as context:
get_uid(22222)
self.assertIn("Invalid run user: 22222", str(context.exception))
self.assertIn("uid not found: 22222", str(context.exception))
def test_givenKownGroupName_GetGid_ReturnsGid(self):
group, gid = get_gid('daemon')
self.assertEqual('daemon', group)
self.assertEqual(gid, 1)
def test_givenUnknownGroupName_GetGid_RaisesException(self):
with self.assertRaises(DropPrivilegesException) as context:
get_uid("groupies_are_none")
self.assertIn("name not found", str(context.exception))
self.assertIn("groupies_are_none", str(context.exception))
def test_givenKnownGid_GetGid_ReturnsGid(self):
group, gid = get_gid(2)
self.assertEqual(2, gid)
self.assertEqual('bin', group)
def test_givenUnknownGid_GetGid_RaisesException(self):
with self.assertRaises(DropPrivilegesException) as context:
get_gid(33333)
self.assertIn("Invalid run group: 33333", str(context.exception))
self.assertIn("gid not found: 33333", str(context.exception))
def test_givenProblem_DropPrivileges_RaisesException(self):
if geteuid() > 0:
with self.assertRaises(DropPrivilegesException) as context:
drop_privileges(1, 0)
self.assertIn("Operation not permitted", str(context.exception))
else:
# Root is allowed to change the uid/gid, so in case the
# tests are run as the root user, use an alternative error
# scenario.
with self.assertRaises(DropPrivilegesException) as context:
drop_privileges(22222, 0)
self.assertIn("uid not found: 22222", str(context.exception))
def test_givenProblem_DropPrivileges_RaisesException(self):
if geteuid() > 0:
with self.assertRaises(DropPrivilegesException) as context:
drop_privileges(1, 0)
self.assertIn("Operation not permitted", str(context.exception))
else:
# Root is allowed to change the uid/gid, so in case the
# tests are run as the root user, use an alternative error
# scenario.
with self.assertRaises(DropPrivilegesException) as context:
drop_privileges(22222, 0)
self.assertIn("uid not found: 22222", str(context.exception))