direct_test.pyโข5.95 kB
#!/usr/bin/env python3
"""
Direct Test for Gmail MCP Server
This script directly tests the Gmail functionality without MCP protocol complexity.
It imports and tests the GmailMCPServer class directly.
"""
import asyncio
import json
import time
from gmail_mcp_server import GmailMCPServer
class DirectGmailTester:
def __init__(self):
self.server = GmailMCPServer()
async def test_authentication(self):
"""Test Gmail authentication directly"""
print("๐ Testing Gmail Authentication...")
try:
# Call the authentication method directly
result = await self.server._authenticate_gmail()
if result and len(result) > 0:
print("โ
Authentication successful!")
print(f"Result: {result[0].text}")
return True
else:
print("โ Authentication failed - no result")
return False
except Exception as e:
print(f"โ Authentication error: {e}")
return False
async def test_list_emails(self):
"""Test listing emails directly"""
print("\n๐ง Testing List Emails...")
try:
result = await self.server._list_emails({"max_results": 3})
if result and len(result) > 0:
print("โ
Email listing successful!")
print(f"Result: {result[0].text[:200]}...") # Show first 200 chars
return True
else:
print("โ Email listing failed - no result")
return False
except Exception as e:
print(f"โ Email listing error: {e}")
return False
async def test_send_email(self, recipient: str):
"""Test sending email directly"""
print(f"\nโ๏ธ Testing Send Email to {recipient}...")
test_subject = "Gmail MCP Server Direct Test Email"
test_body = f"""
Hello!
This is a test email sent directly from the Gmail MCP Server testing suite.
Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}
Test Type: Direct Function Call Test
Server: Gmail MCP Server v1.0.0
This email was sent automatically as part of the direct testing process.
Best regards,
Gmail MCP Server Direct Test Suite
""".strip()
try:
result = await self.server._send_email({
"to": recipient,
"subject": test_subject,
"body": test_body
})
if result and len(result) > 0:
print("โ
Email sent successfully!")
print(f"Result: {result[0].text}")
return True
else:
print("โ Email sending failed - no result")
return False
except Exception as e:
print(f"โ Email sending error: {e}")
return False
async def test_search_emails(self):
"""Test searching emails directly"""
print("\n๐ Testing Search Emails...")
try:
result = await self.server._search_emails({
"query": "is:unread",
"max_results": 3
})
if result and len(result) > 0:
print("โ
Email search successful!")
print(f"Result: {result[0].text[:200]}...") # Show first 200 chars
return True
else:
print("โ Email search failed - no result")
return False
except Exception as e:
print(f"โ Email search error: {e}")
return False
async def run_all_tests(self, test_email: str):
"""Run all tests"""
print("๐งช Gmail MCP Server Direct Test Suite")
print("=" * 60)
print(f"Test Email: {test_email}")
print("=" * 60)
results = {}
# Test authentication
results["authentication"] = await self.test_authentication()
if not results["authentication"]:
print("\nโ Authentication failed - skipping other tests")
print("Please check your GCP OAuth credentials and Gmail API access")
return results
# Test other functionality
results["list_emails"] = await self.test_list_emails()
results["search_emails"] = await self.test_search_emails()
results["send_email"] = await self.test_send_email(test_email)
return results
def print_results(self, results: dict):
"""Print test results"""
print("\n" + "=" * 60)
print("๐ DIRECT TEST RESULTS")
print("=" * 60)
total_tests = len(results)
passed_tests = sum(1 for result in results.values() if result)
for test_name, result in results.items():
status = "โ
PASS" if result else "โ FAIL"
print(f"{test_name:20} {status}")
print("-" * 60)
print(f"Total Tests: {total_tests}")
print(f"Passed: {passed_tests}")
print(f"Failed: {total_tests - passed_tests}")
print(f"Success Rate: {(passed_tests/total_tests)*100:.1f}%")
if passed_tests == total_tests:
print("\n๐ ALL DIRECT TESTS PASSED!")
print("The Gmail MCP Server core functionality is working correctly!")
else:
print(f"\nโ ๏ธ {total_tests - passed_tests} tests failed.")
print("Check the error messages above for details.")
async def main():
"""Main test function"""
tester = DirectGmailTester()
test_email = "navdeepsinghdhangar@gmail.com"
results = await tester.run_all_tests(test_email)
tester.print_results(results)
if __name__ == "__main__":
asyncio.run(main())