1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-01 04:38:12 +00:00

Allow changing location only for local usage

This commit is contained in:
Julien Duponchelle 2015-01-23 16:57:41 +01:00
parent 4f2764c0b4
commit 017c5ac9f6
3 changed files with 34 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import os
import tempfile import tempfile
import shutil import shutil
from uuid import UUID, uuid4 from uuid import UUID, uuid4
from ..config import Config
class Project: class Project:
@ -46,6 +47,10 @@ class Project:
self._location = location self._location = location
if location is None: if location is None:
self._location = tempfile.mkdtemp() self._location = tempfile.mkdtemp()
else:
config = Config.instance().get_section_config("Server")
if config.get("local", False) is False:
raise aiohttp.web.HTTPForbidden(text="You are not allowed to modifiy the project directory location")
self._temporary = temporary self._temporary = temporary
self._vms = set() self._vms = set()

View File

@ -20,10 +20,12 @@ This test suite check /project endpoint
""" """
import uuid import uuid
from unittest.mock import patch
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
def test_create_project_with_dir(server, tmpdir): def test_create_project_with_dir(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/project", {"location": str(tmpdir)}) response = server.post("/project", {"location": str(tmpdir)})
assert response.status == 200 assert response.status == 200
assert response.json["location"] == str(tmpdir) assert response.json["location"] == str(tmpdir)
@ -52,16 +54,9 @@ def test_create_project_with_uuid(server):
assert response.json["uuid"] == "00010203-0405-0607-0809-0a0b0c0d0e0f" assert response.json["uuid"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
def test_create_project_with_uuid(server):
query = {"uuid": "00010203-0405-0607-0809-0a0b0c0d0e0f", "location": "/tmp"}
response = server.post("/project", query, example=True)
assert response.status == 200
assert response.json["uuid"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["location"] == "/tmp"
def test_show_project(server): def test_show_project(server):
query = {"uuid": "00010203-0405-0607-0809-0a0b0c0d0e0f", "location": "/tmp", "temporary": False} query = {"uuid": "00010203-0405-0607-0809-0a0b0c0d0e0f", "location": "/tmp", "temporary": False}
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/project", query) response = server.post("/project", query)
assert response.status == 200 assert response.status == 200
response = server.get("/project/00010203-0405-0607-0809-0a0b0c0d0e0f", example=True) response = server.get("/project/00010203-0405-0607-0809-0a0b0c0d0e0f", example=True)

View File

@ -18,6 +18,7 @@
import os import os
import pytest import pytest
import aiohttp
from unittest.mock import patch from unittest.mock import patch
from gns3server.modules.project import Project from gns3server.modules.project import Project
@ -44,6 +45,7 @@ def test_affect_uuid():
assert p.uuid == '00010203-0405-0607-0809-0a0b0c0d0e0f' assert p.uuid == '00010203-0405-0607-0809-0a0b0c0d0e0f'
@patch("gns3server.config.Config.get_section_config", return_value={"local": True})
def test_path(tmpdir): def test_path(tmpdir):
p = Project(location=str(tmpdir)) p = Project(location=str(tmpdir))
assert p.path == os.path.join(str(tmpdir), p.uuid) assert p.path == os.path.join(str(tmpdir), p.uuid)
@ -56,27 +58,34 @@ def test_temporary_path():
assert os.path.exists(p.path) assert os.path.exists(p.path)
@patch("gns3server.config.Config.get_section_config", return_value={"local": False})
def test_changing_location_not_allowed(mock, tmpdir):
with pytest.raises(aiohttp.web.HTTPForbidden):
p = Project(location=str(tmpdir))
def test_json(tmpdir): def test_json(tmpdir):
p = Project() p = Project()
assert p.__json__() == {"location": p.location, "uuid": p.uuid, "temporary": False} assert p.__json__() == {"location": p.location, "uuid": p.uuid, "temporary": False}
@patch("gns3server.config.Config.get_section_config", return_value={"local": True})
def test_vm_working_directory(tmpdir, vm): def test_vm_working_directory(tmpdir, vm):
p = Project(location=str(tmpdir)) p = Project(location=str(tmpdir))
assert os.path.exists(p.vm_working_directory(vm)) assert os.path.exists(p.vm_working_directory(vm))
assert os.path.exists(os.path.join(str(tmpdir), p.uuid, vm.module_name, vm.uuid)) assert os.path.exists(os.path.join(str(tmpdir), p.uuid, vm.module_name, vm.uuid))
def test_mark_vm_for_destruction(tmpdir, vm): def test_mark_vm_for_destruction(vm):
project = Project(location=str(tmpdir)) project = Project()
project.add_vm(vm) project.add_vm(vm)
project.mark_vm_for_destruction(vm) project.mark_vm_for_destruction(vm)
assert len(project._vms_to_destroy) == 1 assert len(project._vms_to_destroy) == 1
assert len(project.vms) == 0 assert len(project.vms) == 0
def test_commit(tmpdir, manager): def test_commit(manager):
project = Project(location=str(tmpdir)) project = Project()
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm) project.add_vm(vm)
directory = project.vm_working_directory(vm) directory = project.vm_working_directory(vm)
@ -89,23 +98,23 @@ def test_commit(tmpdir, manager):
assert len(project.vms) == 0 assert len(project.vms) == 0
def test_project_delete(tmpdir): def test_project_delete():
project = Project(location=str(tmpdir)) project = Project()
directory = project.path directory = project.path
assert os.path.exists(directory) assert os.path.exists(directory)
project.delete() project.delete()
assert os.path.exists(directory) is False assert os.path.exists(directory) is False
def test_project_add_vm(tmpdir, manager): def test_project_add_vm(manager):
project = Project(location=str(tmpdir)) project = Project()
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm) project.add_vm(vm)
assert len(project.vms) == 1 assert len(project.vms) == 1
def test_project_close(tmpdir, manager): def test_project_close(manager):
project = Project(location=str(tmpdir)) project = Project()
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm) project.add_vm(vm)
with patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.close") as mock: with patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.close") as mock:
@ -113,10 +122,10 @@ def test_project_close(tmpdir, manager):
assert mock.called assert mock.called
def test_project_close_temporary_project(tmpdir, manager): def test_project_close_temporary_project(manager):
"""A temporary project is deleted when closed""" """A temporary project is deleted when closed"""
project = Project(location=str(tmpdir), temporary=True) project = Project(temporary=True)
directory = project.path directory = project.path
assert os.path.exists(directory) assert os.path.exists(directory)
project.close() project.close()