# -*- coding: utf-8 -*-

# Copyright (C) 2013-2014 Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

import unittest

from hamcrest import assert_that
from hamcrest.core import equal_to
from mock import Mock, call, patch
from xivo_agid.modules.user_set_call_rights import get_user_id


class TestUserSetCallRights(unittest.TestCase):

    def setUp(self):
        self._agi = Mock()

    @patch('xivo_dao.user_line_dao.get_id_from_peer')
    def test_get_user_id_from_channel_interface(self, mock_get_id_from_peer):
        self._agi.get_variable.side_effect = \
            lambda x: {'XIVO_USERID': '', 'XIVO_INTERFACE': 'SIP/12345'}.get(x, '')

        mock_get_id_from_peer.return_value = '1122334455'

        self.assertEqual(get_user_id(self._agi), '1122334455')
        mock_get_id_from_peer.assert_called_once_with('SIP/12345')

    @patch('xivo_dao.user_line_dao.get_id_from_peer')
    def test_get_user_id_from_channel_interface_empty_var(self, mock_get_id_from_peer):
        self._agi.get_variable.side_effect = \
            lambda x: {'XIVO_USERID': '', 'XIVO_INTERFACE': ''}.get(x, '')

        mock_get_id_from_peer.return_value = ''

        self.assertEqual(get_user_id(self._agi), '')
        mock_get_id_from_peer.assert_called_once_with('')
