def test_is_request_type_match(): test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=IntentRequest())) request_type_wrapper = is_request_type("IntentRequest") assert request_type_wrapper(test_handler_input), ( "is_request_type matcher didn't match with the correct request type")
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 test_is_intent_name_not_match(): test_intent_name = "TestIntent" test_handler_input = HandlerInput(request_envelope=RequestEnvelope( request=IntentRequest(intent=Intent(name=test_intent_name)))) intent_name_wrapper = is_intent_name("TestIntent1") assert not intent_name_wrapper( test_handler_input), "is_intent_name matcher matched with the " \ "incorrect intent name"
def test_is_intent_name_match(): test_intent_name = "TestIntent" test_handler_input = HandlerInput(request_envelope=RequestEnvelope( request=IntentRequest(intent=Intent(name=test_intent_name)))) intent_name_wrapper = is_intent_name(test_intent_name) assert intent_name_wrapper( test_handler_input), "is_intent_name matcher didn't match with the " \ "correct intent name"
def build_request(self): """ Builds the request Returns: (IntentRequest) the build IntentRequest """ return IntentRequest(request_id=self.request_id, timestamp=datetime.datetime.now(), locale=self.skill_settings.locale, intent=Intent(self.intent_name, self.slots, IntentConfirmationStatus.NONE))
def test_is_intent_not_match_canfulfill_intent(): test_intent_name = "TestIntent" test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=IntentRequest( intent=Intent(name=test_intent_name)))) canfulfill_intent_name_wrapper = is_canfulfill_intent_name(test_intent_name) assert not canfulfill_intent_name_wrapper( test_handler_input), "is_canfulfill_intent_name matcher matched with the " \ "incorrect request type"
def test_timestamp_verification_with_expired_timestamp(self): test_request_envelope = RequestEnvelope(request=IntentRequest( timestamp=datetime(year=2019, month=1, day=1, tzinfo=tzutc()))) self.timestamp_verifier = TimestampVerifier() with self.assertRaises(VerificationException) as exc: self.timestamp_verifier.verify( headers={}, serialized_request_env="", deserialized_request_env=test_request_envelope) self.assertIn("Timestamp verification failed", str(exc.exception))
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_timestamp_verification_with_valid_timestamp(self): test_request_envelope = RequestEnvelope(request=IntentRequest( timestamp=datetime.now(tz=tzlocal()))) self.timestamp_verifier = TimestampVerifier() try: self.timestamp_verifier.verify( headers={}, serialized_request_env="", deserialized_request_env=test_request_envelope) except: # Should never reach here raise self.fail( "Timestamp verification failed for a valid input request")
def test_timestamp_verification_with_valid_future_server_timestamp(self): valid_tolerance = int(DEFAULT_TIMESTAMP_TOLERANCE_IN_MILLIS / 2 / 1000) valid_future_datetime = datetime.now( tzutc()) + timedelta(seconds=valid_tolerance) test_request_envelope = RequestEnvelope(request=IntentRequest( timestamp=valid_future_datetime)) self.timestamp_verifier = TimestampVerifier() try: self.timestamp_verifier.verify( headers={}, serialized_request_env="", deserialized_request_env=test_request_envelope) except: # Should never reach here raise self.fail( "Timestamp verification failed for a valid input request")
def create_intent_request(round_index: int, user_utterance: str) -> IntentRequest: """Creates an intent request.""" intent_request = IntentRequest( request_id='{}.{}'.format(REQUEST_ID_BASE, round_index), timestamp=datetime.datetime.utcnow(), locale='en-US', dialog_state=None, intent=Intent( name='ConverseIntent', slots=dict(Text=Slot( name='Text', value=user_utterance, confirmation_status=SlotConfirmationStatus.NONE, resolutions=Resolutions(resolutions_per_authority=[ Resolution( authority='{}.TEXT'.format(RESOLUTION_AUTHORITY_BASE), status=Status(code=StatusCode.ER_SUCCESS_NO_MATCH)) ]))), confirmation_status=IntentConfirmationStatus.NONE)) return intent_request