From 1e4d28cc196cee68895fbc56a4f0407362c0bec2 Mon Sep 17 00:00:00 2001 From: marination Date: Fri, 27 Jan 2023 16:11:00 +0530 Subject: [PATCH] fix(test): Remove try-finally & ignore perms on test user's report insertion --- frappe/tests/test_boot.py | 83 +++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/frappe/tests/test_boot.py b/frappe/tests/test_boot.py index 7baebac140..ece2c181df 100644 --- a/frappe/tests/test_boot.py +++ b/frappe/tests/test_boot.py @@ -28,49 +28,48 @@ class TestBootData(FrappeTestCase): self.assertListEqual(unseen_notes, []) def test_get_user_pages_or_reports_with_permission_query(self): - try: - # Create a ToDo custom report with admin user - frappe.set_user("Administrator") - frappe.get_doc( - { - "doctype": "Report", - "ref_doctype": "ToDo", - "report_name": "Test Admin Report", - "report_type": "Report Builder", - "is_standard": "No", - } - ).insert() + # Create a ToDo custom report with admin user + frappe.set_user("Administrator") + frappe.get_doc( + { + "doctype": "Report", + "ref_doctype": "ToDo", + "report_name": "Test Admin Report", + "report_type": "Report Builder", + "is_standard": "No", + } + ).insert() - # Add permission query such that each user can only see their own custom reports - frappe.get_doc( - dict( - doctype="Server Script", - name="test_report_permission_query", - script_type="Permission Query", - reference_doctype="Report", - script="""conditions = f"(`tabReport`.is_standard = 'Yes' or `tabReport`.owner = '{frappe.session.user}')" - """, - ) - ).insert() + # Add permission query such that each user can only see their own custom reports + frappe.get_doc( + dict( + doctype="Server Script", + name="test_report_permission_query", + script_type="Permission Query", + reference_doctype="Report", + script="""conditions = f"(`tabReport`.is_standard = 'Yes' or `tabReport`.owner = '{frappe.session.user}')" + """, + ) + ).insert() - # Create a ToDo custom report with test user - frappe.set_user("test@example.com") - frappe.get_doc( - { - "doctype": "Report", - "ref_doctype": "ToDo", - "report_name": "Test User Report", - "report_type": "Report Builder", - "is_standard": "No", - } - ).insert() + # Create a ToDo custom report with test user + frappe.set_user("test@example.com") + frappe.get_doc( + { + "doctype": "Report", + "ref_doctype": "ToDo", + "report_name": "Test User Report", + "report_type": "Report Builder", + "is_standard": "No", + } + ).insert(ignore_permissions=True) - get_user_pages_or_reports("Report") - allowed_reports = frappe.cache().get_value("has_role:Report", user=frappe.session.user) + get_user_pages_or_reports("Report") + allowed_reports = frappe.cache().get_value("has_role:Report", user=frappe.session.user) - # Test user must not see admin user's report - self.assertNotIn("Test Admin Report", allowed_reports) - self.assertIn("Test User Report", allowed_reports) - finally: - frappe.db.rollback() - frappe.set_user("Administrator") + # Test user must not see admin user's report + self.assertNotIn("Test Admin Report", allowed_reports) + self.assertIn("Test User Report", allowed_reports) + + self.addCleanup(frappe.db.rollback) + self.addCleanup(frappe.set_user, "Administrator")