From e591eb2741ca815ccc842ee48720ade98035ae2d Mon Sep 17 00:00:00 2001 From: Pepe Fagoaga Date: Tue, 18 Oct 2022 12:06:20 +0200 Subject: [PATCH] Docs: Patching services not supported (#5577) --- .../docs/services/patching_other_services.rst | 52 +++++++++++++++++++ docs/index.rst | 1 + 2 files changed, 53 insertions(+) create mode 100644 docs/docs/services/patching_other_services.rst diff --git a/docs/docs/services/patching_other_services.rst b/docs/docs/services/patching_other_services.rst new file mode 100644 index 000000000..9111b7017 --- /dev/null +++ b/docs/docs/services/patching_other_services.rst @@ -0,0 +1,52 @@ +.. _patching_other_services: + + +======================= +Patching other Services +======================= + +Since ``moto`` does not support every AWS service available there is a way to patch ``boto3`` calls until they are supported. + +To do so, you need to mock the ``botocore.client.BaseClient._make_api_call`` function using `mock.patch `_: + +.. sourcecode:: python + + import boto3 + import botocore + from unittest.mock import patch + + # Original botocore _make_api_call function + orig = botocore.client.BaseClient._make_api_call + + # Mocked botocore _make_api_call function + def mock_make_api_call(self, operation_name, kwarg): + # For example for the Access Analyzer service + # As you can see the operation_name has the list_analyzers snake_case form but + # we are using the ListAnalyzers form. + # Rationale -> https://github.com/boto/botocore/blob/develop/botocore/client.py#L810:L816 + if operation_name == 'ListAnalyzers': + return { "analyzers": + [{ + "arn": "ARN", + "name": "Test Analyzer" , + "status": "Enabled", + "findings": 0, + "tags":"", + "type": "ACCOUNT", + "region": "eu-west-1" + } + ]} + # If we don't want to patch the API call + return orig(self, operation_name, kwarg) + + + def test_list_findings(): + client = boto3.client("accessanalyzer") + + with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call): + analyzers_list = client.list_analyzers() + assert len(analyzers_list["analyzers"]) == 1 + # include your assertions here + + +Note that this does not use Moto, to keep it simple, but if you use any ``moto``-decorators in addition to the patch, the call to ``orig(self, operation_name, kwarg)`` will be intercepted by Moto. diff --git a/docs/index.rst b/docs/index.rst index 0e35964aa..959f1ec5f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -49,6 +49,7 @@ Additional Resources :caption: Implemented Services docs/services/index + docs/services/patching_other_services .. toctree:: :maxdepth: 1