summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-25 17:11:03 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-25 17:11:03 +0200
commitc0bf71322e4fd510046588e388ada0a81f54631d (patch)
tree7f0af5b0657558a4730b62298534cd6a1d829765 /opendc/api/v2/simulations
parentcae7ee8ab4639963d3da7fef6f078a6078340a0b (diff)
Implement and test path
Diffstat (limited to 'opendc/api/v2/simulations')
-rw-r--r--opendc/api/v2/simulations/endpoint.py63
-rw-r--r--opendc/api/v2/simulations/test_endpoint.py16
2 files changed, 31 insertions, 48 deletions
diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py
index 7ef90e97..dfeb8d5e 100644
--- a/opendc/api/v2/simulations/endpoint.py
+++ b/opendc/api/v2/simulations/endpoint.py
@@ -1,65 +1,32 @@
from datetime import datetime
-from opendc.models_old.authorization import Authorization
-from opendc.models_old.datacenter import Datacenter
-from opendc.models_old.path import Path
-from opendc.models_old.section import Section
-from opendc.models_old.simulation import Simulation
-from opendc.models_old.user import User
-from opendc.util import database, exceptions
+from opendc.models.simulation import Simulation
+from opendc.models.topology import Topology
+from opendc.models.user import User
+from opendc.util import exceptions
+from opendc.util.database import Database
from opendc.util.rest import Response
def POST(request):
"""Create a new simulation, and return that new simulation."""
- # Make sure required parameters are there
-
try:
request.check_required_parameters(body={'simulation': {'name': 'string'}})
-
except exceptions.ParameterError as e:
return Response(400, str(e))
- # Instantiate a Simulation
-
- simulation_data = request.params_body['simulation']
-
- simulation_data['datetimeCreated'] = database.datetime_to_string(datetime.now())
- simulation_data['datetimeLastEdited'] = database.datetime_to_string(datetime.now())
-
- simulation = Simulation.from_JSON(simulation_data)
-
- # Insert this Simulation into the database
+ topology = Topology({'name': 'Default topology'})
+ topology.insert()
+ simulation = Simulation({'simulation': request.params_body['simulation']})
+ 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.insert()
- # Instantiate an Authorization and insert it into the database
-
- authorization = Authorization(user_id=User.from_google_id(request.google_id).id,
- simulation_id=simulation.id,
- authorization_level='OWN')
-
- authorization.insert()
-
- # Instantiate a Path and insert it into the database
-
- path = Path(simulation_id=simulation.id, datetime_created=database.datetime_to_string(datetime.now()))
-
- path.insert()
-
- # Instantiate a Datacenter and insert it into the database
-
- datacenter = Datacenter(starred=0, simulation_id=simulation.id)
-
- datacenter.insert()
-
- # Instantiate a Section and insert it into the database
-
- section = Section(path_id=path.id, datacenter_id=datacenter.id, start_tick=0)
-
- section.insert()
-
- # Return this Simulation
+ user = User.from_google_id(request.google_id)
+ user.obj['authorizations'].append({'simulationId': simulation.obj['_id'], 'authorizationLevel': 'OWN'})
+ user.update()
- return Response(200, 'Successfully created {}.'.format(simulation), simulation.to_JSON())
+ return Response(200, 'Successfully created simulation.', simulation.obj)
diff --git a/opendc/api/v2/simulations/test_endpoint.py b/opendc/api/v2/simulations/test_endpoint.py
new file mode 100644
index 00000000..fe4ac6ed
--- /dev/null
+++ b/opendc/api/v2/simulations/test_endpoint.py
@@ -0,0 +1,16 @@
+from opendc.util.database import DB
+
+
+def test_add_simulation_missing_parameter(client):
+ assert '400' in client.post('/api/v2/simulations').status
+
+
+def test_add_simulation(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': []})
+ mocker.patch.object(DB, 'insert', return_value={'_id': '1', 'datetimeCreated': '000', 'datetimeEdit': '000', 'topologyIds': []})
+ mocker.patch.object(DB, 'update', return_value={})
+ res = client.post('/api/v2/simulations', json={'simulation': {'name': 'test simulation'}})
+ assert 'datetimeCreated' in res.json['content']
+ assert 'datetimeEdit' in res.json['content']
+ assert 'topologyIds' in res.json['content']
+ assert '200' in res.status