Skip to content

Commit 654ffc0

Browse files
authored
Merge pull request #36 from sagarvora/add-whitelist-for-tests-rule
feat: add rule to enforce whitelist_for_tests in test files
2 parents bd46330 + 9d9c706 commit 654ffc0

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

rules/frappe_correctness.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from frappe import _
33

44
from frappe.model.document import Document
5+
from frappe.tests.utils import whitelist_for_tests
56
from frappe.utils import cint
67

78

@@ -215,3 +216,25 @@ def good_cache():
215216
def test_single():
216217
# ok: frappe-single-value-type-safety
217218
frappe.db.get_single_value("ABC", "ABC", ["xyz", "xac"])
219+
220+
221+
# Test file context - these should be in test_*.py files
222+
# ruleid: frappe-test-whitelist-missing-protection
223+
@frappe.whitelist()
224+
def test_endpoint():
225+
return "test"
226+
227+
# ok: frappe-test-whitelist-missing-protection
228+
@whitelist_for_tests()
229+
def test_endpoint_protected():
230+
return "test"
231+
232+
# ruleid: frappe-test-whitelist-missing-protection
233+
@frappe.whitelist(allow_guest=True)
234+
def test_guest_endpoint():
235+
return "test"
236+
237+
# ok: frappe-test-whitelist-missing-protection
238+
@whitelist_for_tests(allow_guest=True)
239+
def test_guest_endpoint_protected():
240+
return "test"

rules/frappe_correctness.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,24 @@ rules:
347347
- "**/test_*.py"
348348
languages: [python]
349349
severity: ERROR
350+
351+
- id: frappe-test-whitelist-missing-protection
352+
patterns:
353+
- pattern: |
354+
@frappe.whitelist(...)
355+
def $FUNC(...):
356+
...
357+
- pattern-not: |
358+
@$ANYTHING.whitelist_for_tests(...)
359+
def $FUNC(...):
360+
...
361+
paths:
362+
include:
363+
- "**/test_*.py"
364+
- "**/frappe/tests/ui_test_helpers.py"
365+
message: |
366+
Test endpoints should use @whitelist_for_tests() instead of @frappe.whitelist() to ensure they're only accessible in test mode.
367+
languages: [python]
368+
severity: ERROR
369+
fix: |
370+
@whitelist_for_tests(...)

0 commit comments

Comments
 (0)