def test_drop_group_member(self, get_group_members): group, gid = 'test_group', 1 user, uid = 'username', 2 # no permission self.context.session.assertPerm.side_effect = koji.ActionNotAllowed with self.assertRaises(koji.ActionNotAllowed): kojihub.drop_group_member(group, user) self.context.session.assertPerm.assert_called_with('admin') self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # non-existent user def get_user1(username, strict=True): if username == user: if strict: raise koji.GenericError else: return None else: return { 'id': gid, 'name': username, 'usertype': koji.USERTYPES['GROUP'] } self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user1 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # non-existent group def get_user2(username, strict=True): if username == group: if strict: raise koji.GenericError else: return None else: return { 'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL'] } self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user2 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # group is not a group def get_user3(username, strict=True): if username == group: return { 'id': gid, 'name': group, 'usertype': koji.USERTYPES['NORMAL'] } elif username == user: return { 'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL'] } self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user3 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # user not in group def get_user4(username, strict=True): if username == group: return { 'id': gid, 'name': group, 'usertype': koji.USERTYPES['GROUP'] } elif username == user: return { 'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL'] } self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user4 get_group_members.return_value = [] with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # valid drop self.get_user.side_effect = get_user4 get_group_members.return_value = [{'id': uid, 'name': user}] kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 1) u = self.updates[0] self.assertEqual(u.table, 'user_groups') self.assertEqual(u.values['user_id'], uid) self.assertEqual(u.values['group_id'], gid)
def test_drop_group_member(self, get_group_members): group, gid = 'test_group', 1 user, uid = 'username', 2 # no permission self.context.session.assertPerm.side_effect = koji.ActionNotAllowed with self.assertRaises(koji.ActionNotAllowed): kojihub.drop_group_member(group, user) self.context.session.assertPerm.assert_called_with('admin') self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # non-existent user def get_user1(username, strict=True): if username == user: if strict: raise koji.GenericError else: return None else: return {'id': gid, 'name': username, 'usertype': koji.USERTYPES['GROUP']} self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user1 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # non-existent group def get_user2(username, strict=True): if username == group: if strict: raise koji.GenericError else: return None else: return {'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL']} self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user2 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # group is not a group def get_user3(username, strict=True): if username == group: return {'id': gid, 'name': group, 'usertype': koji.USERTYPES['NORMAL']} elif username == user: return {'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL']} self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user3 with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # user not in group def get_user4(username, strict=True): if username == group: return {'id': gid, 'name': group, 'usertype': koji.USERTYPES['GROUP']} elif username == user: return {'id': uid, 'name': username, 'usertype': koji.USERTYPES['NORMAL']} self.context.session.assertPerm.side_effect = None self.get_user.side_effect = get_user4 get_group_members.return_value = [] with self.assertRaises(koji.GenericError): kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 0) # valid drop self.get_user.side_effect = get_user4 get_group_members.return_value = [{'id': uid, 'name': user}] kojihub.drop_group_member(group, user) self.assertEqual(len(self.inserts), 0) self.assertEqual(len(self.updates), 1) u = self.updates[0] self.assertEqual(u.table, 'user_groups') self.assertEqual(u.values['user_id'], uid) self.assertEqual(u.values['group_id'], gid)