예제 #1
0
class ProgramyConfiguration(object):
    def __init__(self,
                 client_configuration,
                 brain_config=None,
                 bot_config=None):
        if brain_config is None:
            self._brain_config = BrainConfiguration()
        else:
            self._brain_config = brain_config

        if bot_config is None:
            self._bot_config = BotConfiguration()
        else:
            self._bot_config = bot_config

        self._client_config = client_configuration

    @property
    def brain_configuration(self):
        return self._brain_config

    @property
    def bot_configuration(self):
        return self._bot_config

    @property
    def client_configuration(self):
        return self._client_config

    def load_config_data(self, config_file, bot_root):
        self._brain_config.load_configuration(config_file, bot_root)
        self._bot_config.load_configuration(config_file, bot_root)
        self._client_config.load_configuration(config_file, bot_root)
예제 #2
0
    def test_brain_init_with_secure_config(self):

        yaml = YamlConfigurationFile()
        self.load_os_specific_configuration(yaml, "test_secure_brain.yaml", "test_secure_brain.windows.yaml")

        brain_config = BrainConfiguration()
        brain_config.load_configuration(yaml, os.path.dirname(__file__))

        brain = Brain(brain_config)
        self.assertIsNotNone(brain)
예제 #3
0
    def test_oob_processing(self):

        yaml = YamlConfigurationFile()
        self.load_os_specific_configuration(yaml, "test_brain.yaml", "test_brain.windows.yaml")

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        self.assertEqual("", brain.process_oob(None, "console", "<oob></oob>"))
예제 #4
0
    def test_oob_processing(self):

        yaml = YamlConfigurationFile()
        yaml.load_from_file(os.path.dirname(__file__)+"/test_brain.yaml", ConsoleConfiguration(), os.path.dirname(__file__))

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        self.assertEqual("", brain.process_oob(None, "console", "<oob></oob>"))
예제 #5
0
    def test_brain_init_with_config(self):

        yaml = YamlConfigurationFile()
        self.load_os_specific_configuration(yaml, "test_brain.yaml",
                                            "test_brain.windows.yaml")

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)
        self.assertIsNotNone(brain)

        self.assertIsNotNone(brain.aiml_parser)
        self.assertIsNotNone(brain.denormals)
        self.assertIsNotNone(brain.normals)
        self.assertIsNotNone(brain.genders)
        self.assertIsNotNone(brain.persons)
        self.assertIsNotNone(brain.person2s)
        self.assertIsNotNone(brain.properties)
        self.assertIsNotNone(brain.rdf)
        self.assertIsNotNone(brain.sets)
        self.assertIsNotNone(brain.maps)
        self.assertIsNotNone(brain.preprocessors)
        self.assertIsNotNone(brain.postprocessors)
        self.assertIsNotNone(brain.authentication)
        self.assertIsNotNone(brain.authorisation)
        self.assertIsNotNone(brain.default_oob)
        self.assertIsNotNone(brain.oobs)

        if os.path.exists(brain_config.binaries.binary_filename):
            os.remove(brain_config.binaries.binary_filename)
        self.assertFalse(os.path.exists(brain_config.binaries.binary_filename))
        brain.save_binary(brain_config)
        self.assertTrue(os.path.exists(brain_config.binaries.binary_filename))
        brain.load_binary(brain_config)

        self.assertTrue(brain.authentication.authenticate("console"))
        self.assertTrue(brain.authentication.authenticate("someone"))

        self.assertTrue(brain.authorisation.authorise("console", "somthing"))
        self.assertTrue(brain.authorisation.authorise("someone", "other"))

        oob_content = ET.fromstring("<oob><something>other</something></oob>")
        self.assertEqual(
            "",
            brain.default_oob.process_out_of_bounds(None, "console",
                                                    oob_content))
        oob_content = ET.fromstring("<oob><dial>07777777777</dial></oob>")
        self.assertEqual(
            "",
            brain.oobs['dial'].process_out_of_bounds(None, "console",
                                                     oob_content))
