Bug fixes for uploading the files. I though the changes being made where not supposed to break legacy endpoints. Not sure what happened. Either way things are almost back to normal.

This commit is contained in:
Scott Idem
2026-01-22 16:49:03 -05:00
parent 48d9e38c39
commit 1e6b9d1c18
8 changed files with 521 additions and 17 deletions

View File

@@ -0,0 +1,73 @@
import requests
import io
import json
# Configuration
BASE_URL = "https://dev-api.oneskyit.com"
API_KEY = "IDF68Em5X4HTZlswRNgepQ"
ACCOUNT_ID = "Q8lR8Ai8hx2FjbQ3C_EH1Q"
LINK_TO_TYPE = "archive_content"
LINK_TO_ID = "bZOa7CtUm0E"
def test_live_file_uploads():
print(f"--- Starting Live E2E Upload Tests against {BASE_URL} ---")
headers = {
"X-Aether-API-Key": API_KEY,
"x-account-id": ACCOUNT_ID # Route expects this as a header
}
# 1. Single File Upload
print("\n[Test 1] Single File Upload...")
files = [
("file_list", ("e2e_test_single.txt", io.BytesIO(b"Live E2E Single Upload Test Content"), "text/plain"))
]
data = {
"account_id": ACCOUNT_ID,
"link_to_type": LINK_TO_TYPE,
"link_to_id": LINK_TO_ID
}
url = f"{BASE_URL}/hosted_file/upload_files"
try:
response = requests.post(url, headers=headers, files=files, data=data)
print(f"Status: {response.status_code}")
if response.status_code == 200:
result = response.json()
file_data = result.get('data', [])[0]
print(f"✅ Success! Created hosted_file_id: {file_data.get('id')}")
print(f"Response snippet: {json.dumps(file_data, indent=2)[:200]}...")
else:
print(f"❌ Failed: {response.text}")
return
except Exception as e:
print(f"💥 Exception: {e}")
return
# 2. Triple File Upload
print("\n[Test 2] Triple File Upload...")
files = [
("file_list", ("e2e_multi_1.txt", io.BytesIO(b"Content 1"), "text/plain")),
("file_list", ("e2e_multi_2.txt", io.BytesIO(b"Content 2"), "text/plain")),
("file_list", ("e2e_multi_3.txt", io.BytesIO(b"Content 3"), "text/plain")),
]
try:
response = requests.post(url, headers=headers, files=files, data=data)
print(f"Status: {response.status_code}")
if response.status_code == 200:
result = response.json()
data_list = result.get('data', [])
print(f"✅ Success! Uploaded {len(data_list)} files.")
for i, f in enumerate(data_list):
print(f" File {i+1} ID: {f.get('id')} | Name: {f.get('filename')}")
else:
print(f"❌ Failed: {response.text}")
except Exception as e:
print(f"💥 Exception: {e}")
if __name__ == "__main__":
test_live_file_uploads()

View File

