šŸ” Code Extractor

function test_configuration_v1

Maturity: 42

Validates that all required configuration variables (Azure AD credentials, OpenAI API key, and domain) are properly set and not using placeholder values.

File:
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
Lines:
31 - 75
Complexity:
simple

Purpose

This function performs a comprehensive validation check of configuration settings required for the vendor email extraction system. It verifies that Azure AD authentication credentials (TENANT_ID, CLIENT_ID, CLIENT_SECRET), OpenAI API key, and domain settings are configured with actual values rather than placeholder defaults. The function provides clear visual feedback with checkmarks for valid configurations and error messages for missing or invalid settings, making it useful as a pre-flight check before running the main application.

Source Code

def test_configuration():
    """Test that configuration is properly set"""
    print("\n" + "="*60)
    print("TESTING CONFIGURATION")
    print("="*60)
    
    errors = []
    
    # Check Tenant ID
    if not TENANT_ID or TENANT_ID == "your-tenant-id-here":
        errors.append("āŒ TENANT_ID not configured")
    else:
        print(f"āœ“ TENANT_ID: {TENANT_ID}")
    
    # Check Client ID
    if not CLIENT_ID or CLIENT_ID == "your-client-id-here":
        errors.append("āŒ CLIENT_ID not configured")
    else:
        print(f"āœ“ CLIENT_ID: {CLIENT_ID}")
    
    # Check Client Secret
    if not CLIENT_SECRET or CLIENT_SECRET == "your-client-secret-here":
        errors.append("āŒ CLIENT_SECRET not configured")
    else:
        print(f"āœ“ CLIENT_SECRET: {'*' * 20} (hidden)")
    
    # Check OpenAI API Key
    if not OPENAI_API_KEY:
        errors.append("āŒ OPENAI_API_KEY not configured")
    else:
        print(f"āœ“ OPENAI_API_KEY: {'*' * 20} (hidden)")
    
    # Check Domain
    if not DOMAIN:
        errors.append("āŒ DOMAIN not configured")
    else:
        print(f"āœ“ DOMAIN: {DOMAIN}")
    
    if errors:
        print("\n" + "\n".join(errors))
        print("\nāš ļø  Please fix configuration errors and try again")
        return False
    
    print("\nāœ… Configuration looks good!")
    return True

Return Value

Returns a boolean value: True if all configuration variables are properly set with non-placeholder values, False if any configuration errors are detected. The function also prints detailed status information to stdout, including which configurations passed validation and which failed.

Required Imports

from vendor_email_config import TENANT_ID
from vendor_email_config import CLIENT_ID
from vendor_email_config import CLIENT_SECRET
from vendor_email_config import OPENAI_API_KEY
from vendor_email_config import DOMAIN

Usage Example

# Ensure vendor_email_config.py exists with required variables
# Example vendor_email_config.py:
# TENANT_ID = "12345678-1234-1234-1234-123456789abc"
# CLIENT_ID = "87654321-4321-4321-4321-cba987654321"
# CLIENT_SECRET = "your-secret-value"
# OPENAI_API_KEY = "sk-proj-..."
# DOMAIN = "example.com"

from vendor_email_config import TENANT_ID, CLIENT_ID, CLIENT_SECRET, OPENAI_API_KEY, DOMAIN

def test_configuration():
    # ... function code ...
    pass

# Run the configuration test
if __name__ == "__main__":
    config_valid = test_configuration()
    if config_valid:
        print("Proceeding with application startup...")
    else:
        print("Cannot start application due to configuration errors")
        sys.exit(1)

Best Practices

  • Run this function before initializing the main application to catch configuration errors early
  • Never log or display the actual values of CLIENT_SECRET or OPENAI_API_KEY in production environments
  • Ensure the vendor_email_config.py file is included in .gitignore to prevent credential exposure
  • Consider using environment variables instead of a config file for better security in production
  • The function checks for common placeholder values ('your-tenant-id-here', etc.) to catch incomplete configuration
  • Use the boolean return value to conditionally proceed with application execution or exit gracefully

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function check_configuration_v1 77.4% similar

    Validates the presence of a .env configuration file and checks that all required environment variables (TENANT_ID, CLIENT_ID, CLIENT_SECRET, FROM_EMAIL) are defined.

    From: /tf/active/vicechatdev/email-forwarder/run_service.py
  • function test_configuration_v3 72.5% similar

    A test function that validates the presence and loading of required Microsoft 365 and SMTP configuration settings from a settings module.

    From: /tf/active/vicechatdev/email-forwarder/test_imports.py
  • function validate_config 70.2% similar

    Validates the presence of required Microsoft 365 configuration settings and raises an error if any are missing.

    From: /tf/active/vicechatdev/email-forwarder/src/config/settings.py
  • function test_config 70.0% similar

    A test function that validates the presence and correctness of all required configuration settings for a multi-model RAG (Retrieval-Augmented Generation) system.

    From: /tf/active/vicechatdev/docchat/test_model_selection.py
  • function test_authentication 69.1% similar

    Tests Azure AD authentication for a VendorEmailExtractor instance by attempting to authenticate and providing detailed troubleshooting guidance on failure.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
← Back to Browse