def create_request_envelope(session_attributes: Dict[str, Any],
                            request: Request) -> RequestEnvelope:
    """Creates a request envelope."""
    application = Application(application_id=APPLICATION_ID)
    user = User(user_id=USER_ID,
                access_token=None,
                permissions=Permissions(consent_token=None))
    request_envelope = RequestEnvelope(
        version="1.0",
        session=Session(new=False,
                        session_id=SESSION_ID,
                        user=user,
                        attributes=session_attributes,
                        application=application),
        context=Context(system=SystemState(
            application=application,
            user=user,
            device=Device(device_id=DEVICE_ID,
                          supported_interfaces=SupportedInterfaces(
                              audio_player=AudioPlayerInterface(),
                              display=None,
                              video_app=None)),
            api_endpoint=API_ENDPOINT,
            api_access_token=None)),
        request=request)

    return request_envelope
Beispiel #2
0
 def __init__(self, skill_settings):
     """
     RequestBuilder
     Args:
         skill_settings(SkillSettings):  The settings of the tested Skill
     """
     self.skill_settings = skill_settings
     self.user = User(self.skill_settings.user_id)
     self.application = Application(self.skill_settings.app_id)
     self.request_id = "EdwRequestId.{}".format(uuid.uuid4())
Beispiel #3
0
    def test_skill_invoke_throw_exception_when_skill_id_doesnt_match(self):
        skill_config = self.create_skill_config()
        skill_config.skill_id = "123"
        mock_request_envelope = RequestEnvelope(context=Context(
            system=SystemState(application=Application(
                application_id="test"))))
        skill = Skill(skill_configuration=skill_config)

        with self.assertRaises(AskSdkException) as exc:
            skill.invoke(request_envelope=mock_request_envelope, context=None)

        assert "Skill ID Verification failed" in str(exc.exception), (
            "Skill invocation didn't throw verification error when Skill ID "
            "doesn't match Application ID")
Beispiel #4
0
    def test_skill_invoke_set_service_client_factory_if_api_client_provided(
            self):
        session = Session()
        mock_request_envelope = RequestEnvelope(context=Context(
            system=SystemState(application=Application(application_id="test"),
                               api_access_token="test_api_access_token",
                               api_endpoint="test_api_endpoint")),
                                                session=session)

        self.mock_handler_adapter.supports.return_value = True
        self.mock_handler_adapter.execute.return_value = None

        skill_config = self.create_skill_config()
        skill_config.skill_id = "test"
        skill_config.api_client = "test_api_client"
        skill = CustomSkill(skill_configuration=skill_config)

        skill.invoke(request_envelope=mock_request_envelope, context=None)

        called_args, called_kwargs = self.mock_request_mapper.get_request_handler_chain.call_args
        test_handler_input = called_args[0]

        assert test_handler_input.service_client_factory is not None, (
            "Service Client Factory not initialized when api client is "
            "provided in skill configuration, "
            "during skill invocation")
        assert test_handler_input.service_client_factory.api_configuration.api_client == "test_api_client", (
            "Api Client value in Service Client Factory different than the "
            "one provided in skill configuration")
        assert test_handler_input.service_client_factory.api_configuration.authorization_value == \
            "test_api_access_token", ("Api Access Token value in Service "
                                      "Client Factory different than the one "
                                      "present "
                                      "in request envelope")
        assert test_handler_input.service_client_factory.api_configuration.api_endpoint == \
            "test_api_endpoint", ("Api Endpoint value in Service Client "
                                  "Factory different than the one present "
                                  "in request envelope")
Beispiel #5
0
    def test_skill_invoke_pass_session_attributes_to_response_envelope(self):
        mock_request_envelope = RequestEnvelope(
            context=Context(system=SystemState(application=Application(
                application_id="test"))),
            session=Session(attributes={"foo": "bar"}))

        self.mock_handler_adapter.supports.return_value = True
        self.mock_handler_adapter.execute.return_value = None

        skill_config = self.create_skill_config()
        skill_config.skill_id = "test"
        skill = CustomSkill(skill_configuration=skill_config)

        response_envelope = skill.invoke(
            request_envelope=mock_request_envelope, context=None)

        assert response_envelope.session_attributes is not None, (
            "Session Attributes are not propagated from Request Envelope "
            "session to Response Envelope, "
            "during skill invocation")
        assert response_envelope.session_attributes["foo"] == "bar", (
            "Invalid Session Attributes propagated from Request Envelope "
            "session to Response Envelope, "
            "during skill invocation")