def test_request_dispatch_failure_throw_exception(self): self.test_app.config[VERIFY_SIGNATURE_APP_CONFIG] = False self.test_app.config[VERIFY_TIMESTAMP_APP_CONFIG] = False self.test_app.logger.setLevel(logging.CRITICAL) mocked_handler = mock.MagicMock(spec=WebserviceSkillHandler) mocked_handler.verify_request_and_dispatch.side_effect = \ AskSdkException("test") with mock.patch( "flask_ask_sdk.skill_adapter.WebserviceSkillHandler", return_value=mocked_handler): test_skill_adapter = SkillAdapter( skill=self.mock_skill, skill_id=self.skill_id, app=self.test_app) self.test_app.add_url_rule( "/", "index", test_skill_adapter.dispatch_request, methods=["POST"]) self.test_app.testing = True with self.test_app.test_client() as c: test_response = c.post( "/", data={}, content_type="application/json") self.assertEqual( test_response.status_code, 500, "SkillAdapter didn't raise internal error when " "skill invocation failed")
def test_webservice_skill_handler_dispatch_serialization_failure_throw_exc( self): self.mock_serializer.deserialize.side_effect = AskSdkException( "test deserialization exception") test_webservice_skill_handler = WebserviceSkillHandler( skill=self.mock_skill, verify_signature=False, verify_timestamp=False, verifiers=[self.mock_verifier]) with self.assertRaises(AskSdkException) as exc: test_webservice_skill_handler.verify_request_and_dispatch( http_request_headers=None, http_request_body=None) self.assertIn( "test deserialization exception", str(exc.exception), "Webservice skill handler didn't raise deserialization exception " "during skill dispatch") self.assertFalse( self.mock_verifier.verify.called, "Webservice skill handler called verifier verify when request " "deserialization failed") self.assertFalse( self.mock_skill.invoke.called, "Webservice skill handler called skill invoke when request " "verification failed")
def get_country_name(CountryId): table = boto3.resource('dynamodb').Table('AdvgCountries') country_record = table.query(KeyConditionExpression=Key('CountryId').eq(CountryId)) # dynamo is case-sensitive if country_record['Count'] == 1: return country_record['Items'][0]['Name'] else: logger.error("Cannot find country name for given id {}".format(CountryId)) raise AskSdkException("Cannot find country name for given id {}".format(CountryId))
def test_request_dispatch_failure_throw_exception(self): mocked_handler = mock.MagicMock(spec=WebserviceSkillHandler) mocked_handler.verify_request_and_dispatch.side_effect = \ AskSdkException("test") with mock.patch("django_ask_sdk.skill_adapter.WebserviceSkillHandler", return_value=mocked_handler): test_http_response = SkillAdapter.as_view(skill=self.mock_skill)( self.mock_http_request) self.assertEqual( test_http_response.status_code, 500, "SkillAdapter didn't raise internal error when " "skill invocation failed")
def dispatch_request(self): # type: () -> Response """Method that handles request verification and routing. This method can be used as a function to register on the URL rule. The request is verified through the registered list of verifiers, before invoking the request handlers. The method returns a JSON response for the Alexa service to respond to the request. :return: The skill response for the input request :rtype: flask.Response :raises: :py:class:`werkzeug.exceptions.MethodNotAllowed` if the method is invoked for other than HTTP POST request. :py:class:`werkzeug.exceptions.BadRequest` if the verification fails. :py:class:`werkzeug.exceptions.InternalServerError` for any internal exception. """ if flask_request.method != "POST": raise exceptions.MethodNotAllowed() if self._webservice_handler is None: raise AskSdkException("app not configured with skill handlers") try: content = flask_request.data.decode( verifier_constants.CHARACTER_ENCODING) response = self._webservice_handler.verify_request_and_dispatch( http_request_headers=typing.cast(typing.Dict[str, typing.Any], flask_request.headers), http_request_body=content) return jsonify(response) except VerificationException: current_app.logger.error("Request verification failed", exc_info=True) raise exceptions.BadRequest( description="Incoming request failed verification") except AskSdkException: current_app.logger.error("Skill dispatch exception", exc_info=True) raise exceptions.InternalServerError( description="Exception occurred during skill dispatch")