From c8c6519551cef8b82b55e3e5866b35156c89e6b1 Mon Sep 17 00:00:00 2001 From: jc0b Date: Thu, 30 Jul 2020 21:37:44 +0200 Subject: Added authorizations endpoint for prefabs --- api/opendc/api/v2/paths.json | 3 +- .../api/v2/prefabs/authorizations/__init__.py | 0 .../api/v2/prefabs/authorizations/endpoint.py | 27 ++++++++++++++++ .../api/v2/prefabs/authorizations/test_endpoint.py | 37 ++++++++++++++++++++++ api/opendc/models/prefab.py | 4 +++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 api/opendc/api/v2/prefabs/authorizations/__init__.py create mode 100644 api/opendc/api/v2/prefabs/authorizations/endpoint.py create mode 100644 api/opendc/api/v2/prefabs/authorizations/test_endpoint.py (limited to 'api/opendc') diff --git a/api/opendc/api/v2/paths.json b/api/opendc/api/v2/paths.json index 90d5a2e6..652be5bc 100644 --- a/api/opendc/api/v2/paths.json +++ b/api/opendc/api/v2/paths.json @@ -14,5 +14,6 @@ "/traces", "/traces/{traceId}", "/prefabs", - "/prefabs/{prefabId}" + "/prefabs/{prefabId}", + "/prefabs/authorizations" ] diff --git a/api/opendc/api/v2/prefabs/authorizations/__init__.py b/api/opendc/api/v2/prefabs/authorizations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/api/opendc/api/v2/prefabs/authorizations/endpoint.py b/api/opendc/api/v2/prefabs/authorizations/endpoint.py new file mode 100644 index 00000000..9f0e2232 --- /dev/null +++ b/api/opendc/api/v2/prefabs/authorizations/endpoint.py @@ -0,0 +1,27 @@ +from datetime import datetime + +from opendc.models.prefab import Prefab +from opendc.models.user import User +from opendc.util.database import Database +from opendc.util.rest import Response + + +def GET(request): + """Return all prefabs the user is authorized to access""" + + user = User.from_id(request.google_id) + + user.check_exists() + + prefab_collection = Prefab.get_all() + print(type(prefab_collection)) + print(prefab_collection) + + authorizations = { "authorizations" : []} + + for prefab in prefab_collection: + if prefab['authorId'] == user.get_id() or prefab['visibility'] == "public": + authorizations["authorizations"].append(prefab) + + return Response(200, 'Successfully fetched authorizations.', authorizations) + diff --git a/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py b/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py new file mode 100644 index 00000000..68b701bc --- /dev/null +++ b/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py @@ -0,0 +1,37 @@ +from opendc.util.database import DB + +def test_get_authorizations(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'}) + mocker.patch.object(DB, + 'fetch_all', + return_value=[{ + '_id': '1', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 1, + 'visibility' : 'private' + }, + { + '_id': '2', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 2, + 'visibility' : 'private' + }, + { + '_id': '3', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 1, + 'visibility' : 'public' + }, + { + '_id': '2', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 2, + 'visibility' : 'public' + }]) + res = client.get('/api/v2/prefabs/authorizations', json={'prefab': {'name': 'test prefab'}}) + assert '200' in res.status + diff --git a/api/opendc/models/prefab.py b/api/opendc/models/prefab.py index edf1d4c4..2d20208c 100644 --- a/api/opendc/models/prefab.py +++ b/api/opendc/models/prefab.py @@ -1,5 +1,6 @@ from opendc.models.model import Model from opendc.models.user import User +from opendc.util.database import DB from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -26,3 +27,6 @@ class Prefab(Model): #except KeyError: # OpenDC-authored objects don't necessarily have an authorId # return + + def get_all(): + return DB.fetch_all({}, Prefab.collection_name) -- cgit v1.2.3