🔍 Code Extractor

function test_authentication

Maturity: 44

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

File:
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
Lines:
78 - 95
Complexity:
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_extractor
  • vendor_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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_azure_token 73.4% similar

    Tests Azure AD authentication by attempting to acquire an OAuth2 access token using client credentials flow for Microsoft Graph API access.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function test_mailbox_access 71.8% similar

    Tests the ability to access and retrieve mailboxes from Microsoft Graph API through a VendorEmailExtractor instance, displaying results and troubleshooting information.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function test_configuration_v1 69.1% similar

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

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function run_all_tests 67.6% similar

    Orchestrates a comprehensive test suite for the Vendor Email Extractor system, verifying configuration, authentication, mailbox access, email search, and LLM connectivity.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function test_graph_api_access 64.8% similar

    Tests Microsoft Graph API access by obtaining an OAuth2 token and verifying connectivity to check tenant settings for SharePoint integration.

    From: /tf/active/vicechatdev/SPFCsync/check_tenant_config.py
← Back to Browse