aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'moon_manager/tests/unit_python/api/test_rules.py')
-rw-r--r--moon_manager/tests/unit_python/api/test_rules.py71
1 files changed, 52 insertions, 19 deletions
diff --git a/moon_manager/tests/unit_python/api/test_rules.py b/moon_manager/tests/unit_python/api/test_rules.py
index d12b7186..af1501e4 100644
--- a/moon_manager/tests/unit_python/api/test_rules.py
+++ b/moon_manager/tests/unit_python/api/test_rules.py
@@ -1,5 +1,8 @@
import api.utilities as utilities
import json
+from helpers import data_builder as builder
+from uuid import uuid4
+from helpers import policy_helper
def get_rules(client, policy_id):
@@ -8,10 +11,16 @@ def get_rules(client, policy_id):
return req, rules
-def add_rules(client, policy_id):
+def add_rules(client):
+ sub_id, obj_id, act_id, meta_rule_id, policy_id = builder.create_new_policy("sub_cat" + uuid4().hex,
+ "obj_cat" + uuid4().hex,
+ "act_cat" + uuid4().hex)
+ sub_data_id = builder.create_subject_data(policy_id, sub_id)
+ obj_data_id = builder.create_object_data(policy_id, obj_id)
+ act_data_id = builder.create_action_data(policy_id, act_id)
data = {
- "meta_rule_id": "meta_rule_id1",
- "rule": ["subject_data_id2", "object_data_id2", "action_data_id2"],
+ "meta_rule_id": meta_rule_id,
+ "rule": [sub_data_id, obj_data_id, act_data_id],
"instructions": (
{"decision": "grant"},
),
@@ -23,6 +32,21 @@ def add_rules(client, policy_id):
return req, rules
+def add_rules_without_policy_id(client):
+ data = {
+ "meta_rule_id": "meta_rule_id",
+ "rule": ["sub_data_id", "obj_data_id", "act_data_id"],
+ "instructions": (
+ {"decision": "grant"},
+ ),
+ "enabled": True
+ }
+ req = client.post("/policies/{}/rules".format(None), data=json.dumps(data),
+ headers={'Content-Type': 'application/json'})
+ rules = utilities.get_json(req.data)
+ return req, rules
+
+
def add_rules_without_meta_rule_id(client, policy_id):
data = {
"meta_rule_id": "",
@@ -68,48 +92,57 @@ def test_get_rules():
def test_add_rules():
- policy_id = utilities.get_policy_id()
client = utilities.register_client()
- req, rules = add_rules(client, policy_id)
+ req, rules = add_rules(client, )
assert req.status_code == 200
- assert isinstance(rules, dict)
- value = rules["rules"]
- assert "rules" in rules
- id = list(value.keys())[0]
- assert value[id]["meta_rule_id"] == "meta_rule_id1"
def test_add_rules_without_policy_id():
client = utilities.register_client()
- req, rules = add_rules(client, None)
- assert req.status_code == 500
+ req, rules = add_rules_without_policy_id(client)
+ assert req.status_code == 400
+ assert json.loads(req.data)["message"] == "400: Policy Unknown"
def test_add_rules_without_meta_rule_id():
policy_id = utilities.get_policy_id()
client = utilities.register_client()
req, rules = add_rules_without_meta_rule_id(client, policy_id)
- assert req.status_code == 500
- assert json.loads(req.data)["message"] == 'Empty String'
+ assert req.status_code == 400
+ assert json.loads(req.data)["message"] == "Key: 'meta_rule_id', [Empty String]"
def test_add_rules_without_rule():
policy_id = utilities.get_policy_id()
client = utilities.register_client()
req, rules = add_rules_without_rule(client, policy_id)
- assert req.status_code == 500
+ assert req.status_code == 400
+ assert json.loads(req.data)["message"] == 'Invalid Key :rule not found'
-def test_delete_rules():
+def test_delete_rules_with_invalid_parameters():
client = utilities.register_client()
rules = delete_rules(client, "", "")
- assert rules.status_code == 500
+ assert rules.status_code == 404
def test_delete_rules_without_policy_id():
client = utilities.register_client()
- policy_id = utilities.get_policy_id()
+ subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy()
+ sub_data_id = builder.create_subject_data(policy_id, subject_category_id)
+ obj_data_id = builder.create_object_data(policy_id, object_category_id)
+ act_data_id = builder.create_action_data(policy_id, action_category_id)
+ data = {
+ "meta_rule_id": meta_rule_id,
+ "rule": [sub_data_id, obj_data_id, act_data_id],
+ "instructions": (
+ {"decision": "grant"},
+ ),
+ "enabled": True
+ }
+ client.post("/policies/{}/rules".format(policy_id), data=json.dumps(data),
+ headers={'Content-Type': 'application/json'})
req, added_rules = get_rules(client, policy_id)
- id = added_rules["rules"]['rules'][0]['id']
+ id = list(added_rules["rules"]["rules"])[0]["id"]
rules = delete_rules(client, None, id)
assert rules.status_code == 200