function test_onedrive_auth
Asynchronous function that tests OneDrive authentication by loading configuration from a JSON file and attempting to obtain an access token.
/tf/active/vicechatdev/e-ink-llm/test_mixed_mode.py
89 - 119
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
asynciojsonpathlibonedrive_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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function load_onedrive_config 77.1% similar
-
class OneDriveClient 66.9% similar
-
function test_azure_token 63.3% similar
-
function test_o365_connection 63.0% similar
-
function test_mixed_mode_dry_run 61.6% similar