From 6a4703517e7e1ca5ee2c8d8e0a4c49f32d17c662 Mon Sep 17 00:00:00 2001 From: jc0b Date: Tue, 30 Jun 2020 18:02:06 +0200 Subject: Topologies completed --- .../api/v2/topologies/topologyId/endpoint.py | 33 +++++++++- .../api/v2/topologies/topologyId/test_endpoint.py | 72 +++++++++++++++++++++- 2 files changed, 102 insertions(+), 3 deletions(-) (limited to 'web-server/opendc/api/v2/topologies') diff --git a/web-server/opendc/api/v2/topologies/topologyId/endpoint.py b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py index f0c12876..32e2dc8a 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py @@ -1,7 +1,12 @@ +from datetime import datetime + +from opendc.util.database import Database +from opendc.models.simulation import Simulation from opendc.models.topology import Topology from opendc.util.rest import Response + def GET(request): """Get this Topology.""" @@ -16,7 +21,25 @@ def GET(request): def PUT(request): """Update this topology""" - print(request) + request.check_required_parameters(path={'topologyId': 'int'}, + body={ + 'topology': { + 'name': 'string', + 'rooms': {} + } + }) + topology = Topology.from_id(request.params_path['topologyId']) + + topology.check_exists() + topology.check_user_access(request.google_id, True) + + topology.set_property('name', request.params_body['topology']['name']) + topology.set_property('rooms', request.params_body['topology']['rooms']) + topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + + topology.update() + + return Response(200, 'Successfully updated topology.', topology.obj) def DELETE(request): """Delete this topology""" @@ -25,6 +48,14 @@ def DELETE(request): topology = Topology.from_id(request.params_path['topologyId']) topology.check_exists() + topology.check_user_access(request.google_id, True) + + simulation = Simulation.from_id(topology.obj['simulationId']) + simulation.check_exists() + if request.params_path['topologyId'] in simulation.obj['topologyIds']: + simulation.obj['topologyIds'].remove(request.params_path['topologyId']) + simulation.update() + topology.delete() return Response(200, 'Successfully deleted topology.', topology.obj) diff --git a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py index 6243cc55..ac0d02aa 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py @@ -48,13 +48,81 @@ def test_get_topology_no_authorizations(client, mocker): ''' PUT /topologies/{topologyId} ''' + +def test_update_topology_missing_parameter(client): + assert '400' in client.put('/api/v2/topologies/1').status + +def test_update_topology_non_existent(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.put('/api/v2/topologies/1', + json={ + 'topology': { + 'name': 'test_topology', + 'rooms': {} + } + }).status + +def test_update_topology_not_authorized(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'VIEW' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + assert '403' in client.put('/api/v2/topologies/1', + json={ + 'topology': { + 'name': 'updated_topology', + 'rooms': {} + } + }).status + +def test_update_topology(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'OWN' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + + assert '200' in client.put('/api/v2/topologies/1', + json={ + 'topology': { + 'name': 'updated_topology', + 'rooms': {} + } + }).status + + ''' DELETE /topologies/{topologyId} ''' def test_delete_topology(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'}) - mocker.patch.object(DB, 'delete_one', return_value=None) + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'googleId': 'test', + 'topologyIds': ['1'], + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'OWN' + }] + }) + mocker.patch.object(DB, 'delete_one', return_value={}) + mocker.patch.object(DB, 'update', return_value=None) res = client.delete('/api/v2/topologies/1') assert '200' in res.status -- cgit v1.2.3