def test_add_group_member(self):
        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.add_group_member(group, user)
        self.context.session.assertPerm.assert_called_with('admin')
        self.assertEqual(len(self.inserts), 0)

        # non-existent user
        def get_user1(username):
            if username == user:
                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.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # non-existent group
        def get_user2(username):
            if username == group:
                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.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # user is group
        def get_user3(username):
            if username == group:
                return {
                    'id': gid,
                    'name': group,
                    'usertype': koji.USERTYPES['GROUP']
                }
            elif username == user:
                return {
                    'id': uid,
                    'name': username,
                    'usertype': koji.USERTYPES['GROUP']
                }

        self.context.session.assertPerm.side_effect = None
        self.get_user.side_effect = get_user3
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # group is not a group
        def get_user4(username):
            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_user4
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # strict (should raise an error if user is already in group)
        def get_user5(username):
            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_user5
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # same, but non-strict (simply returns)
        kojihub.add_group_member(group, user, strict=False)
        self.assertEqual(len(self.inserts), 0)

        # valid insert (only one insert to user_groups)
        qp = mock.MagicMock(name='qp')
        qp.return_value.executeOne.return_value = None
        self.QueryProcessor.side_effect = qp
        kojihub.add_group_member(group, user, strict=False)
        self.assertEqual(len(self.inserts), 1)
        i = self.inserts[0]
        self.assertEqual(i.table, 'user_groups')
        self.assertEqual(i.data['group_id'], gid)
        self.assertEqual(i.data['user_id'], uid)
    def test_add_group_member(self):
        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.add_group_member(group, user)
        self.context.session.assertPerm.assert_called_with('admin')
        self.assertEqual(len(self.inserts), 0)

        # non-existent user
        def get_user1(username):
            if username == user:
                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.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # non-existent group
        def get_user2(username):
            if username == group:
                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.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # user is group
        def get_user3(username):
            if username == group:
                return {'id': gid, 'name': group, 'usertype': koji.USERTYPES['GROUP']}
            elif username == user:
                return {'id': uid, 'name': username, 'usertype': koji.USERTYPES['GROUP']}
        self.context.session.assertPerm.side_effect = None
        self.get_user.side_effect = get_user3
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # group is not a group
        def get_user4(username):
            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_user4
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # strict (should raise an error if user is already in group)
        def get_user5(username):
            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_user5
        with self.assertRaises(koji.GenericError):
            kojihub.add_group_member(group, user)
        self.assertEqual(len(self.inserts), 0)

        # same, but non-strict (simply returns)
        kojihub.add_group_member(group, user, strict=False)
        self.assertEqual(len(self.inserts), 0)

        # valid insert (only one insert to user_groups)
        qp = mock.MagicMock(name='qp')
        qp.return_value.executeOne.return_value = None
        self.QueryProcessor.side_effect = qp
        kojihub.add_group_member(group, user, strict=False)
        self.assertEqual(len(self.inserts), 1)
        i = self.inserts[0]
        self.assertEqual(i.table, 'user_groups')
        self.assertEqual(i.data['group_id'], gid)
        self.assertEqual(i.data['user_id'], uid)