summaryrefslogtreecommitdiff
path: root/opendc/api
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-26 14:11:49 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-26 14:11:49 +0200
commit3ad26793527bf077e296d652d22a0e4234a9b98d (patch)
tree1b64fd6105a8e3515b2a9c42b5a301b2834eb3d2 /opendc/api
parentf1017676a150de60b13ff2b33ca83079d87aebfc (diff)
Implement DELETE and fix tests
Diffstat (limited to 'opendc/api')
-rw-r--r--opendc/api/v2/simulations/endpoint.py1
-rw-r--r--opendc/api/v2/simulations/simulationId/endpoint.py9
-rw-r--r--opendc/api/v2/simulations/simulationId/test_endpoint.py18
-rw-r--r--opendc/api/v2/simulations/simulationId/topologies/endpoint.py16
-rw-r--r--opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py2
-rw-r--r--opendc/api/v2/topologies/topologyId/endpoint.py1
6 files changed, 29 insertions, 18 deletions
diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py
index 62257d40..232df2ff 100644
--- a/opendc/api/v2/simulations/endpoint.py
+++ b/opendc/api/v2/simulations/endpoint.py
@@ -20,6 +20,7 @@ def POST(request):
simulation.set_property('datetimeCreated', Database.datetime_to_string(datetime.now()))
simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now()))
simulation.set_property('topologyIds', [topology.obj['_id']])
+ simulation.set_property('experimentIds', [])
simulation.insert()
user = User.from_google_id(request.google_id)
diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py
index 8d29202d..282e3291 100644
--- a/opendc/api/v2/simulations/simulationId/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/endpoint.py
@@ -1,8 +1,7 @@
from datetime import datetime
from opendc.models.simulation import Simulation
-from opendc.models.user import User
-from opendc.util import database, exceptions
+from opendc.models.topology import Topology
from opendc.util.database import Database
from opendc.util.rest import Response
@@ -47,7 +46,11 @@ def DELETE(request):
simulation.check_exists()
simulation.check_user_access(request.google_id, True)
- # FIXME cascading
+ for topology_id in simulation.obj['topologyIds']:
+ topology = Topology.from_id(topology_id)
+ topology.delete()
+
+ # TODO remove all experiments
simulation.delete()
diff --git a/opendc/api/v2/simulations/simulationId/test_endpoint.py b/opendc/api/v2/simulations/simulationId/test_endpoint.py
index ad53fcaa..a0586aab 100644
--- a/opendc/api/v2/simulations/simulationId/test_endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/test_endpoint.py
@@ -77,3 +77,21 @@ def test_update_simulation(client, mocker):
res = client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}})
assert '200' in res.status
+
+
+def test_delete_simulation_non_existing(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value=None)
+ assert '404' in client.delete('/api/v2/simulations/1').status
+
+
+def test_delete_simulation_different_user(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'other_test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'VIEW'}], 'topologyIds': []})
+ mocker.patch.object(DB, 'delete_one', return_value=None)
+ assert '403' in client.delete('/api/v2/simulations/1').status
+
+
+def test_delete_simulation(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []})
+ mocker.patch.object(DB, 'delete_one', return_value={'googleId': 'test'})
+ res = client.delete('/api/v2/simulations/1')
+ assert '200' in res.status
diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py
index ee60e5b4..ab7b7006 100644
--- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py
@@ -10,21 +10,12 @@ from opendc.util.database import Database
def POST(request):
"""Add a new Topology to the specified simulation and return it"""
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}})
- except exceptions.ParameterError as e:
- return Response(400, str(e))
+ request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}})
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
+ simulation.check_exists()
+ simulation.check_user_access(request.google_id, True)
topology = Topology({'name': request.params_body['topology']['name']})
topology.set_property('datetimeCreated', Database.datetime_to_string(datetime.now()))
@@ -34,6 +25,5 @@ def POST(request):
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
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 09f20568..10b5e3c9 100644
--- a/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py
@@ -6,7 +6,7 @@ def test_add_topology_missing_parameter(client):
def test_add_topology(client, mocker):
- mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': []})
+ mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []})
mocker.patch.object(DB,
'insert',
return_value={
diff --git a/opendc/api/v2/topologies/topologyId/endpoint.py b/opendc/api/v2/topologies/topologyId/endpoint.py
index 3470ad94..6c6ab9c2 100644
--- a/opendc/api/v2/topologies/topologyId/endpoint.py
+++ b/opendc/api/v2/topologies/topologyId/endpoint.py
@@ -11,7 +11,6 @@ def GET(request):
topology = Topology.from_id(request.params_path['topologyId'])
topology.check_exists()
-
topology.check_user_access(request.google_id, False)
return Response(200, 'Successfully retrieved topology.', topology.obj)