107 lines
4.2 KiB
Python
107 lines
4.2 KiB
Python
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
|
||
|
|
|
||
|
|
def test_quick_setup_creates_draft_subscription_and_links_existing_connection(monkeypatch):
|
||
|
|
from app.modules.internet_connections.backend import router as internet_router
|
||
|
|
from app.subscriptions.backend import router as subscription_router
|
||
|
|
|
||
|
|
def fake_query_single(sql, params=None):
|
||
|
|
if "FROM customers" in sql:
|
||
|
|
return {"id": 7, "name": "Testkunde"}
|
||
|
|
if "FROM products" in sql:
|
||
|
|
return {
|
||
|
|
"id": 11,
|
||
|
|
"name": "Internet 1000/1000",
|
||
|
|
"short_description": "Fiber internet",
|
||
|
|
"sales_price": 799,
|
||
|
|
"attributes_json": {"network": {"kind": "internet_access"}},
|
||
|
|
"type": "service",
|
||
|
|
}
|
||
|
|
if "FROM internet_connections_connections" in sql:
|
||
|
|
return {"id": 23, "name": "GC kredsløb", "customer_id": None, "subscription_id": None}
|
||
|
|
if "INSERT INTO sag_sager" in sql:
|
||
|
|
return {"id": 31, "titel": "Internet - Testkunde", "customer_id": 7}
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def fake_create_subscription(payload, current_user=None):
|
||
|
|
assert payload["sag_id"] == 31
|
||
|
|
assert payload["line_items"][0]["product_id"] == 11
|
||
|
|
return {"id": 41, "status": "draft"}
|
||
|
|
|
||
|
|
async def fake_update_connection(connection_id, payload):
|
||
|
|
assert connection_id == 23
|
||
|
|
assert payload.subscription_id == 41
|
||
|
|
assert payload.customer_id == 7
|
||
|
|
return {"id": 23, "subscription_id": 41, "customer_id": 7}
|
||
|
|
|
||
|
|
monkeypatch.setattr(internet_router, "execute_query_single", fake_query_single)
|
||
|
|
monkeypatch.setattr(
|
||
|
|
internet_router,
|
||
|
|
"build_network_product_profile",
|
||
|
|
lambda product, fallback_text=None: {"kind": "internet_access"},
|
||
|
|
)
|
||
|
|
monkeypatch.setattr(internet_router, "_resolve_group_id_by_name_tokens", lambda tokens: 3)
|
||
|
|
monkeypatch.setattr(internet_router, "update_connection", fake_update_connection)
|
||
|
|
monkeypatch.setattr(subscription_router, "create_subscription", fake_create_subscription)
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
app.include_router(internet_router.router, prefix="/api/v1")
|
||
|
|
response = TestClient(app).post(
|
||
|
|
"/api/v1/internet-connections/quick-setup",
|
||
|
|
json={
|
||
|
|
"customer_id": 7,
|
||
|
|
"product_id": 11,
|
||
|
|
"start_date": "2026-10-01",
|
||
|
|
"connection_id": 23,
|
||
|
|
"billing_interval": "monthly",
|
||
|
|
"billing_day": 1,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 200, response.text
|
||
|
|
body = response.json()
|
||
|
|
assert body["subscription"]["status"] == "draft"
|
||
|
|
assert body["connection"]["subscription_id"] == 41
|
||
|
|
|
||
|
|
|
||
|
|
def test_quick_setup_shared_fiber_does_not_require_or_create_subscription(monkeypatch):
|
||
|
|
from app.modules.internet_connections.backend import router as internet_router
|
||
|
|
|
||
|
|
def fake_query_single(sql, params=None):
|
||
|
|
if "LOWER(TRIM(c.name))" in sql:
|
||
|
|
return {"id": 1662, "name": "BMC Networks"}
|
||
|
|
if "SELECT id, name FROM customers" in sql:
|
||
|
|
return {"id": 1662, "name": "BMC Networks"}
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def fake_create_connection(payload):
|
||
|
|
assert payload.customer_id == 1662
|
||
|
|
assert payload.allocation_model == "shared"
|
||
|
|
assert payload.value_type == "delefiber"
|
||
|
|
assert payload.is_manual_shared is True
|
||
|
|
assert payload.subscription_id is None
|
||
|
|
return {"id": 88, "name": payload.name, "customer_id": payload.customer_id}
|
||
|
|
|
||
|
|
monkeypatch.setattr(internet_router, "execute_query_single", fake_query_single)
|
||
|
|
monkeypatch.setattr(internet_router, "create_connection", fake_create_connection)
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
app.include_router(internet_router.router, prefix="/api/v1")
|
||
|
|
response = TestClient(app).post(
|
||
|
|
"/api/v1/internet-connections/quick-setup",
|
||
|
|
json={
|
||
|
|
"shared_fiber": True,
|
||
|
|
"connection_name": "Delt fiber - Testvej 1",
|
||
|
|
"vendor_id": 2,
|
||
|
|
"address": "Testvej 1",
|
||
|
|
"monthly_cost": 1500,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 200, response.text
|
||
|
|
body = response.json()
|
||
|
|
assert body["sag"] is None
|
||
|
|
assert body["subscription"] is None
|
||
|
|
assert body["connection"]["id"] == 88
|