From 6f51282cd7c3945ddd0fac68407a7a7be57aa2ba Mon Sep 17 00:00:00 2001 From: jc0b Date: Fri, 26 Jun 2020 12:29:06 +0200 Subject: Reworking topologies POST --- .../simulationId/topologies/endpoint.py | 22 ++++++++++++++-------- .../simulationId/topologies/test_endpoint.py | 7 +++++-- 2 files changed, 19 insertions(+), 10 deletions(-) (limited to 'opendc/api') diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py index 4104c4dd..ee60e5b4 100644 --- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -1,33 +1,39 @@ from datetime import datetime +from opendc.models.simulation import Simulation from opendc.models.topology import Topology from opendc.util import exceptions from opendc.util.rest import Response -from opendc.models.user import User from opendc.util.database import Database def POST(request): - """Add a new Topology and return it""" + """Add a new Topology to the specified simulation and return it""" # Make sure required parameters are there try: - request.check_required_parameters(body={'topology': {'name': 'string'}}) + request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}}) except exceptions.ParameterError as e: return Response(400, str(e)) - # Instantiate a Topology object from our request, and add some metadata + simulation = Simulation.from_id(request.params_path['simulationId']) + validation_error = simulation.validate() + if validation_error is not None: + return validation_error + + access_error = simulation.validate_user_access(request.google_id, False) + if access_error is not None: + return access_error topology = Topology({'name': request.params_body['topology']['name']}) topology.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) topology.insert() + simulation.obj['topologyIds'].append(topology.obj['_id']) + simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + simulation.update() # Instantiate the user from the request, and add this topology object to their authorizations - user = User.from_google_id(request.google_id) - user.obj['authorizations'].append({'topologyId': topology.obj['_id'], 'authorizationLevel': 'OWN'}) - user.update() - return Response(200, 'Successfully inserted topology.', topology.obj) diff --git a/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py index 132aa3d0..09f20568 100644 --- a/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py +++ b/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py @@ -1,11 +1,11 @@ from opendc.util.database import DB -def test_add_simulation_missing_parameter(client): +def test_add_topology_missing_parameter(client): assert '400' in client.post('/api/v2/simulations/1/topologies/').status -def test_add_simulation(client, mocker): +def test_add_topology(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': []}) mocker.patch.object(DB, 'insert', @@ -21,3 +21,6 @@ def test_add_simulation(client, mocker): assert 'datetimeEdit' in res.json['content'] assert 'topologyIds' in res.json['content'] assert '200' in res.status + +def test_add_topology_no_authorizations(client, mocker): + pass \ No newline at end of file -- cgit v1.2.3