summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/opendc/api/v2/paths.json3
-rw-r--r--api/opendc/api/v2/prefabs/authorizations/__init__.py0
-rw-r--r--api/opendc/api/v2/prefabs/authorizations/endpoint.py27
-rw-r--r--api/opendc/api/v2/prefabs/authorizations/test_endpoint.py37
-rw-r--r--api/opendc/models/prefab.py4
5 files changed, 70 insertions, 1 deletions
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
--- /dev/null
+++ b/api/opendc/api/v2/prefabs/authorizations/__init__.py
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)