function test_mailbox_access
Tests the ability to access and retrieve mailboxes from Microsoft Graph API through a VendorEmailExtractor instance, displaying results and troubleshooting information.
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
98 - 124
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_extractorpathlibsys
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_email_search 76.5% similar
-
function test_authentication 71.8% similar
-
function main_v27 70.1% similar
-
function run_all_tests 67.1% similar
-
function main_v57 62.6% similar