From 1969338a8a9783bece7a29d06d145b0c9881b971 Mon Sep 17 00:00:00 2001 From: acsbendi Date: Sat, 27 Jul 2019 00:12:28 +0200 Subject: [PATCH] Fixed set_initial_no_auth_action_count not working in server mode. --- moto/core/responses.py | 30 ++++++++++++++++++++++++------ moto/core/urls.py | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index 2310dea2c..682f02a76 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -7,6 +7,7 @@ import json import logging import re import io +import requests import pytz @@ -128,15 +129,23 @@ class ActionAuthenticatorMixin(object): 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 - original_request_count = ActionAuthenticatorMixin.request_count - settings.INITIAL_NO_AUTH_ACTION_COUNT = initial_no_auth_action_count - ActionAuthenticatorMixin.request_count = 0 + if settings.TEST_SERVER_MODE: + response = requests.get("http://localhost:5000/moto-api/reset-auth") + original_initial_no_auth_action_count = response.json()['INITIAL_NO_AUTH_ACTION_COUNT'] + requests.post("http://localhost:5000/moto-api/reset-auth", data=str(initial_no_auth_action_count).encode()) + else: + original_initial_no_auth_action_count = settings.INITIAL_NO_AUTH_ACTION_COUNT + original_request_count = ActionAuthenticatorMixin.request_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 - ActionAuthenticatorMixin.request_count = original_request_count + if settings.TEST_SERVER_MODE: + requests.post("http://localhost:5000/moto-api/reset-auth", data=str(original_initial_no_auth_action_count).encode()) + else: + ActionAuthenticatorMixin.request_count = original_request_count + settings.INITIAL_NO_AUTH_ACTION_COUNT = original_initial_no_auth_action_count return result functools.update_wrapper(wrapper, function) @@ -624,6 +633,15 @@ class MotoAPIResponse(BaseResponse): return 200, {}, json.dumps({"status": "ok"}) return 400, {}, json.dumps({"Error": "Need to POST to reset Moto"}) + def reset_auth_response(self, request, full_url, headers): + if request.method == "POST": + settings.INITIAL_NO_AUTH_ACTION_COUNT = float(request.data.decode()) + ActionAuthenticatorMixin.request_count = 0 + return 200, {}, json.dumps({"status": "ok"}) + elif request.method == "GET": + return 200, {}, json.dumps({"status": "ok", "INITIAL_NO_AUTH_ACTION_COUNT": str(settings.INITIAL_NO_AUTH_ACTION_COUNT)}) + return 400, {}, json.dumps({"Error": "Need to POST to reset Moto Auth"}) + def model_data(self, request, full_url, headers): from moto.core.models import model_data diff --git a/moto/core/urls.py b/moto/core/urls.py index 4d4906d77..46025221e 100644 --- a/moto/core/urls.py +++ b/moto/core/urls.py @@ -11,4 +11,5 @@ url_paths = { '{0}/moto-api/$': response_instance.dashboard, '{0}/moto-api/data.json': response_instance.model_data, '{0}/moto-api/reset': response_instance.reset_response, + '{0}/moto-api/reset-auth': response_instance.reset_auth_response, }