diff --git a/apps/tasks/tests.py b/apps/tasks/tests.py index ba19a42..b19c659 100644 --- a/apps/tasks/tests.py +++ b/apps/tasks/tests.py @@ -179,3 +179,14 @@ def test_send_submission_is_redirecting(self) -> None: def test_send_submission_without_authentication(self) -> None: response = self.client.post(self.url, data={"code": self.code}) self.assertEqual(response.status_code, 302) + + def test_access_task_that_is_accessible(self) -> None: + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + + def test_access_task_that_is_not_accessible(self) -> None: + self.task.contest.cancelled = True + self.task.contest.save() + + response = self.client.get(self.url) + self.assertEqual(response.status_code, 302) diff --git a/apps/tasks/views.py b/apps/tasks/views.py index 2296b9b..9278226 100644 --- a/apps/tasks/views.py +++ b/apps/tasks/views.py @@ -32,6 +32,17 @@ def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: def get_success_url(self) -> str: return reverse("tasks:detail", args=[self.object.id]) + def get( + self, request: HttpRequest, *args: Any, **kwargs: Any + ) -> HttpResponse: + self.object = self.get_object() + + if not self.object.is_accessible: + return redirect("home") + + context = self.get_context_data(object=self.object) + return self.render_to_response(context) + def post(self, request: HttpRequest, *, pk: int) -> HttpResponse: # Unauthenticated users should not be able to submit # a submission to a task, so we redirect them to the