From f78345a4fb0613fe47c7faee0d6a93daf32333a5 Mon Sep 17 00:00:00 2001 From: faul_sname Date: Sat, 31 Dec 2022 02:06:17 -0800 Subject: [PATCH] [themotte/rDrama#451] Decorator when we do not want to rate limit in tests --- files/tests/util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/files/tests/util.py b/files/tests/util.py index 3caf08c18..938b73c54 100644 --- a/files/tests/util.py +++ b/files/tests/util.py @@ -1,5 +1,6 @@ from bs4 import BeautifulSoup +import functools import json import random import re @@ -14,6 +15,25 @@ def formkey_from(text): def generate_text(): return ''.join(random.choices(string.ascii_lowercase, k=40)) +# Annotation to put around tests that don't care about rate limits +def no_rate_limit(test_function): + @functools.wraps(test_function) + def wrapped(*args, **kwargs): + from files.__main__ import app, limiter + was_enabled = limiter.enabled + app.config['RATE_LIMITER_ENABLED'] = False + limiter.enabled = False + try: + result = test_function(*args, **kwargs) + app.config['RATE_LIMITER_ENABLED'] = was_enabled + limiter.enabled = was_enabled + return result + except Exception as e: + app.config['RATE_LIMITER_ENABLED'] = was_enabled + limiter.enabled = was_enabled + raise e + return wrapped + # this is meant to be a utility class that stores post and comment references so you can use them in other calls # it's pretty barebones and will probably be fleshed out class ItemData: