Esempio n. 1
0
class TestIncallAssociator(unittest.TestCase):

    def setUp(self):
        self.validator = Mock()
        self.user_line_dao = Mock()
        self.incall_dao = Mock()
        self.extension_dao = Mock()
        self.line_extension = Mock(LineExtension, line_id=1, extension_id=2)
        self.associator = IncallAssociator(self.validator, self.user_line_dao, self.incall_dao, self.extension_dao)

    def test_when_associating_then_validates_association_to_user(self):
        self.associator.associate(self.line_extension)

        self.validator.validate_associated_to_user.assert_called_once_with(self.line_extension)

    def test_when_associating_then_creates_incall_with_main_user(self):
        main_user_line = self.user_line_dao.find_main_user_line.return_value = Mock(UserLine, user_id=3)
        expected_incall = Incall(destination='user',
                                 destination_id=main_user_line.user_id,
                                 extension_id=self.line_extension.extension_id)
        new_incall = self.incall_dao.create.return_value

        self.associator.associate(self.line_extension)

        self.user_line_dao.find_main_user_line.assert_called_once_with(self.line_extension.line_id)
        self.incall_dao.create.assert_called_once_with(expected_incall)
        self.extension_dao.associate_destination.assert_called_once_with(self.line_extension.extension_id,
                                                                         'incall',
                                                                         new_incall.id)

    def test_when_dissociating_then_deletes_incall(self):
        incall = self.incall_dao.find_by_extension_id.return_value = Mock(Incall)

        self.associator.dissociate(self.line_extension)

        self.incall_dao.find_by_extension_id.assert_called_once_with(self.line_extension.extension_id)
        self.incall_dao.delete.assert_called_once_with(incall)

    def test_when_dissociating_then_dissociates_extension(self):
        self.associator.dissociate(self.line_extension)

        self.extension_dao.dissociate_extension.assert_called_once_with(self.line_extension.extension_id)