Skip to content

Commit f8d5d4b

Browse files
committed
change utcnow() to Galaxy owned now() function
1 parent 43cdbb4 commit f8d5d4b

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

test/integration/test_notifications.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99
from uuid import uuid4
1010

11+
from galaxy.model.orm.now import now
1112
from galaxy_test.base.populators import (
1213
DatasetPopulator,
1314
WorkflowPopulator,
@@ -61,7 +62,7 @@ def test_notification_status(self):
6162
user1 = self._create_test_user()
6263
user2 = self._create_test_user()
6364

64-
before_creating_notifications = datetime.utcnow()
65+
before_creating_notifications = now()
6566

6667
# Only user1 will receive this notification
6768
subject1 = f"notification_{uuid4()}"
@@ -81,7 +82,7 @@ def test_notification_status(self):
8182
created_response_3 = self._send_broadcast_notification("test_notification_status 3")
8283
assert created_response_3["total_notifications_sent"] == 1
8384

84-
after_creating_notifications = datetime.utcnow()
85+
after_creating_notifications = now()
8586

8687
# The default user should have received only the broadcasted notifications
8788
status = self._get_notifications_status_since(before_creating_notifications)
@@ -158,7 +159,7 @@ def test_delete_notification_by_user(self):
158159
user1 = self._create_test_user()
159160
user2 = self._create_test_user()
160161

161-
before_creating_notifications = datetime.utcnow()
162+
before_creating_notifications = now()
162163

163164
subject = f"notification_{uuid4()}"
164165
created_response = self._send_test_notification_to(
@@ -265,8 +266,8 @@ def test_update_notifications(self):
265266
assert updated_response["source"] == "updated_source"
266267

267268
def test_admins_get_all_broadcasted_even_inactive(self):
268-
tomorrow = datetime.utcnow() + timedelta(days=1)
269-
yesterday = datetime.utcnow() - timedelta(days=1)
269+
tomorrow = now() + timedelta(days=1)
270+
yesterday = now() - timedelta(days=1)
270271
self._send_broadcast_notification(subject="Active")
271272
self._send_broadcast_notification(subject="Scheduled", publication_time=tomorrow)
272273
self._send_broadcast_notification(subject="Expired", expiration_time=yesterday)

test/unit/app/managers/test_NotificationManager.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Role,
2525
User,
2626
)
27+
from galaxy.model.orm.now import now
2728
from galaxy.schema.notifications import (
2829
BroadcastNotificationContent,
2930
BroadcastNotificationCreateRequest,
@@ -88,7 +89,7 @@ def _send_message_notification_to_users(self, users: list[User], notification: O
8889
return created_notification, notifications_sent
8990

9091
def _has_expired(self, expiration_time: Optional[datetime]) -> bool:
91-
return expiration_time < datetime.utcnow() if expiration_time else False
92+
return expiration_time < now() if expiration_time else False
9293

9394
def _assert_notification_expected(self, actual_notification: Any, expected_notification: dict[str, Any]):
9495
assert actual_notification
@@ -146,13 +147,13 @@ def test_get_broadcasted_notification(self):
146147
assert actual_notification.id == created_notification.id
147148

148149
def test_get_all_broadcasted_notifications(self):
149-
now = datetime.utcnow()
150-
next_week = now + timedelta(days=7)
151-
next_month = now + timedelta(days=30)
150+
current_time = now()
151+
next_week = current_time + timedelta(days=7)
152+
next_month = current_time + timedelta(days=30)
152153

153154
notification_data = self._default_broadcast_notification_data()
154155
notification_data["content"]["subject"] = "Recent Notification"
155-
notification_data["publication_time"] = now
156+
notification_data["publication_time"] = current_time
156157
self._send_broadcast_notification(notification_data)
157158

158159
notification_data = self._default_broadcast_notification_data()
@@ -179,18 +180,18 @@ def test_get_all_broadcasted_notifications(self):
179180
assert notifications[0].content["subject"] == "Scheduled Next Month Notification"
180181

181182
def test_update_broadcasted_notification(self):
182-
next_month = datetime.utcnow() + timedelta(days=30)
183+
next_month = now() + timedelta(days=30)
183184
notification_data = self._default_broadcast_notification_data()
184185
notification_data["content"]["subject"] = "Old Scheduled Notification"
185186
notification_data["publication_time"] = next_month
186187
actual_notification = self._send_broadcast_notification(notification_data)
187188

188-
now = datetime.utcnow()
189+
current_time = now()
189190
expected_content = BroadcastNotificationContent(subject="Updated Notification", message="Updated Message")
190191
update_request = NotificationBroadcastUpdateRequest(
191192
source="updated_source",
192193
variant=NotificationVariant.warning,
193-
publication_time=now,
194+
publication_time=current_time,
194195
content=expected_content,
195196
)
196197
updated_count = self.notification_manager.update_broadcasted_notification(
@@ -206,7 +207,7 @@ def test_update_broadcasted_notification(self):
206207
assert content["message"] == expected_content.message
207208

208209
def test_cleanup_expired_broadcast_notifications(self):
209-
one_hour_ago = datetime.utcnow() - timedelta(hours=1)
210+
one_hour_ago = now() - timedelta(hours=1)
210211
notification_data = self._default_broadcast_notification_data()
211212
notification_data["expiration_time"] = one_hour_ago
212213
actual_notification = self._send_broadcast_notification(notification_data)
@@ -290,7 +291,7 @@ def test_update_user_notifications(self):
290291

291292
def test_scheduled_notifications(self):
292293
user = self._create_test_user()
293-
tomorrow = datetime.utcnow() + timedelta(hours=24)
294+
tomorrow = now() + timedelta(hours=24)
294295
expected_notification = self._default_test_notification_data()
295296
expected_notification["source"] = "test_scheduled"
296297
expected_notification["publication_time"] = tomorrow
@@ -347,8 +348,10 @@ def test_update_user_notification_preferences(self):
347348

348349
def test_cleanup_expired_notifications(self):
349350
user = self._create_test_user()
350-
now = datetime.utcnow()
351-
notification, _ = self._send_message_notification_to_users([user], notification={"expiration_time": now})
351+
current_time = now()
352+
notification, _ = self._send_message_notification_to_users(
353+
[user], notification={"expiration_time": current_time}
354+
)
352355
user_notification = self.notification_manager.get_user_notification(user, notification.id, active_only=False)
353356
assert user_notification
354357
assert self._has_expired(user_notification.expiration_time) is True

test/unit/app/managers/test_UserManager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
Executable directly using: python -m test.unit.managers.test_UserManager
55
"""
66

7-
from datetime import datetime
87
from unittest.mock import patch
98

109
from sqlalchemy import (
@@ -21,6 +20,7 @@
2120
histories,
2221
users,
2322
)
23+
from galaxy.model.orm.now import now
2424
from galaxy.security.passwords import check_password
2525
from .base import BaseTestCase
2626

@@ -178,7 +178,7 @@ def test_change_password(self):
178178
)
179179
assert check_password(default_password, user2.password)
180180
assert not check_password(changed_password, user2.password)
181-
prt.expiration_time = datetime.utcnow()
181+
prt.expiration_time = now()
182182
user, message = self.user_manager.change_password(
183183
self.trans, token=prt.token, password=default_password, confirm=default_password
184184
)

0 commit comments

Comments
 (0)