|
| 1 | +# Copyright The IETF Trust 2021, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import datetime |
| 5 | +from io import StringIO |
| 6 | + |
| 7 | +from django.core.management import call_command, CommandError |
| 8 | + |
| 9 | +from ietf.person.factories import PersonApiKeyEventFactory |
| 10 | +from ietf.person.models import PersonApiKeyEvent, PersonEvent |
| 11 | +from ietf.utils.test_utils import TestCase |
| 12 | + |
| 13 | + |
| 14 | +class CommandTests(TestCase): |
| 15 | + @staticmethod |
| 16 | + def _call_command(command_name, *args, **options): |
| 17 | + out = StringIO() |
| 18 | + options['stdout'] = out |
| 19 | + call_command(command_name, *args, **options) |
| 20 | + return out.getvalue() |
| 21 | + |
| 22 | + def _assert_purge_results(self, cmd_output, expected_delete_count, expected_kept_events): |
| 23 | + self.assertNotIn('Dry run requested', cmd_output) |
| 24 | + if expected_delete_count == 0: |
| 25 | + delete_text = 'No events older than' |
| 26 | + else: |
| 27 | + delete_text = 'Deleting {} event'.format(expected_delete_count) |
| 28 | + self.assertIn(delete_text, cmd_output) |
| 29 | + self.assertCountEqual( |
| 30 | + PersonApiKeyEvent.objects.all(), |
| 31 | + expected_kept_events, |
| 32 | + 'Wrong events were deleted' |
| 33 | + ) |
| 34 | + |
| 35 | + def _assert_purge_dry_run_results(self, cmd_output, expected_delete_count, expected_kept_events): |
| 36 | + self.assertIn('Dry run requested', cmd_output) |
| 37 | + if expected_delete_count == 0: |
| 38 | + delete_text = 'No events older than' |
| 39 | + else: |
| 40 | + delete_text = 'Would delete {} event'.format(expected_delete_count) |
| 41 | + self.assertIn(delete_text, cmd_output) |
| 42 | + self.assertCountEqual( |
| 43 | + PersonApiKeyEvent.objects.all(), |
| 44 | + expected_kept_events, |
| 45 | + 'Events were deleted when dry-run option was used' |
| 46 | + ) |
| 47 | + |
| 48 | + def test_purge_old_personal_api_key_events(self): |
| 49 | + keep_days = 10 |
| 50 | + |
| 51 | + # Remember how many PersonEvents were present so we can verify they're cleaned up properly. |
| 52 | + personevents_before = PersonEvent.objects.count() |
| 53 | + |
| 54 | + now = datetime.datetime.now() |
| 55 | + # The first of these events will be timestamped a fraction of a second more than keep_days |
| 56 | + # days ago by the time we call the management command, so will just barely chosen for purge. |
| 57 | + old_events = [ |
| 58 | + PersonApiKeyEventFactory(time=now - datetime.timedelta(days=n)) |
| 59 | + for n in range(keep_days, 2 * keep_days + 1) |
| 60 | + ] |
| 61 | + num_old_events = len(old_events) |
| 62 | + |
| 63 | + recent_events = [ |
| 64 | + PersonApiKeyEventFactory(time=now - datetime.timedelta(days=n)) |
| 65 | + for n in range(0, keep_days) |
| 66 | + ] |
| 67 | + # We did not create recent_event timestamped exactly keep_days ago because it would |
| 68 | + # be treated as an old_event by the management command. Create an event a few seconds |
| 69 | + # on the "recent" side of keep_days old to test the threshold. |
| 70 | + recent_events.append( |
| 71 | + PersonApiKeyEventFactory( |
| 72 | + time=now + datetime.timedelta(seconds=3) - datetime.timedelta(days=keep_days) |
| 73 | + ) |
| 74 | + ) |
| 75 | + num_recent_events = len(recent_events) |
| 76 | + |
| 77 | + # call with dry run |
| 78 | + output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run') |
| 79 | + self._assert_purge_dry_run_results(output, num_old_events, old_events + recent_events) |
| 80 | + |
| 81 | + # call for real |
| 82 | + output = self._call_command('purge_old_personal_api_key_events', str(keep_days)) |
| 83 | + self._assert_purge_results(output, num_old_events, recent_events) |
| 84 | + self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events, |
| 85 | + 'PersonEvents were not cleaned up properly') |
| 86 | + |
| 87 | + # repeat - there should be nothing left to delete |
| 88 | + output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days)) |
| 89 | + self._assert_purge_dry_run_results(output, 0, recent_events) |
| 90 | + |
| 91 | + output = self._call_command('purge_old_personal_api_key_events', str(keep_days)) |
| 92 | + self._assert_purge_results(output, 0, recent_events) |
| 93 | + self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events, |
| 94 | + 'PersonEvents were not cleaned up properly') |
| 95 | + |
| 96 | + # and now delete the remaining events |
| 97 | + output = self._call_command('purge_old_personal_api_key_events', '0') |
| 98 | + self._assert_purge_results(output, num_recent_events, []) |
| 99 | + self.assertEqual(PersonEvent.objects.count(), personevents_before, |
| 100 | + 'PersonEvents were not cleaned up properly') |
| 101 | + |
| 102 | + def test_purge_old_personal_api_key_events_rejects_invalid_arguments(self): |
| 103 | + """The purge_old_personal_api_key_events command should reject invalid arguments""" |
| 104 | + event = PersonApiKeyEventFactory(time=datetime.datetime.now() - datetime.timedelta(days=30)) |
| 105 | + |
| 106 | + with self.assertRaises(CommandError): |
| 107 | + self._call_command('purge_old_personal_api_key_events') |
| 108 | + |
| 109 | + with self.assertRaises(CommandError): |
| 110 | + self._call_command('purge_old_personal_api_key_events', '-15') |
| 111 | + |
| 112 | + with self.assertRaises(CommandError): |
| 113 | + self._call_command('purge_old_personal_api_key_events', '15.3') |
| 114 | + |
| 115 | + with self.assertRaises(CommandError): |
| 116 | + self._call_command('purge_old_personal_api_key_events', '15', '15') |
| 117 | + |
| 118 | + with self.assertRaises(CommandError): |
| 119 | + self._call_command('purge_old_personal_api_key_events', 'abc', '15') |
| 120 | + |
| 121 | + self.assertCountEqual(PersonApiKeyEvent.objects.all(), [event]) |
0 commit comments