Skip to content

Commit

Permalink
fixup!: anonymous subs
Browse files Browse the repository at this point in the history
  • Loading branch information
rgraber committed Jan 8, 2025
1 parent dad2453 commit f81af6b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kobo/apps/audit_log/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def add_instance_to_request(instance, created, **kwargs):
return
if getattr(request, 'instances', None) is None:
request.instances = {}
username = instance.user.username if instance.user else 'AnonymousUser'
username = instance.user.username if instance.user else None
request.instances.update(
{
instance.id: SubmissionUpdate(
Expand Down
43 changes: 31 additions & 12 deletions kobo/apps/audit_log/tests/test_project_history_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,9 @@ def test_log_created_for_duplicate_submission(self, duplicating_user):
)
self.assertEqual(metadata['submission']['submitted_by'], duplicating_user)

def test_update_one(self):
self._add_submission('admin')
@data('admin', None)
def test_update_one_submission_content(self, username):
self._add_submission(username)
submissions_xml = self.asset.deployment.get_submissions(
self.asset.owner, format_type='xml'
)
Expand Down Expand Up @@ -1545,12 +1546,13 @@ def test_update_one(self):
self.assertEqual(log.object_id, self.asset.id)
self.assertEqual(log.action, AuditAction.MODIFY_SUBMISSION)
self._check_common_metadata(log.metadata, PROJECT_HISTORY_LOG_PROJECT_SUBTYPE)
submitted_by = username if username is not None else 'AnonymousUser'
self.assertEqual(log.metadata['submission']['submitted_by'], submitted_by)

self.assertEqual(log.metadata['submission']['submitted_by'], 'admin')

def test_update_multiple(self):
def test_update_multiple_submissions_content(self):
self._add_submission('admin')
self._add_submission('someuser')
self._add_submission(None)

submissions_json = self.asset.deployment.get_submissions(
self.asset.owner, fields=['_id']
Expand All @@ -1570,7 +1572,7 @@ def test_update_multiple(self):
format='json',
)

self.assertEqual(ProjectHistoryLog.objects.count(), 2)
self.assertEqual(ProjectHistoryLog.objects.count(), 3)
log1 = ProjectHistoryLog.objects.filter(
metadata__submission__submitted_by='admin'
).first()
Expand All @@ -1583,8 +1585,15 @@ def test_update_multiple(self):
self._check_common_metadata(log2.metadata, PROJECT_HISTORY_LOG_PROJECT_SUBTYPE)
self.assertEqual(log2.action, AuditAction.MODIFY_SUBMISSION)

def test_single_validation_status(self):
self._add_submission('admin')
log2 = ProjectHistoryLog.objects.filter(
metadata__submission__submitted_by='AnonymousUser'
).first()
self._check_common_metadata(log2.metadata, PROJECT_HISTORY_LOG_PROJECT_SUBTYPE)
self.assertEqual(log2.action, AuditAction.MODIFY_SUBMISSION)

@data('admin', None)
def test_update_single_submission_validation_status(self, username):
self._add_submission(username)
submissions_json = self.asset.deployment.get_submissions(
self.asset.owner, fields=['_id']
)
Expand All @@ -1601,13 +1610,14 @@ def test_single_validation_status(self):
expected_action=AuditAction.MODIFY_SUBMISSION,
expected_subtype=PROJECT_HISTORY_LOG_PROJECT_SUBTYPE,
)

self.assertEqual(log_metadata['submission']['submitted_by'], 'admin')
expected_username = username if username is not None else 'AnonymousUser'
self.assertEqual(log_metadata['submission']['submitted_by'], expected_username)
self.assertEqual(log_metadata['submission']['status'], 'On Hold')

def test_multiple_validation_statuses(self):
def test_multiple_submision_validation_statuses(self):
self._add_submission('admin')
self._add_submission('someuser')
self._add_submission(None)
submissions_json = self.asset.deployment.get_submissions(
self.asset.owner, fields=['_id']
)
Expand All @@ -1627,7 +1637,7 @@ def test_multiple_validation_statuses(self):
format='json',
)

self.assertEqual(ProjectHistoryLog.objects.count(), 2)
self.assertEqual(ProjectHistoryLog.objects.count(), 3)
log1 = ProjectHistoryLog.objects.filter(
metadata__submission__submitted_by='admin'
).first()
Expand All @@ -1645,3 +1655,12 @@ def test_multiple_validation_statuses(self):
self.assertEqual(
log2.metadata['submission']['status'], 'On Hold'
)

log2 = ProjectHistoryLog.objects.filter(
metadata__submission__submitted_by='AnonymousUser'
).first()
self._check_common_metadata(log2.metadata, PROJECT_HISTORY_LOG_PROJECT_SUBTYPE)
self.assertEqual(log2.action, AuditAction.MODIFY_SUBMISSION)
self.assertEqual(
log2.metadata['submission']['status'], 'On Hold'
)
5 changes: 4 additions & 1 deletion kobo/apps/audit_log/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
class SubmissionUpdate:
status: str
action: str
username: str
id: int
username: str = 'AnonymousUser'

def __post_init__(self):
self.username = 'AnonymousUser' if self.username is None else self.username

0 comments on commit f81af6b

Please sign in to comment.