From 4def894801e711a9a720ab1d19532fb44cd0802b Mon Sep 17 00:00:00 2001 From: Goose Date: Sun, 27 Sep 2026 07:17:04 +0000 Subject: [PATCH] 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. --- custom_objects/apps.py | 3 + custom_objects/formula_engine.py | 379 ++++++++++++++++++ ...ddefinition_formula_expression_and_more.py | 48 +++ custom_objects/models.py | 15 + custom_objects/serializers.py | 13 + custom_objects/signals.py | 17 + custom_objects/views.py | 15 + tests/test_formula_engine.py | 149 +++++++ 8 files changed, 639 insertions(+) create mode 100644 custom_objects/formula_engine.py create mode 100644 custom_objects/migrations/0003_customfielddefinition_formula_expression_and_more.py create mode 100644 custom_objects/signals.py create mode 100644 tests/test_formula_engine.py diff --git a/custom_objects/apps.py b/custom_objects/apps.py index f4e645a..a4792a3 100644 --- a/custom_objects/apps.py +++ b/custom_objects/apps.py @@ -3,3 +3,6 @@ from django.apps import AppConfig class CustomObjectsConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'custom_objects' + + def ready(self): + import custom_objects.signals diff --git a/custom_objects/formula_engine.py b/custom_objects/formula_engine.py new file mode 100644 index 0000000..b572868 --- /dev/null +++ b/custom_objects/formula_engine.py @@ -0,0 +1,379 @@ +import ast +import re +from datetime import date, datetime +from decimal import Decimal +from django.core.exceptions import ValidationError +from django.utils import timezone + +ALLOWED_AST_NODES = ( + ast.Expression, + ast.BinOp, + ast.UnaryOp, + ast.Add, + ast.Sub, + ast.Mult, + ast.Div, + ast.Mod, + ast.USub, + ast.UAdd, + ast.Compare, + ast.Eq, + ast.NotEq, + ast.Lt, + ast.LtE, + ast.Gt, + ast.GtE, + ast.BoolOp, + ast.And, + ast.Or, + ast.Not, + ast.Constant, + ast.Name, + ast.Attribute, + ast.Call, + ast.Load, +) + +def fn_if(condition, true_val, false_val): + return true_val if condition else false_val + +def fn_case(*args): + if len(args) < 3: + raise ValueError('CASE requires at least an expression, value, result, and default.') + target = args[0] + pairs = args[1:-1] + default = args[-1] + for i in range(0, len(pairs), 2): + if i + 1 < len(pairs) and target == pairs[i]: + return pairs[i + 1] + return default + +def fn_isblank(val): + return val is None or val == '' or val == [] + +def fn_round(val, digits=0): + if val is None: + return 0 + return round(float(val), int(digits)) + +def fn_today(): + return timezone.now().date() + +def fn_now(): + return timezone.now() + +BUILTIN_FUNCTIONS = { + 'IF': fn_if, + 'CASE': fn_case, + 'ISBLANK': fn_isblank, + 'NOT': lambda x: not bool(x), + 'AND': lambda *args: all(args), + 'OR': lambda *args: any(args), + 'CONCAT': lambda *args: ''.join(str(a) for a in args if a is not None), + 'UPPER': lambda x: str(x).upper() if x is not None else '', + 'LOWER': lambda x: str(x).lower() if x is not None else '', + 'LEN': lambda x: len(str(x)) if x is not None else 0, + 'TRIM': lambda x: str(x).strip() if x is not None else '', + 'ROUND': fn_round, + 'ABS': lambda x: abs(x) if x is not None else 0, + 'MAX': lambda *args: max(args) if args else None, + 'MIN': lambda *args: min(args) if args else None, + 'TODAY': fn_today, + 'NOW': fn_now, +} + +class ASTSecurityValidator(ast.NodeVisitor): + def __init__(self): + self.identifiers = set() + + def generic_visit(self, node): + if not isinstance(node, ALLOWED_AST_NODES): + raise ValidationError(f'Disallowed expression syntax: {type(node).__name__}') + super().generic_visit(node) + + def visit_Call(self, node): + func_name = None + if isinstance(node.func, ast.Name): + func_name = node.func.id + elif isinstance(node.func, ast.Attribute): + func_name = node.func.attr + + if not func_name or func_name.upper() not in BUILTIN_FUNCTIONS: + raise ValidationError(f"Function '{func_name}' is not permitted in formula expressions.") + super().generic_visit(node) + + def visit_Name(self, node): + if node.id.startswith('__'): + raise ValidationError("Private/dunder attributes are forbidden.") + if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', node.id): + raise ValidationError(f'Invalid identifier: {node.id}') + if node.id.upper() not in BUILTIN_FUNCTIONS: + self.identifiers.add(node.id) + super().generic_visit(node) + + def visit_Attribute(self, node): + if node.attr.startswith('__'): + raise ValidationError("Private/dunder attributes are forbidden.") + parts = [] + curr = node + while isinstance(curr, ast.Attribute): + if curr.attr.startswith('__'): + raise ValidationError("Private/dunder attributes are forbidden.") + parts.append(curr.attr) + curr = curr.value + if isinstance(curr, ast.Name): + if curr.id.startswith('__'): + raise ValidationError("Private/dunder attributes are forbidden.") + parts.append(curr.id) + parts.reverse() + full_path = '.'.join(parts) + self.identifiers.add(full_path) + else: + raise ValidationError('Dynamic attribute chaining is not permitted.') + super().generic_visit(node) + +def extract_formula_identifiers(expression): + if not expression or not expression.strip(): + return set() + parsed = ast.parse(expression.strip(), mode='eval') + validator = ASTSecurityValidator() + validator.visit(parsed) + return validator.identifiers + +def validate_formula_syntax(expression): + if not expression or not expression.strip(): + raise ValidationError('Formula expression cannot be empty.') + try: + parsed = ast.parse(expression.strip(), mode='eval') + except SyntaxError as e: + raise ValidationError(f'Invalid formula syntax: {e.msg}') + validator = ASTSecurityValidator() + validator.visit(parsed) + return True + +def topological_sort_formulas(formula_dict): + """ + Sort formula field names topologically based on their dependencies on other formula fields in the dict. + formula_dict: dict of {field_name: formula_expression} + Raises ValidationError on cycle. + """ + deps = {} + in_degree = {name: 0 for name in formula_dict} + graph = {name: [] for name in formula_dict} + + for name, expr in formula_dict.items(): + try: + ids = extract_formula_identifiers(expr) + except Exception: + ids = set() + # Dependencies that are also formula fields + f_deps = {dep for dep in ids if dep in formula_dict and dep != name} + deps[name] = f_deps + for dep in f_deps: + graph[dep].append(name) + in_degree[name] += 1 + + queue = [name for name, deg in in_degree.items() if deg == 0] + sorted_fields = [] + + while queue: + curr = queue.pop(0) + sorted_fields.append(curr) + for neighbor in graph[curr]: + in_degree[neighbor] -= 1 + if in_degree[neighbor] == 0: + queue.append(neighbor) + + if len(sorted_fields) != len(formula_dict): + raise ValidationError("Circular dependency detected in formula fields.") + + return sorted_fields + +def resolve_field_value(record, path): + if not path: + return None + parts = path.split('.') + curr = record + for part in parts: + if curr is None: + return None + if isinstance(curr, dict): + curr = curr.get(part) + elif hasattr(curr, 'custom_fields') and isinstance(curr.custom_fields, dict) and part in curr.custom_fields: + curr = curr.custom_fields[part] + elif hasattr(curr, 'data') and isinstance(curr.data, dict) and part in curr.data: + curr = curr.data[part] + elif hasattr(curr, part): + val = getattr(curr, part) + curr = val() if callable(val) else val + else: + return None + return curr + +class ASTFormulaEvaluator(ast.NodeVisitor): + def __init__(self, record, context=None): + self.record = record + self.context = context or {} + + def evaluate(self, expression): + parsed = ast.parse(expression.strip(), mode='eval') + validator = ASTSecurityValidator() + validator.visit(parsed) + return self.visit(parsed.body) + + def visit_Constant(self, node): + return node.value + + def visit_Name(self, node): + name = node.id + if name.upper() in BUILTIN_FUNCTIONS: + return BUILTIN_FUNCTIONS[name.upper()] + if name in self.context: + return self.context[name] + return resolve_field_value(self.record, name) + + def visit_Attribute(self, node): + parts = [] + curr = node + while isinstance(curr, ast.Attribute): + parts.append(curr.attr) + curr = curr.value + if isinstance(curr, ast.Name): + parts.append(curr.id) + parts.reverse() + full_path = '.'.join(parts) + if full_path in self.context: + return self.context[full_path] + return resolve_field_value(self.record, full_path) + raise ValidationError('Invalid attribute evaluation.') + + def visit_BinOp(self, node): + left = self.visit(node.left) + right = self.visit(node.right) + if isinstance(node.op, ast.Add): + if isinstance(left, str) or isinstance(right, str): + return str(left or '') + str(right or '') + return (left or 0) + (right or 0) + elif isinstance(node.op, ast.Sub): + return (left or 0) - (right or 0) + elif isinstance(node.op, ast.Mult): + return (left or 0) * (right or 0) + elif isinstance(node.op, ast.Div): + if right == 0: + raise ZeroDivisionError('Division by zero in formula evaluation.') + return (left or 0) / right + elif isinstance(node.op, ast.Mod): + return (left or 0) % right + raise ValidationError(f'Unsupported binary operator: {type(node.op).__name__}') + + def visit_UnaryOp(self, node): + operand = self.visit(node.operand) + if isinstance(node.op, ast.USub): + return -operand + elif isinstance(node.op, ast.UAdd): + return +operand + elif isinstance(node.op, ast.Not): + return not operand + raise ValidationError(f'Unsupported unary operator: {type(node.op).__name__}') + + def visit_Compare(self, node): + left = self.visit(node.left) + for op, comparator in zip(node.ops, node.comparators): + right = self.visit(comparator) + if isinstance(op, ast.Eq) and not (left == right): + return False + elif isinstance(op, ast.NotEq) and not (left != right): + return False + elif isinstance(op, ast.Lt) and not (left < right): + return False + elif isinstance(op, ast.LtE) and not (left <= right): + return False + elif isinstance(op, ast.Gt) and not (left > right): + return False + elif isinstance(op, ast.GtE) and not (left >= right): + return False + left = right + return True + + def visit_BoolOp(self, node): + if isinstance(node.op, ast.And): + for val_node in node.values: + if not self.visit(val_node): + return False + return True + elif isinstance(node.op, ast.Or): + for val_node in node.values: + if self.visit(val_node): + return True + return False + raise ValidationError(f'Unsupported boolean operator: {type(node.op).__name__}') + + def visit_Call(self, node): + func = self.visit(node.func) + if not callable(func): + raise ValidationError(f'Expression {node.func} is not a valid function.') + args = [self.visit(arg) for arg in node.args] + return func(*args) + +def evaluate_formula(record, expression, return_type='text', context=None): + if not expression or not expression.strip(): + return None + evaluator = ASTFormulaEvaluator(record, context=context) + val = evaluator.evaluate(expression) + if val is None: + return None + if return_type in ('number', 'percent'): + return float(val) + elif return_type == 'currency': + return round(float(val), 2) + elif return_type in ('boolean', 'checkbox'): + return bool(val) + elif return_type in ('text', 'string'): + return str(val) + return val + +def evaluate_record_formulas(record, entity_name=None): + from custom_objects.models import CustomFieldDefinition + is_custom_record = hasattr(record, 'custom_object_id') and record.custom_object_id + if entity_name: + fields = CustomFieldDefinition.objects.filter(entity_name=entity_name, field_type='formula') + elif is_custom_record: + fields = CustomFieldDefinition.objects.filter(custom_object_id=record.custom_object_id, field_type='formula') + elif hasattr(record, '_meta') and record._meta.model_name: + # Standard model fallback by class name + fields = CustomFieldDefinition.objects.filter(entity_name=record.__class__.__name__, field_type='formula') + else: + return + + if not fields.exists(): + return + + if is_custom_record: + if record.data is None: + record.data = {} + storage = record.data + else: + if not hasattr(record, 'custom_fields') or record.custom_fields is None: + record.custom_fields = {} + storage = record.custom_fields + + formula_dict = {f.name: f.formula_expression for f in fields if f.formula_expression} + if not formula_dict: + return + + try: + execution_order = topological_sort_formulas(formula_dict) + except ValidationError: + execution_order = list(formula_dict.keys()) + + field_map = {f.name: f for f in fields} + for field_name in execution_order: + f = field_map[field_name] + try: + res = evaluate_formula(record, f.formula_expression, return_type=getattr(f, 'formula_return_type', 'text')) + storage[field_name] = res + except Exception: + pass + +def evaluate_all_formulas_for_instance(instance): + evaluate_record_formulas(instance) diff --git a/custom_objects/migrations/0003_customfielddefinition_formula_expression_and_more.py b/custom_objects/migrations/0003_customfielddefinition_formula_expression_and_more.py new file mode 100644 index 0000000..bb9381a --- /dev/null +++ b/custom_objects/migrations/0003_customfielddefinition_formula_expression_and_more.py @@ -0,0 +1,48 @@ +# Generated by Django 4.2.30 on 2026-09-27 04:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('custom_objects', '0002_customfielddefinition_entity_name_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='customfielddefinition', + name='formula_expression', + field=models.TextField(blank=True, help_text='AST expression for formula calculations', null=True), + ), + migrations.AddField( + model_name='customfielddefinition', + name='formula_return_type', + field=models.CharField(blank=True, default='text', max_length=20, null=True), + ), + migrations.AddField( + model_name='customfielddefinition', + name='rollup_field', + field=models.CharField(blank=True, max_length=100, null=True), + ), + migrations.AddField( + model_name='customfielddefinition', + name='rollup_filter', + field=models.JSONField(blank=True, null=True), + ), + migrations.AddField( + model_name='customfielddefinition', + name='rollup_summary_object', + field=models.CharField(blank=True, max_length=100, null=True), + ), + migrations.AddField( + model_name='customfielddefinition', + name='rollup_type', + field=models.CharField(blank=True, max_length=20, null=True), + ), + migrations.AlterField( + model_name='customfielddefinition', + name='field_type', + field=models.CharField(choices=[('text', 'Text'), ('number', 'Number'), ('boolean', 'Boolean'), ('date', 'Date'), ('datetime', 'DateTime'), ('email', 'Email'), ('url', 'URL'), ('select', 'Picklist (Select)'), ('picklist', 'Picklist'), ('lookup', 'Lookup (Relationship)'), ('formula', 'Formula (Calculated)'), ('rollup', 'Roll-Up Summary')], max_length=20), + ), + ] diff --git a/custom_objects/models.py b/custom_objects/models.py index e02554e..11ed225 100644 --- a/custom_objects/models.py +++ b/custom_objects/models.py @@ -68,6 +68,8 @@ class CustomFieldDefinition(models.Model): ('select', 'Picklist (Select)'), ('picklist', 'Picklist'), ('lookup', 'Lookup (Relationship)'), + ('formula', 'Formula (Calculated)'), + ('rollup', 'Roll-Up Summary'), ] custom_object = models.ForeignKey( @@ -88,6 +90,14 @@ class CustomFieldDefinition(models.Model): related_name='referencing_fields', help_text="Target for 'lookup' type" ) + # Formula & Roll-up summary configuration + formula_expression = models.TextField(null=True, blank=True, help_text="AST expression for formula calculations") + formula_return_type = models.CharField(max_length=20, default='text', null=True, blank=True) + rollup_summary_object = models.CharField(max_length=100, null=True, blank=True) + rollup_field = models.CharField(max_length=100, null=True, blank=True) + rollup_type = models.CharField(max_length=20, null=True, blank=True) + rollup_filter = models.JSONField(null=True, blank=True) + created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) @@ -106,6 +116,11 @@ class CustomFieldDefinition(models.Model): raise ValidationError({'choices': 'Select fields require a list of choices.'}) if self.field_type == 'lookup' and not self.target_object: raise ValidationError({'target_object': 'Lookup fields require a target_object.'}) + if self.field_type == 'formula': + if not self.formula_expression: + raise ValidationError({'formula_expression': 'Formula fields require a formula_expression.'}) + from custom_objects.formula_engine import validate_formula_syntax + validate_formula_syntax(self.formula_expression) def __str__(self): parent_name = self.custom_object.name if self.custom_object else self.entity_name diff --git a/custom_objects/serializers.py b/custom_objects/serializers.py index 92bbe14..541076f 100644 --- a/custom_objects/serializers.py +++ b/custom_objects/serializers.py @@ -26,6 +26,19 @@ class CustomFieldDefinitionSerializer(OwnerScopedRelationMixin, serializers.Mode fields = '__all__' read_only_fields = ['id', 'created_at', 'updated_at'] + def validate(self, attrs): + field_type = attrs.get('field_type') or getattr(self.instance, 'field_type', None) + formula_expression = attrs.get('formula_expression') or getattr(self.instance, 'formula_expression', None) + if field_type == 'formula': + if not formula_expression: + raise serializers.ValidationError({'formula_expression': 'Formula fields require a formula_expression.'}) + from custom_objects.formula_engine import validate_formula_syntax + try: + validate_formula_syntax(formula_expression) + except Exception as e: + raise serializers.ValidationError({'formula_expression': str(e)}) + return super().validate(attrs) + class CustomObjectDefinitionSerializer(serializers.ModelSerializer): fields = CustomFieldDefinitionSerializer(many=True, read_only=True) diff --git a/custom_objects/signals.py b/custom_objects/signals.py new file mode 100644 index 0000000..c329300 --- /dev/null +++ b/custom_objects/signals.py @@ -0,0 +1,17 @@ +from django.db.models.signals import pre_save +from django.dispatch import receiver +from custom_objects.models import CustomRecord +from crm_core.models import Account, Contact, Lead, Opportunity, Activity +from outreach.models import Campaign +from custom_objects.formula_engine import evaluate_all_formulas_for_instance + + +@receiver(pre_save, sender=Account) +@receiver(pre_save, sender=Contact) +@receiver(pre_save, sender=Lead) +@receiver(pre_save, sender=Opportunity) +@receiver(pre_save, sender=Activity) +@receiver(pre_save, sender=Campaign) +@receiver(pre_save, sender=CustomRecord) +def evaluate_record_formulas(sender, instance, **kwargs): + evaluate_all_formulas_for_instance(instance) diff --git a/custom_objects/views.py b/custom_objects/views.py index 26a6a95..9dd7b60 100644 --- a/custom_objects/views.py +++ b/custom_objects/views.py @@ -1,5 +1,7 @@ from django.db import models, transaction from rest_framework import viewsets, status +from rest_framework.decorators import action +from rest_framework.response import Response from rest_framework.permissions import IsAuthenticated from rest_framework.exceptions import NotFound, PermissionDenied @@ -66,6 +68,19 @@ class CustomFieldDefinitionViewSet(viewsets.ModelViewSet): raise PermissionDenied("Cannot add fields to another user's custom object.") serializer.save() + @action(detail=False, methods=['post'], url_path='validate-formula') + def validate_formula(self, request): + expr = request.data.get('formula_expression') or request.data.get('expression', '') + if not expr: + return Response({'valid': False, 'error': 'Expression is required.'}, status=status.HTTP_400_BAD_REQUEST) + from custom_objects.formula_engine import validate_formula_syntax, extract_formula_identifiers + try: + validate_formula_syntax(expr) + identifiers = extract_formula_identifiers(expr) + return Response({'valid': True, 'identifiers': list(identifiers)}, status=status.HTTP_200_OK) + except Exception as e: + return Response({'valid': False, 'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) + class CustomRecordViewSet(viewsets.ModelViewSet): permission_classes = [IsAuthenticated, ScopedOwnerAccessPermission] diff --git a/tests/test_formula_engine.py b/tests/test_formula_engine.py new file mode 100644 index 0000000..8e1d0ad --- /dev/null +++ b/tests/test_formula_engine.py @@ -0,0 +1,149 @@ +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