🔍 Code Extractor

function test_onedrive_auth

Maturity: 46

Asynchronous function that tests OneDrive authentication by loading configuration from a JSON file and attempting to obtain an access token.

File:
/tf/active/vicechatdev/e-ink-llm/test_mixed_mode.py
Lines:
89 - 119
Complexity:
simple

Purpose

This function validates OneDrive API authentication setup by reading credentials from 'onedrive_config.json', initializing an OneDrive client, and verifying that an access token can be successfully obtained. It's designed as a diagnostic tool to ensure OneDrive integration is properly configured before attempting actual operations. The function handles missing configuration gracefully and provides clear console feedback about the authentication status.

Source Code

async def test_onedrive_auth():
    """Test OneDrive authentication"""
    print("🔐 Testing OneDrive authentication...")
    
    # Load OneDrive config
    config_file = Path("onedrive_config.json")
    if not config_file.exists():
        print("⚠️ OneDrive config file not found - skipping OneDrive test")
        return False
    
    try:
        with open(config_file, 'r') as f:
            config = json.load(f)
        
        if not config.get('client_id'):
            print("⚠️ OneDrive client_id not found in config - skipping OneDrive test")
            return False
        
        client = OneDriveClient(config)
        token = await client.get_access_token()
        
        if token:
            print("✅ OneDrive authentication successful")
            return True
        else:
            print("❌ OneDrive authentication failed")
            return False
        
    except Exception as e:
        print(f"❌ OneDrive authentication failed: {e}")
        return False

Return Value

Returns a boolean value: True if OneDrive authentication succeeds and an access token is obtained, False if authentication fails, configuration is missing, or any exception occurs during the process. The function also prints status messages to console indicating the outcome.

Dependencies

  • asyncio
  • json
  • pathlib
  • onedrive_client

Required Imports

import asyncio
import json
from pathlib import Path
from onedrive_client import OneDriveClient

Usage Example

import asyncio
import json
from pathlib import Path
from onedrive_client import OneDriveClient

async def test_onedrive_auth():
    """Test OneDrive authentication"""
    print("🔐 Testing OneDrive authentication...")
    
    config_file = Path("onedrive_config.json")
    if not config_file.exists():
        print("⚠️ OneDrive config file not found - skipping OneDrive test")
        return False
    
    try:
        with open(config_file, 'r') as f:
            config = json.load(f)
        
        if not config.get('client_id'):
            print("⚠️ OneDrive client_id not found in config - skipping OneDrive test")
            return False
        
        client = OneDriveClient(config)
        token = await client.get_access_token()
        
        if token:
            print("✅ OneDrive authentication successful")
            return True
        else:
            print("❌ OneDrive authentication failed")
            return False
        
    except Exception as e:
        print(f"❌ OneDrive authentication failed: {e}")
        return False

# Run the test
if __name__ == "__main__":
    result = asyncio.run(test_onedrive_auth())
    print(f"Authentication result: {result}")

Best Practices

  • Ensure onedrive_config.json exists and contains valid OneDrive API credentials before calling this function
  • This function should be called using asyncio.run() or within an existing async context
  • The function gracefully handles missing configuration files and returns False rather than raising exceptions
  • Console output uses emoji indicators for easy visual parsing of test results
  • Consider implementing proper logging instead of print statements for production use
  • The function performs basic validation (checking for client_id) but relies on OneDriveClient for full credential validation
  • Sensitive credentials should be stored securely and not committed to version control
  • The function catches all exceptions broadly - consider more specific exception handling for production code

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function load_onedrive_config 77.1% similar

    Loads OneDrive configuration from a JSON file and returns it as a dictionary, with error handling for missing or invalid files.

    From: /tf/active/vicechatdev/e-ink-llm/main.py
  • class OneDriveClient 66.9% similar

    A comprehensive Microsoft OneDrive client that uses the Microsoft Graph API to authenticate and perform file operations (upload, download, list, delete) on OneDrive storage.

    From: /tf/active/vicechatdev/e-ink-llm/onedrive_client.py
  • function test_azure_token 63.3% 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_o365_connection 63.0% similar

    Tests the connection to Microsoft Office 365 (O365) by attempting to obtain an authentication token through the O365Client.

    From: /tf/active/vicechatdev/email-forwarder/test_service.py
  • function test_mixed_mode_dry_run 61.6% similar

    Asynchronous test function that validates mixed mode initialization by testing reMarkable Cloud authentication and OneDrive configuration without processing any files.

    From: /tf/active/vicechatdev/e-ink-llm/test_mixed_mode.py
← Back to Browse