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

Fix for task id as valid UUID #3744

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
8 changes: 6 additions & 2 deletions rocky/rocky/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,19 @@ def list_tasks(self, **kwargs) -> PaginatedTasksResponse:
filter_key = "filters"
params = {k: v for k, v in kwargs.items() if v is not None if k != filter_key} # filter Nones from kwargs
endpoint = "/tasks"
res = self._client.post(endpoint, params=params, json=kwargs.get(filter_key, None))
res = self._client.post(endpoint, params=params, json=kwargs.get(filter_key))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is automatically removed and is not part of this PR. None is already a default return value.

return PaginatedTasksResponse.model_validate_json(res.content)
except ValidationError:
raise SchedulerValidationError(extra_message=_("Task list: "))
except ConnectError:
raise SchedulerConnectError(extra_message=_("Task list: "))

def get_task_details(self, task_id: str) -> Task:
return Task.model_validate_json(self._get(f"/tasks/{task_id}", "content"))
try:
task_id = str(uuid.UUID(task_id))
return Task.model_validate_json(self._get(f"/tasks/{task_id}", "content"))
except ValueError:
raise SchedulerBadRequestError()

def push_task(self, item: Task) -> None:
try:
Expand Down
1 change: 1 addition & 0 deletions rocky/rocky/views/task_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_context_data(self, **kwargs):
class NormalizerTaskJSONView(TaskDetailView):
task_type = "normalizer"
plugin_type = "normalizer"
template_name = "tasks/normalizer_task_detail.html"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test failed on this part, because it was missing the template name


def get(self, request, *args, **kwargs) -> JsonResponse | HttpResponse:
task = self.get_json_task_details()
Expand Down
25 changes: 25 additions & 0 deletions rocky/tests/scheduler/test_scheduler_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rocky.scheduler import SchedulerConnectError, SchedulerTooManyRequestError, SchedulerValidationError
from rocky.views.task_detail import NormalizerTaskJSONView
from rocky.views.tasks import BoefjesTaskListView
from tests.conftest import setup_request

Expand Down Expand Up @@ -37,3 +38,27 @@ def test_tasks_view_too_many_requests_error(rf, client_member, mock_scheduler):
list(request._messages)[0].message
== "Scheduler is receiving too many requests. Increase SCHEDULER_PQ_MAXSIZE or wait for task to finish."
)


def test_get_task_details_json_bad_task_id(rf, client_member, mock_scheduler):
request = setup_request(rf.get("normalizer_task_view"), client_member.user)

response = NormalizerTaskJSONView.as_view()(
request, organization_code=client_member.organization.code, task_id="/delete"
)

assert response.status_code == 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return a 404, not a 200.


assert list(request._messages)[0].message == "Task could not be found."


def test_reschedule_task_bad_task_id(rf, client_member, mock_bytes_client, mock_scheduler):
request = setup_request(
rf.post("task_list", {"action": "reschedule_task", "task_id": "/delete"}), client_member.user
)

response = BoefjesTaskListView.as_view()(request, organization_code=client_member.organization.code)

assert response.status_code == 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return a 404, not a 200.


assert list(request._messages)[0].message == "Task could not be found."