def test_find_exten_settings_when_line_disabled(self):
        user_row = self.add_user()
        line_row = self.add_line(commented=1)
        extension_row = self.add_extension(exten='13', context='default')
        self.add_user_line(user_id=user_row.id,
                           extension_id=extension_row.id,
                           line_id=line_row.id)

        result = asterisk_conf_dao.find_exten_settings('default')

        assert_that(result, contains())
    def test_find_exten_settings_when_line_enabled(self):
        user_row = self.add_user()
        line_row = self.add_line()
        extension_row = self.add_extension(exten='12', context='default')
        self.add_user_line(user_id=user_row.id,
                           extension_id=extension_row.id,
                           line_id=line_row.id)

        expected_result = [
            {'exten': u'12',
             'commented': 0,
             'context': u'default',
             'typeval': u'',
             'type': 'user',
             'id': extension_row.id}
        ]

        result = asterisk_conf_dao.find_exten_settings('default')

        assert_that(result, contains(*expected_result))
    def test_find_exten_settings_multiple_extensions(self):
        exten1 = self.add_extension(exten='12', context='default')
        exten2 = self.add_extension(exten='23', context='default')
        self.add_extension(exten='41', context='toto')

        expected_result = [
            {'exten': u'12',
             'commented': 0,
             'context': u'default',
             'typeval': u'',
             'type': 'user',
             'id': exten1.id},
            {'exten': u'23',
             'commented': 0,
             'context': u'default',
             'typeval': u'',
             'type': 'user',
             'id': exten2.id}
        ]

        extensions = asterisk_conf_dao.find_exten_settings('default')

        assert_that(extensions, contains_inanyorder(*expected_result))
    def generate(self, output):
        options = output

        if self.contextsconf is not None:
            # load context templates
            conf = ConfigParser.RawConfigParser(
                dict_type=CustomConfigParserStorage)
            try:
                conf.read([self.contextsconf])
            except ConfigParser.DuplicateSectionError:
                raise ValueError("%s has conflicting section names" %
                                 self.contextsconf)
            if not conf.has_section('template'):
                raise ValueError("Template section doesn't exist in %s" %
                                 self.contextsconf)

        # hints & features (init)
        self._generate_global_hints(output)
        extenfeature_names = (
            'bsfilter',
            'fwdbusy',
            'fwdrna',
            'fwdunc',
            'phoneprogfunckey',
            'vmusermsg',
        )
        extenfeatures = asterisk_conf_dao.find_extenfeatures_settings(
            features=extenfeature_names)
        xfeatures = {
            extenfeature.typeval: {
                'exten': extenfeature.exten,
                'commented': extenfeature.commented
            }
            for extenfeature in extenfeatures
        }

        # foreach active context
        for ctx in asterisk_conf_dao.find_context_settings():
            # context name preceded with '!' is ignored
            if conf and conf.has_section('!%s' % ctx['name']):
                continue

            print >> options, "\n[%s]" % ctx['name']

            if conf.has_section(ctx['name']):
                section = ctx['name']
            elif conf.has_section('type:%s' % ctx['contexttype']):
                section = 'type:%s' % ctx['contexttype']
            else:
                section = 'template'

            tmpl = []
            for option_name, option_value in conf.items(section):
                if option_name == 'objtpl':
                    tmpl.append(option_value)
                    continue
                print >> options, "%s = %s" % (option_name,
                                               option_value.replace(
                                                   '%%CONTEXT%%', ctx['name']))

            # context includes
            for row in asterisk_conf_dao.find_contextincludes_settings(
                    ctx['name']):
                print >> options, "include = %s" % row['include']
            print >> options

            # objects extensions (user, group, ...)
            for exten_row in asterisk_conf_dao.find_exten_settings(
                    ctx['name']):
                exten_generator = extension_generators.get(
                    exten_row['type'], GenericExtensionGenerator)
                exten = exten_generator(exten_row).generate()
                self.gen_dialplan_from_template(tmpl, exten, options)

            self._generate_hints(ctx['name'], options)

        print >> options, self._extensions_features(conf, xfeatures)
        self._generate_ivr(output)

        return options.getvalue()