debian-koji/tests/test_hub/test_add_group_member.py
Yuming Zhu ca05418fb5 unittest: use unittest.mock instead of mock
because the absence of unittest.mock on python2.7, we still fallback to
mock
2024-10-23 16:35:30 +00:00

52 lines
1.7 KiB
Python

import unittest
from unittest import mock
import koji
import kojihub
class TestAddGroupMember(unittest.TestCase):
def setUp(self):
self.exports = kojihub.RootExports()
self.context = mock.patch('kojihub.kojihub.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.get_user = mock.patch('kojihub.kojihub.get_user').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()
def test_non_exist_user(self):
data = [{'id': 3,
'name': 'test-group',
'status': 0,
'usertype': 2,
'krb_principals': []},
None,
]
group = 'test-group'
username = 'test-user'
self.get_user.side_effect = data
with self.assertRaises(koji.GenericError) as cm:
self.exports.addGroupMember(group, username)
self.assertEqual("Not a user: %s" % username, str(cm.exception))
def test_non_exist_group(self):
data = [None,
{'id': 1,
'krb_principals': [],
'name': 'test-user',
'status': 0,
'usertype': 0}
]
group = 'test-group'
username = 'test-user'
self.get_user.side_effect = data
with self.assertRaises(koji.GenericError) as cm:
self.exports.addGroupMember(group, username)
self.assertEqual("Not a group: %s" % group, str(cm.exception))