test: cover all market reference import types

parent 8cab77fa
......@@ -588,7 +588,7 @@ async def api_import_market_listing(request: Request, listing_id: int):
owner_username = listing.get('owner_username')
reference_type_map = {
'provider': 'provider',
'model': 'provider',
'model': 'model',
'rotation': 'rotation',
'autoselect': 'autoselect',
}
......
......@@ -73,13 +73,104 @@ class MarketReferenceImportDbStub:
},
"is_active": True,
}
self.rotation_listing = {
"id": 56,
"owner_user_id": 7,
"owner_username": "seller",
"source_scope": "user",
"source_type": "rotation",
"source_id": "seller-rotation",
"listing_key": "rotation:seller-rotation",
"title": "Seller Rotation",
"description": "Shared rotation",
"provider_id": None,
"model_id": None,
"endpoint": None,
"price_per_million_tokens": 2.0,
"price_per_1000_requests": 0.3,
"provider_price_per_million_tokens": None,
"provider_price_per_1000_requests": None,
"currency_code": "USD",
"metadata": {"provider_type": "rotation"},
"config_snapshot": {
"providers": ["seller-provider-a", "seller-provider-b"],
"weights": {"seller-provider-a": 2, "seller-provider-b": 1},
"api_key": "rotation-secret-key",
},
"is_active": True,
}
self.model_listing = {
"id": 58,
"owner_user_id": 7,
"owner_username": "seller",
"source_scope": "user",
"source_type": "model",
"source_id": "seller-provider/seller-model",
"listing_key": "model:seller-provider/seller-model",
"title": "Seller Model",
"description": "Shared model",
"provider_id": "seller-provider",
"model_id": "seller-model",
"endpoint": "https://seller.example/v1",
"price_per_million_tokens": 1.7,
"price_per_1000_requests": 0.25,
"provider_price_per_million_tokens": 1.0,
"provider_price_per_1000_requests": 0.2,
"currency_code": "USD",
"metadata": {"provider_type": "openai"},
"config_snapshot": {
"provider": {
"type": "openai",
"endpoint": "https://seller.example/v1",
"api_key": "model-provider-secret",
},
"model": {
"name": "seller-model",
"api_key": "model-secret-key",
"temperature": 0.2,
},
},
"is_active": True,
}
self.autoselect_listing = {
"id": 57,
"owner_user_id": 7,
"owner_username": "seller",
"source_scope": "user",
"source_type": "autoselect",
"source_id": "seller-autoselect",
"listing_key": "autoselect:seller-autoselect",
"title": "Seller Autoselect",
"description": "Shared autoselect",
"provider_id": None,
"model_id": None,
"endpoint": None,
"price_per_million_tokens": 3.0,
"price_per_1000_requests": 0.4,
"provider_price_per_million_tokens": None,
"provider_price_per_1000_requests": None,
"currency_code": "USD",
"metadata": {"provider_type": "autoselect"},
"config_snapshot": {
"available_models": ["alpha", "beta"],
"fallback": "seller-provider-a/model-a",
"provider_token": "autoselect-secret-token",
},
"is_active": True,
}
def get_market_settings(self):
return dict(self.market_settings)
def get_market_listing(self, listing_id):
if listing_id == self.listing["id"]:
return dict(self.listing)
listings = {
self.listing["id"]: self.listing,
self.rotation_listing["id"]: self.rotation_listing,
self.model_listing["id"]: self.model_listing,
self.autoselect_listing["id"]: self.autoselect_listing,
}
if listing_id in listings:
return dict(listings[listing_id])
return None
def create_market_import_reference(self, **kwargs):
......@@ -234,3 +325,203 @@ def test_import_market_listing_reference_path_does_not_expose_seller_secret_fiel
assert "seller-secret-key" not in serialized_created_args
assert "nested-secret" not in serialized_created_args
assert "api_key" not in serialized_created_args
def test_import_market_listing_creates_market_reference_for_rotation(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.rotation_listing['id']}/import", json={})
assert response.status_code == 200
assert response.json() == {
"success": True,
"imported_config_type": "market_reference",
"imported_config_id": 1,
}
assert db.created_references == [
{
"user_id": 11,
"listing_id": 56,
"reference_type": "rotation",
"display_name": "Seller Rotation",
"owner_username": "seller",
"source_type": "rotation",
"source_id": "seller-rotation",
}
]
assert db.recorded_imports == [
{
"user_id": 11,
"listing_id": 56,
"imported_config_type": "market_reference",
"imported_config_id": "1",
}
]
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
def test_rotation_import_reference_path_does_not_expose_seller_secret_fields(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.rotation_listing['id']}/import", json={})
assert response.status_code == 200
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
reference = db.get_market_import_reference(1)
assert reference["listing_id"] == 56
serialized_reference = json.dumps(reference)
serialized_created_args = json.dumps(db.created_references[0])
assert "rotation-secret-key" not in serialized_reference
assert "seller-provider-a" not in serialized_reference
assert "seller-provider-b" not in serialized_reference
assert "providers" not in serialized_reference
assert "weights" not in serialized_reference
assert "rotation-secret-key" not in serialized_created_args
assert "seller-provider-a" not in serialized_created_args
assert "seller-provider-b" not in serialized_created_args
assert "providers" not in serialized_created_args
assert "weights" not in serialized_created_args
def test_import_market_listing_creates_market_reference_for_model(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.model_listing['id']}/import", json={})
assert response.status_code == 200
assert response.json() == {
"success": True,
"imported_config_type": "market_reference",
"imported_config_id": 1,
}
assert db.created_references == [
{
"user_id": 11,
"listing_id": 58,
"reference_type": "model",
"display_name": "Seller Model",
"owner_username": "seller",
"source_type": "model",
"source_id": "seller-provider/seller-model",
}
]
assert db.recorded_imports == [
{
"user_id": 11,
"listing_id": 58,
"imported_config_type": "market_reference",
"imported_config_id": "1",
}
]
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
def test_model_import_reference_path_does_not_expose_seller_secret_fields(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.model_listing['id']}/import", json={})
assert response.status_code == 200
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
reference = db.get_market_import_reference(1)
assert reference["listing_id"] == 58
serialized_reference = json.dumps(reference)
serialized_created_args = json.dumps(db.created_references[0])
assert "model-provider-secret" not in serialized_reference
assert "model-secret-key" not in serialized_reference
assert "api_key" not in serialized_reference
assert "temperature" not in serialized_reference
assert "model-provider-secret" not in serialized_created_args
assert "model-secret-key" not in serialized_created_args
assert "api_key" not in serialized_created_args
assert "temperature" not in serialized_created_args
def test_import_market_listing_creates_market_reference_for_autoselect(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.autoselect_listing['id']}/import", json={})
assert response.status_code == 200
assert response.json() == {
"success": True,
"imported_config_type": "market_reference",
"imported_config_id": 1,
}
assert db.created_references == [
{
"user_id": 11,
"listing_id": 57,
"reference_type": "autoselect",
"display_name": "Seller Autoselect",
"owner_username": "seller",
"source_type": "autoselect",
"source_id": "seller-autoselect",
}
]
assert db.recorded_imports == [
{
"user_id": 11,
"listing_id": 57,
"imported_config_type": "market_reference",
"imported_config_id": "1",
}
]
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
def test_autoselect_import_reference_path_does_not_expose_seller_secret_fields(monkeypatch):
db = MarketReferenceImportDbStub()
client = TestClient(app)
_login_as_user(client)
monkeypatch.setattr(dashboard_market, "DatabaseRegistry", RegistryStub(db))
response = client.post(f"/api/market/listings/{db.autoselect_listing['id']}/import", json={})
assert response.status_code == 200
assert db.saved_user_providers == []
assert db.saved_user_rotations == []
assert db.saved_user_autoselects == []
reference = db.get_market_import_reference(1)
assert reference["listing_id"] == 57
serialized_reference = json.dumps(reference)
serialized_created_args = json.dumps(db.created_references[0])
assert "autoselect-secret-token" not in serialized_reference
assert "seller-provider-a/model-a" not in serialized_reference
assert "available_models" not in serialized_reference
assert "fallback" not in serialized_reference
assert "autoselect-secret-token" not in serialized_created_args
assert "seller-provider-a/model-a" not in serialized_created_args
assert "available_models" not in serialized_created_args
assert "fallback" not in serialized_created_args
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment