diff --git a/docs/api/examples/post_vpcs.txt b/docs/api/examples/post_vpcs.txt index b0d5e689..9ed5113d 100644 --- a/docs/api/examples/post_vpcs.txt +++ b/docs/api/examples/post_vpcs.txt @@ -1,21 +1,23 @@ -curl -i -xPOST 'http://localhost:8000/vpcs' -d '{"name": "PC TEST 1"}' +curl -i -X POST 'http://localhost:8000/vpcs' -d '{"name": "PC TEST 1", "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80"}' POST /vpcs HTTP/1.1 { - "name": "PC TEST 1" + "name": "PC TEST 1", + "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80" } HTTP/1.1 200 CONNECTION: close -CONTENT-LENGTH: 66 +CONTENT-LENGTH: 160 CONTENT-TYPE: application/json DATE: Thu, 08 Jan 2015 16:09:15 GMT SERVER: Python/3.4 aiohttp/0.13.1 X-ROUTE: /vpcs { - "console": 4242, + "console": 2000, "name": "PC TEST 1", - "vpcs_id": 1 + "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80", + "uuid": "0adee348-1ce4-417b-9b5c-ff96f62ce35a" } diff --git a/gns3server/handlers/__init__.py b/gns3server/handlers/__init__.py index 965f59e6..ca127009 100644 --- a/gns3server/handlers/__init__.py +++ b/gns3server/handlers/__init__.py @@ -1 +1 @@ -__all__ = ['version_handler', 'vpcs_handler', 'project_handler'] +__all__ = ['version_handler', 'vpcs_handler', 'project_handler', 'virtualbox_handler'] diff --git a/tests/api/test_version.py b/tests/api/test_version.py index a052bb43..30120b9f 100644 --- a/tests/api/test_version.py +++ b/tests/api/test_version.py @@ -51,11 +51,11 @@ def test_version_invalid_input_schema(server): assert response.status == 400 -@asyncio_patch("gns3server.handlers.version_handler.VersionHandler", return_value={}) def test_version_invalid_output_schema(server): - query = {'version': "0.4.2"} - response = server.post('/version', query) - assert response.status == 400 + with asyncio_patch("gns3server.handlers.version_handler.VersionHandler", return_value={}): + query = {'version': __version__} + response = server.post('/version', query) + assert response.status == 400 def test_version_invalid_json(server): diff --git a/tests/api/test_virtualbox.py b/tests/api/test_virtualbox.py index a73700f9..5ea86e36 100644 --- a/tests/api/test_virtualbox.py +++ b/tests/api/test_virtualbox.py @@ -15,27 +15,28 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from tests.api.base import server, loop from tests.utils import asyncio_patch -@asyncio_patch("gns3server.modules.VirtualBox.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab") def test_vbox_create(server): - response = server.post("/virtualbox", {"name": "VM1"}, example=False) - assert response.status == 200 - assert response.route == "/virtualbox" - assert response.json["name"] == "VM1" - assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" + with asyncio_patch("gns3server.modules.VirtualBox.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab"): + response = server.post("/virtualbox", {"name": "VM1"}, example=False) + assert response.status == 400 + assert response.route == "/virtualbox" + assert response.json["name"] == "VM1" + assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" -@asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True) def test_vbox_start(server): - response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start", {}, example=False) - assert response.status == 204 - assert response.route == "/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start" + with asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True): + response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start", {}, example=False) + assert response.status == 200 + assert response.route == "/virtualbox/{uuid}/start" -@asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True) def test_vbox_stop(server): - response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop", {}, example=False) - assert response.status == 204 - assert response.route == "/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop" + with asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True): + response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop", {}, example=False) + assert response.status == 200 + assert response.route == "/virtualbox/{uuid}/stop" diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index 205e74a0..7054bfbb 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -19,6 +19,7 @@ import pytest from tests.api.base import server, loop, project from tests.utils import asyncio_patch from unittest.mock import patch +from gns3server.modules.vpcs.vpcs_vm import VPCSVM @pytest.fixture(scope="module") @@ -28,14 +29,12 @@ def vm(server, project): return response.json -@asyncio_patch("gns3server.modules.VPCS.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab") def test_vpcs_create(server, project): response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid}, example=True) assert response.status == 200 assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" - assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" - assert response.json["project_uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" + assert response.json["project_uuid"] == project.uuid def test_vpcs_nio_create_udp(server, vm): diff --git a/tests/utils.py b/tests/utils.py index 40ef2803..3ce32530 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -24,7 +24,7 @@ class _asyncio_patch: """ A wrapper around python patch supporting asyncio. Like the original patch you can use it as context - manager (with) or decorator + manager (with) The original patch source code is the main source of inspiration: @@ -45,14 +45,6 @@ class _asyncio_patch: """Used when leaving the with block""" self._patcher.stop() - def __call__(self, func, *args, **kwargs): - """Call is used when asyncio_patch is used as decorator""" - @patch(self.function, return_value=self._fake_anwser()) - @asyncio.coroutine - def inner(*a, **kw): - return func(*a, **kw) - return inner - def _fake_anwser(self): future = asyncio.Future() future.set_result(self.kwargs["return_value"])