예제 #1
0
def test_upload_validator_using_wrong_extension(rf: RequestFactory):
    widget = AjaxUploadWidget(
        ajax_target_path="/ajax",
        upload_validators=[
            ExtensionValidator(allowed_extensions=(".allowed-extension",))
        ],
    )
    widget.timeout = timedelta(seconds=1)
    content = load_test_data()

    upload_id = generate_new_upload_id(
        test_upload_validator_using_wrong_extension, content
    )

    request = create_upload_file_request(
        rf, content=b"should error", filename="test.wrong_extension"
    )
    response = widget.handle_ajax(request)
    assert response.status_code == 403

    request = create_upload_file_request(
        rf, content=b"should error", filename="test.allowed-extension"
    )
    response = widget.handle_ajax(request)
    assert response.status_code == 200
예제 #2
0
def test_wrong_upload_headers(rf: RequestFactory):
    widget = AjaxUploadWidget(ajax_target_path="/ajax")
    widget.timeout = timedelta(seconds=1)
    post_request = create_upload_file_request(rf)
    post_request.META["CSRF_COOKIE"] = None
    assert isinstance(widget.handle_ajax(post_request), HttpResponseForbidden)
    post_request = create_upload_file_request(rf)
    post_request.method = "PUT"
    assert isinstance(widget.handle_ajax(post_request), HttpResponseBadRequest)
예제 #3
0
    def single_chunk_upload(self, client, filename, content, endpoint):
        """
        Executes a single-chunk upload with the given content. In contrast to
        the `create_file_*` functions, this function utilizes the django-router and
        operates on the AJAX-API, resulting in a more expensive, but also more
        rigid test.

        Parameters
        ----------
        client: Client
            Django test client to use to make conenctions,
        filename: str
            The filename of the uploaded file
        content: bytes
            The content of the file
        endpoint: str
            URL the url to upload to

        Returns
        -------
        HttpResponse resulting from the request.
        """
        return create_upload_file_request(
            client,
            filename=filename,
            content=content,
            url=endpoint,
            csrf_token=self.__csrf_token,
        )
예제 #4
0
def test_wrong_upload_headers(client):
    url = reverse("api:staged-file-list")
    _, token = AuthToken.objects.create(user=UserFactory())

    response = create_upload_file_request(
        client,
        url=url,
        method="put",
        extra_headers={"HTTP_AUTHORIZATION": f"Bearer {token}"},
    )
    assert response.status_code == 403
def test_upload_validator_using_wrong_extension(rf: RequestFactory):
    widget = AjaxUploadWidget(
        ajax_target_path="/ajax",
        upload_validators=[
            ExtensionValidator(allowed_extensions=('.allowed-extension', ))
        ],
    )
    widget.timeout = timedelta(seconds=1)
    content = load_test_data()

    upload_id = generate_new_upload_id(
        test_upload_validator_using_wrong_extension, content)

    request = create_upload_file_request(rf,
                                         content=b"should error",
                                         filename="test.wrong_extension")
    response = widget.handle_ajax(request)
    assert response.status_code == 403

    request = create_upload_file_request(rf,
                                         content=b"should error",
                                         filename="test.allowed-extension")
    response = widget.handle_ajax(request)
    assert response.status_code == 200
예제 #6
0
def test_single_chunk(rf: RequestFactory):
    widget = AjaxUploadWidget(ajax_target_path="/ajax")
    widget.timeout = timedelta(seconds=1)
    filename = "test.bin"
    post_request = create_upload_file_request(rf, filename=filename)
    response = widget.handle_ajax(post_request)
    assert isinstance(response, JsonResponse)
    parsed_json = json.loads(response.content)
    assert len(parsed_json) == 1
    assert parsed_json[0]["filename"] == filename
    assert "uuid" in parsed_json[0]
    assert "extra_attrs" in parsed_json[0]
    staged_file = StagedAjaxFile(uuid.UUID(parsed_json[0]["uuid"]))
    with staged_file.open() as f:
        staged_content = f.read()
    assert staged_content == load_test_data()
def test_single_chunk(rf: RequestFactory):
    widget = AjaxUploadWidget(ajax_target_path="/ajax")
    widget.timeout = timedelta(seconds=1)
    filename = 'test.bin'
    post_request = create_upload_file_request(rf, filename=filename)
    response = widget.handle_ajax(post_request)
    assert isinstance(response, JsonResponse)
    parsed_json = json.loads(response.content)
    assert len(parsed_json) == 1
    assert parsed_json[0]["filename"] == filename
    assert "uuid" in parsed_json[0]
    assert "extra_attrs" in parsed_json[0]
    staged_file = StagedAjaxFile(uuid.UUID(parsed_json[0]["uuid"]))
    with staged_file.open() as f:
        staged_content = f.read()
    assert staged_content == load_test_data()
예제 #8
0
def test_single_chunk_api(client):
    filename = "test.bin"
    token = Token.objects.create(user=UserFactory())

    response = create_upload_file_request(
        rf=client,
        filename=filename,
        url=reverse("api:staged-file-list"),
        extra_headers={"HTTP_AUTHORIZATION": f"Token {token}"},
    )

    assert response.status_code == 201

    parsed_json = response.json()

    assert parsed_json[0]["filename"] == filename
    assert "uuid" in parsed_json[0]
    assert "extra_attrs" in parsed_json[0]
    staged_file = StagedAjaxFile(uuid.UUID(parsed_json[0]["uuid"]))
    with staged_file.open() as f:
        staged_content = f.read()
    assert staged_content == load_test_data()