Skip to content

Commit 97023eb

Browse files
MrButtCodecanihavesomecoffee
authored andcommitted
refactor(ci): convert cache expiration to pure function per review
1 parent d0d322a commit 97023eb

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

tests/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import warnings
55
from collections import namedtuple
66
from contextlib import contextmanager
7-
from unittest import mock
87

98
from flask import g
109
from flask_testing import TestCase
@@ -279,12 +278,13 @@ def create_app(self, mock_config, mock_storage_client):
279278

280279
def setUp(self):
281280
"""Set up all entities."""
282-
# Patch the cache before parent setup runs to prevent HTTP calls
283-
self.cache_patcher = mock.patch.object(
284-
utility, 'cache_has_expired', return_value=False
285-
)
286-
self.cache_patcher.start()
287-
self.addCleanup(self.cache_patcher.stop)
281+
from datetime import datetime
282+
283+
import utility
284+
285+
# Reset the state directly for test isolation. No mocks needed!
286+
utility.cached_load_time = datetime(1970, 1, 1)
287+
utility.cached_web_hook_blocks = []
288288

289289
super().setUp()
290290
self.app.preprocess_request()

utility.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ def decorated_function(*args, **kwargs):
8181
cached_load_time: datetime = datetime(1970, 1, 1)
8282

8383

84-
def cache_has_expired() -> bool:
84+
def cache_has_expired(load_time: datetime) -> bool:
8585
"""
8686
Check if the cache expired.
8787
8888
:return: True if the cache was last updated more than one hour ago.
8989
:rtype: bool
9090
"""
91-
global cached_load_time
9291
from datetime import datetime, timedelta
93-
return cached_load_time + timedelta(hours=1) < datetime.now()
92+
return load_time + timedelta(hours=1) < datetime.now()
9493

9594

9695
def is_github_web_hook_ip(request_ip: Union[IPv4Address, IPv6Address]) -> bool:
@@ -120,7 +119,7 @@ def get_cached_web_hook_blocks() -> List[str]:
120119
global cached_load_time
121120
from run import config
122121

123-
if len(cached_web_hook_blocks) == 0 or cache_has_expired():
122+
if len(cached_web_hook_blocks) == 0 or cache_has_expired(cached_load_time):
124123
client_id = config.get('GITHUB_CLIENT_ID', '')
125124
client_secret = config.get('GITHUB_CLIENT_KEY', '')
126125
meta_json = requests.get(

0 commit comments

Comments
 (0)