/ server / tests / test_renew_intent.py
test_renew_intent.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  """Renew-intent JSON parsing tests."""
16  
17  from datetime import datetime, timedelta, timezone
18  
19  import pytest
20  
21  from opensandbox_server.integrations.renew_intent.intent import parse_renew_intent_json
22  from opensandbox_server.integrations.renew_intent.consumer import RenewIntentConsumer
23  
24  
25  def test_parse_matches_ingress_intent_shape():
26      raw = (
27          '{"sandbox_id":"abc","observed_at":"2026-03-22T12:00:00.123456789Z",'
28          '"port":8080,"request_uri":"/x"}'
29      )
30      intent = parse_renew_intent_json(raw)
31      assert intent is not None
32      assert intent.sandbox_id == "abc"
33      assert intent.port == 8080
34      assert intent.request_uri == "/x"
35      assert intent.observed_at.tzinfo is not None
36  
37  
38  def test_parse_rejects_bad_json():
39      assert parse_renew_intent_json("not json") is None
40  
41  
42  @pytest.mark.parametrize(
43      "observed_at,expect_stale",
44      [
45          (datetime.now(timezone.utc) - timedelta(seconds=400), True),
46          (datetime.now(timezone.utc) - timedelta(seconds=10), False),
47      ],
48  )
49  def test_stale_gate(observed_at, expect_stale):
50      assert RenewIntentConsumer._is_stale(observed_at) is expect_stale