""" Simple test script for verify_sonarr_api function Run this script to manually test the API verification """ import requests from unittest.mock import patch, MagicMock # Configuration (same as in your main script) SONARR_API_KEY = "2537de37fded4874ae83da9cf3c14f34" SONARR_SERVER_URL = "http://192.168.50.111:8989" def verify_sonarr_api(): """Copy of the function from plexsonarr.py for testing""" url = f"{SONARR_SERVER_URL}/api/v3/system/status?apikey={SONARR_API_KEY}" try: response = requests.get(url) response.raise_for_status() print("โœ… Sonarr API is alive and the token is correct.") return True except requests.exceptions.RequestException as e: print(f"โŒ Failed to verify Sonarr API: {e}") return False def test_with_mock_success(): """Test with a mocked successful response""" print("\n๐Ÿงช Testing with mocked SUCCESS response...") with patch('requests.get') as mock_get: # Setup mock mock_response = MagicMock() mock_response.raise_for_status.return_value = None mock_response.status_code = 200 mock_get.return_value = mock_response # Test result = verify_sonarr_api() # Verify assert result == True mock_get.assert_called_once() print("โœ… Mock success test passed!") def test_with_mock_failure(): """Test with a mocked failure response""" print("\n๐Ÿงช Testing with mocked FAILURE response...") with patch('requests.get') as mock_get: # Setup mock to raise an exception mock_get.side_effect = requests.exceptions.ConnectionError("Mocked connection error") # Test result = verify_sonarr_api() # Verify assert result == False print("โœ… Mock failure test passed!") def test_with_mock_http_error(): """Test with a mocked HTTP error (401 Unauthorized)""" print("\n๐Ÿงช Testing with mocked HTTP ERROR response...") with patch('requests.get') as mock_get: # Setup mock mock_response = MagicMock() mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("401 Unauthorized") mock_response.status_code = 401 mock_get.return_value = mock_response # Test result = verify_sonarr_api() # Verify assert result == False print("โœ… Mock HTTP error test passed!") def test_real_api(): """Test with real API call""" print("\n๐ŸŒ Testing with REAL API call...") print(f"๐Ÿ“ก Calling: {SONARR_SERVER_URL}/api/v3/system/status") result = verify_sonarr_api() if result: print("โœ… Real API test passed! Your Sonarr API is working.") else: print("โŒ Real API test failed. Check your configuration.") return result if __name__ == "__main__": print("๐Ÿš€ Starting verify_sonarr_api tests...") print("=" * 50) # Run mock tests test_with_mock_success() test_with_mock_failure() test_with_mock_http_error() # Ask user if they want to test real API print("\n" + "=" * 50) user_input = input("Do you want to test the real API? (y/n): ").lower().strip() if user_input in ['y', 'yes']: success = test_real_api() if success: print("\n๐ŸŽ‰ All tests completed successfully!") else: print("\n๐Ÿ’ก Tip: Check your SONARR_SERVER_URL and SONARR_API_KEY configuration") else: print("\nโœ… Mock tests completed successfully!") print("\n๐Ÿ“‹ Test Summary:") print("- Mock success test: โœ…") print("- Mock failure test: โœ…") print("- Mock HTTP error test: โœ…") if user_input in ['y', 'yes']: print(f"- Real API test: {'โœ…' if success else 'โŒ'}")