test: test auth_hooks

This commit is contained in:
Revant Nandgaonkar 2023-11-17 15:56:17 +05:30
parent 5fc4400eee
commit 29d8f2a568

View file

@ -1,10 +1,9 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
from frappe.cache_manager import clear_controller_cache
from frappe.desk.doctype.todo.todo import ToDo
from frappe.tests.utils import FrappeTestCase
from frappe.tests.utils import FrappeTestCase, patch_hooks
class TestHooks(FrappeTestCase):
@ -95,11 +94,28 @@ class TestHooks(FrappeTestCase):
event.delete()
def test_auth_hook(self):
import requests
with patch_hooks({ "auth_hooks": ["frappe.tests.test_hooks.custom_auth"] }):
site_url = frappe.utils.get_site_url(frappe.local.site)
response = requests.get(
site_url + "/api/method/frappe.auth.get_logged_user",
headers={"Authorization": "Bearer set_test_example_user"}
).json()
# Test!
self.assertTrue(response.get("message") == "test@example.com")
def custom_has_permission(doc, ptype, user):
if doc.flags.dont_touch_me:
return False
def custom_auth():
auth_type, token = frappe.get_request_header("Authorization", "Bearer ").split(" ")
if token == "set_test_example_user":
frappe.set_user("test@example.com")
class CustomToDo(ToDo):
pass