예제 #6
0
    def test_oob_loading(self):

        yaml = YamlConfigurationFile()
        self.load_os_specific_configuration(yaml, "test_brain.yaml", "test_brain.windows.yaml")

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        self.assertIsInstance(brain.default_oob, DefaultOutOfBandProcessor)
        self.assertIsInstance(brain.oobs['dial'], DialOutOfBandProcessor)
        self.assertIsInstance(brain.oobs['email'], EmailOutOfBandProcessor)
예제 #7
0
    def test_oob_loading(self):

        yaml = YamlConfigurationFile()
        yaml.load_from_file(os.path.dirname(__file__)+"/test_brain.yaml", ConsoleConfiguration(), os.path.dirname(__file__))

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        self.assertIsInstance(brain.default_oob, DefaultOutOfBoundsProcessor)
        self.assertIsInstance(brain.oobs['dial'], DialOutOfBoundsProcessor)
        self.assertIsInstance(brain.oobs['email'], EmailOutOfBoundsProcessor)
예제 #8
0
    def __init__(self,
                 client_configuration,
                 brain_config=None,
                 bot_config=None):
        if brain_config is None:
            self._brain_config = BrainConfiguration()
        else:
            self._brain_config = brain_config

        if bot_config is None:
            self._bot_config = BotConfiguration()
        else:
            self._bot_config = bot_config

        self._client_config = client_configuration
예제 #9
0
    def test_bot_chat_loop(self):

        test_brain = Brain(BrainConfiguration())
        self.assertIsNotNone(test_brain)
        self.assertIsInstance(test_brain, Brain)

        bot = Bot(test_brain, BotConfiguration())
        self.assertIsNotNone(bot)
        self.assertIsInstance(bot, Bot)
        bot.configuration._default_response = "Sorry, I don't have an answer for that right now"

        response = bot.ask_question("testid", "hello")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Sorry, I don't have an answer for that right now")

        response = bot.ask_question("testid", "hello again")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Sorry, I don't have an answer for that right now")

        response = bot.ask_question("testid", "goodbye")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Sorry, I don't have an answer for that right now")

        conversation = bot.get_conversation("testid")
        self.assertIsNotNone(conversation)

        self.assertEqual(conversation.nth_question(3).sentence(0).text(), "hello")
        self.assertEqual(conversation.nth_question(3).sentence(0).response, "Sorry, I don't have an answer for that right now")

        self.assertEqual(conversation.nth_question(2).sentence(0).text(), "hello again")
        self.assertEqual(conversation.nth_question(2).sentence(0).response, "Sorry, I don't have an answer for that right now")

        self.assertEqual(conversation.nth_question(1).sentence(0).text(), "goodbye")
        self.assertEqual(conversation.nth_question(1).sentence(0).response, "Sorry, I don't have an answer for that right now")
예제 #10
0
 def test_authenticator_with_empty_config(self):
     testbot = Bot(Brain(BrainConfiguration()), BotConfiguration())
     service = Authenticator(BrainSecurityConfiguration("authentication"))
     self.assertIsNotNone(service)
     self.assertIsNotNone(service.configuration)
     self.assertIsNone(service.get_default_denied_srai())
     self.assertFalse(service.authenticate(testbot, "console"))
    def test_persistence(self):
        brain_config = BrainConfiguration()
        test_brain = Brain(brain_config)
        bot_config = BotConfiguration()
        bot_config.conversations._type = "file"
        bot_config.conversations._storage = BotConversationsFileStorageConfiguration(
            "test")
        bot_config.conversations._storage._dir = os.path.dirname(__file__)
        bot_config.conversations._max_histories = 3
        test_bot = Bot(test_brain, bot_config)

        clientid = "test"

        filename = bot_config.conversations._storage._dir + os.sep + clientid + ".convo"
        if os.path.exists(filename):
            os.remove(filename)
        self.assertFalse(os.path.exists(filename))

        conversation = test_bot.get_conversation(clientid)
        conversation.properties['name'] = "fred"

        test_bot.save_conversation(clientid)
        self.assertTrue(os.path.exists(filename))

        test_bot2 = Bot(test_brain, bot_config)
        conversation2 = test_bot2.get_conversation(clientid)
        self.assertIsNotNone(conversation2.property('name'))
        self.assertEqual('fred', conversation2.property('name'))

        self.assertTrue(os.path.exists(filename))
        if os.path.exists(filename):
            os.remove(filename)
        self.assertFalse(os.path.exists(filename))
 def get_brain_config(self):
     brain_config = BrainConfiguration()
     brain_config.security._authorisation = BrainSecurityConfiguration("authorisation")
     brain_config.security.authorisation._classname = "programy.security.authorise.usergroupsauthorisor.BasicUserGroupAuthorisationService"
     brain_config.security.authorisation._denied_srai = "ACCESS_DENIED"
     brain_config.security.authorisation._usergroups = "$BOT_ROOT/usergroups.yaml"
     return brain_config
