def test_call_with_unknown_task(self):
        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)
        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 400
    def test_success_task_state_without_error_and_custom_data(self, monkeypatch):
        class MockAsyncResult:
            state = TaskState.SUCCESS
            info = {
                "status_message": "again, no state",
                "data": {
                    "what?": "just some custom information"
                }
            }

        monkeypatch.setattr(app, "AsyncResult", lambda task_id: MockAsyncResult())

        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"  # AJAX request
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)

        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 200, "Should be callable"
        assert json.loads(response.content.decode()) == {
            "state": "success",
            "status_message": "again, no state",
            "data": {
                "what?": "just some custom information"
            }
        }
    def test_authenticated_user(self, monkeypatch):
        class MockAsyncResult:
            state = TaskState.PENDING

        monkeypatch.setattr(app, "AsyncResult", lambda task_id: MockAsyncResult())

        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"  # AJAX request
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)

        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 200, "Should be callable"
        assert json.loads(response.content.decode()) == {"state": "pending", "status_message": "try to start task"}
    def test_failed_redis_connection(self, monkeypatch):
        """redis not available"""
        def raise_exception():
            raise redis.ConnectionError()

        monkeypatch.setattr(app, "AsyncResult", lambda task_id: raise_exception())

        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"  # AJAX request
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)

        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 200, "Should be callable"
        assert json.loads(response.content.decode()) == {
            "state": "failed",
            "error_message": "A server process (redis) is not running, please contact the administrator"
        }
    def test_failed_task_state(self, monkeypatch):
        """The task was not successful completed (traceback)"""
        class MockAsyncResult:
            state = TaskState.FAILED
            info = "TRACEBACK"

        monkeypatch.setattr(app, "AsyncResult", lambda task_id: MockAsyncResult())

        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"  # AJAX request
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)

        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 200, "Should be callable"
        assert json.loads(response.content.decode()) == {
            "state": "failed", "error_message": "TRACEBACK"
        }
    def test_success_task_state_with_error(self, monkeypatch):
        """The task itself was successful, but the result was an error"""
        class MockAsyncResult:
            state = TaskState.SUCCESS
            info = {
                "error_message": "something went wrong"
            }

        monkeypatch.setattr(app, "AsyncResult", lambda task_id: MockAsyncResult())

        url = reverse(self.URL_NAME, kwargs={"task_id": "mock_task_id"})
        request = RequestFactory().get(url)
        request.META["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest"  # AJAX request
        request.user = mixer.blend("auth.User", is_superuser=False, is_staff=False)

        response = views.task_status_ajax(request, "mock_task_id")

        assert response.status_code == 200, "Should be callable"
        assert json.loads(response.content.decode()) == {
            "state": "success", "status_message": "", "error_message": "something went wrong"
        }