diff --git a/moto/core/__init__.py b/moto/core/__init__.py index 9406a7ea0..801e675df 100644 --- a/moto/core/__init__.py +++ b/moto/core/__init__.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals -from .models import BaseModel, BaseBackend, moto_api_backend, set_initial_no_auth_action_count # flake8: noqa +from .models import BaseModel, BaseBackend, moto_api_backend # flake8: noqa +from .responses import ActionAuthenticatorMixin moto_api_backends = {"global": moto_api_backend} -set_initial_no_auth_action_count = set_initial_no_auth_action_count +set_initial_no_auth_action_count = ActionAuthenticatorMixin.set_initial_no_auth_action_count diff --git a/moto/core/models.py b/moto/core/models.py index 68f799e3c..9fe1e96bd 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -27,22 +27,6 @@ os.environ.setdefault("AWS_ACCESS_KEY_ID", "foobar_key") os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "foobar_secret") -def set_initial_no_auth_action_count(initial_no_auth_action_count): - def decorator(function): - def wrapper(*args, **kwargs): - original_initial_no_auth_action_count = settings.INITIAL_NO_AUTH_ACTION_COUNT - settings.INITIAL_NO_AUTH_ACTION_COUNT = initial_no_auth_action_count - result = function(*args, **kwargs) - settings.INITIAL_NO_AUTH_ACTION_COUNT = original_initial_no_auth_action_count - return result - - functools.update_wrapper(wrapper, function) - wrapper.__wrapped__ = function - return wrapper - - return decorator - - class BaseMockAWS(object): nested_count = 0 diff --git a/moto/core/responses.py b/moto/core/responses.py index 9f2f61fd6..5a6fdbf5b 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import functools from collections import defaultdict import datetime import json @@ -123,6 +124,25 @@ class ActionAuthenticatorMixin(object): def _authenticate_s3_action(self): self._authenticate_action(S3IAMRequest) + @staticmethod + def set_initial_no_auth_action_count(initial_no_auth_action_count): + def decorator(function): + def wrapper(*args, **kwargs): + original_initial_no_auth_action_count = settings.INITIAL_NO_AUTH_ACTION_COUNT + settings.INITIAL_NO_AUTH_ACTION_COUNT = initial_no_auth_action_count + ActionAuthenticatorMixin.request_count = 0 + try: + result = function(*args, **kwargs) + finally: + settings.INITIAL_NO_AUTH_ACTION_COUNT = original_initial_no_auth_action_count + return result + + functools.update_wrapper(wrapper, function) + wrapper.__wrapped__ = function + return wrapper + + return decorator + class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):