summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--conftest.py3
-rw-r--r--main.py8
-rw-r--r--opendc/api/v2/users/endpoint.py7
-rw-r--r--opendc/api/v2/users/test_endpoint.py15
-rw-r--r--opendc/util/database.py10
-rw-r--r--opendc/util/parameter_checker.py2
7 files changed, 35 insertions, 16 deletions
diff --git a/README.md b/README.md
index cd8bd956..04146f59 100644
--- a/README.md
+++ b/README.md
@@ -121,4 +121,10 @@ To try a different query, use the Postman `Builder` to edit the method, path, bo
When editing the web server code, restart the server (`CTRL` + `c` followed by `python main.py config.json` in the console running the server) to see the result of your changes.
+#### Code Style
+
To format all files, run `format.sh` in this directory.
+
+#### Testing
+
+Run `pytest` in this directory to run all tests.
diff --git a/conftest.py b/conftest.py
index 1e404c1c..05172b04 100644
--- a/conftest.py
+++ b/conftest.py
@@ -4,10 +4,9 @@ from main import FLASK_CORE_APP
@pytest.fixture
-def client(mocker):
+def client():
"""Returns a Flask API client to interact with."""
FLASK_CORE_APP.config['TESTING'] = True
- mocker.patch('opendc.util.database.DB')
with FLASK_CORE_APP.test_client() as client:
yield client
diff --git a/main.py b/main.py
index 70b77cae..9d0a58cc 100644
--- a/main.py
+++ b/main.py
@@ -18,10 +18,10 @@ TEST_MODE = "OPENDC_FLASK_TESTING" in os.environ
if TEST_MODE:
STATIC_ROOT = os.curdir
else:
- database.initialize_database(user=os.environ['OPENDC_DB_USERNAME'],
- password=os.environ['OPENDC_DB_PASSWORD'],
- database=os.environ['OPENDC_DB'],
- host='localhost')
+ database.DB.initialize_database(user=os.environ['OPENDC_DB_USERNAME'],
+ password=os.environ['OPENDC_DB_PASSWORD'],
+ database=os.environ['OPENDC_DB'],
+ host='localhost')
STATIC_ROOT = os.path.join(os.environ['OPENDC_ROOT_DIR'], 'opendc-frontend', 'build')
FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT)
diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py
index dca509ed..7b4158e3 100644
--- a/opendc/api/v2/users/endpoint.py
+++ b/opendc/api/v2/users/endpoint.py
@@ -15,11 +15,12 @@ def GET(request):
return Response(400, str(e))
user = DB.fetch_one({'email': request.params_query['email']}, 'users')
+ print(user)
- if user is not None:
+ if user is None:
return Response(404, f'User with email {request.params_query["email"]} not found')
- return Response(200, 'Successfully retrieved {}.'.format(user), user.to_JSON())
+ return Response(200, 'Successfully retrieved {}.'.format(user), user)
def POST(request):
@@ -40,6 +41,6 @@ def POST(request):
if not request.google_id == user['googleId']:
return Response(403, 'Forbidden from creating this User.')
- user = insert(user, 'users')
+ user = DB.insert(user, 'users')
return Response(200, 'Successfully created {}'.format(user), user)
diff --git a/opendc/api/v2/users/test_endpoint.py b/opendc/api/v2/users/test_endpoint.py
index ffe2ce02..a5073c08 100644
--- a/opendc/api/v2/users/test_endpoint.py
+++ b/opendc/api/v2/users/test_endpoint.py
@@ -1,2 +1,15 @@
+from opendc.util.database import DB
+
+
+def test_get_user(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value={'email': 'test@test.com'})
+ assert '200' in client.get('/api/v2/users?email=test@test.com').status
+
+
+def test_get_user_non_existing(client, mocker):
+ mocker.patch.object(DB, 'fetch_one', return_value=None)
+ assert '404' in client.get('/api/v2/users?email=test@test.com').status
+
+
def test_get_user_missing_parameter(client):
- print(client.get('/api/v2/users'))
+ assert '400' in client.get('/api/v2/users').status
diff --git a/opendc/util/database.py b/opendc/util/database.py
index 24572279..50bc93a8 100644
--- a/opendc/util/database.py
+++ b/opendc/util/database.py
@@ -7,11 +7,13 @@ from pymongo import MongoClient
DATETIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S'
CONNECTION_POOL = None
-DB = None
class Database:
- def __init__(self, user, password, database, host):
+ def __init__(self):
+ self.opendc_db = None
+
+ def init_database(self, user, password, database, host):
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)
@@ -88,6 +90,4 @@ class Database:
return datetime.strptime(string_to_convert, DATETIME_STRING_FORMAT)
-def initialize_database(user, password, database, host):
- global DB
- DB = Database(user, password, database, host)
+DB = Database()
diff --git a/opendc/util/parameter_checker.py b/opendc/util/parameter_checker.py
index d3e7ef13..bf02ba9b 100644
--- a/opendc/util/parameter_checker.py
+++ b/opendc/util/parameter_checker.py
@@ -48,7 +48,7 @@ def _incorrect_parameter(params_required, params_actual, parent=''):
if param_required == 'int' and not isinstance(param_actual, int):
return '{}.{}'.format(parent, param_name)
- if param_required == 'string' and not isinstance(param_actual, basestring):
+ if param_required == 'string' and not isinstance(param_actual, str):
return '{}.{}'.format(parent, param_name)
if param_required.startswith('list') and not isinstance(param_actual, list):