class Test(unittest.TestCase):

    def setUp(self):
        self._agi = Mock()
        self._cursor = Mock()
        self._args = Mock()
        self.outgoing_features = OutgoingFeatures(self._agi, self._cursor, self._args)

    def tearDown(self):
        pass

    def test_set_caller_id(self):
        
        outcall = (an_outcall()
                        .external()
                        .withCallerId('27857218')
                        .build())
        
        self.outgoing_features.outcall = outcall
        self.outgoing_features.callerid = None

        self.outgoing_features._set_caller_id()
        
        self.assertEqual(self.outgoing_features.callerid, '27857218','caller id should be set for external interconnexions')
        
    def test_do_not_set_caller_id_for_internal_outcall(self):
        
        outcall = (an_outcall()
                        .internal()
                        .withCallerId('23456')
                        )
        
        self.outgoing_features.outcall = outcall
        self.outgoing_features.callerid = None
        
        self.outgoing_features._set_caller_id()
        
        self.assertEqual(None, self.outgoing_features.callerid, 'caller id should not be set for internal interconnexions')
        

    def test_set_caller_id_no_forced_caller_id(self):

        outcall = (an_outcall()
                        .external()
                        .withCallerId('')
                        )

        self.outgoing_features.outcall = outcall
        self.outgoing_features.callerid = None

        self.outgoing_features._set_caller_id()
        self.assertEqual(self.outgoing_features.callerid, None)


    def test_retreive_outcall(self):
        outcall = Mock(objects.Outcall)
        self.outgoing_features.outcall = outcall
        self.outgoing_features.dstid = 23
        
        self.outgoing_features._retrieve_outcall()
        
        outcall.retrieve_values.assert_called_once_with(23)
class TestOutgoingFeatures(unittest.TestCase):

    def setUp(self):
        self._agi = Mock()
        self._cursor = Mock()
        self._args = Mock()
        self.outgoing_features = OutgoingFeatures(self._agi, self._cursor, self._args)

    def test_set_userfield(self):
        userfield = 'CP1234'
        user = a_user().withUserField(userfield).build()
        outcall = an_outcall().build()

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_userfield()

        self._agi.set_variable.assert_called_once_with('CHANNEL(userfield)', userfield)

    def test_set_userfield_empty(self):
        user = a_user().build()
        outcall = an_outcall().build()

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_userfield()

        assert_that(self._agi.set_variable.call_count, equal_to(0), 'Set variable call count')

    def test_set_userfield_no_user_no_error(self):
        outcall = an_outcall().build()

        self.outgoing_features.outcall = outcall

        self.outgoing_features._set_userfield()

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_outcall_internal(self, mock_set_caller_id):
        user = (a_user()
                .build())
        outcall = (an_outcall()
                   .internal()
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        self.assertFalse(mock_set_caller_id.called)

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_no_user_and_outcall_external(self, mock_set_caller_id):
        user = None
        outcall = (an_outcall()
                   .external()
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        self.assertFalse(mock_set_caller_id.called)

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_no_user_and_outcall_external_caller_id(self, mock_set_caller_id):
        user = None
        outcall = (an_outcall()
                   .external()
                   .withCallerId('27857218')
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        mock_set_caller_id.assert_called_once_with(self._agi, '27857218')

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_default_and_outcall_external(self, mock_set_caller_id):
        user = (a_user()
                .withDefaultOutCallerId()
                .build())
        outcall = (an_outcall()
                   .external()
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        self.assertFalse(mock_set_caller_id.called)

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_default_and_outcall_external_caller_id(self, mock_set_caller_id):
        user = (a_user()
                .withDefaultOutCallerId()
                .build())
        outcall = (an_outcall()
                   .external()
                   .withCallerId('27857218')
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        mock_set_caller_id.assert_called_once_with(self._agi, '27857218')

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_anonymous_and_outcall_external(self, mock_set_caller_id):
        user = (a_user()
                .withAnonymousOutCallerId()
                .build())
        outcall = (an_outcall()
                   .external()
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        expected_calls = [
            call('CALLERID(name-pres)', 'prohib'),
            call('CALLERID(num-pres)', 'prohib'),
        ]
        self.assertEqual(self._agi.set_variable.call_args_list, expected_calls)
        self.assertFalse(mock_set_caller_id.called)

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_anonymous_and_outcall_external_caller_id(self, mock_set_caller_id):
        user = (a_user()
                .withAnonymousOutCallerId()
                .build())
        outcall = (an_outcall()
                   .external()
                   .withCallerId('27857218')
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        expected_calls = [
            call('CALLERID(name-pres)', 'prohib'),
            call('CALLERID(num-pres)', 'prohib'),
        ]
        self.assertEqual(self._agi.set_variable.call_args_list, expected_calls)
        self.assertFalse(mock_set_caller_id.called)

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_custom_and_outcall_external(self, mock_set_caller_id):
        user = (a_user()
                .withCustomOutCallerId('"Custom1"')
                .build())
        outcall = (an_outcall()
                   .external()
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        mock_set_caller_id.assert_called_once_with(self._agi, '"Custom1"')

    @patch('xivo_agid.objects.CallerID.set')
    def test_set_caller_id_user_custom_and_outcall_external_caller_id(self, mock_set_caller_id):
        user = (a_user()
                .withCustomOutCallerId('"Custom1"')
                .build())
        outcall = (an_outcall()
                   .external()
                   .withCallerId('27857218')
                   .build())

        self.outgoing_features.outcall = outcall
        self.outgoing_features.user = user

        self.outgoing_features._set_caller_id()

        mock_set_caller_id.assert_called_once_with(self._agi, '"Custom1"')

    def test_retreive_outcall(self):
        outcall = Mock(objects.Outcall)
        self.outgoing_features.outcall = outcall
        self.outgoing_features.dialpattern_id = 23

        self.outgoing_features._retrieve_outcall()

        outcall.retrieve_values.assert_called_once_with(23)