summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--web-server/.pylintrc6
-rwxr-xr-xweb-server/check.sh1
-rw-r--r--web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py6
-rw-r--r--web-server/opendc/api/v2/traces/endpoint.py2
-rw-r--r--web-server/opendc/api/v2/traces/traceId/endpoint.py2
-rw-r--r--web-server/opendc/models/experiment.py9
-rw-r--r--web-server/opendc/models/model.py15
-rw-r--r--web-server/opendc/models/simulation.py8
-rw-r--r--web-server/opendc/models/topology.py11
-rw-r--r--web-server/opendc/models/trace.py2
-rw-r--r--web-server/opendc/models/user.py10
-rw-r--r--web-server/opendc/util/database.py3
-rw-r--r--web-server/opendc/util/exceptions.py2
-rw-r--r--web-server/opendc/util/parameter_checker.py9
-rw-r--r--web-server/opendc/util/rest.py6
15 files changed, 75 insertions, 17 deletions
diff --git a/web-server/.pylintrc b/web-server/.pylintrc
index c7855f0e..f25e4fc2 100644
--- a/web-server/.pylintrc
+++ b/web-server/.pylintrc
@@ -62,7 +62,9 @@ confidence=
# --disable=W".
disable=duplicate-code,
missing-module-docstring,
- invalid-name
+ invalid-name,
+ bare-except,
+ too-few-public-methods
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
@@ -484,7 +486,7 @@ valid-metaclass-classmethod-first-arg=cls
max-args=5
# Maximum number of attributes for a class (see R0902).
-max-attributes=7
+max-attributes=12
# Maximum number of boolean expressions in an if statement (see R0916).
max-bool-expr=5
diff --git a/web-server/check.sh b/web-server/check.sh
new file mode 100755
index 00000000..abe2c596
--- /dev/null
+++ b/web-server/check.sh
@@ -0,0 +1 @@
+pylint opendc --ignore-patterns=test_.*?py
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 d16f7ee2..48bd6846 100644
--- a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py
+++ b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py
@@ -9,8 +9,9 @@ def test_get_topology(client, mocker):
'fetch_one',
return_value={
'_id': '1',
+ 'simulationId': '1',
'authorizations': [{
- 'topologyId': '1',
+ 'simulationId': '1',
'authorizationLevel': 'EDIT'
}]
})
@@ -28,8 +29,9 @@ def test_get_topology_not_authorized(client, mocker):
'fetch_one',
return_value={
'_id': '1',
+ 'simulationId': '1',
'authorizations': [{
- 'topologyId': '2',
+ 'simulationId': '2',
'authorizationLevel': 'OWN'
}]
})
diff --git a/web-server/opendc/api/v2/traces/endpoint.py b/web-server/opendc/api/v2/traces/endpoint.py
index 720c6a1e..ee699e02 100644
--- a/web-server/opendc/api/v2/traces/endpoint.py
+++ b/web-server/opendc/api/v2/traces/endpoint.py
@@ -2,7 +2,7 @@ from opendc.models.trace import Trace
from opendc.util.rest import Response
-def GET(request):
+def GET(_):
"""Get all available Traces."""
traces = Trace.get_all()
diff --git a/web-server/opendc/api/v2/traces/traceId/endpoint.py b/web-server/opendc/api/v2/traces/traceId/endpoint.py
index 672e256c..670f88d1 100644
--- a/web-server/opendc/api/v2/traces/traceId/endpoint.py
+++ b/web-server/opendc/api/v2/traces/traceId/endpoint.py
@@ -11,4 +11,4 @@ def GET(request):
trace.check_exists()
- return Response(200, f'Successfully retrieved trace.', trace.obj)
+ return Response(200, 'Successfully retrieved trace.', trace.obj)
diff --git a/web-server/opendc/models/experiment.py b/web-server/opendc/models/experiment.py
index dd7aa4f8..ac606d64 100644
--- a/web-server/opendc/models/experiment.py
+++ b/web-server/opendc/models/experiment.py
@@ -5,9 +5,18 @@ from opendc.util.rest import Response
class Experiment(Model):
+ """Model representing a Experiment."""
+
collection_name = 'experiments'
def check_user_access(self, google_id, edit_access):
+ """Raises an error if the user with given [google_id] has insufficient access.
+
+ Checks access on the parent simulation.
+
+ :param google_id: The Google ID of the user.
+ :param edit_access: True when edit access should be checked, otherwise view access.
+ """
user = User.from_google_id(google_id)
authorizations = list(
filter(lambda x: str(x['simulationId']) == str(self.obj['simulationId']), user.obj['authorizations']))
diff --git a/web-server/opendc/models/model.py b/web-server/opendc/models/model.py
index b2fd1844..2b8eb4dc 100644
--- a/web-server/opendc/models/model.py
+++ b/web-server/opendc/models/model.py
@@ -4,31 +4,40 @@ from opendc.util.rest import Response
class Model:
+ """Base class for all models."""
+
collection_name = '<specified in subclasses>'
@classmethod
def from_id(cls, _id):
- return cls(DB.fetch_one({'_id': _id}, Model.collection_name))
+ """Fetches the document with given ID from the collection."""
+ return cls(DB.fetch_one({'_id': _id}, cls.collection_name))
@classmethod
def get_all(cls):
- return cls(DB.fetch_all({}, Model.collection_name))
+ """Fetches all documents from the collection."""
+ return cls(DB.fetch_all({}, cls.collection_name))
def __init__(self, obj):
self.obj = obj
def check_exists(self):
+ """Raises an error if the enclosed object does not exist."""
if self.obj is None:
raise ClientError(Response(404, 'Not found.'))
def set_property(self, key, value):
+ """Sets the given property on the enclosed object."""
self.obj[key] = value
def insert(self):
+ """Inserts the enclosed object and updates the internal reference to the newly inserted object."""
self.obj = DB.insert(self.obj, self.collection_name)
def update(self):
+ """Updates the enclosed object and updates the internal reference to the newly inserted object."""
self.obj = DB.update(self.obj['_id'], self.obj, self.collection_name)
def delete(self):
- self.obj = DB.delete_one({'_id': self.obj['_id']}, self.collection_name)
+ """Deletes the enclosed object in the database."""
+ DB.delete_one({'_id': self.obj['_id']}, self.collection_name)
diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/simulation.py
index a77697ab..bf19368c 100644
--- a/web-server/opendc/models/simulation.py
+++ b/web-server/opendc/models/simulation.py
@@ -6,9 +6,16 @@ from opendc.util.rest import Response
class Simulation(Model):
+ """Model representing a Simulation."""
+
collection_name = 'simulations'
def check_user_access(self, google_id, edit_access):
+ """Raises an error if the user with given [google_id] has insufficient access.
+
+ :param google_id: The Google ID of the user.
+ :param edit_access: True when edit access should be checked, otherwise view access.
+ """
user = User.from_google_id(google_id)
authorizations = list(
filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations']))
@@ -16,6 +23,7 @@ class Simulation(Model):
raise ClientError(Response(403, "Forbidden from retrieving simulation."))
def get_all_authorizations(self):
+ """Get all user IDs having access to this simulation."""
return [
user['_id'] for user in DB.fetch_all({'authorizations': {
'simulationId': self.obj['_id']
diff --git a/web-server/opendc/models/topology.py b/web-server/opendc/models/topology.py
index 37b4c5c8..1447af98 100644
--- a/web-server/opendc/models/topology.py
+++ b/web-server/opendc/models/topology.py
@@ -5,11 +5,20 @@ from opendc.util.rest import Response
class Topology(Model):
+ """Model representing a Simulation."""
+
collection_name = 'topologies'
def check_user_access(self, google_id, edit_access):
+ """Raises an error if the user with given [google_id] has insufficient access.
+
+ Checks access on the parent simulation.
+
+ :param google_id: The Google ID of the user.
+ :param edit_access: True when edit access should be checked, otherwise view access.
+ """
user = User.from_google_id(google_id)
- authorizations = list(filter(lambda x: str(x['topologyId']) == str(self.obj['_id']),
+ authorizations = list(filter(lambda x: str(x['simulationId']) == str(self.obj['simulationId']),
user.obj['authorizations']))
if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
raise ClientError(Response(403, "Forbidden from retrieving topology."))
diff --git a/web-server/opendc/models/trace.py b/web-server/opendc/models/trace.py
index c18f8ea2..2f6e4926 100644
--- a/web-server/opendc/models/trace.py
+++ b/web-server/opendc/models/trace.py
@@ -2,4 +2,6 @@ from opendc.models.model import Model
class Trace(Model):
+ """Model representing a Trace."""
+
collection_name = 'traces'
diff --git a/web-server/opendc/models/user.py b/web-server/opendc/models/user.py
index cd314457..8e8ff945 100644
--- a/web-server/opendc/models/user.py
+++ b/web-server/opendc/models/user.py
@@ -5,21 +5,31 @@ from opendc.util.rest import Response
class User(Model):
+ """Model representing a User."""
+
collection_name = 'users'
@classmethod
def from_email(cls, email):
+ """Fetches the user with given email from the collection."""
return User(DB.fetch_one({'email': email}, User.collection_name))
@classmethod
def from_google_id(cls, google_id):
+ """Fetches the user with given Google ID from the collection."""
return User(DB.fetch_one({'googleId': google_id}, User.collection_name))
def check_correct_user(self, request_google_id):
+ """Raises an error if a user tries to modify another user.
+
+ :param request_google_id:
+ """
if request_google_id is not None and self.obj['googleId'] != request_google_id:
raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.'))
def check_already_exists(self):
+ """Checks if the user already exists in the database."""
+
existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name)
if existing_user is not None:
diff --git a/web-server/opendc/util/database.py b/web-server/opendc/util/database.py
index 50bc93a8..09d241b5 100644
--- a/web-server/opendc/util/database.py
+++ b/web-server/opendc/util/database.py
@@ -10,10 +10,13 @@ CONNECTION_POOL = None
class Database:
+ """Object holding functionality for database access."""
def __init__(self):
self.opendc_db = None
def init_database(self, user, password, database, host):
+ """Initializes the database connection."""
+
user = urllib.parse.quote_plus(user) # TODO: replace this with environment variable
password = urllib.parse.quote_plus(password) # TODO: same as above
database = urllib.parse.quote_plus(database)
diff --git a/web-server/opendc/util/exceptions.py b/web-server/opendc/util/exceptions.py
index 8fb82e4b..7724a407 100644
--- a/web-server/opendc/util/exceptions.py
+++ b/web-server/opendc/util/exceptions.py
@@ -12,7 +12,7 @@ class MissingRequestParameterError(RequestInitializationError):
class UnsupportedMethodError(RequestInitializationError):
"""Raised when a Request does not use a supported REST method.
-
+
The method must be in all-caps, supported by REST, and implemented by the module.
"""
diff --git a/web-server/opendc/util/parameter_checker.py b/web-server/opendc/util/parameter_checker.py
index f55e780e..d37256e0 100644
--- a/web-server/opendc/util/parameter_checker.py
+++ b/web-server/opendc/util/parameter_checker.py
@@ -1,4 +1,5 @@
-from opendc.util import database, exceptions
+from opendc.util import exceptions
+from opendc.util.database import Database
def _missing_parameter(params_required, params_actual, parent=''):
@@ -41,7 +42,7 @@ def _incorrect_parameter(params_required, params_actual, parent=''):
if param_required == 'datetime':
try:
- database.string_to_datetime(param_actual)
+ Database.string_to_datetime(param_actual)
except:
return '{}.{}'.format(parent, param_name)
@@ -54,6 +55,8 @@ def _incorrect_parameter(params_required, params_actual, parent=''):
if param_required.startswith('list') and not isinstance(param_actual, list):
return '{}.{}'.format(parent, param_name)
+ return None
+
def _format_parameter(parameter):
"""Format the output of a parameter check."""
@@ -64,7 +67,7 @@ def _format_parameter(parameter):
def check(request, **kwargs):
- """Return True if all required parameters are there."""
+ """Check if all required parameters are there."""
for location, params_required in kwargs.items():
params_actual = getattr(request, 'params_{}'.format(location))
diff --git a/web-server/opendc/util/rest.py b/web-server/opendc/util/rest.py
index dc5478de..abd2f3de 100644
--- a/web-server/opendc/util/rest.py
+++ b/web-server/opendc/util/rest.py
@@ -1,7 +1,6 @@
import importlib
import json
import os
-import sys
from oauth2client import client, crypt
@@ -9,7 +8,7 @@ from opendc.util import exceptions, parameter_checker
from opendc.util.exceptions import ClientError
-class Request(object):
+class Request:
"""WebSocket message to REST request mapping."""
def __init__(self, message=None):
""""Initialize a Request from a socket message."""
@@ -122,11 +121,12 @@ class Request(object):
return id_info['sub']
-class Response(object):
+class Response:
"""Response to websocket mapping"""
def __init__(self, status_code, status_description, content=None):
"""Initialize a new Response."""
+ self.id = 0
self.status = {'code': status_code, 'description': status_description}
self.content = content