@@ -0,0 +1,140 @@
import sys
import os
import io
import json
from unittest.mock import MagicMock, patch
# Add project root to path
sys.path.append(os.getcwd())
# --- Robust Mocking BEFORE App Imports ---
mock_config = MagicMock()
mock_settings = MagicMock()
# Mock DB settings to prevent SQLAlchemy unpack errors
mock_settings.DB = {
'server': 'localhost',
'port': 3306,
'username': 'user',
'password': 'pass',
'database': 'db',
'connect_timeout': 10,
'pool_recycle': 3600,
'pool_timeout': 30,
'pool_pre_ping': True
}
mock_settings.REDIS = {
'server': 'localhost',
'port': 6379
}
mock_settings.FILES_PATH = {
'hosted_files_root': '/tmp/aether_test_files'
}
mock_config.settings = mock_settings
sys.modules["app.config"] = mock_config
# Mock other low-level deps
sys.modules["redis"] = MagicMock()
sys.modules["app.log"] = MagicMock()
# Mock DB engine and session
mock_sql_core = MagicMock()
mock_sql_core.engine = MagicMock()
sys.modules["app.lib_sql_core"] = mock_sql_core
# --- End Mocking ---
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
# Valid random IDs (22 chars)
ACCOUNT_ID_RANDOM = "Q8lR8Ai8hx2FjbQ3C_EH1Q"
LINK_TO_ID_RANDOM = "bZOa7CtUm0E8hx2FjbQ3C_" # Padded to 22
LINK_TO_TYPE = "archive_content"
def test_single_file_upload():
print("--- Testing Single File Upload Logic ---")
file_content = b"Test content"
files = [("file_list", ("test.txt", io.BytesIO(file_content), "text/plain"))]
data = {
"account_id": ACCOUNT_ID_RANDOM,
"link_to_type": LINK_TO_TYPE,
"link_to_id": LINK_TO_ID_RANDOM
}
headers = {"x-account-id": ACCOUNT_ID_RANDOM}
# Patch the internal methods to avoid real FS/DB ops while testing route flow
with patch('app.routers.hosted_file.save_file', return_value={'saved': True, 'already_exists': False, 'hash_sha256': 'abc', 'extension_allowed': True, 'copy_timer': 0.1, 'filename': 'test.txt', 'extension': 'txt'}), \
patch('app.routers.hosted_file.create_hosted_file_obj', return_value=123), \
patch('app.routers.hosted_file.load_hosted_file_obj', return_value=MagicMock(dict=lambda **kwargs: {'id': 'NEW_ID', 'hosted_file_id': 'NEW_ID', 'filename': 'test.txt'})), \
patch('app.routers.hosted_file.create_hosted_file_link', return_value=True), \
patch('app.routers.hosted_file.redis_lookup_id_random', return_value=1):
response = client.post("/hosted_file/upload_files", files=files, data=data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
file_resp = result["data"][0]
print(f"Result ID: {file_resp.get('id')}")
assert file_resp.get('id') is not None
print("✅ Single file upload logic verified.")
return True
else:
print(f"FAILED: {response.text}")
return False
def test_triple_file_upload():
print("\n--- Testing Triple File Upload Logic ---")
files = [
("file_list", ("file1.txt", io.BytesIO(b"1"), "text/plain")),
("file_list", ("file2.txt", io.BytesIO(b"2"), "text/plain")),
("file_list", ("file3.txt", io.BytesIO(b"3"), "text/plain")),
]
data = {
"account_id": ACCOUNT_ID_RANDOM,
"link_to_type": LINK_TO_TYPE,
"link_to_id": LINK_TO_ID_RANDOM
}
headers = {"x-account-id": ACCOUNT_ID_RANDOM}
with patch('app.routers.hosted_file.save_file', side_effect=[
{'saved': True, 'already_exists': False, 'hash_sha256': 'h1', 'extension_allowed': True, 'copy_timer': 0.1, 'filename': 'f1.txt', 'extension': 'txt'},
{'saved': True, 'already_exists': False, 'hash_sha256': 'h2', 'extension_allowed': True, 'copy_timer': 0.1, 'filename': 'f2.txt', 'extension': 'txt'},
{'saved': True, 'already_exists': False, 'hash_sha256': 'h3', 'extension_allowed': True, 'copy_timer': 0.1, 'filename': 'f3.txt', 'extension': 'txt'},
]), \
patch('app.routers.hosted_file.create_hosted_file_obj', return_value=123), \
patch('app.routers.hosted_file.load_hosted_file_obj', side_effect=[
MagicMock(dict=lambda **kwargs: {'id': 'ID1', 'hosted_file_id': 'ID1'}),
MagicMock(dict=lambda **kwargs: {'id': 'ID2', 'hosted_file_id': 'ID2'}),
MagicMock(dict=lambda **kwargs: {'id': 'ID3', 'hosted_file_id': 'ID3'}),
]), \
patch('app.routers.hosted_file.create_hosted_file_link', return_value=True), \
patch('app.routers.hosted_file.redis_lookup_id_random', return_value=1):
response = client.post("/hosted_file/upload_files", files=files, data=data, headers=headers)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"Result Count: {len(result['data'])}")
assert len(result['data']) == 3
for i, item in enumerate(result['data']):
print(f" File {i+1} ID: {item.get('id')}")
assert item.get('id') is not None
print("✅ Triple file upload logic verified.")
return True
else:
print(f"FAILED: {response.text}")
return False
if __name__ == "__main__":
s1 = test_single_file_upload()
s2 = test_triple_file_upload()
if s1 and s2:
print("\n🎉 ALL LOGIC TESTS PASSED!")
else:
sys.exit(1)

