| |
| """ |
| Test script to verify the complete OAuth flow and account creation process. |
| """ |
|
|
| import os |
| import sys |
| import logging |
| import json |
| from datetime import datetime |
|
|
| |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__))) |
|
|
| from flask import Flask |
| from backend.config import Config |
| from backend.utils.database import init_supabase |
| from backend.services.linkedin_service import LinkedInService |
|
|
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| ) |
| logger = logging.getLogger(__name__) |
|
|
| def test_linkedin_service(): |
| """Test LinkedIn service initialization and basic functionality.""" |
| logger.info("π Testing LinkedIn service...") |
| |
| app = Flask(__name__) |
| app.config.from_object(Config) |
| |
| try: |
| |
| supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY']) |
| app.supabase = supabase |
| |
| |
| linkedin_service = LinkedInService() |
| logger.info("β
LinkedIn service initialized successfully") |
| |
| |
| logger.info(f"π LinkedIn Configuration:") |
| logger.info(f" Client ID: {linkedin_service.client_id[:10]}...") |
| logger.info(f" Client Secret: {linkedin_service.client_secret[:10]}...") |
| logger.info(f" Redirect URI: {linkedin_service.redirect_uri}") |
| logger.info(f" Scope: {linkedin_service.scope}") |
| |
| |
| logger.info("π Testing authorization URL generation...") |
| state = "test_state_123" |
| try: |
| auth_url = linkedin_service.get_authorization_url(state) |
| logger.info(f"β
Authorization URL generated successfully") |
| logger.info(f" URL length: {len(auth_url)}") |
| logger.info(f" Contains state: {'state=' + state in auth_url}") |
| except Exception as e: |
| logger.error(f"β Authorization URL generation failed: {str(e)}") |
| return False |
| |
| logger.info("π LinkedIn service test completed successfully!") |
| return True |
| |
| except Exception as e: |
| logger.error(f"β LinkedIn service test failed: {str(e)}") |
| import traceback |
| logger.error(f"β Traceback: {traceback.format_exc()}") |
| return False |
|
|
| def test_account_creation_flow(): |
| """Test the account creation flow with mock data.""" |
| logger.info("π Testing account creation flow...") |
| |
| app = Flask(__name__) |
| app.config.from_object(Config) |
| |
| try: |
| |
| supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY']) |
| app.supabase = supabase |
| |
| |
| test_user_id = "test_user_123" |
| test_account_data = { |
| "social_network": "LinkedIn", |
| "account_name": "Test LinkedIn Account", |
| "id_utilisateur": test_user_id, |
| "token": "test_access_token_12345", |
| "sub": "test_linkedin_id_456", |
| "given_name": "Test", |
| "family_name": "User", |
| "picture": "https://test.com/avatar.jpg" |
| } |
| |
| logger.info(f"π Testing account insertion with data: {test_account_data}") |
| |
| |
| response = ( |
| app.supabase |
| .table("Social_network") |
| .insert(test_account_data) |
| .execute() |
| ) |
| |
| logger.info(f"β
Account insertion response: {response}") |
| logger.info(f"β
Response data: {response.data}") |
| logger.info(f"β
Response error: {getattr(response, 'error', None)}") |
| |
| if response.data: |
| account_id = response.data[0].get('id') |
| logger.info(f"β
Account inserted successfully with ID: {account_id}") |
| |
| |
| logger.info("π Testing account retrieval...") |
| retrieve_response = ( |
| app.supabase |
| .table("Social_network") |
| .select("*") |
| .eq("id_utilisateur", test_user_id) |
| .execute() |
| ) |
| |
| logger.info(f"β
Retrieved {len(retrieve_response.data)} accounts for user {test_user_id}") |
| |
| |
| logger.info("π Testing account deletion...") |
| delete_response = ( |
| app.supabase |
| .table("Social_network") |
| .delete() |
| .eq("id", account_id) |
| .execute() |
| ) |
| |
| logger.info(f"β
Account deletion response: {delete_response}") |
| |
| logger.info("π Account creation flow test completed successfully!") |
| return True |
| else: |
| logger.error("β Account insertion failed - no data returned") |
| return False |
| |
| except Exception as e: |
| logger.error(f"β Account creation flow test failed: {str(e)}") |
| import traceback |
| logger.error(f"β Traceback: {traceback.format_exc()}") |
| return False |
|
|
| def test_oauth_callback_simulation(): |
| """Simulate the OAuth callback process.""" |
| logger.info("π Simulating OAuth callback process...") |
| |
| app = Flask(__name__) |
| app.config.from_object(Config) |
| |
| try: |
| |
| supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY']) |
| app.supabase = supabase |
| |
| |
| test_user_id = "test_user_456" |
| test_code = "test_authorization_code_789" |
| test_state = "test_state_456" |
| test_social_network = "LinkedIn" |
| |
| |
| test_token_response = { |
| "access_token": "test_access_token_456", |
| "token_type": "Bearer", |
| "expires_in": 3600 |
| } |
| |
| |
| test_user_info = { |
| "sub": "test_linkedin_id_789", |
| "name": "Test User", |
| "given_name": "Test", |
| "family_name": "User", |
| "picture": "https://test.com/avatar.jpg" |
| } |
| |
| logger.info(f"π Simulating OAuth callback for user: {test_user_id}") |
| logger.info(f"π Received code: {test_code}") |
| logger.info(f"π Received state: {test_state}") |
| logger.info(f"π Token response: {test_token_response}") |
| logger.info(f"π User info: {test_user_info}") |
| |
| |
| account_data = { |
| "social_network": test_social_network, |
| "account_name": test_user_info.get('name', 'LinkedIn Account'), |
| "id_utilisateur": test_user_id, |
| "token": test_token_response['access_token'], |
| "sub": test_user_info.get('sub'), |
| "given_name": test_user_info.get('given_name'), |
| "family_name": test_user_info.get('family_name'), |
| "picture": test_user_info.get('picture') |
| } |
| |
| logger.info(f"π Inserting account data: {account_data}") |
| |
| response = ( |
| app.supabase |
| .table("Social_network") |
| .insert(account_data) |
| .execute() |
| ) |
| |
| logger.info(f"β
OAuth callback simulation response: {response}") |
| logger.info(f"β
Response data: {response.data}") |
| logger.info(f"β
Response error: {getattr(response, 'error', None)}") |
| |
| if response.data: |
| logger.info("π OAuth callback simulation completed successfully!") |
| |
| |
| account_id = response.data[0].get('id') |
| delete_response = ( |
| app.supabase |
| .table("Social_network") |
| .delete() |
| .eq("id", account_id) |
| .execute() |
| ) |
| logger.info(f"π§Ή Cleaned up test data: {delete_response}") |
| |
| return True |
| else: |
| logger.error("β OAuth callback simulation failed - no data returned") |
| return False |
| |
| except Exception as e: |
| logger.error(f"β OAuth callback simulation failed: {str(e)}") |
| import traceback |
| logger.error(f"β Traceback: {traceback.format_exc()}") |
| return False |
|
|
| def main(): |
| """Main test function.""" |
| logger.info("π Starting OAuth flow tests...") |
| logger.info(f"Test started at: {datetime.now().isoformat()}") |
| |
| |
| linkedin_success = test_linkedin_service() |
| |
| |
| account_success = test_account_creation_flow() |
| |
| |
| oauth_success = test_oauth_callback_simulation() |
| |
| |
| logger.info("π Test Summary:") |
| logger.info(f" LinkedIn Service: {'β
PASS' if linkedin_success else 'β FAIL'}") |
| logger.info(f" Account Creation Flow: {'β
PASS' if account_success else 'β FAIL'}") |
| logger.info(f" OAuth Callback Simulation: {'β
PASS' if oauth_success else 'β FAIL'}") |
| |
| if linkedin_success and account_success and oauth_success: |
| logger.info("π All tests passed! OAuth flow is working correctly.") |
| return 0 |
| else: |
| logger.error("β Some tests failed. Please check the configuration.") |
| return 1 |
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |