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
def setUp(self): self.test_locale = "foo_locale" self.test_request_type = "LaunchRequest" self.test_dialog_state = DialogState.COMPLETED self.test_intent_name = "foo_intent" self.test_slot_name = "foo_slot" self.test_slot_value = "foo_slot_value" self.test_slot = Slot(name=self.test_slot_name, value=self.test_slot_value) self.test_api_access_token = "foo_api_access_token" self.test_access_token = "foo_account_linking_access_token" self.test_device_id = "foo_device_id" self.test_supported_interfaces = SupportedInterfaces( display=DisplayInterface(template_version="test_template", markup_version="test_markup")) self.test_new_session = False self.test_launch_request = LaunchRequest(locale=self.test_locale) self.test_intent_request = IntentRequest( dialog_state=self.test_dialog_state, intent=Intent(name=self.test_intent_name, slots={ self.test_slot_name: Slot(name=self.test_slot_name, value=self.test_slot_value) })) self.test_request_envelope = RequestEnvelope( session=Session(new=self.test_new_session), context=Context( system=SystemState(user=User( access_token=self.test_access_token), api_access_token=self.test_api_access_token, device=Device(device_id=self.test_device_id, supported_interfaces=self. test_supported_interfaces))))
def get_session_data(self): """ Create a session according to the skill_settings Returns: (Session) the session """ return Session(new=True, session_id="SessionId.{}", user=self.user, attributes={}, application=self.application)
def create_handler_input(slots: Dict[str, Slot], attributes: Dict) -> HandlerInput: request_envelope = RequestEnvelope(version="v1", session=Session(attributes=attributes), request=IntentRequest( request_id=str(uuid4()), intent=Intent(name="test intent", slots=slots))) attributes_manager = AttributesManager(request_envelope=request_envelope) handler_input = HandlerInput(request_envelope=request_envelope, attributes_manager=attributes_manager) return handler_input
def test_skill_invoke_throw_exception_when_skill_id_doesnt_match(self): skill_config = self.create_skill_config() skill_config.skill_id = "123" session = Session() mock_request_envelope = RequestEnvelope( context=Context(system=SystemState(application=Application( application_id="test"))), session=session) skill = CustomSkill(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")
def test_skill_invoke_null_response_in_response_envelope(self): session = Session() mock_request_envelope = RequestEnvelope(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 = CustomSkill(skill_configuration=skill_config) response_envelope = skill.invoke( request_envelope=mock_request_envelope, context=None) assert response_envelope.response is None, ( "Skill invocation returned incorrect response from " "request dispatch")
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")
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")