@@ -2389,7 +2389,7 @@ async def admin_edit_gateway(
2389
2389
request : Request ,
2390
2390
db : Session = Depends (get_db ),
2391
2391
user : str = Depends (require_auth ),
2392
- ) -> RedirectResponse :
2392
+ ) -> JSONResponse :
2393
2393
"""Edit a gateway via the admin UI.
2394
2394
2395
2395
Expects form fields:
@@ -2419,52 +2419,68 @@ async def admin_edit_gateway(
2419
2419
>>> gateway_id = "gateway-to-edit"
2420
2420
>>>
2421
2421
>>> # 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
+ ... ])
2423
2430
>>> mock_request_success = MagicMock(spec=Request, scope={"root_path": ""})
2424
2431
>>> mock_request_success.form = AsyncMock(return_value=form_data_success)
2425
2432
>>> original_update_gateway = gateway_service.update_gateway
2426
2433
>>> gateway_service.update_gateway = AsyncMock()
2427
2434
>>>
2428
2435
>>> async def test_admin_edit_gateway_success():
2429
2436
... 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
2431
2438
>>>
2432
2439
>>> asyncio.run(test_admin_edit_gateway_success())
2433
2440
True
2434
2441
>>>
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
+ # >>>
2447
2455
>>> # 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
2449
2458
>>> mock_request_error = MagicMock(spec=Request, scope={"root_path": ""})
2450
2459
>>> mock_request_error.form = AsyncMock(return_value=form_data_error)
2451
2460
>>> gateway_service.update_gateway = AsyncMock(side_effect=Exception("Update failed"))
2452
2461
>>>
2453
2462
>>> async def test_admin_edit_gateway_exception():
2454
2463
... 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
+ ... )
2456
2470
>>>
2457
2471
>>> asyncio.run(test_admin_edit_gateway_exception())
2458
2472
True
2459
2473
>>>
2460
2474
>>> # 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
2462
2477
>>> mock_request_validation_error = MagicMock(spec=Request, scope={"root_path": ""})
2463
2478
>>> mock_request_validation_error.form = AsyncMock(return_value=form_data_validation_error)
2464
2479
>>>
2465
2480
>>> async def test_admin_edit_gateway_validation_error():
2466
2481
... 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
2468
2484
>>>
2469
2485
>>> asyncio.run(test_admin_edit_gateway_validation_error())
2470
2486
True
@@ -2474,7 +2490,6 @@ async def admin_edit_gateway(
2474
2490
"""
2475
2491
logger .debug (f"User { user } is editing gateway ID { gateway_id } " )
2476
2492
form = await request .form ()
2477
- is_inactive_checked = form .get ("is_inactive_checked" , "false" )
2478
2493
try :
2479
2494
gateway = GatewayUpdate ( # Pydantic validation happens here
2480
2495
name = form .get ("name" ),
@@ -2489,18 +2504,22 @@ async def admin_edit_gateway(
2489
2504
auth_header_value = form .get ("auth_header_value" , None ),
2490
2505
)
2491
2506
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 )
2504
2523
2505
2524
2506
2525
@admin_router .post ("/gateways/{gateway_id}/delete" )
0 commit comments