summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/tests/api/test_prefabs.py
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-15 13:09:06 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2021-05-18 15:46:40 +0200
commit2281d3265423d01e60f8cc088de5a5730bb8a910 (patch)
tree8dc81338cfd30845717f1b9025176d26c82fe930 /opendc-web/opendc-web-api/tests/api/test_prefabs.py
parent05d2318538eba71ac0555dc5ec146499d9cb0592 (diff)
api: Migrate to Flask Restful
This change updates the API to use Flask Restful instead of our own in-house REST library. This change reduces the maintenance effort and allows us to drastically simplify the API implementation needed for the OpenDC v2 API.
Diffstat (limited to 'opendc-web/opendc-web-api/tests/api/test_prefabs.py')
-rw-r--r--opendc-web/opendc-web-api/tests/api/test_prefabs.py252
1 files changed, 252 insertions, 0 deletions
diff --git a/opendc-web/opendc-web-api/tests/api/test_prefabs.py b/opendc-web/opendc-web-api/tests/api/test_prefabs.py
new file mode 100644
index 00000000..ea3d92d6
--- /dev/null
+++ b/opendc-web/opendc-web-api/tests/api/test_prefabs.py
@@ -0,0 +1,252 @@
+# Copyright (c) 2021 AtLarge Research
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+from unittest.mock import Mock
+from opendc.exts import db
+
+test_id = 24 * '1'
+test_id_2 = 24 * '2'
+
+
+def test_add_prefab_missing_parameter(client):
+ assert '400' in client.post('/prefabs/').status
+
+
+def test_add_prefab(client, mocker):
+ mocker.patch.object(db, 'fetch_one', return_value={'_id': test_id, 'authorizations': []})
+ mocker.patch.object(db,
+ 'insert',
+ return_value={
+ '_id': test_id,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': test_id
+ })
+ res = client.post('/prefabs/', json={'prefab': {'name': 'test prefab'}})
+ assert 'datetimeCreated' in res.json['data']
+ assert 'datetimeLastEdited' in res.json['data']
+ assert 'authorId' in res.json['data']
+ assert '200' in res.status
+
+
+def test_get_prefabs(client, mocker):
+ db.fetch_all = Mock()
+ mocker.patch.object(db, 'fetch_one', return_value={'_id': test_id})
+ db.fetch_all.side_effect = [
+ [{
+ '_id': test_id,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': test_id,
+ 'visibility' : 'private'
+ },
+ {
+ '_id': '2' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': test_id,
+ 'visibility' : 'private'
+ },
+ {
+ '_id': '3' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': test_id,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '4' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': test_id,
+ 'visibility' : 'public'
+ }],
+ [{
+ '_id': '5' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': '2' * 24,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '6' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': '2' * 24,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '7' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': '2' * 24,
+ 'visibility' : 'public'
+ },
+ {
+ '_id': '8' * 24,
+ 'datetimeCreated': '000',
+ 'datetimeLastEdited': '000',
+ 'authorId': '2' * 24,
+ 'visibility' : 'public'
+ }]
+ ]
+ mocker.patch.object(db, 'fetch_one', return_value={'_id': test_id})
+ res = client.get('/prefabs/')
+ assert '200' in res.status
+
+
+def test_get_prefab_non_existing(client, mocker):
+ mocker.patch.object(db, 'fetch_one', return_value=None)
+ assert '404' in client.get(f'/prefabs/{test_id}').status
+
+
+def test_get_private_prefab_not_authorized(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': test_id_2,
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ res = client.get(f'/prefabs/{test_id}')
+ assert '403' in res.status
+
+
+def test_get_private_prefab(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': 'test',
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ res = client.get(f'/prefabs/{test_id}')
+ assert '200' in res.status
+
+
+def test_get_public_prefab(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': test_id_2,
+ 'visibility': 'public',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ res = client.get(f'/prefabs/{test_id}')
+ assert '200' in res.status
+
+
+def test_update_prefab_missing_parameter(client):
+ assert '400' in client.put(f'/prefabs/{test_id}').status
+
+
+def test_update_prefab_non_existing(client, mocker):
+ mocker.patch.object(db, 'fetch_one', return_value=None)
+ assert '404' in client.put(f'/prefabs/{test_id}', json={'prefab': {'name': 'S'}}).status
+
+
+def test_update_prefab_not_authorized(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': test_id_2,
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ mocker.patch.object(db, 'update', return_value={})
+ assert '403' in client.put(f'/prefabs/{test_id}', json={'prefab': {'name': 'test prefab', 'rack': {}}}).status
+
+
+def test_update_prefab(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': 'test',
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ mocker.patch.object(db, 'update', return_value={})
+ res = client.put(f'/prefabs/{test_id}', json={'prefab': {'name': 'test prefab', 'rack': {}}})
+ assert '200' in res.status
+
+
+def test_delete_prefab_non_existing(client, mocker):
+ mocker.patch.object(db, 'fetch_one', return_value=None)
+ assert '404' in client.delete(f'/prefabs/{test_id}').status
+
+
+def test_delete_prefab_different_user(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': test_id_2,
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ mocker.patch.object(db, 'delete_one', return_value=None)
+ assert '403' in client.delete(f'/prefabs/{test_id}').status
+
+
+def test_delete_prefab(client, mocker):
+ db.fetch_one = Mock()
+ db.fetch_one.side_effect = [{
+ '_id': test_id,
+ 'name': 'test prefab',
+ 'authorId': 'test',
+ 'visibility': 'private',
+ 'rack': {}
+ },
+ {
+ '_id': test_id
+ }
+ ]
+ mocker.patch.object(db, 'delete_one', return_value={'prefab': {'name': 'name'}})
+ res = client.delete(f'/prefabs/{test_id}')
+ assert '200' in res.status