openforce/tests/test_formula_engine.py
Goose 4def894801
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 29s
feat(formulas): implement safe AST formula execution engine and pre-save evaluation signals (Sprint 4.5A & 4.5B)
- Add zero-trust AST-based formula engine in custom_objects/formula_engine.py supporting arithmetic, logical, string, and date operations.
- Enforce strict AST whitelisting and attribute/function safety checks.
- Add cycle detection and topological sorting of formula dependencies.
- Extend CustomFieldDefinition model with formula_expression and formula_return_type fields.
- Wire pre-save signals to evaluate formula fields across standard models and CustomRecord.
- Expose AST syntax validator endpoint at POST /api/custom-fields/validate-formula/.
- Add comprehensive test suite in tests/test_formula_engine.py with 100% pass rate.
2026-09-27 07:17:04 +00:00

149 lines
5.5 KiB
Python

import pytest
from custom_objects.formula_engine import (
validate_formula_syntax, evaluate_formula, extract_formula_identifiers,
topological_sort_formulas
)
from django.core.exceptions import ValidationError
from rest_framework.test import APIClient
from django.contrib.auth.models import User
from crm_core.models import Opportunity, Account
from custom_objects.models import CustomFieldDefinition, CustomObjectDefinition, CustomRecord
@pytest.mark.django_db
class TestFormulaEngine:
def test_arithmetic_evaluation(self):
ctx = {'amount': 100, 'tax_rate': 0.05}
result = evaluate_formula(ctx, 'amount * (1 + tax_rate)', return_type='currency')
assert result == 105.0
def test_logical_and_conditional_functions(self):
ctx = {'amount': 50000, 'stage': 'Closed Won'}
res_if = evaluate_formula(ctx, 'IF(amount >= 50000, "Enterprise", "SMB")', return_type='text')
assert res_if == 'Enterprise'
res_case = evaluate_formula(ctx, 'CASE(stage, "Closed Won", 1.0, "Closed Lost", 0.0, 0.5)', return_type='number')
assert res_case == 1.0
ctx_blank = {'notes': ''}
assert evaluate_formula(ctx_blank, 'ISBLANK(notes)', return_type='checkbox') is True
def test_string_manipulation(self):
ctx = {'first_name': 'alice', 'last_name': 'smith'}
res = evaluate_formula(ctx, 'CONCAT(UPPER(first_name), " ", UPPER(last_name))', return_type='text')
assert res == 'ALICE SMITH'
assert evaluate_formula(ctx, 'LEN(first_name)', return_type='number') == 5
def test_syntax_validation_whitelist(self):
# Valid expressions
assert validate_formula_syntax('IF(amount > 10, "Yes", "No")') is True
assert validate_formula_syntax('ROUND(amount * 0.15, 2)') is True
# Syntax error
with pytest.raises(ValidationError):
validate_formula_syntax('amount *')
# Security sandbox violations
with pytest.raises(ValidationError):
validate_formula_syntax('__import__("os").system("ls")')
with pytest.raises(ValidationError):
validate_formula_syntax('exec("import os")')
with pytest.raises(ValidationError):
validate_formula_syntax('record.__class__.__bases__')
def test_cycle_detection(self):
formulas = {
'A__c': 'B__c + 1',
'B__c': 'A__c * 2',
}
with pytest.raises(ValidationError, match="Circular dependency"):
topological_sort_formulas(formulas)
valid_formulas = {
'A__c': 'amount * 2',
'B__c': 'A__c + 10',
'C__c': 'B__c / 2',
}
order = topological_sort_formulas(valid_formulas)
assert order == ['A__c', 'B__c', 'C__c']
def test_pre_save_standard_model_evaluation(self):
u = User.objects.create_user('formula_user', 'fu@example.com', 'p')
acc = Account.objects.create(name='Test Corp', annual_revenue=1000000, owner=u)
# Create formula field on Account: rev_per_month__c = annual_revenue / 12
CustomFieldDefinition.objects.create(
entity_name='Account',
name='rev_per_month__c',
label='Revenue Per Month',
field_type='formula',
formula_expression='annual_revenue / 12',
formula_return_type='currency'
)
acc.refresh_from_db()
acc.save() # Triggers pre_save
acc.refresh_from_db()
assert 'rev_per_month__c' in acc.custom_fields
assert round(float(acc.custom_fields['rev_per_month__c']), 2) == 83333.33
def test_pre_save_custom_record_evaluation(self):
u = User.objects.create_user('formula_user2', 'fu2@example.com', 'p')
cobj = CustomObjectDefinition.objects.create(
name='Invoice',
label='Invoice',
api_name='invoice__c',
created_by=u
)
CustomFieldDefinition.objects.create(
custom_object=cobj,
name='subtotal__c',
label='Subtotal',
field_type='number'
)
CustomFieldDefinition.objects.create(
custom_object=cobj,
name='tax_rate__c',
label='Tax Rate',
field_type='number'
)
CustomFieldDefinition.objects.create(
custom_object=cobj,
name='total__c',
label='Total',
field_type='formula',
formula_expression='subtotal__c * (1 + tax_rate__c)',
formula_return_type='currency'
)
rec = CustomRecord.objects.create(
custom_object=cobj,
owner=u,
data={'subtotal__c': 200, 'tax_rate__c': 0.1}
)
assert 'total__c' in rec.data
assert float(rec.data['total__c']) == 220.0
def test_api_syntax_validation_endpoint(self):
client = APIClient()
u = User.objects.create_user('api_formula_user', 'api@fu.com', 'p')
client.force_authenticate(user=u)
# Valid
res = client.post('/api/custom-fields/validate-formula/', {
'expression': 'IF(amount > 100, amount * 0.9, amount)'
}, format='json', HTTP_HOST='localhost')
assert res.status_code == 200
assert res.data['valid'] is True
# Invalid
res_bad = client.post('/api/custom-fields/validate-formula/', {
'expression': 'eval("1+1")'
}, format='json', HTTP_HOST='localhost')
assert res_bad.status_code == 400
assert res_bad.data['valid'] is False