summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-30 11:29:36 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-08-24 19:42:27 +0200
commit9b910a49270311fd6f099f0f0674a1fd0ab48a20 (patch)
tree5589838a6ba0dd794e0d421fd452044d5cdef38b
parent4ec6212a220c3627bdad070ac2f0e05e2d663979 (diff)
Add experiment endpoint tests
-rw-r--r--web-server/opendc/api/v2/experiments/experimentId/endpoint.py4
-rw-r--r--web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py136
2 files changed, 138 insertions, 2 deletions
diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py
index 8632e19e..2637fca7 100644
--- a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py
+++ b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py
@@ -34,7 +34,7 @@ def PUT(request):
experiment.set_property('topologyId', request.params_body['experiment']['topologyId'])
experiment.set_property('traceId', request.params_body['experiment']['traceId'])
- experiment.set_property('scheduleName', request.params_body['experiment']['scheduleName'])
+ experiment.set_property('schedulerName', request.params_body['experiment']['schedulerName'])
experiment.set_property('name', request.params_body['experiment']['name'])
experiment.update()
@@ -45,7 +45,7 @@ def PUT(request):
def DELETE(request):
"""Delete this Experiment."""
- request.check_required_parameters(path={'userId': 'string'})
+ request.check_required_parameters(path={'experimentId': 'string'})
experiment = Experiment.from_id(request.params_path['experimentId'])
diff --git a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py
new file mode 100644
index 00000000..0c9c9abc
--- /dev/null
+++ b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py
@@ -0,0 +1,136 @@
+from opendc.util.database import DB
+
+
+def test_get_experiment_non_existing(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value=None)
+ assert '404' in client.get('/api/v2/experiments/1').status
+
+
+def test_get_experiment_no_authorizations(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value={'simulationId': '1', 'authorizations': []})
+ res = client.get('/api/v2/experiments/1')
+ assert '403' in res.status
+
+
+def test_get_experiment_not_authorized(client, mocker):
+ mocker.patch.object(DB,
+ 'fetch_one',
+ return_value={
+ 'simulationId': '1',
+ '_id': '1',
+ 'authorizations': [{
+ 'simulationId': '2',
+ 'authorizationLevel': 'OWN'
+ }]
+ })
+ res = client.get('/api/v2/experiments/1')
+ assert '403' in res.status
+
+
+def test_get_experiment(client, mocker):
+ mocker.patch.object(DB,
+ 'fetch_one',
+ return_value={
+ 'simulationId': '1',
+ '_id': '1',
+ 'authorizations': [{
+ 'simulationId': '1',
+ 'authorizationLevel': 'EDIT'
+ }]
+ })
+ res = client.get('/api/v2/experiments/1')
+ assert '200' in res.status
+
+
+def test_update_experiment_missing_parameter(client):
+ assert '400' in client.put('/api/v2/experiments/1').status
+
+
+def test_update_experiment_non_existing(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value=None)
+ assert '404' in client.put('/api/v2/experiments/1', json={'experiment': {
+ 'topologyId': '1',
+ 'traceId': '1',
+ 'schedulerName': 'default',
+ 'name': 'test',
+ }}).status
+
+
+def test_update_experiment_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/experiments/1', json={'experiment': {
+ 'topologyId': '1',
+ 'traceId': '1',
+ 'schedulerName': 'default',
+ 'name': 'test',
+ }}).status
+
+
+def test_update_experiment(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={})
+
+ res = client.put('/api/v2/experiments/1', json={'experiment': {
+ 'topologyId': '1',
+ 'traceId': '1',
+ 'schedulerName': 'default',
+ 'name': 'test',
+ }})
+ 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/experiments/1').status
+
+
+def test_delete_simulation_different_user(client, mocker):
+ mocker.patch.object(DB,
+ 'fetch_one',
+ return_value={
+ '_id': '1',
+ 'simulationId': '1',
+ 'googleId': 'other_test',
+ 'authorizations': [{
+ 'simulationId': '1',
+ 'authorizationLevel': 'VIEW'
+ }]
+ })
+ mocker.patch.object(DB, 'delete_one', return_value=None)
+ assert '403' in client.delete('/api/v2/experiments/1').status
+
+
+def test_delete_simulation(client, mocker):
+ mocker.patch.object(DB,
+ 'fetch_one',
+ return_value={
+ '_id': '1',
+ 'simulationId': '1',
+ 'googleId': 'test',
+ 'authorizations': [{
+ 'simulationId': '1',
+ 'authorizationLevel': 'OWN'
+ }]
+ })
+ mocker.patch.object(DB, 'delete_one', return_value={})
+ res = client.delete('/api/v2/experiments/1')
+ assert '200' in res.status