function test_rest_client
A test function that validates the SharePoint REST API client by testing authentication, document listing, and file download capabilities.
/tf/active/vicechatdev/SPFCsync/test_rest_client.py
16 - 59
moderate
Purpose
This function serves as an integration test for the SharePointRestClient class. It verifies that the client can successfully authenticate with SharePoint using Azure credentials, retrieve a list of documents from a specified SharePoint path, and download file content. The function provides detailed console output showing the test progress and results, making it useful for debugging and validation during development or deployment.
Source Code
def test_rest_client():
"""Test the REST-based SharePoint client."""
print("Testing SharePoint REST API Client")
print("=" * 40)
try:
from sharepoint_rest_client import SharePointRestClient
from config import Config
print("Creating SharePoint REST client...")
client = SharePointRestClient(
Config.SHAREPOINT_SITE_URL,
Config.AZURE_CLIENT_ID,
Config.AZURE_CLIENT_SECRET
)
print("✅ SharePoint REST client created successfully")
# Test document listing
print("\nTesting document listing...")
documents = client.get_all_documents(Config.SHAREPOINT_DOCUMENTS_PATH)
print(f"✅ Found {len(documents)} documents")
if documents:
print("\nSample documents:")
for doc in documents[:3]: # Show first 3
print(f" - {doc['name']} (size: {doc['size']} bytes, modified: {doc['modified']})")
if len(documents) > 3:
print(f" ... and {len(documents) - 3} more")
# Test file download (if there are documents)
if documents:
print(f"\nTesting file download for: {documents[0]['name']}")
content = client.download_file_content(documents[0]['server_relative_url'])
if content:
print(f"✅ Successfully downloaded {len(content)} bytes")
else:
print("❌ Failed to download file content")
return True
except Exception as e:
print(f"❌ SharePoint REST client failed: {e}")
return False
Return Value
Returns a boolean value: True if all SharePoint REST client operations (initialization, document listing, and file download) succeed without exceptions; False if any exception occurs during testing. The return value can be used to determine if the SharePoint integration is working correctly.
Dependencies
sharepoint_rest_clientconfig
Required Imports
from sharepoint_rest_client import SharePointRestClient
from config import Config
Conditional/Optional Imports
These imports are only needed under specific conditions:
from sharepoint_rest_client import SharePointRestClient
Condition: imported inside try block, only loaded when function executes
Required (conditional)from config import Config
Condition: imported inside try block, only loaded when function executes
Required (conditional)Usage Example
# Ensure config.py has required settings:
# SHAREPOINT_SITE_URL = 'https://yourcompany.sharepoint.com/sites/yoursite'
# AZURE_CLIENT_ID = 'your-client-id'
# AZURE_CLIENT_SECRET = 'your-client-secret'
# SHAREPOINT_DOCUMENTS_PATH = 'Shared Documents'
# Run the test
result = test_rest_client()
if result:
print('SharePoint REST client test passed')
else:
print('SharePoint REST client test failed')
# Example output:
# Testing SharePoint REST API Client
# ========================================
# Creating SharePoint REST client...
# ✅ SharePoint REST client created successfully
# Testing document listing...
# ✅ Found 15 documents
# Sample documents:
# - report.pdf (size: 524288 bytes, modified: 2024-01-15)
# - data.xlsx (size: 102400 bytes, modified: 2024-01-14)
# - notes.docx (size: 51200 bytes, modified: 2024-01-13)
Best Practices
- Ensure all required configuration values are set in Config before running this test
- This function prints output directly to console, so it's best used in testing/debugging contexts rather than production code
- The function catches all exceptions broadly, which is appropriate for a test function but may hide specific error details
- Only the first 3 documents are displayed to avoid cluttering console output with large document lists
- The function tests file download only on the first document found, so ensure at least one document exists in the SharePoint path
- Run this function in an environment where SharePoint credentials are properly configured and accessible
- The function returns boolean for success/failure, making it suitable for use in test suites or CI/CD pipelines
- Consider running this test during deployment validation to ensure SharePoint connectivity is working
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_graph_client 83.6% similar
-
function test_sharepoint_api_call 77.9% similar
-
function test_sharepoint_listing 76.4% similar
-
function test_sharepoint_with_token 76.0% similar
-
function test_sharepoint_connection 75.5% similar