@@ -2389,7 +2389,7 @@ async def admin_edit_gateway(
23892389 request : Request ,
23902390 db : Session = Depends (get_db ),
23912391 user : str = Depends (require_auth ),
2392- ) -> RedirectResponse :
2392+ ) -> JSONResponse :
23932393 """Edit a gateway via the admin UI.
23942394
23952395 Expects form fields:
@@ -2419,52 +2419,68 @@ async def admin_edit_gateway(
24192419 >>> gateway_id = "gateway-to-edit"
24202420 >>>
24212421 >>> # Happy path: Edit gateway successfully
2422- >>> form_data_success = FormData([("name", "Updated Gateway"), ("url", "http://updated.com"), ("is_inactive_checked", "false"), ("auth_type", "basic")]) # Added auth_type
2422+ >>> form_data_success = FormData([
2423+ ... ("name", "Updated Gateway"),
2424+ ... ("url", "http://updated.com"),
2425+ ... ("is_inactive_checked", "false"),
2426+ ... ("auth_type", "basic"),
2427+ ... ("auth_username", "user"),
2428+ ... ("auth_password", "pass")
2429+ ... ])
24232430 >>> mock_request_success = MagicMock(spec=Request, scope={"root_path": ""})
24242431 >>> mock_request_success.form = AsyncMock(return_value=form_data_success)
24252432 >>> original_update_gateway = gateway_service.update_gateway
24262433 >>> gateway_service.update_gateway = AsyncMock()
24272434 >>>
24282435 >>> async def test_admin_edit_gateway_success():
24292436 ... response = await admin_edit_gateway(gateway_id, mock_request_success, mock_db, mock_user)
2430- ... return isinstance(response, RedirectResponse ) and response.status_code == 303 and "/admin#gateways" in response.headers["location"]
2437+ ... return isinstance(response, JSONResponse ) and response.status_code == 200 and json.loads( response.body)["success"] is True
24312438 >>>
24322439 >>> asyncio.run(test_admin_edit_gateway_success())
24332440 True
24342441 >>>
2435- >>> # Edge case: Edit gateway with inactive checkbox checked
2436- >>> form_data_inactive = FormData([("name", "Inactive Edit"), ("url", "http://inactive.com"), ("is_inactive_checked", "true"), ("auth_type", "basic")]) # Added auth_type
2437- >>> mock_request_inactive = MagicMock(spec=Request, scope={"root_path": "/api"})
2438- >>> mock_request_inactive.form = AsyncMock(return_value=form_data_inactive)
2439- >>>
2440- >>> async def test_admin_edit_gateway_inactive_checked():
2441- ... response = await admin_edit_gateway(gateway_id, mock_request_inactive, mock_db, mock_user)
2442- ... return isinstance(response, RedirectResponse) and response.status_code == 303 and "/api/admin/?include_inactive=true#gateways" in response.headers["location"]
2443- >>>
2444- >>> asyncio.run(test_admin_edit_gateway_inactive_checked())
2445- True
2446- >>>
2442+ # >>> # Edge case: Edit gateway with inactive checkbox checked
2443+ # >>> form_data_inactive = FormData([("name", "Inactive Edit"), ("url", "http://inactive.com"), ("is_inactive_checked", "true"), ("auth_type", "basic"), ("auth_username", "user"),
2444+ # ... ("auth_password", "pass")]) # Added auth_type
2445+ # >>> mock_request_inactive = MagicMock(spec=Request, scope={"root_path": "/api"})
2446+ # >>> mock_request_inactive.form = AsyncMock(return_value=form_data_inactive)
2447+ # >>>
2448+ # >>> async def test_admin_edit_gateway_inactive_checked():
2449+ # ... response = await admin_edit_gateway(gateway_id, mock_request_inactive, mock_db, mock_user)
2450+ # ... return isinstance(response, RedirectResponse) and response.status_code == 303 and "/api/admin/?include_inactive=true#gateways" in response.headers["location"]
2451+ # >>>
2452+ # >>> asyncio.run(test_admin_edit_gateway_inactive_checked())
2453+ # True
2454+ # >>>
24472455 >>> # Error path: Simulate an exception during update
2448- >>> form_data_error = FormData([("name", "Error Gateway"), ("url", "http://error.com"), ("auth_type", "basic")]) # Added auth_type
2456+ >>> form_data_error = FormData([("name", "Error Gateway"), ("url", "http://error.com"), ("auth_type", "basic"),("auth_username", "user"),
2457+ ... ("auth_password", "pass")]) # Added auth_type
24492458 >>> mock_request_error = MagicMock(spec=Request, scope={"root_path": ""})
24502459 >>> mock_request_error.form = AsyncMock(return_value=form_data_error)
24512460 >>> gateway_service.update_gateway = AsyncMock(side_effect=Exception("Update failed"))
24522461 >>>
24532462 >>> async def test_admin_edit_gateway_exception():
24542463 ... response = await admin_edit_gateway(gateway_id, mock_request_error, mock_db, mock_user)
2455- ... return isinstance(response, RedirectResponse) and response.status_code == 303 and "/admin#gateways" in response.headers["location"]
2464+ ... return (
2465+ ... isinstance(response, JSONResponse)
2466+ ... and response.status_code == 500
2467+ ... and json.loads(response.body)["success"] is False
2468+ ... and "Update failed" in json.loads(response.body)["message"]
2469+ ... )
24562470 >>>
24572471 >>> asyncio.run(test_admin_edit_gateway_exception())
24582472 True
24592473 >>>
24602474 >>> # Error path: Pydantic Validation Error (e.g., invalid URL format)
2461- >>> form_data_validation_error = FormData([("name", "Bad URL Gateway"), ("url", "invalid-url"), ("auth_type", "basic")]) # Added auth_type
2475+ >>> form_data_validation_error = FormData([("name", "Bad URL Gateway"), ("url", "invalid-url"), ("auth_type", "basic"),("auth_username", "user"),
2476+ ... ("auth_password", "pass")]) # Added auth_type
24622477 >>> mock_request_validation_error = MagicMock(spec=Request, scope={"root_path": ""})
24632478 >>> mock_request_validation_error.form = AsyncMock(return_value=form_data_validation_error)
24642479 >>>
24652480 >>> async def test_admin_edit_gateway_validation_error():
24662481 ... response = await admin_edit_gateway(gateway_id, mock_request_validation_error, mock_db, mock_user)
2467- ... return isinstance(response, RedirectResponse) and response.status_code == 303 and "/admin#gateways" in response.headers["location"]
2482+ ... body = json.loads(response.body.decode())
2483+ ... return isinstance(response, JSONResponse) and response.status_code in (422,400) and body["success"] is False
24682484 >>>
24692485 >>> asyncio.run(test_admin_edit_gateway_validation_error())
24702486 True
@@ -2474,7 +2490,6 @@ async def admin_edit_gateway(
24742490 """
24752491 logger .debug (f"User { user } is editing gateway ID { gateway_id } " )
24762492 form = await request .form ()
2477- is_inactive_checked = form .get ("is_inactive_checked" , "false" )
24782493 try :
24792494 gateway = GatewayUpdate ( # Pydantic validation happens here
24802495 name = form .get ("name" ),
@@ -2489,18 +2504,22 @@ async def admin_edit_gateway(
24892504 auth_header_value = form .get ("auth_header_value" , None ),
24902505 )
24912506 await gateway_service .update_gateway (db , gateway_id , gateway )
2492-
2493- root_path = request .scope .get ("root_path" , "" )
2494- if is_inactive_checked .lower () == "true" :
2495- return RedirectResponse (f"{ root_path } /admin/?include_inactive=true#gateways" , status_code = 303 )
2496- return RedirectResponse (f"{ root_path } /admin#gateways" , status_code = 303 )
2497- except Exception as e : # Catch all exceptions including ValidationError for redirect
2498- logger .error (f"Error editing gateway: { e } " )
2499-
2500- root_path = request .scope .get ("root_path" , "" )
2501- if is_inactive_checked .lower () == "true" :
2502- return RedirectResponse (f"{ root_path } /admin/?include_inactive=true#gateways" , status_code = 303 )
2503- return RedirectResponse (f"{ root_path } /admin#gateways" , status_code = 303 )
2507+ return JSONResponse (
2508+ content = {"message" : "Gateway update successfully!" , "success" : True },
2509+ status_code = 200 ,
2510+ )
2511+ except Exception as ex :
2512+ if isinstance (ex , GatewayConnectionError ):
2513+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 502 )
2514+ if isinstance (ex , ValueError ):
2515+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 400 )
2516+ if isinstance (ex , RuntimeError ):
2517+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
2518+ if isinstance (ex , ValidationError ):
2519+ return JSONResponse (content = ErrorFormatter .format_validation_error (ex ), status_code = 422 )
2520+ if isinstance (ex , IntegrityError ):
2521+ return JSONResponse (status_code = 409 , content = ErrorFormatter .format_database_error (ex ))
2522+ return JSONResponse (content = {"message" : str (ex ), "success" : False }, status_code = 500 )
25042523
25052524
25062525@admin_router .post ("/gateways/{gateway_id}/delete" )
0 commit comments