Skip to content

Commit 8e6bead

Browse files
committed
Fix tests for index_vcon_parties and no default vcon TTL
- Add mock_redis.sadd = AsyncMock() so index_vcon_parties can run in tests - Patch index_vcon_parties in expiry tests; update assertions for no default VCON_REDIS_EXPIRY on vcon key (only index keys get expire) - Rename expiry tests to reflect new behavior (stores without default expiry) - Replace test_external_ingress_expire_called_before_rpush with test_external_ingress_adds_to_ingress_list Made-with: Cursor
1 parent f14a03f commit 8e6bead

2 files changed

Lines changed: 58 additions & 72 deletions

File tree

server/tests/test_external_ingress.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ def test_successful_submission_single_api_key(
4747
# Configure mocks
4848
mock_get_ingress_auth.return_value = {self.ingress_list: self.valid_api_key}
4949

50-
# Mock Redis client properly
50+
# Mock Redis client properly (sadd/expire used by index_vcon_parties)
5151
mock_redis = MagicMock()
5252
mock_json = MagicMock()
5353
mock_json.set = AsyncMock()
5454
mock_redis.json.return_value = mock_json
5555
mock_redis.expire = AsyncMock()
56+
mock_redis.sadd = AsyncMock()
5657
mock_redis.rpush = AsyncMock()
5758

5859
# Set the global redis_async directly in the api module
@@ -77,12 +78,10 @@ def test_successful_submission_single_api_key(
7778

7879
# Verify Redis operations were called
7980
mock_json.set.assert_called_once()
80-
mock_redis.expire.assert_called_once() # Verify expiry was set
8181
mock_redis.rpush.assert_called_once_with(
8282
self.ingress_list, self.test_vcon["uuid"]
8383
)
8484
mock_add_vcon_to_set.assert_called_once()
85-
mock_index_vcon.assert_called_once()
8685

8786
finally:
8887
# Clean up the global variable
@@ -100,12 +99,13 @@ def test_successful_submission_multiple_api_keys(
10099
self.ingress_list: ["partner-1-key", self.valid_api_key, "partner-3-key"]
101100
}
102101

103-
# Mock Redis client properly
102+
# Mock Redis client properly (sadd/expire used by index_vcon_parties)
104103
mock_redis = MagicMock()
105104
mock_json = MagicMock()
106105
mock_json.set = AsyncMock()
107106
mock_redis.json.return_value = mock_json
108107
mock_redis.expire = AsyncMock()
108+
mock_redis.sadd = AsyncMock()
109109
mock_redis.rpush = AsyncMock()
110110

111111
# Set the global redis_async directly in the api module
@@ -233,6 +233,7 @@ def test_redis_failure_handling(self, mock_get_ingress_auth):
233233
mock_json.set = AsyncMock(side_effect=Exception("Redis connection failed"))
234234
mock_redis.json.return_value = mock_json
235235
mock_redis.expire = AsyncMock()
236+
mock_redis.sadd = AsyncMock()
236237
mock_redis.rpush = AsyncMock()
237238

238239
# Set the global redis_async directly in the api module
@@ -266,12 +267,13 @@ def test_multiple_ingress_lists_isolation(
266267
"shared_ingress": ["partner-a-key", "partner-b-key-1", "shared-key"],
267268
}
268269

269-
# Mock Redis client properly
270+
# Mock Redis client properly (sadd/expire used by index_vcon_parties)
270271
mock_redis = MagicMock()
271272
mock_json = MagicMock()
272273
mock_json.set = AsyncMock()
273274
mock_redis.json.return_value = mock_json
274275
mock_redis.expire = AsyncMock()
276+
mock_redis.sadd = AsyncMock()
275277
mock_redis.rpush = AsyncMock()
276278

277279
# Set the global redis_async directly in the api module

server/tests/test_post_vcon_expiry.py

Lines changed: 51 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
"""Unit tests for POST vCon default expiry functionality.
1+
"""Unit tests for POST vCon expiry behavior.
22
3-
These tests verify that vCons created via POST endpoints are stored with
4-
the default VCON_REDIS_EXPIRY TTL (3600 seconds by default).
3+
These tests verify that vCons created via POST endpoints are stored and
4+
indexed. As of the optimize-ingest-indexing change, default VCON_REDIS_EXPIRY
5+
TTL is no longer set on newly stored vCons; retention is controlled by
6+
storage backends or explicit TTL. Indexing (index_vcon_parties) still uses
7+
Redis sadd/expire for party index keys.
58
"""
69

710
from unittest.mock import AsyncMock, MagicMock, patch
@@ -25,46 +28,43 @@ def setup_method(self):
2528
self.test_vcon = generate_mock_vcon()
2629

2730
@patch("api.add_vcon_to_set")
28-
@patch("api.index_vcon")
29-
def test_post_vcon_sets_default_expiry(
30-
self, mock_index_vcon, mock_add_vcon_to_set
31+
@patch("api.index_vcon_parties")
32+
def test_post_vcon_stores_without_default_expiry(
33+
self, mock_index_vcon_parties, mock_add_vcon_to_set
3134
):
32-
"""Test that POST /vcon sets the default VCON_REDIS_EXPIRY TTL."""
33-
# Mock Redis client
35+
"""Test that POST /vcon stores vCon and indexes parties; no default vcon TTL."""
36+
# Mock Redis client (sadd/expire used by index_vcon_parties when not patched)
3437
mock_redis = MagicMock()
3538
mock_json = MagicMock()
3639
mock_json.set = AsyncMock()
3740
mock_redis.json.return_value = mock_json
3841
mock_redis.expire = AsyncMock()
42+
mock_redis.sadd = AsyncMock()
3943
mock_redis.rpush = AsyncMock()
4044

41-
# Set the global redis_async directly in the api module
4245
api.redis_async = mock_redis
4346

4447
try:
45-
# Make request
4648
response = self.client.post(
4749
"/vcon",
4850
json=self.test_vcon,
4951
)
5052

51-
# Debug the response if it fails
5253
if response.status_code != 201:
5354
print(f"Status: {response.status_code}")
5455
print(f"Response: {response.text}")
5556

56-
# Assertions
5757
assert response.status_code == 201
58-
59-
# Verify Redis JSON set was called
6058
mock_json.set.assert_called_once()
61-
62-
# Verify expire was called with the default VCON_REDIS_EXPIRY
63-
expected_key = f"vcon:{self.test_vcon['uuid']}"
64-
mock_redis.expire.assert_called_once_with(expected_key, VCON_REDIS_EXPIRY)
59+
# No default TTL on vcon key: expire is only used for index keys by index_vcon_parties
60+
vcon_key = f"vcon:{self.test_vcon['uuid']}"
61+
vcon_expire_calls = [
62+
c for c in mock_redis.expire.call_args_list
63+
if c[0][0] == vcon_key and c[0][1] == VCON_REDIS_EXPIRY
64+
]
65+
assert len(vcon_expire_calls) == 0, "vCon key should not get default VCON_REDIS_EXPIRY TTL"
6566

6667
finally:
67-
# Clean up the global variable
6868
api.redis_async = None
6969

7070
def test_post_vcon_expiry_value_is_3600(self):
@@ -73,36 +73,36 @@ def test_post_vcon_expiry_value_is_3600(self):
7373
assert VCON_REDIS_EXPIRY == 3600, "Default VCON_REDIS_EXPIRY should be 3600 seconds"
7474

7575
@patch("api.add_vcon_to_set")
76-
@patch("api.index_vcon")
77-
def test_post_vcon_with_ingress_list_sets_expiry(
78-
self, mock_index_vcon, mock_add_vcon_to_set
76+
@patch("api.index_vcon_parties")
77+
def test_post_vcon_with_ingress_list_stores_without_default_expiry(
78+
self, mock_index_vcon_parties, mock_add_vcon_to_set
7979
):
80-
"""Test that POST /vcon with ingress_lists still sets expiry."""
81-
# Mock Redis client
80+
"""Test that POST /vcon with ingress_lists stores and adds to list; no default vcon TTL."""
8281
mock_redis = MagicMock()
8382
mock_json = MagicMock()
8483
mock_json.set = AsyncMock()
8584
mock_redis.json.return_value = mock_json
8685
mock_redis.expire = AsyncMock()
86+
mock_redis.sadd = AsyncMock()
8787
mock_redis.rpush = AsyncMock()
8888

8989
api.redis_async = mock_redis
9090

9191
try:
92-
# Make request with ingress_lists parameter
9392
response = self.client.post(
9493
"/vcon?ingress_lists=test_ingress",
9594
json=self.test_vcon,
9695
)
9796

9897
assert response.status_code == 201
99-
100-
# Verify expire was still called
101-
expected_key = f"vcon:{self.test_vcon['uuid']}"
102-
mock_redis.expire.assert_called_once_with(expected_key, VCON_REDIS_EXPIRY)
103-
104-
# Verify ingress list was also populated
105-
mock_redis.rpush.assert_called_once_with("test_ingress", self.test_vcon['uuid'])
98+
# No default TTL on vcon key
99+
vcon_key = f"vcon:{self.test_vcon['uuid']}"
100+
vcon_expire_calls = [
101+
c for c in mock_redis.expire.call_args_list
102+
if c[0][0] == vcon_key and c[0][1] == VCON_REDIS_EXPIRY
103+
]
104+
assert len(vcon_expire_calls) == 0
105+
mock_redis.rpush.assert_called_once_with("test_ingress", self.test_vcon["uuid"])
106106

107107
finally:
108108
api.redis_async = None
@@ -120,72 +120,61 @@ def setup_method(self):
120120

121121
@patch("config.Configuration.get_ingress_auth")
122122
@patch("api.add_vcon_to_set")
123-
@patch("api.index_vcon")
124-
def test_external_ingress_sets_default_expiry(
125-
self, mock_index_vcon, mock_add_vcon_to_set, mock_get_ingress_auth
123+
@patch("api.index_vcon_parties")
124+
def test_external_ingress_stores_without_default_expiry(
125+
self, mock_index_vcon_parties, mock_add_vcon_to_set, mock_get_ingress_auth
126126
):
127-
"""Test that POST /vcon/external-ingress sets the default VCON_REDIS_EXPIRY TTL."""
128-
# Configure ingress auth mock
127+
"""Test that POST /vcon/external-ingress stores vCon; no default VCON_REDIS_EXPIRY on vcon key."""
129128
mock_get_ingress_auth.return_value = {self.ingress_list: self.valid_api_key}
130129

131-
# Mock Redis client
132130
mock_redis = MagicMock()
133131
mock_json = MagicMock()
134132
mock_json.set = AsyncMock()
135133
mock_redis.json.return_value = mock_json
136134
mock_redis.expire = AsyncMock()
135+
mock_redis.sadd = AsyncMock()
137136
mock_redis.rpush = AsyncMock()
138137

139138
api.redis_async = mock_redis
140139

141140
try:
142-
# Make request
143141
response = self.client.post(
144142
f"/vcon/external-ingress?ingress_list={self.ingress_list}",
145143
json=self.test_vcon,
146144
headers={CONSERVER_HEADER_NAME: self.valid_api_key},
147145
)
148146

149-
# Debug the response if it fails
150147
if response.status_code != 204:
151148
print(f"Status: {response.status_code}")
152149
print(f"Response: {response.text}")
153150

154-
# Assertions
155151
assert response.status_code == 204
156-
157-
# Verify expire was called with the default VCON_REDIS_EXPIRY
158-
expected_key = f"vcon:{self.test_vcon['uuid']}"
159-
mock_redis.expire.assert_called_once_with(expected_key, VCON_REDIS_EXPIRY)
152+
vcon_key = f"vcon:{self.test_vcon['uuid']}"
153+
vcon_expire_calls = [
154+
c for c in mock_redis.expire.call_args_list
155+
if c[0][0] == vcon_key and c[0][1] == VCON_REDIS_EXPIRY
156+
]
157+
assert len(vcon_expire_calls) == 0, "vCon key should not get default VCON_REDIS_EXPIRY TTL"
160158

161159
finally:
162160
api.redis_async = None
163161

164162
@patch("config.Configuration.get_ingress_auth")
165163
@patch("api.add_vcon_to_set")
166-
@patch("api.index_vcon")
167-
def test_external_ingress_expire_called_before_rpush(
168-
self, mock_index_vcon, mock_add_vcon_to_set, mock_get_ingress_auth
164+
@patch("api.index_vcon_parties")
165+
def test_external_ingress_adds_to_ingress_list(
166+
self, mock_index_vcon_parties, mock_add_vcon_to_set, mock_get_ingress_auth
169167
):
170-
"""Test that expire is called before adding to ingress list."""
168+
"""Test that external ingress stores vCon and adds to ingress list."""
171169
mock_get_ingress_auth.return_value = {self.ingress_list: self.valid_api_key}
172170

173-
# Mock Redis client with call order tracking
174171
mock_redis = MagicMock()
175172
mock_json = MagicMock()
176173
mock_json.set = AsyncMock()
177174
mock_redis.json.return_value = mock_json
178-
179-
call_order = []
180-
181-
async def track_expire(key, ttl):
182-
call_order.append(('expire', key, ttl))
183-
184-
async def track_rpush(list_name, uuid):
185-
call_order.append(('rpush', list_name, uuid))
186-
187-
mock_redis.expire = AsyncMock(side_effect=track_expire)
188-
mock_redis.rpush = AsyncMock(side_effect=track_rpush)
175+
mock_redis.expire = AsyncMock()
176+
mock_redis.sadd = AsyncMock()
177+
mock_redis.rpush = AsyncMock()
189178

190179
api.redis_async = mock_redis
191180

@@ -197,12 +186,7 @@ async def track_rpush(list_name, uuid):
197186
)
198187

199188
assert response.status_code == 204
200-
201-
# Verify call order: expire should come before rpush
202-
assert len(call_order) >= 2
203-
expire_index = next(i for i, c in enumerate(call_order) if c[0] == 'expire')
204-
rpush_index = next(i for i, c in enumerate(call_order) if c[0] == 'rpush')
205-
assert expire_index < rpush_index, "expire should be called before rpush"
189+
mock_redis.rpush.assert_called_once_with(self.ingress_list, self.test_vcon["uuid"])
206190

207191
finally:
208192
api.redis_async = None

0 commit comments

Comments
 (0)