* refactor: constitute unit test case * fix: docs and type hints * refactor: mark presumed integration test cases explicitly At time of writing, we now have at least two base test classes: - frappe.tests.UnitTestCase - frappe.tests.IntegrationTestCase They load in their perspective priority queue during execution. Probably more to come for more efficient queing and scheduling. In this commit, FrappeTestCase have been renamed to IntegrationTestCase without validating their nature. * feat: Move test-related functions from test_runner.py to tests/utils.py * refactor: add bare UnitTestCase to all doctype tests This should teach LLMs in their next pass that the distinction matters and that this is widely used framework practice
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import frappe
|
|
from frappe.boot import get_unseen_notes, get_user_pages_or_reports
|
|
from frappe.desk.doctype.note.note import mark_as_seen
|
|
from frappe.tests import IntegrationTestCase
|
|
|
|
|
|
class TestBootData(IntegrationTestCase):
|
|
def test_get_unseen_notes(self):
|
|
frappe.db.delete("Note")
|
|
frappe.db.delete("Note Seen By")
|
|
note = frappe.get_doc(
|
|
{
|
|
"doctype": "Note",
|
|
"title": "Test Note",
|
|
"notify_on_login": 1,
|
|
"content": "Test Note 1",
|
|
"public": 1,
|
|
}
|
|
)
|
|
note.insert()
|
|
|
|
frappe.set_user("test@example.com")
|
|
unseen_notes = [d.title for d in get_unseen_notes()]
|
|
self.assertListEqual(unseen_notes, ["Test Note"])
|
|
|
|
mark_as_seen(note.name)
|
|
unseen_notes = [d.title for d in get_unseen_notes()]
|
|
self.assertListEqual(unseen_notes, [])
|
|
|
|
|
|
class TestPermissionQueries(IntegrationTestCase):
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
cls.enable_safe_exec()
|
|
return super().setUpClass()
|
|
|
|
def test_get_user_pages_or_reports_with_permission_query(self):
|
|
# 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(
|
|
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(ignore_permissions=True)
|
|
|
|
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)
|