Add Plex Sonarr integration script and testing framework
- Implemented a Python script for integrating Plex Media Server with Sonarr to manage TV show seasons based on viewing habits. - Added features for automated season management, exclusion list support, and detailed logging. - Created a requirements.txt file to include necessary packages: plexapi, requests, pytest, pytest-mock, and requests-mock. - Developed unit tests for the API verification function using pytest and requests_mock. - Included a standalone script to unmonitor excluded shows in Sonarr. - Added a test script for manual API verification with mock and real API tests.
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
"""
|
||||
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 '❌'}")
|
||||
Reference in New Issue
Block a user