72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
import sys
|
|
import os
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
# Add project root to path
|
|
sys.path.append(os.getcwd())
|
|
|
|
# Mock dependencies
|
|
mock_config = MagicMock()
|
|
mock_config.settings = MagicMock()
|
|
sys.modules["app.config"] = mock_config
|
|
sys.modules["redis"] = MagicMock()
|
|
sys.modules["app.log"] = MagicMock()
|
|
sys.modules["app.lib_general"] = MagicMock()
|
|
|
|
# Valid random IDs are typically 11 or 22 chars
|
|
VALID_RAND_ID = "Q8lR8Ai8hx2FjbQ3C_EH1Q"
|
|
|
|
from app.lib_redis_helpers import lookup_id_random_pop
|
|
|
|
def test_hosted_file_resolver_logic():
|
|
print("--- Testing ID Resolver logic (lookup_id_random_pop) ---")
|
|
|
|
# Correctly patch the function where it is DEFINED
|
|
with patch('app.lib_redis_helpers.redis_lookup_id_random', side_effect=lambda record_id_random, table_name: 123 if record_id_random == VALID_RAND_ID else 999):
|
|
|
|
# 1. Test Vision-style payload (account_id is a string)
|
|
payload = {
|
|
"account_id": VALID_RAND_ID,
|
|
"filename": "test.txt"
|
|
}
|
|
|
|
result = lookup_id_random_pop(payload.copy())
|
|
print(f"Vision Payload Result: {result}")
|
|
|
|
# Verify it resolved the string to an integer
|
|
assert result.get("account_id") == 123
|
|
|
|
# 2. Test Legacy-style payload (account_id_random is a string)
|
|
payload_legacy = {
|
|
"account_id_random": VALID_RAND_ID,
|
|
"filename": "test.txt"
|
|
}
|
|
|
|
result_legacy = lookup_id_random_pop(payload_legacy.copy())
|
|
print(f"Legacy Payload Result: {result_legacy}")
|
|
|
|
# Verify it resolved and popped the random key
|
|
assert result_legacy.get("account_id") == 123
|
|
assert "account_id_random" not in result_legacy
|
|
|
|
# 3. Test mixed/polymorphic
|
|
payload_poly = {
|
|
"link_to_type": "archive_content",
|
|
"link_to_id": VALID_RAND_ID
|
|
}
|
|
result_poly = lookup_id_random_pop(payload_poly.copy())
|
|
print(f"Polymorphic Payload Result: {result_poly}")
|
|
assert result_poly.get("link_to_id") == 123
|
|
|
|
print("✅ ID Resolver logic verified.")
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
test_hosted_file_resolver_logic()
|
|
print("\n🎉 RESOLVER LOGIC TEST PASSED!")
|
|
except Exception as e:
|
|
print(f"\n❌ TEST FAILED: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|