View File

@@ -0,0 +1,70 @@
import sys
import os
from unittest.mock import MagicMock
# Add project root to path
sys.path.append(os.getcwd())
# Mock dependencies to allow importing the model without side effects
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()
# Mock app.db_sql
mock_db_sql = MagicMock()
mock_db_sql.get_id_random.return_value = "random_str_abc"
sys.modules["app.db_sql"] = mock_db_sql
from app.models.hosted_file_models import Hosted_File_Base
def test_hosted_file_model_id_mapping():
print("--- Testing Hosted_File_Base ID Mapping ---")
# 1. Test simulation of database record (integers)
db_record = {
"id": 123,
"id_random": "random_str_abc",
"account_id": 456,
"account_id_random": "acc_rand_xyz",
"filename": "test.txt",
"extension": "txt"
}
model = Hosted_File_Base(**db_record)
result = model.dict(by_alias=True)
print(f"Model Data (by_alias=True): {result}")
# Verify that the frontend sees string IDs
assert result.get("id") == "random_str_abc"
assert result.get("hosted_file_id") == "random_str_abc"
assert result.get("account_id") == "acc_rand_xyz"
# 2. Test simulation of manual creation with mixed IDs
manual_data = {
"account_id": 456, # already resolved integer
"account_id_random": "acc_rand_xyz",
"id_random": "new_file_id"
}
model2 = Hosted_File_Base(**manual_data)
result2 = model2.dict(by_alias=True)
print(f"Model Data 2 (by_alias=True): {result2}")
assert result2.get("account_id") == "acc_rand_xyz"
assert result2.get("id") == "new_file_id"
print("✅ Hosted_File_Base ID mapping verified.")
if __name__ == "__main__":
try:
test_hosted_file_model_id_mapping()
print("\n🎉 MODEL LOGIC TEST PASSED!")
except Exception as e:
print(f"\n❌ TEST FAILED: {e}")
import traceback
traceback.print_exc()
sys.exit(1)

View File

@@ -0,0 +1,71 @@
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)

View File

