как имитировать исключение клиента boto3 - PullRequest
0 голосов
/ 16 июня 2020

Я пытаюсь имитировать вызов функции в boto3.client("cognito-idp"). Мне удалось успешно имитировать нормальный поток кода, но исключения оказываются сложнее. У меня есть что-то вроде:

@patch("boto3.client")
def test_failure_cannot_create_duplicate_user_in_cognito(self, mock_client):
  client = boto3.client("cognito-idp")
  boto3_mock = MagicMock()
  boto3_mock.admin_create_user.side_effect = client.exceptions.UsernameExistsException()
  mock_client.return_value = boto3_mock
  response = handler(event, None)
  body = json.loads(response["body"])
  self.assertEqual(HTTPStatus.CONFLICT, response["statusCode"])

, а затем в обработчике:

client = boto3.client("cognito-idp")
try:
    create_user_response = client.admin_create_user(
      UserPoolId=user_pool,
      Username=user_name,
      UserAttributes=[
        {
           "Name": user_attribute,
           "Value": user_name
        },
        {
            "Name": verify,
            "Value": "True"
        },
      ],
      TemporaryPassword=temp_pass,
            DesiredDeliveryMediums=[delivery],
    )
    cognito_user_name = create_user_response["User"]["Username"]
except client.exceptions.UsernameExistsException:
   return conflict(Errors.DUPLICATE_USER)

except не обрабатывает исключение

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...