Skip to content

Commit 16eb418

Browse files
[fix] Fixed timezone handling in delete_old_radiusbatch_users command #611
- Fixed date/datetime comparison issue in delete_old_radiusbatch_users command - Added freezegun to test for triggering bug on purpose - Loop over objects with iterator() in delete_old_radiusbatch_users in delete_old_radiusbatch_users Closes #611 --------- Co-authored-by: Federico Capoano <f.capoano@openwisp.io>
1 parent 30bb222 commit 16eb418

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

openwisp_radius/management/commands/base/delete_old_radiusbatch_users.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ def handle(self, *args, **options):
3333
days = 30 * options["older_than_months"]
3434
else:
3535
days = BATCH_DELETE_EXPIRED
36-
threshold_date = now() - timedelta(days=days)
37-
36+
threshold_date = (now() - timedelta(days=days)).date()
3837
batches = RadiusBatch.objects.filter(expiration_date__lt=threshold_date)
39-
time_period = threshold_date.strftime("%Y-%m-%d %H:%M:%S")
40-
41-
for b in batches:
38+
for b in batches.iterator():
4239
b.delete()
40+
time_period = threshold_date.strftime("%Y-%m-%d %H:%M:%S")
4341
self.stdout.write(f"Deleted accounts older than {time_period}")

openwisp_radius/tests/test_commands.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from django.contrib.auth import get_user_model
77
from django.core.management import CommandError, call_command
88
from django.utils.timezone import get_default_timezone, now
9+
from freezegun import freeze_time
910
from netaddr import EUI, mac_unix
1011
from openvpn_status.models import Routing
1112

1213
from openwisp_utils.tests import capture_any_output, capture_stdout
1314

1415
from .. import settings as app_settings
1516
from ..utils import load_model
16-
from . import _RADACCT, CallCommandMixin, FileMixin
17+
from . import _RADACCT, _TEST_DATE, CallCommandMixin, FileMixin
1718
from .mixins import BaseTestCase
1819

1920
User = get_user_model()
@@ -164,8 +165,9 @@ def test_deactivate_expired_users_command(self):
164165
call_command("deactivate_expired_users")
165166
self.assertEqual(get_user_model().objects.filter(is_active=True).count(), 0)
166167

168+
@freeze_time(_TEST_DATE)
167169
@capture_stdout()
168-
def test_delete_old_radiusbatch_users_command(self):
170+
def test_delete_old_radiusbatch_users_command(self, subtest_id=None):
169171
# Create RadiusBatch users that expired more than 18 months ago
170172
path = self._get_path("static/test_batch.csv")
171173
options = dict(
@@ -214,9 +216,11 @@ def test_delete_old_radiusbatch_users_command(self):
214216

215217
with self.subTest("Test executing command with both arguments"):
216218
options["name"] = "test3"
217-
call_command("batch_add_users", **options)
219+
self._call_command("batch_add_users", **options)
218220
call_command(
219-
"delete_old_radiusbatch_users", older_than_days=9, older_than_months=12
221+
"delete_old_radiusbatch_users",
222+
older_than_days=9,
223+
older_than_months=12,
220224
)
221225
# Users that expired more than 9 days ago should be deleted
222226
self.assertEqual(get_user_model().objects.all().count(), 0)

0 commit comments

Comments
 (0)