#!/usr/bin/env python3 """ OAuth Endpoint Test Suite Tests OAuth endpoints with real implementation """ from datetime import datetime import os import sys # Add backend to path sys.path.insert(0, '/Users/rushiparikh/projects/atom/backend') def test_oauth_configs(): """Test OAuth configurations are loaded""" print("\n" + "=" * 70) print("TESTING OAUTH CONFIGURATIONS") print("=" * 70) from core.oauth_handler import ( GITHUB_OAUTH_CONFIG, GOOGLE_OAUTH_CONFIG, MICROSOFT_OAUTH_CONFIG, SLACK_OAUTH_CONFIG, ) # Test Google OAuth Config print("\n1. Google OAuth Config:") client_id_display = f"{GOOGLE_OAUTH_CONFIG.client_id[:20]}..." if GOOGLE_OAUTH_CONFIG.client_id else 'NOT SET' print(f" Client ID: {client_id_display}") print(f" Redirect URI: {GOOGLE_OAUTH_CONFIG.redirect_uri}") print(f" Scopes: {len(GOOGLE_OAUTH_CONFIG.scopes)} scopes defined") print(f" ✅ Google OAuth config loaded") # Test Microsoft OAuth Config print("\n2. Microsoft OAuth Config:") ms_client_id = f"{MICROSOFT_OAUTH_CONFIG.client_id[:20]}..." if MICROSOFT_OAUTH_CONFIG.client_id else 'NOT SET' print(f" Client ID: {ms_client_id}") print(f" Redirect URI: {MICROSOFT_OAUTH_CONFIG.redirect_uri}") print(f" Scopes: {len(MICROSOFT_OAUTH_CONFIG.scopes)} scopes defined") print(f" ✅ Microsoft OAuth config loaded") # Test Slack OAuth Config print("\n3. Slack OAuth Config:") slack_client_id_display = f"{SLACK_OAUTH_CONFIG.client_id[:20]}..." if SLACK_OAUTH_CONFIG.client_id else 'NOT SET' print(f" Client ID: {slack_client_id_display}") print(f" Redirect URI: {SLACK_OAUTH_CONFIG.redirect_uri}") print(f" Scopes: {len(SLACK_OAUTH_CONFIG.scopes)} scopes defined") print(f" ✅ Slack OAuth config loaded") # Test GitHub OAuth Config print("\n4. GitHub OAuth Config:") github_client_id = f"{GITHUB_OAUTH_CONFIG.client_id[:20]}..." if GITHUB_OAUTH_CONFIG.client_id else 'NOT SET' print(f" Client ID: {github_client_id}") print(f" Redirect URI: {GITHUB_OAUTH_CONFIG.redirect_uri}") print(f" Scopes: {len(GITHUB_OAUTH_CONFIG.scopes)} scopes defined") print(f" ✅ GitHub OAuth config loaded") print("\n" + "=" * 70) print("✅ ALL OAUTH CONFIGURATIONS LOADED SUCCESSFULLY") print("=" * 70) return True def test_oauth_authorization_urls(): """Test OAuth authorization URL generation""" print("\n" + "=" * 70) print("TESTING OAUTH AUTHORIZATION URL GENERATION") print("=" * 70) from core.oauth_handler import GOOGLE_OAUTH_CONFIG, SLACK_OAUTH_CONFIG, OAuthHandler # Test Google OAuth URL generation print("\n1. Google OAuth URL:") try: google_handler = OAuthHandler(GOOGLE_OAUTH_CONFIG) google_auth_url = google_handler.get_authorization_url(state="test_state") print(f" URL: {google_auth_url[:80]}...") assert "accounts.google.com" in google_auth_url assert "client_id=" in google_auth_url assert "redirect_uri=" in google_auth_url assert "state=test_state" in google_auth_url print(" ✅ Google OAuth URL generated correctly") except Exception as e: print(f" ❌ Google OAuth URL generation failed: {e}") # Test Slack OAuth URL generation print("\n2. Slack OAuth URL:") try: slack_handler = OAuthHandler(SLACK_OAUTH_CONFIG) slack_auth_url = slack_handler.get_authorization_url(state="test_state") print(f" URL: {slack_auth_url[:80]}...") assert "slack.com" in slack_auth_url assert "client_id=" in slack_auth_url assert "redirect_uri=" in slack_auth_url assert "state=test_state" in slack_auth_url print(" ✅ Slack OAuth URL generated correctly") except Exception as e: print(f" ❌ Slack OAuth URL generation failed: {e}") print("\n" + "=" * 70) print("✅ AUTHORIZATION URL GENERATION WORKING") print("=" * 70) return True def test_token_storage(): """Test token storage mechanism""" print("\n" + "=" * 70) print("TESTING TOKEN STORAGE") print("=" * 70) from core.token_storage import token_storage # Test saving a token print("\n1. Saving token:") test_token = { "access_token": "test_access_token_123", "refresh_token": "test_refresh_token_456", "expires_at": (datetime.now().timestamp() + 3600) } try: token_storage.save_token("test_provider", test_token) print(" ✅ Token saved successfully") except Exception as e: print(f" ❌ Failed to save token: {e}") # Test retrieving a token print("\n2. Retrieving token:") try: retrieved = token_storage.get_token("test_provider") assert retrieved is not None assert retrieved["access_token"] == "test_access_token_123" print(f" ✅ Token retrieved: {retrieved['access_token'][:20]}...") except Exception as e: print(f" ❌ Failed to retrieve token: {e}") # Test deleting a token print("\n3. Deleting token:") try: token_storage.delete_token("test_provider") print(" ✅ Token deleted successfully") except Exception as e: print(f" ❌ Failed to delete token: {e}") print("\n" + "=" * 70) print("✅ TOKEN STORAGE WORKING") print("=" * 70) return True def test_connection_service(): """Test ConnectionService for OAuth connections""" print("\n" + "=" * 70) print("TESTING CONNECTION SERVICE") print("=" * 70) try: from core.connection_service import ConnectionService print(" ✅ ConnectionService imported") except Exception as e: print(f" ❌ ConnectionService import failed: {e}") return False # Test creating a connection print("\n1. Creating connection:") try: conn_service = ConnectionService() print(" ✅ ConnectionService instantiated") except Exception as e: print(f" ❌ ConnectionService instantiation failed: {e}") print("\n" + "=" * 70) print("✅ CONNECTION SERVICE AVAILABLE") print("=" * 70) return True def test_slack_credentials(): """Test Slack OAuth credentials from environment""" print("\n" + "=" * 70) print("TESTING SLACK OAUTH CREDENTIALS") print("=" * 70) slack_client_id = os.getenv("SLACK_CLIENT_ID") slack_client_secret = os.getenv("SLACK_CLIENT_SECRET") slack_redirect_uri = os.getenv("SLACK_REDIRECT_URI") print("\n1. Environment Variables:") slack_id_display = f"{slack_client_id[:20]}..." if slack_client_id else 'NOT SET' print(f" SLACK_CLIENT_ID: {slack_id_display}") print(f" SLACK_CLIENT_SECRET: {'SET' if slack_client_secret else 'NOT SET'}") print(f" SLACK_REDIRECT_URI: {slack_redirect_uri or 'NOT SET'}") if slack_client_id and slack_client_secret and slack_redirect_uri: print("\n ✅ All Slack OAuth credentials are configured") else: print("\n ⚠️ Some Slack OAuth credentials are missing") print("\n" + "=" * 70) print("✅ SLACK CREDENTIALS CHECK COMPLETE") print("=" * 70) return True def main(): """Run all OAuth endpoint tests""" print("\n" + "=" * 70) print("ATOM OAUTH ENDPOINT TEST SUITE") print(f"Timestamp: {datetime.now().isoformat()}") print("=" * 70) tests = [ ("OAuth Configurations", test_oauth_configs), ("OAuth Authorization URLs", test_oauth_authorization_urls), ("Token Storage", test_token_storage), ("Connection Service", test_connection_service), ("Slack Credentials", test_slack_credentials), ] results = {} passed = 0 failed = 0 for test_name, test_func in tests: try: if test_func(): results[test_name] = "✅ PASSED" passed += 1 except Exception as e: results[test_name] = f"❌ FAILED: {e}" failed += 1 import traceback traceback.print_exc() # Summary print("\n" + "=" * 70) print("TEST SUMMARY") print("=" * 70) for test_name, result in results.items(): print(f"{test_name}: {result}") print(f"\nTotal Tests: {len(tests)}") print(f"Passed: {passed}") print(f"Failed: {failed}") print(f"Success Rate: {(passed / len(tests) * 100):.1f}%") print("=" * 70) return failed == 0 if __name__ == "__main__": success = main() sys.exit(0 if success else 1)