mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
API for list & download files of a project
This commit is contained in:
1 parent
3c4a60cd0a
commit
6f9f004ebb
6 files changed
+205
-4
No files matched your search
+5
-2
@@ -24,6 +24,8 @@ import shutil
|
||||
import os
|
||||
import sys
|
||||
from aiohttp import web
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
sys._called_from_test = True
|
||||
# Prevent execution of external binaries
|
||||
@@ -100,10 +102,11 @@ def server(request, loop, port_manager, monkeypatch):
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def project():
|
||||
def project(tmpdir):
|
||||
"""A GNS3 lab"""
|
||||
|
||||
return ProjectManager.instance().create_project(project_id="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
||||
p = ProjectManager.instance().create_project(project_id="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
||||
return p
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
|
||||
@@ -20,12 +20,14 @@ This test suite check /project endpoint
|
||||
"""
|
||||
|
||||
import uuid
|
||||
import os
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
from gns3server.handlers.api.project_handler import ProjectHandler
|
||||
from gns3server.modules.project_manager import ProjectManager
|
||||
|
||||
|
||||
def test_create_project_with_path(server, tmpdir):
|
||||
@@ -175,6 +177,42 @@ def test_notification(server, project, loop):
|
||||
assert response.body == b'{"action": "ping"}\n{"action": "vm.created", "event": {"a": "b"}}\n'
|
||||
|
||||
|
||||
def test_notification_invalid_id(server, project):
|
||||
def test_notification_invalid_id(server):
|
||||
response = server.get("/projects/{project_id}/notifications".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_list_files(server, project):
|
||||
files = [
|
||||
{
|
||||
"path": "test.txt",
|
||||
"md5sum": "ad0234829205b9033196ba818f7a872b"
|
||||
},
|
||||
{
|
||||
"path": "vm-1/dynamips/test.bin",
|
||||
"md5sum": "098f6bcd4621d373cade4e832627b4f6"
|
||||
}
|
||||
]
|
||||
with asyncio_patch("gns3server.modules.project.Project.list_files", return_value=files) as mock:
|
||||
response = server.get("/projects/{project_id}/files".format(project_id=project.id), example=True)
|
||||
assert response.status == 200
|
||||
assert response.json == files
|
||||
|
||||
|
||||
def test_get_file(server, tmpdir):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
||||
project = ProjectManager.instance().create_project()
|
||||
|
||||
with open(os.path.join(project.path, "hello"), "w+") as f:
|
||||
f.write("world")
|
||||
|
||||
response = server.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True, example=True)
|
||||
assert response.status == 200
|
||||
assert response.body == b"world"
|
||||
|
||||
response = server.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
|
||||
assert response.status == 404
|
||||
|
||||
response = server.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
|
||||
assert response.status == 403
|
||||
@@ -229,3 +229,29 @@ def test_clean_project_directory(tmpdir):
|
||||
assert os.path.exists(str(project1))
|
||||
assert os.path.exists(str(oldproject))
|
||||
assert not os.path.exists(str(project2))
|
||||
|
||||
|
||||
def test_list_files(tmpdir, loop):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
||||
project = Project()
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test.bin"), "w+") as f:
|
||||
f.write("test")
|
||||
open(os.path.join(path, "vm-1", "dynamips", "test.ghost"), "w+").close()
|
||||
with open(os.path.join(path, "test.txt"), "w+") as f:
|
||||
f.write("test2")
|
||||
|
||||
files = loop.run_until_complete(asyncio.async(project.list_files()))
|
||||
|
||||
assert files == [
|
||||
{
|
||||
"path": "test.txt",
|
||||
"md5sum": "ad0234829205b9033196ba818f7a872b"
|
||||
},
|
||||
{
|
||||
"path": os.path.join("vm-1", "dynamips", "test.bin"),
|
||||
"md5sum": "098f6bcd4621d373cade4e832627b4f6"
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user