From acf13230d7929b4c1ebcbade2a37aa8a8bd19231 Mon Sep 17 00:00:00 2001 From: "Akihiko (Aki) Kuroda" <16141898+akihikokuroda@users.noreply.github.com> Date: Mon, 15 Jul 2024 18:54:22 -0400 Subject: [PATCH] function provider access to logs (#1403) * function provider access to logs --- gateway/api/views.py | 13 ++++++-- gateway/tests/api/test_job.py | 48 +++++++++++++++++++++++++++- gateway/tests/fixtures/fixtures.json | 15 ++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/gateway/api/views.py b/gateway/api/views.py index 03a301903..1caae4b64 100644 --- a/gateway/api/views.py +++ b/gateway/api/views.py @@ -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""" diff --git a/gateway/tests/api/test_job.py b/gateway/tests/api/test_job.py index 98bafd5c0..32559755d 100644 --- a/gateway/tests/api/test_job.py +++ b/gateway/tests/api/test_job.py @@ -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" ) @@ -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") diff --git a/gateway/tests/fixtures/fixtures.json b/gateway/tests/fixtures/fixtures.json index a9bf462ca..ca8182d4f 100644 --- a/gateway/tests/fixtures/fixtures.json +++ b/gateway/tests/fixtures/fixtures.json @@ -75,7 +75,8 @@ "created": "2023-02-01T15:30:43.281796Z", "result": "{\"somekey\":1}", "status": "SUCCEEDED", - "author": 1 + "author": 1, + "logs": "log entry 2" } }, { @@ -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",