def test_invoke_serializes_response_if_present(self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_response = ApiClientResponse() fake_response.body = "test_body" fake_response.status_code = 200 fake_response_type = "test_response_type" fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response mocked_serializer.deserialize.return_value = "deserialized_payload" fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) response = fake_base_service_client.invoke( method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=fake_response_type) self.assertEqual( response.body, "deserialized_payload", "Response from api client not deserialized by base service client") mocked_serializer.deserialize.assert_called_with(payload=fake_response.body, obj_type=fake_response_type), \ "Base service client called deserialize on Response from api client with wrong values"
def test_invoke_api_client_throws_exception(self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.side_effect = Exception("test exception") fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) with raises(ServiceException) as exc: fake_base_service_client.invoke(method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=None) self.assertEqual(str(exc.value), "Call to service failed: test exception", \ "Service exception not raised during base service client invoke when api client invoke raises") self.assertEqual(exc.value.status_code, 500, ( "Service exception raised during base service client invoke has different status code than expected, " "when api client invoke raises"))
def service_client_null_serializer(): mocked_api_client = MockedApiClient() api_configuration = ApiConfiguration(serializer=None, api_client=mocked_api_client, authorization_value="test_token", api_endpoint="test_endpoint") return BaseServiceClient(api_configuration=api_configuration)
def client(self): # type: () -> SkillManagementServiceClient """Creates the smapi client object using AuthenticationConfiguration and ApiConfiguration registered values. :return: A smapi object that can be used for making SMAPI method invocations. :rtype: :py:class:`ask_smapi_model.services.skill_management.SkillManagementServiceClient` """ if self.serializer is None: self.serializer = DefaultSerializer() if self.api_client is None: self.api_client = DefaultApiClient() if self.api_endpoint is None: self.api_endpoint = DEFAULT_API_ENDPOINT api_configuration = ApiConfiguration(serializer=self.serializer, api_client=self.api_client, api_endpoint=self.api_endpoint) authentication_configuration = AuthenticationConfiguration( client_id=self.client_id, client_secret=self.client_secret, refresh_token=self.refresh_token) return SkillManagementServiceClient( api_configuration=api_configuration, authentication_configuration=authentication_configuration)
def service_client_null_api_client(): mocked_serializer = mock.MagicMock(spec=Serializer) api_configuration = ApiConfiguration(serializer=mocked_serializer, api_client=None, authorization_value="test_token", api_endpoint="test_endpoint") return BaseServiceClient(api_configuration=api_configuration)
def test_get_access_token_api_call_fails_throws_exception(self): mocked_serializer = mock.MagicMock(spec=Serializer) mocked_serializer.serialize.return_value = "access token request" test_scope = "test" fake_response = ApiClientResponse() fake_response.status_code = 400 fake_response.body = "test_body" fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response mocked_serializer.deserialize.return_value = "test error body" test_lwa_client = LwaClient( api_configuration=ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client), authentication_configuration=AuthenticationConfiguration()) with raises(ServiceException) as exc: _actual_token_value = test_lwa_client.get_access_token_for_scope( scope=test_scope) self.assertIn( "Bad Request", str(exc.value), ("LWA Client get access token threw unknown exception when " "the LWA API call failed with an known exception"))
def test_invoke_throw_exception_if_no_response_status_code(self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_response = ApiClientResponse() fake_response.status_code = None fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) with raises(ServiceException) as exc: fake_base_service_client.invoke(method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=None) self.assertEqual(str(exc.value), "Invalid Response, no status code", ( "Service exception raised during base service client invoke, when response is unsuccessful, " "has no status code"))
def test_invoke_throw_exception_if_no_definitions_provided_for_unsuccessful_response( self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_response = ApiClientResponse() fake_response.status_code = 450 fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) with raises(ServiceException) as exc: fake_base_service_client.invoke(method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=None) self.assertEqual(str(exc.value), "Unknown error", ( "Service exception not raised during base service client invoke when response is unsuccessful and " "no response definitions provided")) self.assertEqual(exc.value.status_code, 450, ( "Service exception raised during base service client invoke has different status code than expected, " "when response is unsuccessful and no response definitions provided" ))
def empty_response(): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) api_configuration = ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client, authorization_value="test_token", api_endpoint="test_endpoint") return BaseServiceClient(api_configuration=api_configuration)
def test_lwa_client_init_no_auth_config_throw_exception(self): with raises(ValueError) as exc: lwa_client = LwaClient(api_configuration=ApiConfiguration(), authentication_configuration=None) self.assertEqual( str(exc.value), ("authentication_configuration must be provided"), ("LwaClient Initialization didn't throw exception if a null " "Authentication Configuration is passed"))
def test_get_access_token_cache_miss_api_call_success(self): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) mocked_serializer.serialize.return_value = "access token request" local_now = datetime.datetime.now(tz.tzutc()) test_scope = "test" test_client_id = "test_client_id" test_client_secret = "test_client_secret" expected_token_value = "test_token" expected_headers = [('Content-type', 'application/x-www-form-urlencoded')] expected_request_method = "POST" expected_request_url = "https://api.amazon.com/auth/O2/token" expected_request_body = ( "grant_type=client_credentials&client_id={}&client_secret={}" "&scope={}").format(test_client_id, test_client_secret, test_scope) mocked_serializer.deserialize.return_value = AccessTokenResponse( access_token=expected_token_value, expires_in=10, scope=test_scope) test_lwa_client = LwaClient( api_configuration=ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client), authentication_configuration=AuthenticationConfiguration( client_id=test_client_id, client_secret=test_client_secret)) with mock.patch( "ask_sdk_model_runtime.lwa.lwa_client.datetime") as mock_date: mock_date.now.return_value = local_now actual_token_value = test_lwa_client.get_access_token_for_scope( scope=test_scope) self.assertEqual(expected_token_value, actual_token_value, ( "LWA Client get access token call didn't retrieve scoped access token" )) actual_token_expiry = test_lwa_client._scoped_token_cache[ test_scope].expiry self.assertEqual( (local_now + datetime.timedelta(seconds=10)), actual_token_expiry, ("LWA Client get access token call cached wrong access token " "expiry date")) self.assertEqual( mocked_api_client.request.headers, expected_headers, ("LWA Client get access token called API with wrong headers")) self.assertEqual( mocked_api_client.request.method, expected_request_method, ("LWA Client get access token called API with wrong HTTP method")) self.assertEqual( mocked_api_client.request.url, expected_request_url, ("LWA Client get access token called API with wrong HTTP URL")) mocked_serializer.serialize.assert_called_with(expected_request_body)
def test_get_access_token_for_null_scope_throw_exception(self): test_lwa_client = LwaClient( api_configuration=ApiConfiguration(), authentication_configuration=AuthenticationConfiguration()) with raises(ValueError) as exc: test_lwa_client.get_access_token_for_scope(scope=None) self.assertEqual( str(exc.value), "scope must be provided", ("LWA Client get access token call didn't throw exception if a " "null scope is passed"))
def test_invoke_throw_exception_with_matched_definition_for_unsuccessful_response( self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_response = ApiClientResponse() fake_response.status_code = 450 fake_response.body = "test_body" fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response mocked_serializer.deserialize.return_value = "deserialized_error_body" fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) fake_response_definition_1 = ServiceClientResponse( response_type="test_definition_1", status_code=450, message="test exception with definition 1") fake_response_definition_2 = ServiceClientResponse( response_type="test_definition_2", status_code=475, message="test exception with definition 2") fake_response_definitions = [ fake_response_definition_2, fake_response_definition_1 ] with raises(ServiceException) as exc: fake_base_service_client.invoke( method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=fake_response_definitions, body=None, response_type=None) self.assertEqual(str(exc.value), "test exception with definition 1", ( "Incorrect service exception raised during base service client invoke when response is unsuccessful and " "matching response definitions provided")) mocked_serializer.deserialize.assert_called_with( payload=fake_response.body, obj_type="test_definition_1") self.assertEqual(str(exc.value.body), "deserialized_error_body", ( "Service exception raised during base service client invoke, when response is unsuccessful, " "has different body that expected response"))
def test_invoke_return_api_response_with_none_body(self): mocked_serializer = mock.MagicMock(spec=Serializer) fake_response = ApiClientResponse() fake_response.body = "" fake_response.status_code = 204 fake_response.headers = "test_headers" fake_response_type = "test_response_type" fake_api_client = mock.MagicMock(spec=ApiClient) fake_api_client.invoke.return_value = fake_response fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=fake_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) response = fake_base_service_client.invoke( method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=fake_response_type) self.assertIsNone(response.body, ( "Base service client returns invalid api response when status code is 204" )) self.assertEqual(response.headers, "test_headers", ( "Base service client return invalid api response with null headers" )) self.assertEqual(response.status_code, 204, ( "Base service client return invalid api response with null status_code" )) self.assertIsNot(mocked_serializer.deserialize.called, ( "Base service client invoke method deserialized no content response body" ))
def test_get_access_token_retrieve_from_cache(self): test_lwa_client = LwaClient( api_configuration=ApiConfiguration(), authentication_configuration=AuthenticationConfiguration()) test_scope = "test" expected_token_value = "test_token" test_token_expiry = (datetime.datetime.now(tz.tzutc()) + datetime.timedelta(hours=1)) test_access_token = AccessToken(token=expected_token_value, expiry=test_token_expiry) test_lwa_client._scoped_token_cache[test_scope] = test_access_token actual_token_value = test_lwa_client.get_access_token_for_scope( scope=test_scope) self.assertEqual( expected_token_value, actual_token_value, ("LWA Client get access token call didn't retrieve unexpired " "scoped access token from cache when available"))
def test_get_access_token_for_null_lwa_response_throw_exception(self): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) test_scope = "test" test_lwa_client = LwaClient( api_configuration=ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client), authentication_configuration=AuthenticationConfiguration()) with mock.patch.object(test_lwa_client, "_generate_access_token", return_value=None): with raises(ValueError) as exc: test_lwa_client.get_access_token_for_scope(scope=test_scope) self.assertEqual(str(exc.value), "Invalid response from LWA Client " \ "generate access token call", ( "LWA Client get access token call didn't throw exception if a " "generate access token returns None "))
def test_invoke_serializes_body_not_run_for_null_body(self): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) fake_base_service_client.invoke(method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=None, response_type=None) self.assertNotEqual(mocked_serializer.serialize.called, "Invoke called serializer for null body")
def test_get_access_token_for_default_endpoint_api_success(self): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) mocked_serializer.serialize.return_value = "access token request" local_now = datetime.datetime.now(tz.tzutc()) test_scope = "test" test_client_id = "test_client_id" test_client_secret = "test_client_secret" test_endpoint = "https://foo.com" expected_token_value = "test_token" expected_request_url = "{}/auth/O2/token".format(test_endpoint) mocked_serializer.deserialize.return_value = AccessTokenResponse( access_token=expected_token_value, expires_in=10, scope=test_scope) test_lwa_client = LwaClient( api_configuration=ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client, api_endpoint=test_endpoint), authentication_configuration=AuthenticationConfiguration( client_id=test_client_id, client_secret=test_client_secret)) with mock.patch( "ask_sdk_model_runtime.lwa.lwa_client.datetime") as mock_date: mock_date.now.return_value = local_now actual_token_value = test_lwa_client.get_access_token_for_scope( scope=test_scope) self.assertEqual( expected_token_value, actual_token_value, ("LWA Client get access token call didn't retrieve scoped access " "token when a custom endpoint is passed")) self.assertEqual( mocked_api_client.request.url, expected_request_url, ("LWA Client get access token called API with wrong HTTP URL, " "when a custom endpoint is passed"))
def test_invoke_serializes_body_when_passed(self): mocked_api_client = MockedApiClient() mocked_serializer = mock.MagicMock(spec=Serializer) mocked_serializer.serialize.return_value = "serialized_payload" fake_body = mock.Mock() fake_api_config = ApiConfiguration(serializer=mocked_serializer, api_client=mocked_api_client, authorization_value="test_token", api_endpoint="test_endpoint") fake_base_service_client = BaseServiceClient( api_configuration=fake_api_config) fake_base_service_client.invoke(method="GET", endpoint="http://test.com", path="", query_params=[], header_params=[], path_params={}, response_definitions=[], body=fake_body, response_type=None) mocked_serializer.serialize.assert_called_with(fake_body)