예제 #13
0
    def test_authorise_exception(self):
        testbot = Bot(Brain(BrainConfiguration()), BotConfiguration())

        service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication"))
        service.should_authorised = True
        service.raise_exception = True
        self.assertFalse(service.authenticate(testbot, "unknown"))
예제 #14
0
    def test_init(self):
        testbot = Bot(Brain(BrainConfiguration()), BotConfiguration())

        service = ClientIdAuthenticationService(BrainSecurityConfiguration("authentication"))
        self.assertIsNotNone(service)
        self.assertTrue(service.authenticate(testbot, "console"))
        self.assertFalse(service.authenticate(testbot, "anyone"))
예제 #15
0
    def test_bot_init_with_config(self):
        test_brain = Brain(BrainConfiguration())
        bot_config = BotConfiguration()
        bot_config._license_keys          = None
        bot_config._bot_root              = BotConfiguration.DEFAULT_ROOT
        bot_config._prompt                = BotConfiguration.DEFAULT_PROMPT
        bot_config._default_response      = BotConfiguration.DEFAULT_RESPONSE
        bot_config._exit_response         = BotConfiguration.DEFAULT_EXIT_RESPONSE
        bot_config._initial_question      = BotConfiguration.DEFAULT_INITIAL_QUESTION
        bot_config._empty_string          = BotConfiguration.DEFAULT_EMPTY_STRING
        bot_config._override_predicates   = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES
        bot_config._max_recursion         = 10

        bot = Bot(test_brain, bot_config)

        self.assertIsNone(bot.spell_checker)
        self.assertIsNotNone(bot.brain)
        self.assertIsNotNone(bot.conversations)
        self.assertIsNotNone(bot.license_keys)
        self.assertIsNotNone(bot.prompt)
        self.assertIsNotNone(bot.default_response)
        self.assertIsNotNone(bot.exit_response)
        self.assertIsNotNone(bot.initial_question)
        self.assertTrue(bot.override_predicates)
        self.assertIsNotNone(bot.get_version_string)
예제 #16
0
    def test_log_question_and_answer(self):

        brain_config = BrainConfiguration()

        if os.name == 'posix':
            brain_config.files.aiml_files._conversation = "/tmp/tmp-conversation.txt"
        elif os.name == 'nt':
            brain_config.files.aiml_files._conversation = 'C:\Windows\Temp\\tmp-conversation.txt'
        else:
            raise Exception("Unknown os [%s]" % os.name)

        test_brain = Brain(brain_config)

        bot = Bot(test_brain, BotConfiguration())

        if os.path.exists(brain_config.files.aiml_files._conversation):
            os.remove(brain_config.files.aiml_files._conversation)

        self.assertFalse(
            os.path.exists(brain_config.files.aiml_files._conversation))

        bot.log_question_and_answer("testid", "question", "answer")

        self.assertTrue(
            os.path.exists(brain_config.files.aiml_files._conversation))
예제 #17
0
 def test_bot_init_with_license_keys(self):
     test_brain = Brain(BrainConfiguration())
     bot_config = BotConfiguration()
     bot_config._license_keys = os.path.dirname(
         __file__) + os.sep + "test_license.keys"
     bot = Bot(test_brain, bot_config)
     self.assertIsNotNone(bot)
