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 validation tests: (1) verifies that template variables are correctly replaced with actual data using regex-based conditional and variable substitution, (2) confirms that CDocs application links are present and properly 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 in the CDocs system work correctly after fixes.
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. The function also prints detailed test results to stdout.
Dependencies
osresys
Required Imports
import os
import re
import sys
Usage Example
import os
import re
import sys
# Ensure the CDocs templates directory exists
if not os.path.exists('/tf/active/CDocs/templates/email/'):
print('Warning: Template directory not found')
# Run the comprehensive test
result = test_fixes()
if result:
print('All CDocs email notification tests passed!')
else:
print('Some tests failed. Check output for details.')
Best Practices
- This function expects specific file paths (/tf/active/CDocs/templates/email/) - ensure these paths exist before running
- The function performs file I/O operations and should have appropriate read permissions
- Template rendering uses simple regex-based substitution rather than a full template engine - suitable for basic variable replacement
- The function prints extensive output to stdout for debugging purposes
- Returns False on first failure, so later tests may not run if earlier ones fail
- The regex pattern for conditionals only handles simple {% if variable %} blocks, not complex expressions
- Template files are expected to be HTML format with specific placeholder syntax
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function check_fixes 81.8% similar
-
function main_v4 80.5% similar
-
function test_complex_url_hyperlink 58.9% similar
-
function test_template_with_data 55.7% similar
-
function test_markdown_link_parsing 55.2% similar