/ server / tests / test_routes_pause_resume.py
test_routes_pause_resume.py
 1  # Copyright 2025 Alibaba Group Holding Ltd.
 2  #
 3  # Licensed under the Apache License, Version 2.0 (the "License");
 4  # you may not use this file except in compliance with the License.
 5  # You may obtain a copy of the License at
 6  #
 7  #     http://www.apache.org/licenses/LICENSE-2.0
 8  #
 9  # Unless required by applicable law or agreed to in writing, software
10  # distributed under the License is distributed on an "AS IS" BASIS,
11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  # See the License for the specific language governing permissions and
13  # limitations under the License.
14  
15  from fastapi.exceptions import HTTPException
16  from fastapi.testclient import TestClient
17  
18  from opensandbox_server.api import lifecycle
19  
20  
21  def test_pause_route_calls_service_and_returns_202(
22      client: TestClient,
23      auth_headers: dict,
24      monkeypatch,
25  ) -> None:
26      calls: list[str] = []
27  
28      class StubService:
29          @staticmethod
30          def pause_sandbox(sandbox_id: str) -> None:
31              calls.append(sandbox_id)
32  
33      monkeypatch.setattr(lifecycle, "sandbox_service", StubService())
34  
35      response = client.post("/v1/sandboxes/sbx-001/pause", headers=auth_headers)
36  
37      assert response.status_code == 202
38      assert calls == ["sbx-001"]
39  
40  
41  def test_resume_route_calls_service_and_returns_202(
42      client: TestClient,
43      auth_headers: dict,
44      monkeypatch,
45  ) -> None:
46      calls: list[str] = []
47  
48      class StubService:
49          @staticmethod
50          def resume_sandbox(sandbox_id: str) -> None:
51              calls.append(sandbox_id)
52  
53      monkeypatch.setattr(lifecycle, "sandbox_service", StubService())
54  
55      response = client.post("/v1/sandboxes/sbx-001/resume", headers=auth_headers)
56  
57      assert response.status_code == 202
58      assert calls == ["sbx-001"]
59  
60  
61  def test_pause_route_propagates_service_http_error(
62      client: TestClient,
63      auth_headers: dict,
64      monkeypatch,
65  ) -> None:
66      class StubService:
67          @staticmethod
68          def pause_sandbox(sandbox_id: str) -> None:
69              raise HTTPException(
70                  status_code=404,
71                  detail={"code": "SANDBOX_NOT_FOUND", "message": f"Sandbox {sandbox_id} not found"},
72              )
73  
74      monkeypatch.setattr(lifecycle, "sandbox_service", StubService())
75  
76      response = client.post("/v1/sandboxes/missing/pause", headers=auth_headers)
77  
78      assert response.status_code == 404
79      assert response.json() == {
80          "code": "SANDBOX_NOT_FOUND",
81          "message": "Sandbox missing not found",
82      }
83  
84  
85  def test_pause_route_requires_api_key(client: TestClient) -> None:
86      response = client.post("/v1/sandboxes/sbx-001/pause")
87  
88      assert response.status_code == 401
89      assert response.json()["code"] == "MISSING_API_KEY"