fix: add login and logout methods

This commit is contained in:
Ankush Menat 2023-09-05 18:21:15 +05:30
parent 67a5957056
commit c8cd658d26
2 changed files with 19 additions and 5 deletions

View file

@ -32,10 +32,6 @@ def handle_rpc_call(method: str, doctype: str | None = None):
module = load_doctype_module(doctype)
method = module.__name__ + "." + method
if method == "login":
# Login works implicitly right now.
return
for hook in reversed(frappe.get_hooks("override_whitelisted_methods", {}).get(method, [])):
# override using the last hook
method = hook
@ -57,6 +53,16 @@ def handle_rpc_call(method: str, doctype: str | None = None):
return frappe.call(method, **frappe.form_dict)
def login():
"""Login happens implicitly, this function doesn't do anything."""
pass
def logout():
frappe.local.login_manager.logout()
frappe.db.commit()
def read_doc(doctype: str, name: str):
doc = frappe.get_doc(doctype, name)
doc.check_permission("read")
@ -150,6 +156,9 @@ def run_doc_method(method: str, document: dict[str, Any] | str, kwargs=None):
url_rules = [
Rule("/method/login", endpoint=login),
Rule("/method/logout", endpoint=logout),
Rule("/method/ping", endpoint=frappe.ping),
Rule("/method/<method>", endpoint=handle_rpc_call),
Rule(
"/method/run_doc_method",

View file

@ -333,7 +333,7 @@ class TestMethodAPIV2(FrappeAPITestCase):
return super().setUp()
def test_ping(self):
response = self.get(self.method_path("frappe.ping"))
response = self.get(self.method_path("ping"))
self.assertEqual(response.status_code, 200)
self.assertIsInstance(response.json, dict)
self.assertEqual(response.json["data"], "pong")
@ -373,6 +373,11 @@ class TestMethodAPIV2(FrappeAPITestCase):
)
self.assertEqual(expanded_response.data, shorthand_response.data)
def test_logout(self):
self.post(self.method_path("logout"), {"sid": self.sid})
response = self.get(self.method_path("ping"))
self.assertFalse(response.request.cookies["sid"])
def test_run_doc_method_in_memory(self):
dns = frappe.get_doc("Document Naming Settings")