123 lines
4.6 KiB
Python
123 lines
4.6 KiB
Python
import frappe
|
|
from sidrah_business.api import (
|
|
create_delegation,
|
|
respond_to_delegation,
|
|
revoke_delegation,
|
|
get_delegations,
|
|
get_my_delegated_functions,
|
|
get_my_delegated_organizations,
|
|
apply_incident_workflow,
|
|
apply_idea_workflow,
|
|
)
|
|
|
|
def run_tests():
|
|
print("=== STARTING DELEGATION & WORKFLOW TESTS ===")
|
|
|
|
# 1. Find or create an Auditor and a Manager
|
|
auditor_user = "auditor@example.com"
|
|
manager_user = "manager@example.com"
|
|
|
|
if not frappe.db.exists("User", auditor_user):
|
|
u = frappe.get_doc({
|
|
"doctype": "User", "email": auditor_user, "first_name": "Test", "last_name": "Auditor",
|
|
"roles": [{"role": "SB Auditor"}]
|
|
}).insert(ignore_permissions=True)
|
|
if not frappe.db.exists("User", manager_user):
|
|
u = frappe.get_doc({
|
|
"doctype": "User", "email": manager_user, "first_name": "Test", "last_name": "Manager",
|
|
"roles": [{"role": "SB Manager"}]
|
|
}).insert(ignore_permissions=True)
|
|
|
|
frappe.set_user(auditor_user)
|
|
|
|
# Clean up existing test delegations
|
|
frappe.db.sql("DELETE FROM `tabSB Delegation` WHERE auditor=%s OR manager=%s", (auditor_user, manager_user))
|
|
frappe.db.commit()
|
|
|
|
# 2. Test create_delegation with incident_initial_evaluation and idea_initial_evaluation
|
|
del_doc = create_delegation(
|
|
manager=manager_user,
|
|
functions_json='["incident_initial_evaluation", "idea_initial_evaluation"]',
|
|
organization="",
|
|
remarks="Test delegation"
|
|
)
|
|
print("Created Delegation:", del_doc["name"], "Status:", del_doc["status"])
|
|
assert del_doc["incident_initial_evaluation"] == 1
|
|
assert del_doc["idea_initial_evaluation"] == 1
|
|
|
|
# 3. Test respond_to_delegation as Manager
|
|
frappe.set_user(manager_user)
|
|
my_funcs = get_my_delegated_functions()
|
|
print("Manager funcs before accept:", my_funcs)
|
|
assert len(my_funcs) == 0
|
|
|
|
res = respond_to_delegation(del_doc["name"], "Accepted", "Accepted by test manager")
|
|
print("Manager response:", res["status"])
|
|
assert res["status"] == "Accepted"
|
|
|
|
my_funcs = get_my_delegated_functions()
|
|
print("Manager funcs after accept:", my_funcs)
|
|
assert "incident_initial_evaluation" in my_funcs
|
|
assert "idea_initial_evaluation" in my_funcs
|
|
|
|
my_orgs = get_my_delegated_organizations()
|
|
print("Manager delegated orgs:", my_orgs)
|
|
assert my_orgs["incident_initial_evaluation"] is None # Unrestricted
|
|
assert my_orgs["idea_initial_evaluation"] is None
|
|
|
|
# 4. Test Incident Initial Evaluation by delegated Manager
|
|
inc = frappe.get_doc({
|
|
"doctype": "SB Incident",
|
|
"title": "Test Delegation Incident",
|
|
"organization": "",
|
|
"workflow_state": "Initial Evaluation",
|
|
"status": "Initial Evaluation",
|
|
}).insert(ignore_permissions=True)
|
|
frappe.db.commit()
|
|
|
|
# Manager executes 'Assign to Manager'
|
|
updated_inc = apply_incident_workflow(
|
|
name=inc.name,
|
|
action="Assign to Manager",
|
|
fields_json='{"assigned_to": "%s", "priority": "High"}' % manager_user
|
|
)
|
|
print("Incident transitioned to:", updated_inc["workflow_state"], "Assigned to:", updated_inc["assigned_to"])
|
|
assert updated_inc["workflow_state"] == "Accept & Under Progress"
|
|
assert updated_inc["assigned_to"] == manager_user
|
|
|
|
# 5. Test Idea Initial Evaluation by delegated Manager
|
|
idea = frappe.get_doc({
|
|
"doctype": "SB Idea",
|
|
"title": "Test Delegation Idea",
|
|
"organization": "",
|
|
"workflow_state": "Initial Evaluation",
|
|
"status": "Initial Evaluation",
|
|
}).insert(ignore_permissions=True)
|
|
frappe.db.commit()
|
|
|
|
# Manager executes 'Assign to Manager'
|
|
updated_idea = apply_idea_workflow(
|
|
name=idea.name,
|
|
action="Assign to Manager",
|
|
fields_json='{"assigned_to": "%s", "priority": "Normal"}' % manager_user
|
|
)
|
|
print("Idea transitioned to:", updated_idea["workflow_state"], "Assigned to:", updated_idea["assigned_to"])
|
|
assert updated_idea["workflow_state"] == "Accept & Under Progress"
|
|
assert updated_idea["assigned_to"] == manager_user
|
|
|
|
# 6. Test Revoke
|
|
frappe.set_user(auditor_user)
|
|
rev = revoke_delegation(del_doc["name"], "Revoked by test")
|
|
print("Revocation status:", rev["status"])
|
|
assert rev["status"] == "Revoked"
|
|
|
|
frappe.set_user(manager_user)
|
|
my_funcs_after_revoke = get_my_delegated_functions()
|
|
print("Manager funcs after revoke:", my_funcs_after_revoke)
|
|
assert len(my_funcs_after_revoke) == 0
|
|
|
|
print("=== ALL TESTS PASSED SUCCESSFULLY! ===")
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|