예제 #18
0
    def test_bot_defaultresponses(self):
        test_brain = Brain(BrainConfiguration())
        bot = Bot(test_brain, BotConfiguration())
        self.assertIsNotNone(bot)

        self.assertEqual(bot.prompt, ">>> ")
        self.assertEqual(bot.default_response, "")
        self.assertEqual(bot.exit_response, "Bye!")
예제 #19
0
    def setUp(self):
        config = BrainConfiguration()
        config.files.aiml_files._errors = "/tmp/errors.txt"

        test_brain = Brain(configuration=config)
        self.parser = AIMLParser(brain=test_brain)
        self.parser.create_debug_storage(config)
        self.assertIsNotNone(self.parser)
예제 #20
0
 def setUp(self):
     self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration())
     self.bot.brain.denormals.process_splits([" dot com ", ".com"])
     self.bot.brain.denormals.process_splits([" atsign ", "@"])
     self.denormalize = DenormalizePostProcessor()
     self.punctuation = FormatPunctuationProcessor()
     self.numbers = FormatNumbersPostProcessor()
     self.multispaces = RemoveMultiSpacePostProcessor()
예제 #21
0
    def test_service(self):
        testbot = Bot(Brain(BrainConfiguration()), BotConfiguration())

        service = BasicPassThroughAuthenticationService(
            BrainServiceConfiguration("authentication"))
        self.assertIsNotNone(service)
        self.assertIsNotNone(service.configuration)
        self.assertTrue(service.authenticate(testbot, "console"))
        self.assertTrue(service.authenticate(testbot, "anyone"))
예제 #22
0
    def test_brain_init_with_secure_config(self):

        yaml = YamlConfigurationFile()
        yaml.load_from_file(os.path.dirname(__file__)+ os.sep + "test_secure_brain.yaml", ConsoleConfiguration(), os.path.dirname(__file__))

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, os.path.dirname(__file__))

        brain = Brain(brain_config)
        self.assertIsNotNone(brain)

        self.assertTrue(brain.authentication.authenticate("console"))
        self.assertFalse(brain.authentication.authenticate("someone"))

        self.assertTrue(brain.authorisation.authorise("console", "root"))
        self.assertFalse(brain.authorisation.authorise("console", "unknown"))
        with self.assertRaises(AuthorisationException):
            brain.authorisation.authorise("someone", "root")
예제 #23
0
    def test_authorise_success(self):
        testbot = Bot(Brain(BrainConfiguration()), BotConfiguration())

        service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication"))
        service.should_authorised = True
        self.assertTrue("console" in service.authorised)
        self.assertTrue(service.authenticate(testbot, "console"))
        self.assertFalse("unknown" in service.authorised)
        self.assertTrue(service.authenticate(testbot, "unknown"))
        self.assertTrue("unknown" in service.authorised)
