function test_authentication
Tests Azure AD authentication for a VendorEmailExtractor instance by attempting to authenticate and providing detailed troubleshooting guidance on failure.
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
78 - 95
simple
Purpose
This function serves as a diagnostic test to verify that Azure AD authentication is properly configured and working. It attempts to authenticate using the provided extractor object and returns a boolean indicating success or failure. On failure, it prints comprehensive troubleshooting steps to help diagnose common authentication issues such as incorrect credentials, expired secrets, missing permissions, or lack of admin consent.
Source Code
def test_authentication(extractor):
"""Test Azure AD authentication"""
print("\n" + "="*60)
print("TESTING AUTHENTICATION")
print("="*60)
try:
extractor.authenticate()
print("✅ Authentication successful!")
return True
except Exception as e:
print(f"❌ Authentication failed: {str(e)}")
print("\nTroubleshooting:")
print("- Verify TENANT_ID, CLIENT_ID, and CLIENT_SECRET are correct")
print("- Check that client secret hasn't expired")
print("- Ensure app has Application permissions (not Delegated)")
print("- Verify admin consent was granted")
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
extractor |
- | - | positional_or_keyword |
Parameter Details
extractor: An instance of VendorEmailExtractor class that has an authenticate() method. This object should be pre-configured with Azure AD credentials (TENANT_ID, CLIENT_ID, CLIENT_SECRET) and is used to test the authentication flow against Azure AD services.
Return Value
Returns a boolean value: True if authentication succeeds (extractor.authenticate() completes without exception), False if authentication fails (any exception is raised during authentication). The function also prints status messages and troubleshooting information to stdout.
Dependencies
vendor_email_extractorvendor_email_config
Required Imports
from vendor_email_extractor import VendorEmailExtractor
Usage Example
from vendor_email_extractor import VendorEmailExtractor
from vendor_email_config import TENANT_ID, CLIENT_ID, CLIENT_SECRET
# Create extractor instance with Azure AD credentials
extractor = VendorEmailExtractor(
tenant_id=TENANT_ID,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET
)
# Test authentication
auth_success = test_authentication(extractor)
if auth_success:
print("Ready to proceed with email extraction")
else:
print("Fix authentication issues before continuing")
Best Practices
- Always run this test function before attempting to use the extractor for actual email extraction operations
- Ensure the extractor object is properly initialized with valid Azure AD credentials before calling this function
- Review the printed troubleshooting steps carefully if authentication fails - they cover the most common configuration issues
- Verify that the Azure AD application has been granted admin consent for the required permissions
- Check that the client secret has not expired in the Azure portal
- Use Application permissions rather than Delegated permissions for service-to-service authentication
- This function prints to stdout, so consider redirecting output if running in automated environments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_azure_token 73.4% similar
-
function test_mailbox_access 71.8% similar
-
function test_configuration_v1 69.1% similar
-
function run_all_tests 67.6% similar
-
function test_graph_api_access 64.8% similar