Skip to content

Commit 2994cde

Browse files
committed
Use new api endpoints in client
1 parent 84642eb commit 2994cde

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

tests/unit_tests/service/test_rest_api.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class MyModel(BaseModel):
117117
plan = Plan(name="my-plan", model=MyModel)
118118
mock_runner.run.return_value = [PlanModel.from_plan(plan)]
119119

120-
response_get = client_with_cors.get("/plans")
120+
response_get = client_with_cors.get("/api/v1/plans")
121121
assert response_get.status_code == status.HTTP_200_OK
122122

123123

@@ -135,7 +135,7 @@ def test_rest_config_with_cors(
135135

136136
# Allowed method
137137
response_post = client_with_cors.post(
138-
"/tasks",
138+
"/api/v1/tasks",
139139
json=task.model_dump(),
140140
headers={"Accept": "application/json", "Content-Type": "application/json"},
141141
)
@@ -150,7 +150,7 @@ class MyModel(BaseModel):
150150
plan = Plan(name="my-plan", model=MyModel)
151151
mock_runner.run.return_value = [PlanModel.from_plan(plan)]
152152

153-
response = client.get("/plans")
153+
response = client.get("/api/v1/plans")
154154

155155
assert response.status_code == status.HTTP_200_OK
156156
assert response.json() == {
@@ -176,7 +176,7 @@ class MyModel(BaseModel):
176176
plan = Plan(name="my-plan", model=MyModel)
177177
mock_runner.run.return_value = PlanModel.from_plan(plan)
178178

179-
response = client.get("/plans/my-plan")
179+
response = client.get("/api/v1/plans/my-plan")
180180

181181
mock_runner.run.assert_called_once_with(get_plan, "my-plan")
182182
assert response.status_code == status.HTTP_200_OK
@@ -194,7 +194,7 @@ class MyModel(BaseModel):
194194

195195
def test_get_non_existent_plan_by_name(mock_runner: Mock, client: TestClient) -> None:
196196
mock_runner.run.side_effect = KeyError("my-plan")
197-
response = client.get("/plans/my-plan")
197+
response = client.get("/api/v1/plans/my-plan")
198198

199199
assert response.status_code == status.HTTP_404_NOT_FOUND
200200
assert response.json() == {"detail": "Item not found"}
@@ -204,7 +204,7 @@ def test_get_devices(mock_runner: Mock, client: TestClient) -> None:
204204
device = MinimalDevice("my-device")
205205
mock_runner.run.return_value = [DeviceModel.from_device(device)]
206206

207-
response = client.get("/devices")
207+
response = client.get("/api/v1/devices")
208208

209209
assert response.status_code == status.HTTP_200_OK
210210
assert response.json() == {
@@ -221,7 +221,7 @@ def test_get_device_by_name(mock_runner: Mock, client: TestClient) -> None:
221221
device = MinimalDevice("my-device")
222222

223223
mock_runner.run.return_value = DeviceModel.from_device(device)
224-
response = client.get("/devices/my-device")
224+
response = client.get("/api/v1/devices/my-device")
225225

226226
mock_runner.run.assert_called_once_with(get_device, "my-device")
227227
assert response.status_code == status.HTTP_200_OK
@@ -233,7 +233,7 @@ def test_get_device_by_name(mock_runner: Mock, client: TestClient) -> None:
233233

234234
def test_get_non_existent_device_by_name(mock_runner: Mock, client: TestClient) -> None:
235235
mock_runner.run.side_effect = KeyError("my-device")
236-
response = client.get("/devices/my-device")
236+
response = client.get("/api/v1/devices/my-device")
237237

238238
assert response.status_code == status.HTTP_404_NOT_FOUND
239239
assert response.json() == {"detail": "Item not found"}
@@ -249,7 +249,7 @@ def test_create_task(mock_runner: Mock, client: TestClient) -> None:
249249

250250
mock_runner.run.side_effect = [task_id]
251251

252-
response = client.post("/tasks", json=task.model_dump())
252+
response = client.post("/api/v1/tasks", json=task.model_dump())
253253

254254
mock_runner.run.assert_called_with(submit_task, task, {"user": "Unknown"})
255255
assert response.json() == {"task_id": task_id}
@@ -270,7 +270,7 @@ def test_create_task_inserts_auth_metadata(
270270
# mock_runner.run.side_effect = [task_id]
271271
mock_runner.run.return_value = [task_id]
272272

273-
client_with_auth.post("/tasks", json=task.model_dump())
273+
client_with_auth.post("/api/v1/tasks", json=task.model_dump())
274274

275275
mock_runner.run.assert_called_with(submit_task, task, {"user": "jd1"})
276276

@@ -288,7 +288,7 @@ def test_create_task_validation_error(mock_runner: Mock, client: TestClient) ->
288288
]
289289

290290
response = client.post(
291-
"/tasks",
291+
"/api/v1/tasks",
292292
json={
293293
"name": "my-plan",
294294
"instrument_session": FAKE_INSTRUMENT_SESSION,
@@ -310,7 +310,7 @@ def test_create_task_validation_error(mock_runner: Mock, client: TestClient) ->
310310
def test_put_plan_begins_task(client: TestClient) -> None:
311311
task_id = "04cd9aa6-b902-414b-ae4b-49ea4200e957"
312312

313-
resp = client.put("/worker/task", json={"task_id": task_id})
313+
resp = client.put("/api/v1/worker/task", json={"task_id": task_id})
314314

315315
assert resp.status_code == status.HTTP_200_OK
316316
assert resp.json() == {"task_id": task_id}
@@ -325,7 +325,7 @@ def test_put_plan_fails_if_not_idle(mock_runner: Mock, client: TestClient) -> No
325325
task=Task(name="none"), task_id=task_id_current, is_complete=False
326326
)
327327

328-
resp = client.put("/worker/task", json={"task_id": task_id_new})
328+
resp = client.put("/api/v1/worker/task", json={"task_id": task_id_new})
329329

330330
assert resp.status_code == status.HTTP_409_CONFLICT
331331
assert resp.json() == {"detail": "Worker already active"}
@@ -344,7 +344,7 @@ def test_get_tasks(mock_runner: Mock, client: TestClient) -> None:
344344

345345
mock_runner.run.return_value = tasks
346346

347-
response = client.get("/tasks")
347+
response = client.get("/api/v1/tasks")
348348
assert response.status_code == status.HTTP_200_OK
349349

350350
assert response.json() == {
@@ -391,7 +391,7 @@ def test_get_tasks_by_status(mock_runner: Mock, client: TestClient) -> None:
391391

392392
mock_runner.run.return_value = tasks
393393

394-
response = client.get("/tasks", params={"task_status": "PENDING"})
394+
response = client.get("/api/v1/tasks", params={"task_status": "PENDING"})
395395
assert response.json() == {
396396
"tasks": [
397397
{
@@ -412,22 +412,22 @@ def test_get_tasks_by_status(mock_runner: Mock, client: TestClient) -> None:
412412

413413

414414
def test_get_tasks_by_status_invalid(client: TestClient) -> None:
415-
response = client.get("/tasks", params={"task_status": "AN_INVALID_STATUS"})
415+
response = client.get("/api/v1/tasks", params={"task_status": "AN_INVALID_STATUS"})
416416
assert response.status_code == status.HTTP_400_BAD_REQUEST
417417

418418

419419
def test_delete_submitted_task(mock_runner: Mock, client: TestClient) -> None:
420420
task_id = str(uuid.uuid4())
421421
mock_runner.run.return_value = task_id
422-
response = client.delete(f"/tasks/{task_id}")
422+
response = client.delete(f"/api/v1/tasks/{task_id}")
423423
assert response.json() == {"task_id": f"{task_id}"}
424424

425425

426426
def test_set_active_task(client: TestClient) -> None:
427427
task_id = str(uuid.uuid4())
428428
task = WorkerTask(task_id=task_id)
429429

430-
response = client.put("/worker/task", json=task.model_dump())
430+
response = client.put("/api/v1/worker/task", json=task.model_dump())
431431

432432
assert response.status_code == status.HTTP_200_OK
433433
assert response.json() == {"task_id": f"{task_id}"}
@@ -446,7 +446,7 @@ def test_set_active_task_active_task_complete(
446446
is_pending=False,
447447
)
448448

449-
response = client.put("/worker/task", json=task.model_dump())
449+
response = client.put("/api/v1/worker/task", json=task.model_dump())
450450

451451
assert response.status_code == status.HTTP_200_OK
452452
assert response.json() == {"task_id": f"{task_id}"}
@@ -465,7 +465,7 @@ def test_set_active_task_worker_already_running(
465465
is_pending=False,
466466
)
467467

468-
response = client.put("/worker/task", json=task.model_dump())
468+
response = client.put("/api/v1/worker/task", json=task.model_dump())
469469

470470
assert response.status_code == status.HTTP_409_CONFLICT
471471
assert response.json() == {"detail": "Worker already active"}
@@ -485,7 +485,7 @@ def test_get_task(mock_runner: Mock, client: TestClient):
485485

486486
mock_runner.run.return_value = task
487487

488-
response = client.get(f"/tasks/{task_id}")
488+
response = client.get(f"/api/v1/tasks/{task_id}")
489489
assert response.json() == {
490490
"errors": [],
491491
"is_complete": False,
@@ -513,7 +513,7 @@ def test_get_all_tasks(mock_runner: Mock, client: TestClient):
513513
]
514514

515515
mock_runner.run.return_value = tasks
516-
response = client.get("/tasks")
516+
response = client.get("/api/v1/tasks")
517517
assert response.status_code == status.HTTP_200_OK
518518
assert response.json() == {
519519
"tasks": [
@@ -538,7 +538,7 @@ def test_get_task_error(mock_runner: Mock, client: TestClient):
538538
task_id = 567
539539
mock_runner.run.return_value = None
540540

541-
response = client.get(f"/tasks/{task_id}")
541+
response = client.get(f"/api/v1/tasks/{task_id}")
542542
assert response.json() == {"detail": "Item not found"}
543543

544544

@@ -550,15 +550,15 @@ def test_get_active_task(mock_runner: Mock, client: TestClient):
550550
)
551551
mock_runner.run.return_value = task
552552

553-
response = client.get("/worker/task")
553+
response = client.get("/api/v1/worker/task")
554554

555555
assert response.json() == {"task_id": f"{task_id}"}
556556

557557

558558
def test_get_active_task_none(mock_runner: Mock, client: TestClient):
559559
mock_runner.run.return_value = None
560560

561-
response = client.get("/worker/task")
561+
response = client.get("/api/v1/worker/task")
562562

563563
assert response.json() == {"task_id": None}
564564

@@ -567,7 +567,7 @@ def test_get_state(mock_runner: Mock, client: TestClient):
567567
state = WorkerState.SUSPENDING
568568
mock_runner.run.return_value = state
569569

570-
response = client.get("/worker/state")
570+
response = client.get("/api/v1/worker/state")
571571
assert response.json() == state
572572

573573

@@ -577,7 +577,8 @@ def test_set_state_running_to_paused(mock_runner: Mock, client: TestClient):
577577
mock_runner.run.side_effect = [current_state, None, final_state]
578578

579579
response = client.put(
580-
"/worker/state", json=StateChangeRequest(new_state=final_state).model_dump()
580+
"/api/v1/worker/state",
581+
json=StateChangeRequest(new_state=final_state).model_dump(),
581582
)
582583

583584
mock_runner.run.assert_any_call(pause_worker, False)
@@ -591,7 +592,8 @@ def test_set_state_paused_to_running(mock_runner: Mock, client: TestClient):
591592
mock_runner.run.side_effect = [current_state, None, final_state]
592593

593594
response = client.put(
594-
"/worker/state", json=StateChangeRequest(new_state=final_state).model_dump()
595+
"/api/v1/worker/state",
596+
json=StateChangeRequest(new_state=final_state).model_dump(),
595597
)
596598

597599
mock_runner.run.assert_any_call(resume_worker)
@@ -605,7 +607,8 @@ def test_set_state_running_to_aborting(mock_runner: Mock, client: TestClient):
605607
mock_runner.run.side_effect = [current_state, None, final_state]
606608

607609
response = client.put(
608-
"/worker/state", json=StateChangeRequest(new_state=final_state).model_dump()
610+
"/api/v1/worker/state",
611+
json=StateChangeRequest(new_state=final_state).model_dump(),
609612
)
610613

611614
mock_runner.run.assert_any_call(cancel_active_task, True, None)
@@ -622,7 +625,7 @@ def test_set_state_running_to_stopping_including_reason(
622625
mock_runner.run.side_effect = [current_state, None, final_state]
623626

624627
response = client.put(
625-
"/worker/state",
628+
"/api/v1/worker/state",
626629
json=StateChangeRequest(new_state=final_state, reason=reason).model_dump(),
627630
)
628631

@@ -638,7 +641,7 @@ def test_set_state_transition_error(mock_runner: Mock, client: TestClient):
638641
mock_runner.run.side_effect = [current_state, TransitionError(), final_state]
639642

640643
response = client.put(
641-
"/worker/state",
644+
"/api/v1/worker/state",
642645
json=StateChangeRequest(new_state=final_state).model_dump(),
643646
)
644647

@@ -654,7 +657,7 @@ def test_set_state_invalid_transition(mock_runner: Mock, client: TestClient):
654657
mock_runner.run.side_effect = [current_state, final_state]
655658

656659
response = client.put(
657-
"/worker/state",
660+
"/api/v1/worker/state",
658661
json=StateChangeRequest(new_state=requested_state).model_dump(),
659662
)
660663

@@ -670,7 +673,7 @@ def test_get_environment_idle(mock_runner: Mock, client: TestClient) -> None:
670673
error_message=None,
671674
)
672675

673-
assert client.get("/environment").json() == {
676+
assert client.get("/api/v1/environment").json() == {
674677
"environment_id": str(environment_id),
675678
"initialized": True,
676679
"error_message": None,
@@ -684,7 +687,7 @@ def test_delete_environment(mock_runner: Mock, client: TestClient) -> None:
684687
initialized=True,
685688
error_message=None,
686689
)
687-
response = client.delete("/environment")
690+
response = client.delete("/api/v1/environment")
688691
assert response.status_code is status.HTTP_200_OK
689692
assert response.json() == {
690693
"environment_id": str(environment_id),
@@ -703,7 +706,7 @@ def test_subprocess_enabled_by_default(mp_pool_mock: MagicMock):
703706

704707
def test_get_without_authentication(mock_runner: Mock, client: TestClient) -> None:
705708
mock_runner.run.side_effect = jwt.PyJWTError
706-
response = client.get("/devices/my-device")
709+
response = client.get("/api/v1/devices/my-device")
707710

708711
assert response.status_code == status.HTTP_401_UNAUTHORIZED
709712
assert response.json() == {"detail": "Not authenticated"}
@@ -743,7 +746,7 @@ def test_get_python_environment(mock_runner: Mock, client: TestClient):
743746
]
744747
)
745748
mock_runner.run.return_value = packages
746-
response = client.get("/python_environment")
749+
response = client.get("/api/v1/python_environment")
747750
assert response.status_code == status.HTTP_200_OK
748751
assert response.json() == packages.model_dump()
749752

@@ -764,7 +767,7 @@ def test_logout(
764767
oidc_config.logout_redirect_endpoint = "/oauth2/sign_out/"
765768
mock_runner.run.return_value = oidc_config
766769
client_with_auth.follow_redirects = False
767-
response = client_with_auth.get("/logout")
770+
response = client_with_auth.get("/api/v1/logout")
768771
assert response.status_code == status.HTTP_308_PERMANENT_REDIRECT
769772
assert (
770773
response.headers.get("location")
@@ -796,5 +799,5 @@ def test_logout_when_oidc_config_invalid(
796799
else:
797800
mock_runner.run.return_value = None
798801

799-
response = client_with_auth.get("/logout")
802+
response = client_with_auth.get("/api/v1/logout")
800803
assert response.status_code == status.HTTP_205_RESET_CONTENT

0 commit comments

Comments
 (0)