예제 #24
0
    def test_conversation(self):
        brain_config = BrainConfiguration()
        test_brain = Brain(brain_config)
        bot_config = BotConfiguration()
        bot_config.conversations._max_histories = 3
        test_bot = Bot(test_brain, bot_config)

        conversation = Conversation("test", test_bot)
        self.assertIsNotNone(conversation)
        self.assertIsNotNone(conversation._bot)
        self.assertIsNotNone(conversation._clientid)
        self.assertEqual(conversation._clientid, "test")
        self.assertEqual(0, len(conversation._questions))
        self.assertEqual(3, conversation._max_histories)
        self.assertEqual(1, len(conversation._properties))

        with self.assertRaises(Exception):
            conversation.current_question()
        with self.assertRaises(Exception):
            conversation.previous_nth_question(0)

        question1 = Question.create_from_text(test_bot.brain.tokenizer,
                                              "Hello There")
        conversation.record_dialog(question1)
        self.assertEqual(question1, conversation.current_question())
        with self.assertRaises(Exception):
            conversation.previous_nth_question(1)

        question2 = Question.create_from_text(test_bot.brain.tokenizer,
                                              "Hello There Again")
        conversation.record_dialog(question2)
        self.assertEqual(question2, conversation.current_question())
        self.assertEqual(question1, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(3)

        question3 = Question.create_from_text(test_bot.brain.tokenizer,
                                              "Hello There Again Again")
        conversation.record_dialog(question3)
        self.assertEqual(question3, conversation.current_question())
        self.assertEqual(question2, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(4)

        # Max Histories for this test is 3
        # Therefore we should see the first question, pop of the stack

        question4 = Question.create_from_text(test_bot.brain.tokenizer,
                                              "Hello There Again Again Again")
        conversation.record_dialog(question4)
        self.assertEqual(question4, conversation.current_question())
        self.assertEqual(question3, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(5)
예제 #25
0
    def test_get_initial_question_empty_string(self):

        brain_config = BrainConfiguration()
        self.assertIsNotNone(brain_config)
        test_brain = Brain(brain_config)
        self.assertIsNotNone(test_brain)
        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)
        bot = Bot(test_brain, bot_config)
        self.assertIsNotNone(bot)

        self.assertEquals("Hello", bot.get_initial_question("testid"))
예제 #26
0
    def test_oob_stripping(self):

        yaml = YamlConfigurationFile()
        yaml.load_from_file(os.path.dirname(__file__)+"/test_brain.yaml", ConsoleConfiguration(), os.path.dirname(__file__))

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        response, oob = brain.strip_oob("<oob>command</oob>")
        self.assertEqual("", response)
        self.assertEqual("<oob>command</oob>", oob)

        response, oob = brain.strip_oob("This <oob>command</oob>")
        self.assertEqual("This ", response)
        self.assertEqual("<oob>command</oob>", oob)

        response, oob = brain.strip_oob("This <oob>command</oob> That")
        self.assertEqual("This That", response)
        self.assertEqual("<oob>command</oob>", oob)
예제 #27
0
    def test_get_exit_response_empty_string(self):

        brain_config = BrainConfiguration()
        self.assertIsNotNone(brain_config)
        test_brain = Brain(brain_config)
        self.assertIsNotNone(test_brain)
        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)
        bot = Bot(test_brain, bot_config)
        self.assertIsNotNone(bot)

        self.assertEquals("Bye!", bot.get_exit_response("testid"))
예제 #28
0
    def test_oob_stripping(self):

        yaml = YamlConfigurationFile()
        self.load_os_specific_configuration(yaml, "test_brain.yaml", "test_brain.windows.yaml")

        brain_config = BrainConfiguration()
        brain_config.load_config_section(yaml, ".")

        brain = Brain(brain_config)

        response, oob = brain.strip_oob("<oob>command</oob>")
        self.assertEqual("", response)
        self.assertEqual("<oob>command</oob>", oob)

        response, oob = brain.strip_oob("This <oob>command</oob>")
        self.assertEqual("This ", response)
        self.assertEqual("<oob>command</oob>", oob)

        response, oob = brain.strip_oob("This <oob>command</oob> That")
        self.assertEqual("This That", response)
        self.assertEqual("<oob>command</oob>", oob)
예제 #29
0
    def test_load_services(self):

        service_config = BrainServiceConfiguration("mock")
        service_config._classname = 'programytest.services.test_service.MockService'

        brain_config = BrainConfiguration()
        brain_config.services._services['mock'] = service_config

        ServiceFactory.preload_services(brain_config.services)

        self.assertIsNotNone(ServiceFactory.get_service("mock"))
        self.assertIsInstance(ServiceFactory.get_service("mock"), MockService)
예제 #30
0
    def test_max_recusion(self):

        test_brain = Brain(BrainConfiguration())
        self.assertIsNotNone(test_brain)

        bot = Bot(test_brain, BotConfiguration())
        self.assertIsNotNone(bot)
        bot.configuration._default_response = "Sorry, I don't have an answer for that right now"
        bot.configuration._max_recursion = 0

        with self.assertRaises(Exception):
            response = bot.ask_question("testid", "hello")