I'm trying to use the botocore.stub.Stubber to mock my boto3.client, and I am getting a botocore.model.OperationNotFoundError when trying to add the mocked generate_presigned_post response:
class S3FileTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.s3 = botocore.session.get_session().create_client('s3')
cls.region_name = 'eu-west-2'
@staticmethod
def _mock__get_s3(region_name):
client = boto3.client('s3', config=boto3.session.Config(signature_version='s3v4'), region_name=region_name)
stubber = Stubber(client)
stubber.add_response('generate_presigned_post', {'test':1}, {'bucket_name': 'test_bucket', 'region_name': region_name, 'object_name': 'test.csv'})
return stubber
@patch('uploader.models.s3_file.S3File._get_s3', new=_mock__get_s3)
def test_create_presigned_post(self):
response = S3File.create_presigned_post('stuart-special-testing-bucket', self.region_name, 'test.csv')
print(response)
When I run test_create_presigned_post I get the OperationNotFoundError in the add_reponse. Does anyone have any idea why this might be?
Note: S3File.create_presigned_post is taken directly from the docs with the only change being that the client is moved into a function so it can be mocked (and adding a region parameter).