def test_extract_input_channel(test_input, expected): from rasa.core.channels import RestInput input_channel = RestInput() fake_request = MagicMock() fake_request.json = test_input assert input_channel._extract_input_channel(fake_request) == expected
def test_channel_registration_with_absolute_url_prefix_overwrites_route(): from rasa.core.channels import RestInput import rasa.core input_channel = RestInput() test_route = "/absolute_route" input_channel.url_prefix = lambda: test_route app = Sanic(__name__) ignored_base_route = "/should_be_ignored" rasa.core.channels.channel.register( [input_channel], app, route="/should_be_ignored" ) # Assure that an absolute url returned by `url_prefix` overwrites route parameter # given in `register`. routes_list = utils.list_routes(app) assert routes_list["custom_webhook_RestInput.health"].startswith(test_route) assert ignored_base_route not in routes_list.get("custom_webhook_RestInput.health")
def test_register_channel_without_route(): """Check we properly connect the input channel blueprint if route is None""" from rasa.core.channels import RestInput import rasa.core input_channel = RestInput() app = Sanic(__name__) rasa.core.channels.channel.register([input_channel], app, route=None) routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.receive").startswith("/webhook")
def test_channel_inheritance(): from rasa.core.channels import RestInput from rasa.core.channels.rasa_chat import RasaChatInput rasa_input = RasaChatInput("https://example.com") s = rasa.core.run.configure_app([RestInput(), rasa_input], port=5004) routes_list = utils.list_routes(s) assert routes_list["custom_webhook_RasaChatInput.health"].startswith( "/webhooks/rasa") assert routes_list["custom_webhook_RasaChatInput.receive"].startswith( "/webhooks/rasa/webhook")
def test_register_channel_without_route(): """Check we properly connect the input channel blueprint if route is None""" from rasa.core.channels import RestInput import rasa.core # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RestInput() app = Sanic(__name__) rasa.core.channels.channel.register([input_channel], app, route=None) routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.receive").startswith( "/webhook")
def test_channel_inheritance(): from rasa.core.channels import RestInput from rasa.core.channels import RasaChatInput from rasa.core.agent import Agent from rasa.core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) rasa_input = RasaChatInput("https://example.com") s = agent.handle_channels([RestInput(), rasa_input], 5004) routes_list = utils.list_routes(s) assert routes_list.get("custom_webhook_RasaChatInput.health").startswith( "/webhooks/rasa") assert routes_list.get("custom_webhook_RasaChatInput.receive").startswith( "/webhooks/rasa/webhook")
test_sender = "test_execute_with_not_existing_action" _create_tracker_for_sender(rasa_app, test_sender) data = {"name": "ka[pa[opi[opj[oj[oija"} _, response = rasa_app.post( "/conversations/{}/execute".format(test_sender), json=data) assert response.status == 500 @pytest.mark.parametrize( "input_channels, output_channel_to_use, expected_channel", [ (None, "slack", CollectingOutputChannel), ([], None, CollectingOutputChannel), ([RestInput()], "slack", CollectingOutputChannel), ([RestInput()], "rest", CollectingOutputChannel), ([RestInput(), SlackInput("test")], "slack", SlackBot), ], ) def test_get_output_channel(input_channels: List[Text], output_channel_to_use, expected_channel: Type): request = MagicMock() app = MagicMock() app.input_channels = input_channels request.app = app request.args = {"output_channel": output_channel_to_use} actual = rasa.server._get_output_channel(request, None) assert isinstance(actual, expected_channel)
async def rasa_server_without_api() -> Sanic: app = rasa.core.run._create_app_without_api() channel.register([RestInput()], app, "/webhooks/") return app
async def rasa_non_trained_server_secured(empty_agent: Agent) -> Sanic: app = server.create_app(agent=empty_agent, auth_token="rasa", jwt_secret="core") channel.register([RestInput()], app, "/webhooks/") return app
async def rasa_nlu_server(nlu_agent: Agent) -> Sanic: app = server.create_app(agent=nlu_agent) channel.register([RestInput()], app, "/webhooks/") return app
async def rasa_non_trained_server(empty_agent: Agent) -> Sanic: app = server.create_app(agent=empty_agent) channel.register([RestInput()], app, "/webhooks/") return app
def rasa_server_secured(default_agent: Agent) -> Sanic: app = server.create_app(agent=default_agent, auth_token="rasa", jwt_secret="core") channel.register([RestInput()], app, "/webhooks/") return app
async def rasa_core_server(core_agent): app = server.create_app(agent=core_agent) channel.register([RestInput()], app, "/webhooks/") return app
async def core_server_secured(prepared_agent): app = server.create_app(prepared_agent, auth_token="rasa", jwt_secret="core") channel.register([RestInput()], app, "/webhooks/") return app
async def core_server(prepared_agent): app = server.create_app(prepared_agent) channel.register([RestInput()], app, "/webhooks/") return app
await _create_tracker_for_sender(rasa_app, test_sender) data = {INTENT_NAME_KEY: "ka[pa[opi[opj[oj[oija"} _, response = await rasa_app.post( f"/conversations/{test_sender}/trigger_intent", json=data ) assert response.status == 404 @pytest.mark.parametrize( "input_channels, output_channel_to_use, expected_channel", [ (None, "slack", CollectingOutputChannel), ([], None, CollectingOutputChannel), ([RestInput()], "slack", CollectingOutputChannel), ([RestInput()], "rest", CollectingOutputChannel), ( [RestInput(), SlackInput("test", slack_signing_secret="foobar")], "slack", SlackBot, ), ], ) def test_get_output_channel( input_channels: List[Text], output_channel_to_use: Text, expected_channel: Type ): request = MagicMock() app = MagicMock() app.input_channels = input_channels request.app = app