summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api/opendc/api/v2/prefabs/authorizations/endpoint.py10
-rw-r--r--api/opendc/api/v2/prefabs/authorizations/test_endpoint.py45
-rw-r--r--api/opendc/models/prefab.py4
3 files changed, 44 insertions, 15 deletions
diff --git a/api/opendc/api/v2/prefabs/authorizations/endpoint.py b/api/opendc/api/v2/prefabs/authorizations/endpoint.py
index 9f0e2232..7b421662 100644
--- a/api/opendc/api/v2/prefabs/authorizations/endpoint.py
+++ b/api/opendc/api/v2/prefabs/authorizations/endpoint.py
@@ -13,15 +13,13 @@ def GET(request):
user.check_exists()
- prefab_collection = Prefab.get_all()
- print(type(prefab_collection))
- print(prefab_collection)
+ own_prefabs = Prefab.get_all({'authorId' : user.get_id()})
+ public_prefabs = Prefab.get_all({'visibility' : 'public'})
authorizations = { "authorizations" : []}
- for prefab in prefab_collection:
- if prefab['authorId'] == user.get_id() or prefab['visibility'] == "public":
- authorizations["authorizations"].append(prefab)
+ authorizations["authorizations"].append(own_prefabs)
+ authorizations["authorizations"].append(public_prefabs)
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
index 68b701bc..38401045 100644
--- a/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py
+++ b/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py
@@ -1,10 +1,11 @@
from opendc.util.database import DB
+from unittest.mock import Mock
def test_get_authorizations(client, mocker):
+ DB.fetch_all = Mock()
mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'})
- mocker.patch.object(DB,
- 'fetch_all',
- return_value=[{
+ DB.fetch_all.side_effect = [
+ [{
'_id': '1',
'datetimeCreated': '000',
'datetimeLastEdited': '000',
@@ -15,7 +16,7 @@ def test_get_authorizations(client, mocker):
'_id': '2',
'datetimeCreated': '000',
'datetimeLastEdited': '000',
- 'authorId': 2,
+ 'authorId': 1,
'visibility' : 'private'
},
{
@@ -26,12 +27,42 @@ def test_get_authorizations(client, mocker):
'visibility' : 'public'
},
{
- '_id': '2',
+ '_id': '4',
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': 1,
+ 'visibility' : 'public'
+ }],
+ [{
+ '_id': '5',
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': 2,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '6',
'datetimeCreated': '000',
'datetimeLastEdited': '000',
'authorId': 2,
'visibility' : 'public'
- }])
- res = client.get('/api/v2/prefabs/authorizations', json={'prefab': {'name': 'test prefab'}})
+ },
+ {
+ '_id': '7',
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': 2,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '8',
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': 2,
+ 'visibility' : 'public'
+ }]
+ ]
+ mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'})
+ res = client.get('/api/v2/prefabs/authorizations')
assert '200' in res.status
diff --git a/api/opendc/models/prefab.py b/api/opendc/models/prefab.py
index 2d20208c..3d5dcf54 100644
--- a/api/opendc/models/prefab.py
+++ b/api/opendc/models/prefab.py
@@ -28,5 +28,5 @@ class Prefab(Model):
# OpenDC-authored objects don't necessarily have an authorId
# return
- def get_all():
- return DB.fetch_all({}, Prefab.collection_name)
+ def get_all(query):
+ return DB.fetch_all(query, Prefab.collection_name)