def test_api_file_custom_location():
    """
    Test file uploading with custom location
    """
    app = create_ctfd()
    with app.app_context():
        with login_as_user(app, name="admin") as client:
            with client.session_transaction() as sess:
                nonce = sess.get("nonce")
            r = client.post(
                "/api/v1/files",
                content_type="multipart/form-data",
                data={
                    "file": (BytesIO(b"test file content"), "test.txt"),
                    "location": "testing/asdf.txt",
                    "nonce": nonce,
                },
            )
            assert r.status_code == 200
            f = Files.query.filter_by(id=1).first()
            assert f.sha1sum == "9032bbc224ed8b39183cb93b9a7447727ce67f9d"
            assert f.location == "testing/asdf.txt"
            r = client.get("/files/" + f.location)
            assert r.get_data(as_text=True) == "test file content"

            r = client.get("/api/v1/files/1")
            response = r.get_json()
            assert (
                response["data"]["sha1sum"]
                == "9032bbc224ed8b39183cb93b9a7447727ce67f9d"
            )
            assert response["data"]["location"] == "testing/asdf.txt"

            # Test deletion
            r = client.delete("/api/v1/files/1", json="")
            assert r.status_code == 200
            assert Files.query.count() == 0

            target = pathlib.Path(app.config["UPLOAD_FOLDER"]) / f.location
            assert target.exists() is False

            # Test invalid locations
            invalid_paths = [
                "testing/prefix/asdf.txt",
                "/testing/asdf.txt",
                "asdf.txt",
            ]
            for path in invalid_paths:
                r = client.post(
                    "/api/v1/files",
                    content_type="multipart/form-data",
                    data={
                        "file": (BytesIO(b"test file content"), "test.txt"),
                        "location": path,
                        "nonce": nonce,
                    },
                )
                assert r.status_code == 400
    destroy_ctfd(app)

    Test file uploading with custom location
    
tests/api/v1/test_files.py