mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-04 14:31:13 +02:00
refactor: ruff autofixes
This commit is contained in:
455 files changed
+12322
-14762
No files matched your search
@@ -28,7 +28,6 @@ pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class TestDynamipsNodesRoutes:
|
||||
|
||||
# @pytest.yield_fixture(scope="module")
|
||||
# async def vm(compute_api, compute_project, fake_image):
|
||||
#
|
||||
@@ -47,7 +46,6 @@ class TestDynamipsNodesRoutes:
|
||||
# #with asyncio_patch("gns3server.compute.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
|
||||
# # yield response.json
|
||||
|
||||
|
||||
# async def test_dynamips_vm_create(compute_api, compute_project, fake_image):
|
||||
#
|
||||
# params = {
|
||||
@@ -66,7 +64,6 @@ class TestDynamipsNodesRoutes:
|
||||
# assert response.json["project_id"] == compute_project.id
|
||||
# assert response.json["dynamips_id"]
|
||||
|
||||
|
||||
# def test_dynamips_vm_get(compute_api, project, vm):
|
||||
# response = compute_api.get("/projects/{project_id}/dynamips/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), example=True)
|
||||
# assert response.status == 200
|
||||
@@ -102,7 +99,6 @@ class TestDynamipsNodesRoutes:
|
||||
# assert mock.called
|
||||
# assert response.status == 204
|
||||
|
||||
|
||||
# def test_vbox_nio_create_udp(compute_api, vm):
|
||||
#
|
||||
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
|
||||
@@ -142,48 +138,42 @@ class TestDynamipsNodesRoutes:
|
||||
# assert response.json["name"] == "test"
|
||||
# assert response.json["console"] == free_console_port
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_image(self, tmpdir) -> str:
|
||||
"""Create a fake Dynamips image on disk"""
|
||||
|
||||
path = str(tmpdir / "7200.bin")
|
||||
with open(path, "wb+") as f:
|
||||
f.write(b'\x7fELF\x01\x02\x01')
|
||||
f.write(b"\x7fELF\x01\x02\x01")
|
||||
os.chmod(path, stat.S_IREAD)
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_file(self, tmpdir) -> str:
|
||||
"""Create a fake file disk"""
|
||||
|
||||
path = str(tmpdir / "7200.txt")
|
||||
with open(path, "w+") as f:
|
||||
f.write('1')
|
||||
f.write("1")
|
||||
os.chmod(path, stat.S_IREAD)
|
||||
return path
|
||||
|
||||
|
||||
async def test_images(
|
||||
self, app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
tmpdir, fake_image: str,
|
||||
fake_file: str
|
||||
self, app: FastAPI, compute_client: AsyncClient, tmpdir, fake_image: str, fake_file: str
|
||||
) -> None:
|
||||
|
||||
with patch("gns3server.utils.images.default_images_directory", return_value=str(tmpdir)):
|
||||
response = await compute_client.get(app.url_path_for("compute:get_dynamips_images"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json() == [{"filename": "7200.bin",
|
||||
"path": "7200.bin",
|
||||
"filesize": 7,
|
||||
"md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e"}]
|
||||
|
||||
assert response.json() == [
|
||||
{"filename": "7200.bin", "path": "7200.bin", "filesize": 7, "md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e"}
|
||||
]
|
||||
|
||||
async def test_upload_image(self, app: FastAPI, compute_client: AsyncClient, images_dir: str) -> None:
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename="test2"), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename="test2"), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
with open(os.path.join(images_dir, "IOS", "test2")) as f:
|
||||
@@ -193,35 +183,33 @@ class TestDynamipsNodesRoutes:
|
||||
checksum = f.read()
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
async def test_upload_image_forbidden_location(self, app: FastAPI, compute_client: AsyncClient) -> None:
|
||||
|
||||
file_path = "%2e%2e/hello"
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename=file_path), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename=file_path), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
async def test_download_image(self, app: FastAPI, compute_client: AsyncClient, images_dir: str) -> None:
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename="test3"), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename="test3"), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||
|
||||
response = await compute_client.get(app.url_path_for("compute:download_dynamips_image", filename="test3"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
async def test_download_image_forbidden(self, app: FastAPI, compute_client: AsyncClient, tmpdir) -> None:
|
||||
|
||||
file_path = "foo/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd"
|
||||
response = await compute_client.get(app.url_path_for("compute:download_dynamips_image", filename=file_path))
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.getuid() == 0, reason="Root can delete any image")
|
||||
async def test_upload_image_permission_denied(
|
||||
self, app: FastAPI,
|
||||
compute_client: AsyncClient,
|
||||
images_dir: str
|
||||
self, app: FastAPI, compute_client: AsyncClient, images_dir: str
|
||||
) -> None:
|
||||
|
||||
os.makedirs(os.path.join(images_dir, "IOS"), exist_ok=True)
|
||||
@@ -229,5 +217,7 @@ class TestDynamipsNodesRoutes:
|
||||
f.write("")
|
||||
os.chmod(os.path.join(images_dir, "IOS", "test2.tmp"), 0)
|
||||
|
||||
response = await compute_client.post(app.url_path_for("compute:upload_dynamips_image", filename="test2"), content=b"TEST")
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:upload_dynamips_image", filename="test2"), content=b"TEST"
|
||||
)
|
||||
assert response.status_code == status.HTTP_409_CONFLICT
|
||||
Reference in new issue
Block a user