Files
gns3-gui/tests/test_project.py

191 lines
5.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest.mock import patch, MagicMock
from uuid import uuid4
from gns3.project import Project
2016-06-15 18:49:26 +02:00
def test_project_create(tmpdir, controller):
"""
Test a post on a local servers. The project
2015-03-11 23:09:01 -06:00
is not created on the server and should be created automatically.
And after make the call
"""
uuid = str(uuid4())
project = Project()
project.setFilesDir(str(tmpdir))
2016-06-15 18:49:26 +02:00
project.setName("test")
2016-06-15 18:49:26 +02:00
project.create()
2016-05-20 13:17:07 +02:00
mock = controller._http_client.createHTTPQuery
assert mock.called
args, kwargs = mock.call_args
assert args[0] == "POST"
assert args[1] == "/projects"
2016-06-15 18:49:26 +02:00
assert kwargs["body"] == {"name": "test",
2018-02-19 12:55:52 +01:00
"path": str(tmpdir),
"grid_size": 75,
"drawing_grid_size": 25,
"show_grid": False,
"snap_to_grid": False,
2018-02-19 12:55:52 +01:00
"show_interface_labels": False}
2016-07-22 18:10:16 +02:00
args[2]({"project_id": uuid, "name": "test"})
2016-05-20 13:17:07 +02:00
assert project._closed is False
2015-01-30 21:47:06 +01:00
2016-05-20 13:17:07 +02:00
def test_project_post_on_created_project(controller):
"""
Test a post on a remote servers.
The project is already created on the server
"""
uuid = uuid4()
project = Project()
2016-03-10 09:07:30 +01:00
project._created = True
project.setId(uuid)
2016-05-20 13:17:07 +02:00
project.post("/test", lambda: 0, body={"test": "test"})
2016-05-20 13:17:07 +02:00
mock = controller._http_client.createHTTPQuery
args, kwargs = mock.call_args
assert args[0] == "POST"
assert args[1] == "/projects/{uuid}/test".format(uuid=uuid)
assert kwargs["body"] == {"test": "test"}
2016-05-20 13:17:07 +02:00
def test_project_get_on_created_project(controller):
"""
Test a get on a remote servers.
The project is already created on the server
"""
uuid = uuid4()
project = Project()
2016-03-10 09:07:30 +01:00
project._created = True
project.setId(uuid)
2016-05-20 13:17:07 +02:00
project.get("/test", lambda: 0)
mock = controller._http_client.createHTTPQuery
2016-05-20 13:17:07 +02:00
args, kwargs = mock.call_args
assert args[0] == "GET"
assert args[1] == "/projects/{uuid}/test".format(uuid=uuid)
2016-05-20 13:17:07 +02:00
def test_project_put_on_created_project(controller):
"""
Test a put on a remote servers.
The project is already created on the server
"""
uuid = uuid4()
project = Project()
2016-03-10 09:07:30 +01:00
project._created = True
project.setId(uuid)
2016-05-20 13:17:07 +02:00
project.put("/test", lambda: 0, body={"test": "test"})
mock = controller._http_client.createHTTPQuery
2016-05-20 13:17:07 +02:00
args, kwargs = mock.call_args
assert args[0] == "PUT"
assert args[1] == "/projects/{uuid}/test".format(uuid=uuid)
assert kwargs["body"] == {"test": "test"}
2016-05-20 13:17:07 +02:00
def test_project_delete_on_created_project(controller):
"""
Test a delete on a remote servers.
The project is already created on the server
"""
uuid = uuid4()
project = Project()
2016-03-10 09:07:30 +01:00
project._created = True
project.setId(uuid)
2016-05-20 13:17:07 +02:00
project.delete("/test", lambda: 0)
mock = controller._http_client.createHTTPQuery
2015-01-30 21:47:06 +01:00
2016-05-20 13:17:07 +02:00
args, kwargs = mock.call_args
assert args[0] == "DELETE"
assert args[1] == "/projects/{uuid}/test".format(uuid=uuid)
2015-02-06 22:50:12 +01:00
2015-01-30 21:47:06 +01:00
def test_project_destroy(controller):
project = Project()
project.setId(str(uuid4()))
project.destroy()
mock = controller._http_client.createHTTPQuery
assert mock.called
args, kwargs = mock.call_args
assert args[0] == "DELETE"
assert args[1] == "/projects/{project_id}".format(project_id=project.id())
2018-05-08 13:00:32 +02:00
def test_project_variables():
project = Project()
project.setVariables([{'name': 'TEST'}])
variables = project.variables()
assert variables == [{'name': 'TEST'}]
def test_project_supplier():
project = Project()
project.setSupplier({'logo': 'test.png', 'url': 'http://domain'})
supplier = project.supplier()
assert supplier == {'logo': 'test.png', 'url': 'http://domain'}
def test_project_parse_response():
result = {
'project_id': 'projectid',
'name': 'projectname',
'filename': 'filename.gns3',
'variables': [{'name': 'TEST'}],
'supplier': {'logo': 'test.png', 'url': 'http://domain'}
}
project = Project()
project._parseResponse(result)
assert project.id() == 'projectid'
assert project.name() == 'projectname'
assert project.filename() == 'filename.gns3'
assert project.variables() == [{'name': 'TEST'}]
assert project.supplier() == {'logo': 'test.png', 'url': 'http://domain'}
def test_project_update(controller):
project = Project()
project.setVariables([{'name': 'TEST'}])
project.setSupplier({'logo': 'test.png', 'url': 'http://domain'})
project.update()
mock = controller._http_client.createHTTPQuery
args, kwargs = mock.call_args
body = kwargs['body']
assert body['variables'] == [{'name': 'TEST'}]
assert body['supplier'] == {'logo': 'test.png', 'url': 'http://domain'}