function test_fixes
A comprehensive test function that validates email template rendering and CDocs application link presence in a document management system's email notification templates.
/tf/active/vicechatdev/test_comprehensive_fixes.py
10 - 148
moderate
Purpose
This function performs three main tests: (1) validates that template variables are correctly replaced with actual data using regex-based template rendering, (2) verifies that CDocs application links are present and correctly rendered in the output, and (3) checks that all key email template files exist and contain required placeholders and CDocs links. It's designed to ensure email notifications work correctly after fixes to template rendering and link inclusion issues.
Source Code
def test_fixes():
"""Test both issues: template rendering and CDocs app links."""
print("=" * 60)
print("COMPREHENSIVE TEST OF CDOCS EMAIL NOTIFICATION FIXES")
print("=" * 60)
# Test 1: Template rendering with actual data
print("\n1. Testing template rendering...")
test_data = {
'doc_number': 'SOP-TEST-001',
'title': 'Test Standard Operating Procedure for Email Templates',
'doc_type': 'SOP',
'version_number': '1.0',
'due_date': 'June 15, 2025',
'instructions': 'Please review this document carefully and ensure compliance.',
'app_name': 'Controlled Document Management System',
'app_url': 'https://cdocs.vicebio.com',
'current_year': 2025,
'sender_name': 'CDocs System',
'cdocs_app_url': 'https://cdocs.vicebio.com',
'cdocs_app_text': 'Access the CDocs Application',
'document_url': 'https://cdocs.vicebio.com/document/test-123',
'approval_url': 'https://cdocs.vicebio.com/approval/test-456',
'message': 'Please approve this test document.'
}
# Test template rendering
template_content = '''
<div class="doc-info">
<p><strong>Document Number:</strong> {{ doc_number }}</p>
<p><strong>Title:</strong> {{ title }}</p>
<p><strong>Document Type:</strong> {{ doc_type }}</p>
<p><strong>Version:</strong> {{ version_number }}</p>
<p><strong>Due Date:</strong> {{ due_date }}</p>
</div>
{% if instructions %}
<div class="instructions">
<h3>Instructions:</h3>
<p>{{ instructions }}</p>
</div>
{% endif %}
<div class="cdocs-link">
<a href="{{ cdocs_app_url }}">{{ cdocs_app_text }}</a>
</div>
'''
# Manual rendering
result = template_content
# Process conditionals
import re
if_pattern = re.compile(r'{%\s*if\s+([^%]+?)\s*%}(.*?){%\s*endif\s*%}', re.DOTALL)
def replace_conditional(match):
condition = match.group(1).strip()
content = match.group(2)
condition_value = bool(test_data.get(condition))
return content if condition_value else ''
result = if_pattern.sub(replace_conditional, result)
# Process variables
for key, value in test_data.items():
placeholder1 = '{{' + key + '}}'
placeholder2 = '{{ ' + key + ' }}'
result = result.replace(placeholder1, str(value))
result = result.replace(placeholder2, str(value))
print("\nTemplate rendering test:")
print("Original template (excerpt):")
print(" {{ doc_number }} - {{ title }} (v{{ version_number }})")
print("Rendered result (excerpt):")
print(f" {test_data['doc_number']} - {test_data['title']} (v{test_data['version_number']})")
# Check for remaining placeholders
remaining = re.findall(r'{{[^}]*}}', result)
if remaining:
print(f"❌ ERROR: Remaining placeholders: {remaining}")
return False
else:
print("✅ SUCCESS: All placeholders replaced correctly!")
# Test 2: CDocs app link verification
print("\n2. Testing CDocs app link presence...")
if 'cdocs_app_url' in result and 'https://cdocs.vicebio.com' in result:
print("✅ SUCCESS: CDocs app link is present and correctly rendered!")
else:
print("❌ ERROR: CDocs app link missing or not rendered correctly!")
return False
# Test 3: Check that templates exist and have correct structure
print("\n3. Checking key email templates...")
template_files = [
'/tf/active/CDocs/templates/email/approval_requested.html',
'/tf/active/CDocs/templates/email/approval_completed.html',
'/tf/active/CDocs/templates/email/approval_reminder.html',
'/tf/active/CDocs/templates/email/approval_overdue.html',
'/tf/active/CDocs/templates/email/document_created.html',
'/tf/active/CDocs/templates/email/document_status_changed.html',
'/tf/active/CDocs/templates/email/review_requested.html'
]
all_templates_ok = True
for template_file in template_files:
if os.path.exists(template_file):
with open(template_file, 'r') as f:
content = f.read()
has_placeholders = '{{ doc_number }}' in content and '{{ title }}' in content
has_cdocs_link = 'cdocs_app_url' in content or 'cdocs.vicebio.com' in content
template_name = os.path.basename(template_file)
if has_placeholders and has_cdocs_link:
print(f" ✅ {template_name}: Has placeholders and CDocs link")
else:
print(f" ❌ {template_name}: Missing placeholders ({has_placeholders}) or CDocs link ({has_cdocs_link})")
all_templates_ok = False
else:
print(f" ❌ {os.path.basename(template_file)}: File not found!")
all_templates_ok = False
if all_templates_ok:
print("✅ SUCCESS: All key templates have correct structure!")
else:
print("❌ ERROR: Some templates have issues!")
return False
print("\n" + "=" * 60)
print("SUMMARY: All tests passed! The fixes should resolve both issues:")
print("1. ✅ Template variables are properly replaced")
print("2. ✅ CDocs app links are present in all email notifications")
print("=" * 60)
return True
Return Value
Returns a boolean value: True if all tests pass (template rendering works correctly, CDocs links are present, and all template files have correct structure), False if any test fails (remaining placeholders found, missing CDocs links, or template structure issues).
Dependencies
reos
Required Imports
import re
import os
Usage Example
# Run the comprehensive test suite
if __name__ == '__main__':
import re
import os
# Execute the test function
test_result = test_fixes()
if test_result:
print('All CDocs email notification tests passed!')
else:
print('Some tests failed. Check output for details.')
sys.exit(1)
Best Practices
- This function is designed for testing purposes and should be run in a development or testing environment where the CDocs template files are accessible
- The function expects specific file paths (/tf/active/CDocs/templates/email/) - ensure these paths exist or modify them for your environment
- The function performs file I/O operations and should have appropriate read permissions for the template directory
- The regex-based template rendering is a simplified implementation for testing; production code should use a proper template engine like Jinja2
- The function prints detailed output to stdout for debugging - consider redirecting or capturing output in automated test environments
- Returns False on first failure to enable early exit in test pipelines
- Template files are expected to use Django/Jinja2-style template syntax with {{ variable }} and {% if condition %} blocks
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function main_v29 82.0% similar
-
function check_fixes 81.3% similar
-
function test_complex_url_hyperlink 58.7% similar
-
function test_basic_functionality 58.3% similar
-
function test_markdown_processing 56.9% similar