From 0f65b4b1956d35134c58a1fbac129b3318a4770f Mon Sep 17 00:00:00 2001 From: tulu-g559 Date: Wed, 18 Feb 2026 22:48:22 +0530 Subject: [PATCH 1/2] Removed main() logging --introduced pytest --- test_oauth.py | 192 ++++++++++++++++++++++++-------------------------- 1 file changed, 92 insertions(+), 100 deletions(-) diff --git a/test_oauth.py b/test_oauth.py index 151673e..fa2ad2a 100644 --- a/test_oauth.py +++ b/test_oauth.py @@ -1,110 +1,102 @@ """ -Test script for OAuth functionality -Run this to test OAuth configuration without starting the full app +Pytest-based OAuth Test Suite +Run with: pytest -v """ -import os -import sys +import pytest +from urllib.parse import urlparse from dotenv import load_dotenv # Load environment variables load_dotenv() + +# ----------------------------- +# OAuth Config Tests +# ----------------------------- + def test_oauth_config(): - """Test OAuth configuration""" - print("šŸ” Testing OAuth Configuration...") - - try: - from auth.oauth_config import oauth_config - - print(f"Available providers: {oauth_config.get_available_providers()}") - - for provider in oauth_config.get_available_providers(): - print(f"\nāœ… {provider.upper()} OAuth configured") - provider_config = oauth_config.get_provider(provider) - print(f" Client ID: {provider_config.client_id[:10]}...") - print(f" Redirect URI: {provider_config.redirect_uri}") - - if not oauth_config.get_available_providers(): - print("āŒ No OAuth providers configured") - print("Please set up your OAuth credentials in .env file") - return False - - return True - - except Exception as e: - print(f"āŒ Error testing OAuth config: {e}") - return False - -def test_database(): - """Test database initialization""" - print("\nšŸ—„ļø Testing Database...") - - try: - from auth.auth_utils import init_db - init_db() - print("āœ… Database initialized successfully") - return True - except Exception as e: - print(f"āŒ Error initializing database: {e}") - return False - -def test_oauth_utils(): - """Test OAuth utility functions""" - print("\nšŸ”§ Testing OAuth Utils...") - - try: - from auth.oauth_utils import generate_state, store_oauth_state, verify_oauth_state - - # Test state generation - state = generate_state() - print(f"āœ… State generated: {state[:10]}...") - - # Test state storage and verification - store_oauth_state(state, "google") - verified_provider = verify_oauth_state(state) - - if verified_provider == "google": - print("āœ… State storage and verification working") - else: - print("āŒ State verification failed") - return False - - return True - - except Exception as e: - print(f"āŒ Error testing OAuth utils: {e}") - return False - -def main(): - """Run all tests""" - print("šŸš€ TalkHeal OAuth Test Suite") - print("=" * 40) - - tests = [ - test_oauth_config, - test_database, - test_oauth_utils - ] - - passed = 0 - total = len(tests) - - for test in tests: - if test(): - passed += 1 - - print("\n" + "=" * 40) - print(f"šŸ“Š Test Results: {passed}/{total} tests passed") - - if passed == total: - print("šŸŽ‰ All tests passed! OAuth is ready to use.") - else: - print("āš ļø Some tests failed. Please check the configuration.") - - return passed == total - -if __name__ == "__main__": - success = main() - sys.exit(0 if success else 1) + """Ensure at least one OAuth provider is configured properly""" + from auth.oauth_config import oauth_config + + providers = oauth_config.get_available_providers() + assert isinstance(providers, list) + + assert len(providers) > 0, ( + "No OAuth providers configured. " + "Ensure credentials are set in .env" + ) + + for provider in providers: + config = oauth_config.get_provider(provider) + + assert config.client_id is not None + assert config.client_secret is not None + assert config.redirect_uri is not None + + # Validate redirect URI structure + parsed = urlparse(config.redirect_uri) + assert parsed.scheme in ["http", "https"] + assert parsed.netloc != "" + + +# ----------------------------- +# Database Tests +# ----------------------------- + +def test_database_initialization(): + """Ensure database initializes without error""" + from auth.auth_utils import init_db + + # Should not raise any exception + init_db() + + +# ----------------------------- +# OAuth Utility Tests +# ----------------------------- + +def test_state_generation_entropy(): + """Ensure generated states are unique and sufficiently long""" + from auth.oauth_utils import generate_state + + states = {generate_state() for _ in range(300)} + + # Ensure uniqueness + assert len(states) == 300 + + # Ensure reasonable entropy length + for state in states: + assert len(state) >= 32 + + +def test_state_storage_and_verification(): + """Ensure stored state verifies correctly""" + from auth.oauth_utils import generate_state, store_oauth_state, verify_oauth_state + + state = generate_state() + store_oauth_state(state, "google") + + provider = verify_oauth_state(state) + assert provider == "google" + + +def test_state_reuse_protection(): + """Ensure state cannot be reused (prevents replay attacks)""" + from auth.oauth_utils import generate_state, store_oauth_state, verify_oauth_state + + state = generate_state() + store_oauth_state(state, "google") + + # First verification should pass + assert verify_oauth_state(state) == "google" + + # Second verification should fail + assert verify_oauth_state(state) is None + + +def test_invalid_state_rejected(): + """Ensure invalid state is rejected""" + from auth.oauth_utils import verify_oauth_state + assert verify_oauth_state("invalid-state") is None From 159ae9e7413ccd34a8137b158268eb3db43c7b62 Mon Sep 17 00:00:00 2001 From: tulu-g559 Date: Wed, 18 Feb 2026 22:51:14 +0530 Subject: [PATCH 2/2] np.unicode_ warning --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index cdbdf2e..36b2de7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,7 +19,7 @@ streamlit_js_eval extra-streamlit-components scikit-learn joblib -numpy +numpy #<2.0 (recomended) PyJWT statsmodels prophet