/ server / tests / test_endpoint_auth.py
test_endpoint_auth.py
 1  # Copyright 2026 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 opensandbox_server.services.constants import OPEN_SANDBOX_EGRESS_AUTH_HEADER
16  from opensandbox_server.services.endpoint_auth import (
17      build_egress_auth_headers,
18      generate_egress_token,
19      merge_endpoint_headers,
20  )
21  
22  
23  def test_generate_egress_token_returns_random_urlsafe_strings() -> None:
24      first = generate_egress_token()
25      second = generate_egress_token()
26  
27      assert first
28      assert second
29      assert first != second
30  
31  
32  def test_build_egress_auth_headers_uses_expected_header_name() -> None:
33      token = "egress-token"
34  
35      assert build_egress_auth_headers(token) == {
36          OPEN_SANDBOX_EGRESS_AUTH_HEADER: token,
37      }
38  
39  
40  def test_merge_endpoint_headers_preserves_existing_headers() -> None:
41      existing = {"OpenSandbox-Ingress-To": "sbx-1-18080"}
42      extra = {OPEN_SANDBOX_EGRESS_AUTH_HEADER: "egress-token"}
43  
44      merged = merge_endpoint_headers(existing, extra)
45  
46      assert merged == {
47          "OpenSandbox-Ingress-To": "sbx-1-18080",
48          OPEN_SANDBOX_EGRESS_AUTH_HEADER: "egress-token",
49      }
50      assert existing == {"OpenSandbox-Ingress-To": "sbx-1-18080"}
51  
52  
53  def test_merge_endpoint_headers_handles_missing_existing_headers() -> None:
54      merged = merge_endpoint_headers(None, {OPEN_SANDBOX_EGRESS_AUTH_HEADER: "egress-token"})
55  
56      assert merged == {OPEN_SANDBOX_EGRESS_AUTH_HEADER: "egress-token"}