test: server script rate limiting

This commit is contained in:
Ankush Menat 2023-05-15 10:56:15 +05:30
parent 8eb9d2ea18
commit 95287c060c
2 changed files with 54 additions and 0 deletions

View file

@ -206,6 +206,10 @@ def setup_scheduler_events(script_name, frequency):
def execute_api_server_script(script=None, *args, **kwargs):
# These are only added for compatibility with rate limiter.
del args
del kwargs
if script.script_type != "API":
raise frappe.DoesNotExistError

View file

@ -3,6 +3,7 @@
import requests
import frappe
from frappe.frappeclient import FrappeClient, FrappeException
from frappe.tests.utils import FrappeTestCase
from frappe.utils import get_site_url
@ -233,3 +234,52 @@ frappe.qb.from_(todo).select(todo.name).where(todo.name == "{todo.name}").run()
)
script.insert()
script.execute_method()
def test_server_script_rate_limiting(self):
# why not
script1 = frappe.get_doc(
doctype="Server Script",
name="rate_limited_server_script",
script_type="API",
enable_rate_limit=1,
allow_guest=1,
rate_limit_count=5,
api_method="rate_limited_endpoint",
script="""frappe.flags = {"test": True}""",
)
script1.insert()
script2 = frappe.get_doc(
doctype="Server Script",
name="rate_limited_server_script2",
script_type="API",
enable_rate_limit=1,
allow_guest=1,
rate_limit_count=5,
api_method="rate_limited_endpoint2",
script="""frappe.flags = {"test": False}""",
)
script2.insert()
frappe.db.commit()
site = frappe.utils.get_site_url(frappe.local.site)
client = FrappeClient(site)
# Exhaust rate limti
for _ in range(5):
client.get_api(script1.api_method)
self.assertRaises(FrappeException, client.get_api, script1.api_method)
# Exhaust rate limti
for _ in range(5):
client.get_api(script2.api_method)
self.assertRaises(FrappeException, client.get_api, script2.api_method)
script1.delete()
script2.delete()
frappe.db.commit()