def test_rest_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ brain: services: REST: classname: programr.services.rest.GenericRESTService method: GET host: 0.0.0.0 """, ConsoleConfiguration(), ".") brain_config = yaml.get_section("brain") self.assertIsNotNone(brain_config) services_config = yaml.get_section("services", brain_config) self.assertIsNotNone(services_config) service_config = BrainServiceConfiguration("REST") service_config.load_config_section(yaml, services_config, ".") self.assertEqual("programr.services.rest.GenericRESTService", service_config.classname) self.assertEqual("GET", service_config.method) self.assertEqual("0.0.0.0", service_config.host) self.assertIsNone(service_config.port) self.assertIsNone(service_config.url)
def test_wikipedia_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ brain: services: Wikipedia: classname: programr.services.wikipediaservice.WikipediaService """, ConsoleConfiguration(), ".") brain_config = yaml.get_section("brain") self.assertIsNotNone(brain_config) services_config = yaml.get_section("services", brain_config) self.assertIsNotNone(services_config) service_config = BrainServiceConfiguration("Wikipedia") service_config.load_config_section(yaml, services_config, ".") self.assertEqual("programr.services.wikipediaservice.WikipediaService", service_config.classname) self.assertIsNone(service_config.method) self.assertIsNone(service_config.host) self.assertIsNone(service_config.port) self.assertIsNone(service_config.url)
def test_pandora_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ brain: services: Pandora: classname: programr.services.pandora.PandoraService url: http://www.pandorabots.com/pandora/talk-xml """, ConsoleConfiguration(), ".") brain_config = yaml.get_section("brain") self.assertIsNotNone(brain_config) services_config = yaml.get_section("services", brain_config) self.assertIsNotNone(services_config) service_config = BrainServiceConfiguration("Pandora") service_config.load_config_section(yaml, services_config, ".") self.assertEqual("programr.services.pandora.PandoraService", service_config.classname) self.assertEqual("http://www.pandorabots.com/pandora/talk-xml", service_config.url) self.assertIsNone(service_config.method) self.assertIsNone(service_config.host) self.assertIsNone(service_config.port)
def test_load_services(self): service_config = BrainServiceConfiguration("mock") service_config._classname = 'programrtest.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)
def load_config_section(self, configuration_file, configuration, bot_root): services = configuration_file.get_section("services", configuration) if services is not None: service_keys = configuration_file.get_keys(services) for name in service_keys: service = BrainServiceConfiguration(name) service.load_config_section(configuration_file, services, bot_root) self._services[name] = service else: YLogger.warning( self, "Config section [services] missing from Brain, no services loaded" )
def test_call_service(self): service_config = BrainServiceConfiguration("mock") service_config._classname = 'programrtest.services.test_service.MockService' brain_config = BrainConfiguration() brain_config.services._services['mock'] = service_config ServiceFactory.preload_services(brain_config.services) root = TemplateNode() node = TemplateSRAIXNode() node.service = "mock" root.append(node) node.append(TemplateWordNode("Hello")) self.assertEqual("asked", node.resolve(self._client_context))
def test_service(self): client_context = ClientContext(TestClient(), "unknown") client_context.bot = Bot(BotConfiguration()) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain service = BasicPassThroughAuthenticationService( BrainServiceConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) client_context._userid = "console" self.assertTrue(service.authenticate(client_context)) client_context._userid = "anyone" self.assertTrue(service.authenticate(client_context))
def test_rest_without_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ brain: services: REST: """, ConsoleConfiguration(), ".") brain_config = yaml.get_section("brain") self.assertIsNotNone(brain_config) services_config = yaml.get_section("services", brain_config) self.assertIsNotNone(services_config) service_config = BrainServiceConfiguration("REST") service_config.load_config_section(yaml, services_config, ".") self.assertIsNone(service_config.classname) self.assertIsNone(service_config.method) self.assertIsNone(service_config.host) self.assertIsNone(service_config.port) self.assertIsNone(service_config.url)
def test_parse_response(self): client = TestClient() client_context = client.create_client_context("testid") config = BrainServiceConfiguration("rest") config._classname = "programr.testclass" config._method = "GET" config._host = "localhost" config._port = 8080 config._url = "/api/v1.0/ask" service = ProgramrRESTService(config, api=None) self.assertEquals("Hello", service._parse_response('[{"response": {"answer": "Hello"}}]'))
def test_format_get_url(self): client = TestClient() client_context = client.create_client_context("testid") config = BrainServiceConfiguration("rest") config._classname = "programr.testclass" config._method = "GET" config._host = "localhost" config._port = 8080 config._url = "/api/v1.0/ask" service = ProgramrRESTService(config, api=None) self.assertEquals("/api/v1.0/ask?question=Hello&userid=testid", service._format_get_url("/api/v1.0/ask", client_context, "Hello"))
def test_authorisor(self): service = PassThroughAuthorisationService( BrainServiceConfiguration("authorisation")) self.assertIsNotNone(service) self.assertTrue(service.authorise("console", "sysadmin")) self.assertTrue(service.authorise("anyone", "sysadmin"))