# -*- coding: UTF-8 -*-

# Copyright (C) 2018 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA..

import unittest
from hamcrest import assert_that, equal_to
from mock import Mock, patch

from collections import namedtuple
from xivo_dao.alchemy.userfeatures import UserFeatures

from xivo_confd.plugins.device.update import DeviceUpdater
from xivo_confd.plugins.user.service import UserService

UserfeaturesAndAgentnumber = namedtuple('UserfeaturesAndAgentnumber', ['UserFeatures', 'agent_number'])

class TestUserService(unittest.TestCase):

    def setUp(self):
        self.user_dao = Mock()
        self.validator = Mock()
        self.notifier = Mock()
        self.device_updater = Mock(DeviceUpdater)

        self.service = UserService(self.user_dao,
                                    self.validator,
                                    self.notifier,
                                    self.device_updater)

        self.uf = UserFeatures(firstname='foo')

    @patch('xivo_dao.user_dao.get_user_with_agent_number')
    def test_get_returns_user_with_agentnumber(self, mock_get_user_wagent):
        ufa = UserfeaturesAndAgentnumber(self.uf, '8000')
        mock_get_user_wagent.return_value = ufa

        res = self.service.get_with_agent(1)

        assert_that(res, equal_to(ufa))

    @patch('xivo_dao.user_dao.get_user_with_agent_number')
    def test_get_returns_user_with_no_agentnumber(self, mock_get_user_wagent):
        ufa = UserfeaturesAndAgentnumber(self.uf, None)
        mock_get_user_wagent.return_value = ufa

        res = self.service.get_with_agent(1)

        assert_that(res, equal_to(ufa))