class TestSysconfdClient(TestCase):

    def setUp(self):
        self.dao = Mock()
        self.url = "http://localhost:8668"
        self.client = SysconfdClient(self.url, self.dao)
        session_init_patch = patch('xivo_confd.helpers.sysconfd_connector.requests.Session')
        session_init = session_init_patch.start()
        self.session = session_init.return_value
        self.addCleanup(session_init.stop)

    def test_delete_voicemail_storage(self):
        self.session.get.return_value = Mock(status_code=200)

        self.client.delete_voicemail("123", "default")

        url = "http://localhost:8668/delete_voicemail"
        self.session.get.assert_called_once_with(url, params={'mailbox': '123', 'context': 'default'})

    def test_move_voicemail_storage(self):
        self.session.get.return_value = Mock(status_code=200)

        self.client.move_voicemail("100", "default", "2000", "newcontext")

        url = "http://localhost:8668/move_voicemail"
        params = {'old_mailbox': '100',
                  'old_context': 'default',
                  'new_mailbox': '2000',
                  'new_context': 'newcontext'}
        self.session.get.assert_called_once_with(url, params=params)

    def test_exec_request_handlers_live_reload_enabled(self):
        self.session.post.return_value = Mock(status_code=200)
        self.dao.is_live_reload_enabled.return_value = True

        commands = {'ctibus': [],
                    'ipbx': []}

        self.client.exec_request_handlers(commands)

        call = self.session.post.call_args_list[0]
        url, body = call[0][0], call[1]['data']

        expected_url = "http://localhost:8668/exec_request_handlers"
        self.assertEquals(url, expected_url)
        self.assertEquals(json.loads(body), commands)

        self.dao.is_live_reload_enabled.assert_called_once_with()

    def test_exec_request_handlers_live_reload_disabled(self):
        self.dao.is_live_reload_enabled.return_value = False

        commands = {'ctibus': [],
                    'ipbx': []}

        self.client.exec_request_handlers(commands)

        self.assertFalse(self.session.post.called)
        self.dao.is_live_reload_enabled.assert_called_once_with()
 def setUp(self):
     self.dao = Mock()
     self.url = "http://localhost:8668"
     self.client = SysconfdClient(self.url, self.dao)
     session_init_patch = patch('xivo_confd.helpers.sysconfd_connector.requests.Session')
     session_init = session_init_patch.start()
     self.session = session_init.return_value
     self.addCleanup(session_init.stop)