Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow anonymous users to get condor job status #1119

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/unit_tests/test_tethys_compute/test_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ def test_JobManager_create_job_string(self, mock_cj, mock_guw):
workspace="test_user_workspace",
)

@mock.patch("tethys_compute.job_manager.isinstance")
@mock.patch("tethys_compute.job_manager.get_anonymous_user")
@mock.patch("tethys_compute.job_manager.get_user_workspace")
@mock.patch("tethys_compute.job_manager.CondorJob")
def test_JobManager_create_job_anonymous_user(
self, mock_cj, mock_guw, mock_get_anonymous_user, mock_isinstance
):
mock_app = mock.MagicMock()
mock_app.package = "test_label"
mock_guw().path = "test_user_workspace"
mock_user = mock.MagicMock(is_staff=False, is_anonymous=True)
mock_user.has_perm.return_value = False
mock_anonymous_user = mock.MagicMock(is_staff=False)
mock_anonymous_user.has_perm.return_value = False
mock_get_anonymous_user.return_value = mock_anonymous_user
mock_isinstance.return_value = True

# Execute
ret_jm = JobManager(mock_app)
with mock.patch.dict(JOB_TYPES, {"CONDOR": mock_cj}):
ret_jm.create_job(name="test_name", user=mock_user, job_type="CONDOR")
mock_cj.assert_called_with(
label="test_label",
name="test_name",
user=mock_anonymous_user,
workspace="test_user_workspace",
)

def test_JobManager_list_job_with_user(self):
mgr = JobManager(self.app_model)
ret = mgr.list_jobs(user=self.user_model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,36 @@ def tearDown(self):

@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass")
async def test_get_job(self, mock_tj):
mock_user = mock.MagicMock(is_staff=False)
mock_user = mock.MagicMock(is_staff=False, is_anonymous=False)
mock_user.has_perm.return_value = False
await tethys_compute_update_status.get_job("job_id", mock_user)
mock_tj.assert_called_with(id="job_id", user=mock_user)

@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass")
async def test_get_job_staff(self, mock_tj):
mock_user = mock.MagicMock(is_staff=True)
mock_user = mock.MagicMock(is_staff=True, is_anonymous=False)
await tethys_compute_update_status.get_job("job_id", mock_user)
mock_tj.assert_called_with(id="job_id")

@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass")
async def test_get_job_has_permission(self, mock_tj):
mock_user = mock.MagicMock(is_staff=False)
mock_user = mock.MagicMock(is_staff=False, is_anonymous=False)
mock_user.has_perm.return_value = True
await tethys_compute_update_status.get_job("job_id", mock_user)
mock_tj.assert_called_with(id="job_id")

@mock.patch("tethys_compute.views.update_status.get_anonymous_user")
@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass")
async def test_get_job_anonymous_user(self, mock_tj, mock_get_anonymous_user):
mock_user = mock.MagicMock(is_staff=False, is_anonymous=True)
mock_user.has_perm.return_value = False
mock_anonymous_user = mock.MagicMock(is_staff=False)
mock_anonymous_user.has_perm.return_value = False
mock_get_anonymous_user.return_value = mock_anonymous_user
await tethys_compute_update_status.get_job("job_id", mock_user)
mock_get_anonymous_user.assert_called_once()
mock_tj.assert_called_with(id="job_id", user=mock_anonymous_user)

@mock.patch("tethys_compute.views.update_status.logger")
@mock.patch("tethys_compute.views.update_status.JsonResponse")
@mock.patch("tethys_compute.views.update_status.TethysJob")
Expand Down
11 changes: 11 additions & 0 deletions tethys_apps/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
from .base import has_permission


def async_login_required(func):
@wraps(func)
async def wrapper(request, *args, **kwargs):
redirect = login_required(lambda r: r)(request)
if redirect != request:
return redirect
return await func(request, *args, **kwargs)

return wrapper


def login_required(
function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None
):
Expand Down
5 changes: 4 additions & 1 deletion tethys_compute/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import logging


from django.contrib.auth.models import User
from django.urls import reverse
from guardian.utils import get_anonymous_user

from tethys_compute.models.tethys_job import TethysJob
from tethys_compute.models.basic_job import BasicJob
Expand Down Expand Up @@ -68,6 +69,8 @@ def create_job(self, name, user, groups=None, job_type=None, **kwargs):
# Allow the job class to be passed in as job_type.
if isinstance(job_type, str):
job_type = JOB_TYPES[job_type]
if isinstance(user, User) and user.is_anonymous:
user = get_anonymous_user()
user_workspace = get_user_workspace(self.app, user)
kwrgs = dict(
name=name, user=user, label=self.label, workspace=user_workspace.path
Expand Down
3 changes: 3 additions & 0 deletions tethys_compute/views/update_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging

from django.http import JsonResponse
from guardian.utils import get_anonymous_user
from channels.db import database_sync_to_async

from tethys_compute.models import TethysJob, DaskJob
Expand All @@ -33,6 +34,8 @@ def get_job(job_id, user=None):
Returns: `TethysJob` object

"""
if user is not None and user.is_anonymous:
user = get_anonymous_user()
if (
user is None
or user.is_staff
Expand Down
14 changes: 1 addition & 13 deletions tethys_gizmos/views/gizmos/jobs_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import inspect
import logging
import re
from functools import wraps

from django.http import JsonResponse
from django.template.loader import render_to_string
from django.contrib.auth.decorators import login_required
from channels.db import database_sync_to_async

from tethys_apps.decorators import async_login_required
from tethys_compute.models import CondorWorkflow, DaskJob, DaskScheduler
from tethys_gizmos.gizmo_options.jobs_table import JobsTable
from tethys_sdk.gizmos import SelectInput
Expand All @@ -30,17 +29,6 @@
logger = logging.getLogger(f"tethys.{__name__}")


def async_login_required(func):
@wraps(func)
async def wrapper(request, *args, **kwargs):
redirect = login_required(lambda r: r)(request)
if redirect != request:
return redirect
return await func(request, *args, **kwargs)

return wrapper


async def _get_log_content(job, key1, key2):
# Get the Job logs.
if inspect.iscoroutinefunction(job.get_logs):
Expand Down
Loading