🔍 Code Extractor

function test_mailbox_access

Maturity: 42

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

File:
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
Lines:
98 - 124
Complexity:
simple

Purpose

This function serves as a diagnostic test to verify that the application has proper permissions and configuration to access Microsoft 365 mailboxes. It attempts to retrieve up to 5 mailboxes, displays their information, and provides detailed troubleshooting guidance if access fails. This is typically used during setup or debugging to validate Microsoft Graph API connectivity and permissions.

Source Code

def test_mailbox_access(extractor):
    """Test accessing mailboxes"""
    print("\n" + "="*60)
    print("TESTING MAILBOX ACCESS")
    print("="*60)
    
    try:
        mailboxes = extractor.get_mailboxes(max_mailboxes=5)
        
        if mailboxes:
            print(f"✅ Successfully accessed {len(mailboxes)} mailboxes")
            print("\nSample mailboxes:")
            for mb in mailboxes[:5]:
                print(f"  - {mb.get('displayName')} ({mb.get('mail')})")
            return True
        else:
            print("⚠️  No mailboxes found in organization")
            print("   This might be normal if filtering by domain")
            return True
            
    except Exception as e:
        print(f"❌ Failed to access mailboxes: {str(e)}")
        print("\nTroubleshooting:")
        print("- Verify app has Mail.Read application permission")
        print("- Verify app has User.Read.All application permission")
        print("- Ensure 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 been initialized with Microsoft Graph API credentials (tenant_id, client_id, client_secret). This object provides the get_mailboxes() method to interact with Microsoft Graph API.

Return Value

Returns a boolean value: True if mailbox access was successful (including cases where no mailboxes are found, which may be normal), or False if an exception occurred indicating a permission or configuration problem. The function also prints detailed status messages and troubleshooting information to stdout.

Dependencies

  • vendor_email_extractor
  • pathlib
  • sys

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

# Initialize the extractor
extractor = VendorEmailExtractor(
    tenant_id=TENANT_ID,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET
)

# Test mailbox access
success = test_mailbox_access(extractor)

if success:
    print("Mailbox access test passed")
else:
    print("Mailbox access test failed - check permissions")

Best Practices

  • Always initialize the VendorEmailExtractor with valid credentials before calling this function
  • Ensure Azure AD application has been granted admin consent for required permissions before testing
  • Use this function during initial setup to validate configuration before attempting to extract emails
  • Review the printed troubleshooting messages if the test fails to identify permission issues
  • The function considers finding zero mailboxes as a success case, which may occur with domain filtering
  • This is a diagnostic function that prints to stdout, so it's best used in testing/setup scripts rather than production code

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_email_search 76.5% similar

    Tests the email search functionality of a VendorEmailExtractor instance by searching for emails containing common business terms in the first available mailbox.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function test_authentication 71.8% 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
  • function main_v27 70.1% similar

    Demonstrates example usage of the VendorEmailExtractor class by searching for vendor emails across Office 365 mailboxes and displaying results.

    From: /tf/active/vicechatdev/find_email/vendor_email_extractor.py
  • function run_all_tests 67.1% 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 main_v57 62.6% similar

    Performs a comprehensive status check of an email forwarder service, verifying process status, port availability, SMTP communication, and configuration settings.

    From: /tf/active/vicechatdev/email-forwarder/service_status.py
← Back to Browse