Skip to content

Commit

Permalink
allow provider to get the job by id
Browse files Browse the repository at this point in the history
  • Loading branch information
akihikokuroda committed Sep 18, 2024
1 parent 74565d0 commit ec8343f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gateway/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ def retrieve(self, request, pk=None): # pylint: disable=unused-argument
tracer = trace.get_tracer("gateway.tracer")
ctx = TraceContextTextMapPropagator().extract(carrier=request.headers)
with tracer.start_as_current_span("gateway.job.retrieve", context=ctx):
job = Job.objects.filter(pk=pk).first()
if job is None:
logger.warning("Job [%s] not found", pk)
return Response(status=404)
author = self.request.user
if job.program and job.program.provider:
provider_groups = job.program.provider.admin_groups.all()
author_groups = author.groups.all()
has_access = any(group in provider_groups for group in author_groups)
if has_access:
serializer = self.get_serializer(job)
return Response(serializer.data)
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
Expand Down
13 changes: 13 additions & 0 deletions gateway/tests/api/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ def test_job_detail(self):
self.assertEqual(jobs_response.data.get("status"), "SUCCEEDED")
self.assertEqual(jobs_response.data.get("result"), '{"somekey":1}')

def test_job_provider_detail(self):
"""Tests job detail authorized."""
user = models.User.objects.get(username="test_user_2")
self.client.force_authenticate(user=user)

jobs_response = self.client.get(
reverse("v1:jobs-detail", args=["1a7947f9-6ae8-4e3d-ac1e-e7d608deec86"]),
format="json",
)
self.assertEqual(jobs_response.status_code, status.HTTP_200_OK)
self.assertEqual(jobs_response.data.get("status"), "QUEUED")
self.assertEqual(jobs_response.data.get("result"), '{"somekey":1}')

def test_not_authorized_job_detail(self):
"""Tests job detail fails trying to access to other user job."""
self._authorize()
Expand Down

0 comments on commit ec8343f

Please sign in to comment.