Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RuthShryock committed Dec 3, 2024
1 parent fee5f37 commit 6eac898
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
38 changes: 25 additions & 13 deletions kobo/apps/audit_log/tests/api/v2/test_api_audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_export_as_anonymous_returns_unauthorized(self):
response = self.client.post(self.url)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

def test_export_for_user_commences(self):
def test_export_for_user_returns_success(self):
self.force_login_user(User.objects.get(username='anotheruser'))
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
Expand All @@ -462,12 +462,19 @@ def test_create_export_task_on_post(self):
.first()
)
self.assertIsNotNone(task)
self.assertEqual(task.status, 'complete')
self.assertEqual(task.status in ['created', 'processing', 'complete'])

def test_get_status_of_tasks(self):
self.force_login_user(User.objects.get(username='anotheruser'))
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
test_user = User.objects.get(username='anotheruser')
self.force_login_user(test_user)

AccessLogExportTask.objects.create(
user=test_user,
get_all_logs=False,
data={
'type': 'access_logs_export',
},
)

response_status = self.client.get(self.url)
self.assertEqual(response_status.status_code, status.HTTP_200_OK)
Expand All @@ -482,7 +489,6 @@ def test_get_status_of_tasks(self):
self.assertIn('uid', first_task)
self.assertIn('status', first_task)
self.assertIn('date_created', first_task)
self.assertEqual(first_task['status'], 'complete')

def test_multiple_export_tasks_not_allowed(self):
test_user = User.objects.get(username='anotheruser')
Expand Down Expand Up @@ -522,12 +528,12 @@ def test_regular_user_cannot_export_access_logs(self):
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_export_access_logs_for_superuser_commences(self):
def test_export_access_logs_for_superuser_returns_success(self):
self.force_login_user(User.objects.get(username='admin'))
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)

def test__superuser_create_export_task_on_post(self):
def test_superuser_create_export_task_on_post(self):
test_superuser = User.objects.get(username='admin')
self.force_login_user(test_superuser)

Expand All @@ -540,12 +546,19 @@ def test__superuser_create_export_task_on_post(self):
.first()
)
self.assertIsNotNone(task)
self.assertEqual(task.status, 'complete')
self.assertEqual(task.status in ['created', 'processing', 'complete'])

def test_superuser_get_status_tasks(self):
self.force_login_user(User.objects.get(username='admin'))
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
test_user = User.objects.get(username='anotheruser')
self.force_login_user(test_user)

AccessLogExportTask.objects.create(
user=test_user,
get_all_logs=False,
data={
'type': 'access_logs_export',
},
)

response_status = self.client.get(self.url)
self.assertEqual(response_status.status_code, status.HTTP_200_OK)
Expand All @@ -560,7 +573,6 @@ def test_superuser_get_status_tasks(self):
self.assertIn('uid', first_task)
self.assertIn('status', first_task)
self.assertIn('date_created', first_task)
self.assertEqual(first_task['status'], 'complete')

def test_permission_denied_for_non_superusers_on_get_status(self):
non_superuser = User.objects.get(username='anotheruser')
Expand Down
13 changes: 7 additions & 6 deletions kobo/apps/audit_log/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,11 @@ def create_task(self, request, get_all_logs):
status=status.HTTP_202_ACCEPTED,
)

def list_tasks(self, request):
tasks = AccessLogExportTask.objects.filter(user=request.user).order_by(
'-date_created'
)
def list_tasks(self, user=None):
tasks = AccessLogExportTask.objects.all()
if user is not None:
tasks = tasks.filter(user=user)
tasks = tasks.order_by('-date_created')

tasks_data = [
{'uid': task.uid, 'status': task.status, 'date_created': task.date_created}
Expand Down Expand Up @@ -427,7 +428,7 @@ def create(self, request, *args, **kwargs):
return self.create_task(request, get_all_logs=False)

def list(self, request, *args, **kwargs):
return self.list_tasks(request)
return self.list_tasks(request.user)


class AllAccessLogsExportViewSet(BaseAccessLogsExportViewSet):
Expand Down Expand Up @@ -493,4 +494,4 @@ def create(self, request, *args, **kwargs):
return self.create_task(request, get_all_logs=True)

def list(self, request, *args, **kwargs):
return self.list_tasks(request)
return self.list_tasks()

0 comments on commit 6eac898

Please sign in to comment.