mirror of
https://github.com/jazzband/django-axes.git
synced 2026-03-16 22:30:23 +00:00
added delete_access_logs management commands as well as tests for it #455
This commit is contained in:
parent
fa83253056
commit
1253d6aeaa
2 changed files with 44 additions and 1 deletions
18
axes/management/commands/axes_delete_access_logs.py
Normal file
18
axes/management/commands/axes_delete_access_logs.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from axes.models import AccessLog
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Deletes all logs that are older than `age` days old.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('age', type=int, help='In days.')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
limit_date = timezone.now().date() - timezone.timedelta(days=options['age'])
|
||||
to_be_deleted = AccessLog.objects.filter(attempt_time__gt=limit_date)
|
||||
|
||||
self.stdout.write(f'{to_be_deleted.count()} logs will be deleted.')
|
||||
|
||||
to_be_deleted.delete()
|
||||
|
|
@ -1,10 +1,35 @@
|
|||
from io import StringIO
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.utils import timezone
|
||||
|
||||
from axes.models import AccessAttempt
|
||||
from axes.models import AccessAttempt, AccessLog
|
||||
from axes.tests.base import AxesTestCase
|
||||
|
||||
class DeleteAccessLogsManagementCommandTestCase(AxesTestCase):
|
||||
def setUp(self):
|
||||
yesterday = timezone.now() - timezone.timedelta(days=1)
|
||||
with patch('django.utils.timezone.now', Mock(return_value=yesterday)):
|
||||
AccessLog.objects.create()
|
||||
|
||||
ten_days_ago = timezone.now() - timezone.timedelta(days=10)
|
||||
with patch('django.utils.timezone.now', Mock(return_value=ten_days_ago)):
|
||||
AccessLog.objects.create()
|
||||
|
||||
def test_axes_delete_access_logs(self):
|
||||
expected = '1 logs will be deleted.\n'
|
||||
|
||||
out = StringIO()
|
||||
call_command('axes_delete_access_logs', 5, stdout=out)
|
||||
|
||||
self.assertEqual(expected, out.getvalue())
|
||||
|
||||
out = StringIO()
|
||||
call_command('axes_delete_access_logs', 15, stdout=out)
|
||||
|
||||
self.assertEqual(expected, out.getvalue())
|
||||
|
||||
|
||||
class ManagementCommandTestCase(AxesTestCase):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue