Skip to content

Commit

Permalink
function provider access to logs (#1403)
Browse files Browse the repository at this point in the history
* function provider access to logs
  • Loading branch information
akihikokuroda authored Jul 15, 2024
1 parent f6fdf25 commit acf1323
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
13 changes: 11 additions & 2 deletions gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,18 @@ def logs(self, request, pk=None): # pylint: disable=invalid-name,unused-argumen
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.job.logs", context=ctx):
job = self.get_object()
job = Job.objects.filter(pk=pk).first()
if job is None:
return Response(status=404)
logs = job.logs
return Response({"logs": logs})
author = self.request.user
if job.program and job.program.provider:
if job.program.provider.admin_group in author.groups.all():
return Response({"logs": logs})
return Response({"logs": "No available logs"})
if author == job.author:
return Response({"logs": logs})
return Response({"logs": "No available logs"})

def get_runtime_job(self, job):
"""get runtime job for job"""
Expand Down
48 changes: 47 additions & 1 deletion gateway/tests/api/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_job_list(self):

jobs_response = self.client.get(reverse("v1:jobs-list"), format="json")
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("count"), 2)
self.assertEqual(jobs_response.data.get("count"), 3)
self.assertEqual(
jobs_response.data.get("results")[0].get("status"), "SUCCEEDED"
)
Expand Down Expand Up @@ -90,3 +90,49 @@ def test_stop_job(self):
).first()
self.assertEqual(job.status, Job.STOPPED)
self.assertEqual(job_stop_response.data.get("message"), "Job has been stopped.")

def test_job_logs_by_author_for_function_without_provider(self):
"""Tests job log by job author."""
self._authorize()

jobs_response = self.client.get(
reverse("v1:jobs-logs", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec82"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("logs"), "log entry 2")

def test_job_logs_by_author_for_function_with_provider(self):
"""Tests job log by job author."""
self._authorize()

jobs_response = self.client.get(
reverse("v1:jobs-logs", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec85"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("logs"), "No available logs")

def test_job_logs_by_function_provider(self):
"""Tests job log by fuction provider."""
user = models.User.objects.get(username="test_user_2")
self.client.force_authenticate(user=user)

jobs_response = self.client.get(
reverse("v1:jobs-logs", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec85"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("logs"), "log entry 1")

def test_job_logs(self):
"""Tests job log non-authorized."""
user = models.User.objects.get(username="test_user_3")
self.client.force_authenticate(user=user)

jobs_response = self.client.get(
reverse("v1:jobs-logs", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec85"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("logs"), "No available logs")
15 changes: 14 additions & 1 deletion gateway/tests/fixtures/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"created": "2023-02-01T15:30:43.281796Z",
"result": "{\"somekey\":1}",
"status": "SUCCEEDED",
"author": 1
"author": 1,
"logs": "log entry 2"
}
},
{
Expand All @@ -100,6 +101,18 @@
"author": 2
}
},
{
"model": "api.job",
"pk": "1a7947f9-6ae8-4e3d-ac1e-e7d608deec85",
"fields": {
"program": "6160a2ff-e482-443d-af23-15110b646ae2",
"created": "2023-02-01T15:30:43.281796Z",
"result": "{\"somekey\":1}",
"status": "QUEUED",
"author": 1,
"logs": "log entry 1"
}
},
{
"model": "api.runtimejob",
"pk": "runtime_job_1",
Expand Down

0 comments on commit acf1323

Please sign in to comment.