@@ -0,0 +1,138 @@
import sys
import os
import asyncio
from unittest.mock import MagicMock, AsyncMock, patch
# Add project root to path
sys.path.append(os.getcwd())
# Mock EVERYTHING before imports
mock_config = MagicMock()
mock_settings = MagicMock()
mock_settings.DB = {'connect_timeout': 10}
mock_config.settings = mock_settings
sys.modules["app.config"] = mock_config
sys.modules["redis"] = MagicMock()
sys.modules["app.log"] = MagicMock()
sys.modules["app.lib_general"] = MagicMock()
sys.modules["app.db_sql"] = MagicMock()
sys.modules["app.db_connection"] = MagicMock()
# Import the target router
import app.routers.hosted_file as router_mod
from app.models.hosted_file_models import Hosted_File_Base
async def test_upload_files_logic_flow():
print("--- Testing upload_files() Logic Flow (Unit) ---")
# Mock parameters
mock_file = MagicMock()
mock_file.filename = "test.txt"
mock_file.content_type = "text/plain"
file_list = [mock_file]
account_id_rand = "Q8lR8Ai8hx2FjbQ3C_EH1Q"
link_to_type = "archive_content"
link_to_id_rand = "bZOa7CtUm0E8hx2FjbQ3C_"
# Mock internal function returns
save_file_ret = {
'saved': True,
'already_exists': False,
'hash_sha256': 'mock_hash',
'extension_allowed': True,
'copy_timer': 0.1,
'filename': 'test.txt',
'extension': 'txt',
'subdirectory_path': 'mo'
}
# Mock load_hosted_file_obj to return a model that prioritizes strings
mock_model = Hosted_File_Base(
id="FILE_RAND_ID",
hosted_file_id="FILE_RAND_ID",
account_id=account_id_rand,
filename="test.txt"
)
with patch('app.routers.hosted_file.redis_lookup_id_random', side_effect=[1, 2]), \
patch('app.routers.hosted_file.save_file', AsyncMock(return_value=save_file_ret)), \
patch('app.routers.hosted_file.create_hosted_file_obj', return_value=123), \
patch('app.routers.hosted_file.load_hosted_file_obj', return_value=mock_model.dict(by_alias=True)), \
patch('app.routers.hosted_file.create_hosted_file_link', return_value=True), \
patch('app.routers.hosted_file.mk_resp', side_effect=lambda data, **kwargs: data):
# Call the router function directly
result = await router_mod.upload_files(
file_list=file_list,
account_id=account_id_rand,
link_to_type=link_to_type,
link_to_id=link_to_id_rand,
x_account_id=account_id_rand,
response=MagicMock()
)
print(f"Result List Count: {len(result)}")
file_resp = result[0]
print(f"File Response Keys: {list(file_resp.keys())}")
print(f"File ID: {file_resp.get('id')}")
assert len(result) == 1
assert file_resp.get('id') == "FILE_RAND_ID"
assert file_resp.get('hosted_file_id') == "FILE_RAND_ID"
print("✅ Single upload flow verified.")
async def test_triple_upload_logic_flow():
print("\n--- Testing Triple upload_files() Logic Flow (Unit) ---")
file_list = [MagicMock(filename=f"f{i}.txt") for i in range(3)]
account_id_rand = "Q8lR8Ai8hx2FjbQ3C_EH1Q"
save_file_side_effect = [
{'saved': True, 'already_exists': False, 'hash_sha256': f'h{i}', 'extension_allowed': True, 'copy_timer': 0.1, 'filename': f'f{i}.txt', 'extension': 'txt'}
for i in range(3)
]
load_hosted_side_effect = [
{'id': f'ID_{i}', 'hosted_file_id': f'ID_{i}', 'filename': f'f{i}.txt'}
for i in range(3)
]
with patch('app.routers.hosted_file.redis_lookup_id_random', return_value=1), \
patch('app.routers.hosted_file.save_file', AsyncMock(side_effect=save_file_side_effect)), \
patch('app.routers.hosted_file.create_hosted_file_obj', return_value=123), \
patch('app.routers.hosted_file.load_hosted_file_obj', side_effect=load_hosted_side_effect), \
patch('app.routers.hosted_file.create_hosted_file_link', return_value=True), \
patch('app.routers.hosted_file.mk_resp', side_effect=lambda data, **kwargs: data):
result = await router_mod.upload_files(
file_list=file_list,
account_id=account_id_rand,
link_to_type="archive_content",
link_to_id="some_id",
x_account_id=account_id_rand,
response=MagicMock()
)
print(f"Result List Count: {len(result)}")
assert len(result) == 3
for i, item in enumerate(result):
print(f" File {i+1} ID: {item.get('id')}")
assert item.get('id') == f'ID_{i}'
print("✅ Triple upload flow verified.")
async def main():
await test_upload_files_logic_flow()
await test_triple_upload_logic_flow()
if __name__ == "__main__":
try:
asyncio.run(main())
print("\n🎉 ALL UPLOAD FLOW TESTS PASSED!")
except Exception as e:
print(f"\n❌ TEST FAILED: {e}")
import traceback
traceback.print_exc()
sys.exit(1)