def setUp(self): self.request_envelope = RequestEnvelope() self.context = Context() self.system = SystemState() self.user = User() self.device = Device() self.person = Person()
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_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 test_is_request_type_not_match(): test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=SessionEndedRequest())) intent_name_wrapper = is_request_type("IntentRequest") assert not intent_name_wrapper(test_handler_input), ( "is_request_type matcher matched with the incorrect request type")
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 test_is_new_session_throws_exception_if_session_not_exists(self): test_input = HandlerInput(request_envelope=RequestEnvelope()) with self.assertRaises( TypeError, msg="is_new_session method didn't throw TypeError when an " "input request without session is passed"): is_new_session(handler_input=test_input)
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(self): """ Builds a request according to the skillSettings Returns: (RequestEnvelope) The request envelope """ envelope = RequestEnvelope(version="1.0", session=self.get_session_data(), context=self.get_context_data(), request=self.build_request()) self.modify_request_envelope(envelope) return envelope
def test_viewport_map_to_unknown_for_no_viewport(self): viewport_state = None test_request_env = RequestEnvelope( context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.UNKNOWN_VIEWPORT_PROFILE), ( "Viewport profile couldn't resolve UNKNOWN_VIEWPORT_PROFILE")
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_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 test_viewport_map_to_hub_round_small(self): viewport_state = ViewportState(shape=Shape.ROUND, dpi=float(160), current_pixel_width=float(300), current_pixel_height=float(300)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.HUB_ROUND_SMALL), ( "Viewport profile couldn't resolve HUB_ROUND_SMALL")
def test_viewport_map_to_hub_landscape_large(self): viewport_state = ViewportState(shape=Shape.RECTANGLE, dpi=float(160), current_pixel_width=float(1280), current_pixel_height=float(960)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.HUB_LANDSCAPE_LARGE), ( "Viewport profile couldn't resolve HUB_LANDSCAPE_LARGE")
def test_viewport_map_to_tv_landscape_medium(self): viewport_state = ViewportState(shape=Shape.RECTANGLE, dpi=float(320), current_pixel_width=float(960), current_pixel_height=float(600)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.TV_LANDSCAPE_MEDIUM), ( "Viewport profile couldn't resolve TV_LANDSCAPE_MEDIUM")
def test_viewport_map_to_mobile_portrait_medium(self): viewport_state = ViewportState(shape=Shape.RECTANGLE, dpi=float(240), current_pixel_width=float(600), current_pixel_height=float(960)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.MOBILE_PORTRAIT_MEDIUM), ( "Viewport profile couldn't resolve MOBILE_PORTRAIT_MEDIUM")
def test_viewport_map_to_mobile_landscape_small(self): viewport_state = ViewportState(shape=Shape.RECTANGLE, dpi=float(240), current_pixel_width=float(600), current_pixel_height=float(300)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert (viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.MOBILE_LANDSCAPE_SMALL), ( "Viewport profile couldn't resolve MOBILE_LANDSCAPE_SMALL")
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_viewport_map_to_unknown(self): viewport_state = ViewportState(shape=Shape.ROUND, dpi=float(240), current_pixel_width=float(600), current_pixel_height=float(600)) test_request_env = RequestEnvelope(context=Context( viewport=viewport_state)) assert ( viewport.get_viewport_profile(test_request_env) is viewport.ViewportProfile.UNKNOWN_VIEWPORT_PROFILE), ( "Viewport profile couldn't resolve UNKNOWN_VIEWPORT_PROFILE")
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 setUp(self, mock_cache, mock_enumerator): self.test_template_name = 'test' self.test_dir_path = '.' self.test_enumerator = mock_enumerator.return_value mock_cache.get.return_value = None self.cache = mock_cache self.test_handler_input = HandlerInput( request_envelope=RequestEnvelope(request=CanFulfillIntentRequest( locale='en-GB'))) self.test_loader = FileSystemTemplateLoader( dir_path=self.test_dir_path, enumerator=self.test_enumerator, cache=mock_cache, encoding='utf-8')
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")
def setUp(self): self.dynamodb_resource = mock.Mock() self.partition_keygen = mock.Mock() self.request_envelope = RequestEnvelope() self.expected_key_schema = [{'AttributeName': 'id', 'KeyType': 'HASH'}] self.expected_attribute_definitions = [{ 'AttributeName': 'id', 'AttributeType': 'S' }] self.expected_provision_throughput = { 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } self.attributes = {"test_key": "test_val"}
def test_skill_invoke_null_response_in_response_envelope(self): mock_request_envelope = RequestEnvelope() self.mock_handler_adapter.supports.return_value = True self.mock_handler_adapter.execute.return_value = None skill_config = self.create_skill_config() skill = Skill(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_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 test_request_verification_for_valid_request(self): with mock.patch.object(RequestVerifier, '_retrieve_and_validate_certificate_chain'): with mock.patch.object(RequestVerifier, '_valid_request_body'): self.headers[ SIGNATURE_CERT_CHAIN_URL_HEADER] = self.PREPOPULATED_CERT_URL self.headers[SIGNATURE_HEADER] = self.generate_private_key() try: RequestVerifier().verify( headers=self.headers, serialized_request_env="test", deserialized_request_env=RequestEnvelope()) except: # Should never reach here self.fail( "Request verifier couldn't verify a valid signed request" )
def test_timestamp_verification_with_expired_timestamp_skill_event(self): test_request_envelope = RequestEnvelope(request=SkillEnabledRequest( timestamp=datetime(year=2020, month=1, day=1, hour=0, minute=0, second=0, 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 test_request_verification_for_valid_request(self): test_content = "This is some test content" self.create_certificate() signature = self.sign_data(data=test_content) self.headers[ SIGNATURE_CERT_CHAIN_URL_HEADER] = self.PREPOPULATED_CERT_URL self.headers[SIGNATURE_HEADER] = base64.b64encode(signature) try: self.request_verifier.verify( headers=self.headers, serialized_request_env=test_content, deserialized_request_env=RequestEnvelope()) except: # Should never reach here self.fail( "Request verifier couldn't verify a valid signed request")
def test_request_verification_for_invalid_request(self): with mock.patch.object(RequestVerifier, '_retrieve_and_validate_certificate_chain'): with mock.patch.object(RequestVerifier, '_valid_request_body', side_effect=VerificationException( 'Request body is not valid')): self.headers[ SIGNATURE_CERT_CHAIN_URL_HEADER] = self.PREPOPULATED_CERT_URL self.headers[SIGNATURE_HEADER] = self.generate_private_key() with self.assertRaises(VerificationException) as exc: RequestVerifier().verify( headers=self.headers, serialized_request_env="test", deserialized_request_env=RequestEnvelope()) self.assertIn("Request body is not valid", str(exc.exception))
def test_request_verification_for_invalid_request(self): test_content = "This is some test content" self.create_certificate() different_private_key = self.generate_private_key() signature = self.sign_data( data=test_content, private_key=different_private_key) self.headers[ SIGNATURE_CERT_CHAIN_URL_HEADER] = self.PREPOPULATED_CERT_URL self.headers[SIGNATURE_HEADER] = base64.b64encode(signature) with self.assertRaises(VerificationException) as exc: self.request_verifier.verify( headers=self.headers, serialized_request_env=test_content, deserialized_request_env=RequestEnvelope()) self.assertIn("Request body is not valid", str(exc.exception))