|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.test import TestCase |
| 5 | +from django.utils import timezone |
| 6 | +from django_dynamic_fixture import get |
| 7 | + |
| 8 | +from readthedocs.audit.models import AuditLog |
| 9 | +from readthedocs.audit.tasks import delete_old_personal_audit_logs |
| 10 | +from readthedocs.organizations.models import Organization |
| 11 | +from readthedocs.projects.models import Project |
| 12 | + |
| 13 | + |
| 14 | +class AuditTasksTest(TestCase): |
| 15 | + def setUp(self): |
| 16 | + self.user = get(User) |
| 17 | + self.project = get( |
| 18 | + Project, |
| 19 | + slug="project", |
| 20 | + ) |
| 21 | + self.organization = get( |
| 22 | + Organization, |
| 23 | + owners=[self.user], |
| 24 | + name="testorg", |
| 25 | + ) |
| 26 | + self.organization.projects.add(self.project) |
| 27 | + |
| 28 | + self.another_user = get(User) |
| 29 | + self.another_project = get( |
| 30 | + Project, |
| 31 | + slug="another-project", |
| 32 | + users=[self.user], |
| 33 | + ) |
| 34 | + |
| 35 | + @mock.patch("django.utils.timezone.now") |
| 36 | + def test_delete_old_personal_audit_logs(self, now_mock): |
| 37 | + now_mock.return_value = timezone.datetime( |
| 38 | + year=2021, |
| 39 | + month=5, |
| 40 | + day=5, |
| 41 | + ) |
| 42 | + newer_date = timezone.datetime( |
| 43 | + year=2021, |
| 44 | + month=4, |
| 45 | + day=30, |
| 46 | + ) |
| 47 | + middle_date = timezone.datetime( |
| 48 | + year=2021, |
| 49 | + month=4, |
| 50 | + day=5, |
| 51 | + ) |
| 52 | + old_date = timezone.datetime( |
| 53 | + year=2021, |
| 54 | + month=3, |
| 55 | + day=20, |
| 56 | + ) |
| 57 | + for date in [newer_date, middle_date, old_date]: |
| 58 | + for user in [self.user, self.another_user]: |
| 59 | + # Log attached to a project and organization. |
| 60 | + get( |
| 61 | + AuditLog, |
| 62 | + user=user, |
| 63 | + project=self.project, |
| 64 | + created=date, |
| 65 | + action=AuditLog.PAGEVIEW, |
| 66 | + ) |
| 67 | + # Log attached to a project only. |
| 68 | + get( |
| 69 | + AuditLog, |
| 70 | + user=user, |
| 71 | + project=self.another_project, |
| 72 | + created=date, |
| 73 | + action=AuditLog.PAGEVIEW, |
| 74 | + ) |
| 75 | + |
| 76 | + # Log attached to the user only. |
| 77 | + get( |
| 78 | + AuditLog, |
| 79 | + user=user, |
| 80 | + created=date, |
| 81 | + action=AuditLog.AUTHN, |
| 82 | + ) |
| 83 | + |
| 84 | + # Log without a user. |
| 85 | + get( |
| 86 | + AuditLog, |
| 87 | + created=date, |
| 88 | + action=AuditLog.AUTHN_FAILURE, |
| 89 | + ) |
| 90 | + |
| 91 | + # Log with a organization, and without a user. |
| 92 | + get( |
| 93 | + AuditLog, |
| 94 | + project=self.project, |
| 95 | + created=date, |
| 96 | + action=AuditLog.AUTHN_FAILURE, |
| 97 | + ) |
| 98 | + |
| 99 | + self.assertEqual(AuditLog.objects.all().count(), 24) |
| 100 | + |
| 101 | + # We don't have logs older than 90 days. |
| 102 | + delete_old_personal_audit_logs(days=90) |
| 103 | + self.assertEqual(AuditLog.objects.all().count(), 24) |
| 104 | + |
| 105 | + # Only 5 logs can be deteled. |
| 106 | + delete_old_personal_audit_logs(days=30) |
| 107 | + self.assertEqual(AuditLog.objects.all().count(), 19) |
| 108 | + |
| 109 | + # Only 5 logs can be deteled. |
| 110 | + delete_old_personal_audit_logs(days=10) |
| 111 | + self.assertEqual(AuditLog.objects.all().count(), 14) |
| 112 | + |
| 113 | + # Only 5 logs can be deteled. |
| 114 | + delete_old_personal_audit_logs(days=1) |
| 115 | + self.assertEqual(AuditLog.objects.all().count(), 9) |
0 commit comments