From 5dffe7e0889dd76885d313c1d44a4b389f4b8667 Mon Sep 17 00:00:00 2001 From: "ahmed.helmy" Date: Thu, 5 Apr 2018 15:41:37 +0200 Subject: continue integrating validation Change-Id: Ifbda169affd649f9125581ac5b688b509d1f0d82 Signed-off-by: ahmed.helmy --- .../unit_python/api/import_export_utilities.py | 12 +- .../tests/unit_python/api/meta_data_test.py | 42 ++++ .../tests/unit_python/api/meta_rules_test.py | 93 +++++++++ .../tests/unit_python/api/test_assignemnt.py | 75 ++++++- moon_manager/tests/unit_python/api/test_data.py | 64 ++++++ moon_manager/tests/unit_python/api/test_import.py | 4 +- moon_manager/tests/unit_python/api/test_pdp.py | 118 +++++++++++ .../tests/unit_python/api/test_perimeter.py | 231 ++++++++++++++------- moon_manager/tests/unit_python/api/test_rules.py | 58 +++++- .../tests/unit_python/api/test_unit_models.py | 94 +++++++++ 10 files changed, 691 insertions(+), 100 deletions(-) (limited to 'moon_manager/tests/unit_python/api') diff --git a/moon_manager/tests/unit_python/api/import_export_utilities.py b/moon_manager/tests/unit_python/api/import_export_utilities.py index 3c921e21..b1c8a541 100644 --- a/moon_manager/tests/unit_python/api/import_export_utilities.py +++ b/moon_manager/tests/unit_python/api/import_export_utilities.py @@ -35,8 +35,8 @@ def clean_policies(client): def clean_subjects(client): subjects = test_perimeter.get_subjects(client) logger.info("subjects {}".format(subjects)) - for key in subjects["subjects"]: - subject = subjects["subjects"][key] + for key in subjects[1]["subjects"]: + subject = subjects[1]["subjects"][key] policy_keys = subject["policy_list"] logger.info("subjects policy_keys {}".format(policy_keys)) for policy_key in policy_keys: @@ -48,8 +48,8 @@ def clean_subjects(client): def clean_objects(client): objects = test_perimeter.get_objects(client) logger.info("objects {}".format(objects)) - for key in objects["objects"]: - object_ = objects["objects"][key] + for key in objects[1]["objects"]: + object_ = objects[1]["objects"][key] policy_keys = object_["policy_list"] logger.info("objects policy_keys {}".format(policy_keys)) for policy_key in policy_keys: @@ -62,8 +62,8 @@ def clean_objects(client): def clean_actions(client): actions = test_perimeter.get_actions(client) logger.info("objects {}".format(actions)) - for key in actions["actions"]: - action = actions["actions"][key] + for key in actions[1]["actions"]: + action = actions[1]["actions"][key] policy_keys = action["policy_list"] logger.info("action policy_keys {}".format(policy_keys)) for policy_key in policy_keys: diff --git a/moon_manager/tests/unit_python/api/meta_data_test.py b/moon_manager/tests/unit_python/api/meta_data_test.py index 0d67a8cd..8609f0b5 100644 --- a/moon_manager/tests/unit_python/api/meta_data_test.py +++ b/moon_manager/tests/unit_python/api/meta_data_test.py @@ -54,6 +54,20 @@ def test_add_subject_categories(): assert value['description'] == "description of {}".format("testuser") +def test_add_subject_categories_with_empty_user(): + client = utilities.register_client() + req, subject_categories = add_subject_categories(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_subject_categories_with_user_contain_space(): + client = utilities.register_client() + req, subject_categories = add_subject_categories(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + def test_delete_subject_categories(): client = utilities.register_client() req = delete_subject_categories(client, "testuser") @@ -119,6 +133,20 @@ def test_add_object_categories(): assert value['description'] == "description of {}".format("testuser") +def test_add_object_categories_with_empty_user(): + client = utilities.register_client() + req, object_categories = add_object_categories(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_object_categories_with_user_contain_space(): + client = utilities.register_client() + req, object_categories = add_object_categories(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + def test_delete_object_categories(): client = utilities.register_client() req = delete_object_categories(client, "testuser") @@ -184,6 +212,20 @@ def test_add_action_categories(): assert value['description'] == "description of {}".format("testuser") +def test_add_action_categories_with_empty_user(): + client = utilities.register_client() + req, action_categories = add_action_categories(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_action_categories_with_user_contain_space(): + client = utilities.register_client() + req, action_categories = add_action_categories(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + def test_delete_action_categories(): client = utilities.register_client() req = delete_action_categories(client, "testuser") diff --git a/moon_manager/tests/unit_python/api/meta_rules_test.py b/moon_manager/tests/unit_python/api/meta_rules_test.py index b5b1ecf8..a87c16f3 100644 --- a/moon_manager/tests/unit_python/api/meta_rules_test.py +++ b/moon_manager/tests/unit_python/api/meta_rules_test.py @@ -22,6 +22,46 @@ def add_meta_rules(client, name): return req, meta_rules +def add_meta_rules_without_subject_category_ids(client, name): + data = { + "name": name, + "subject_categories": [], + "object_categories": ["object_category_id1"], + "action_categories": ["action_category_id1"] + } + req = client.post("/meta_rules", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + meta_rules = utilities.get_json(req.data) + return req, meta_rules + + +def update_meta_rules(client, name, metaRuleId): + data = { + "name": name, + "subject_categories": ["subject_category_id1_update", + "subject_category_id2_update"], + "object_categories": ["object_category_id1_update"], + "action_categories": ["action_category_id1_update"] + } + req = client.patch("/meta_rules/{}".format(metaRuleId), data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + meta_rules = utilities.get_json(req.data) + return req, meta_rules + + +def update_meta_rules_without_subject_category_ids(client, name): + data = { + "name": name, + "subject_categories": [], + "object_categories": ["object_category_id1"], + "action_categories": ["action_category_id1"] + } + req = client.post("/meta_rules", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + meta_rules = utilities.get_json(req.data) + return req, meta_rules + + def delete_meta_rules(client, name): request, meta_rules = get_meta_rules(client) for key, value in meta_rules['meta_rules'].items(): @@ -57,6 +97,27 @@ def test_add_meta_rules(): assert value["action_categories"][0] == "action_category_id1" +def test_add_meta_rules_with_empty_user(): + client = utilities.register_client() + req, meta_rules = add_meta_rules(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_meta_rules_with_user_contain_space(): + client = utilities.register_client() + req, meta_rules = add_meta_rules(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_add_meta_rules_without_subject_categories(): + client = utilities.register_client() + req, meta_rules = add_meta_rules_without_subject_category_ids(client, "testuser") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty Container' + + def test_delete_meta_rules(): client = utilities.register_client() req = delete_meta_rules(client, "testuser") @@ -67,3 +128,35 @@ def test_delete_meta_rules_without_id(): client = utilities.register_client() req = delete_meta_rules_without_id(client) assert req.status_code == 500 + + +def test_update_meta_rules(): + client = utilities.register_client() + req = add_meta_rules(client, "testuser") + meta_rule_id = list(req[1]['meta_rules'])[0] + req_update = update_meta_rules(client, "testuser", meta_rule_id) + assert req_update[0].status_code == 200 + value = list(req_update[1]["meta_rules"].values())[0] + assert value["subject_categories"][0] == "subject_category_id1_update" + delete_meta_rules(client, "testuser") + get_meta_rules(client) + + +def test_update_meta_rules_without_id(): + client = utilities.register_client() + req_update = update_meta_rules(client, "testuser", "") + assert req_update[0].status_code == 500 + + +def test_update_meta_rules_without_user(): + client = utilities.register_client() + req_update = update_meta_rules(client, "", "") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty String" + + +def test_update_meta_rules_without_subject_categories(): + client = utilities.register_client() + req_update = update_meta_rules_without_subject_category_ids(client, "testuser") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty Container" diff --git a/moon_manager/tests/unit_python/api/test_assignemnt.py b/moon_manager/tests/unit_python/api/test_assignemnt.py index 9fd83857..4e2acddc 100644 --- a/moon_manager/tests/unit_python/api/test_assignemnt.py +++ b/moon_manager/tests/unit_python/api/test_assignemnt.py @@ -17,14 +17,14 @@ def add_subject_assignment(client, policy_id, category_id): "category_id": category_id, "data_id": "data_id1" } - req = client.post("/policies/{}/subject_assignments/{}".format(policy_id, category_id), data=json.dumps(data), + req = client.post("/policies/{}/subject_assignments".format(policy_id), data=json.dumps(data), headers={'Content-Type': 'application/json'}) subject_assignment = utilities.get_json(req.data) return req, subject_assignment -def delete_subject_assignment(client, policy_id): - req = client.delete("/policies/{}/subject_assignments".format(policy_id)) +def delete_subject_assignment(client, policy_id, sub_id, cat_id,data_id): + req = client.delete("/policies/{}/subject_assignments/{}/{}/{}".format(policy_id, sub_id, cat_id,data_id)) return req @@ -42,6 +42,14 @@ def test_add_subject_assignment(): assert value[id]['subject_id'] == "id1" +def test_add_subject_assignment_without_cat_id(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, subject_assignment = add_subject_assignment(client, policy_id, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty String' + + def test_get_subject_assignment(): policy_id = utilities.get_policy_id() client = utilities.register_client() @@ -54,9 +62,19 @@ def test_get_subject_assignment(): def test_delete_subject_assignment(): client = utilities.register_client() policy_id = utilities.get_policy_id() - success_req = delete_subject_assignment(client, policy_id) + req, subject_assignment = get_subject_assignment(client, policy_id) + value = subject_assignment["subject_assignments"] + id = list(value.keys())[0] + success_req = delete_subject_assignment(client, policy_id, value[id]['subject_id'], value[id]['category_id'],value[id]['assignments'][0]) assert success_req.status_code == 200 + +def test_delete_subject_assignment_without_policy_id(): + client = utilities.register_client() + success_req = delete_subject_assignment(client, "", "id1", "111" ,"data_id1") + assert success_req.status_code == 500 + + # --------------------------------------------------------------------------- # object_categories_test @@ -80,8 +98,8 @@ def add_object_assignment(client, policy_id, category_id): return req, object_assignment -def delete_object_assignment(client, policy_id): - req = client.delete("/policies/{}/object_assignments".format(policy_id)) +def delete_object_assignment(client, policy_id, obj_id, cat_id, data_id): + req = client.delete("/policies/{}/object_assignments/{}/{}/{}".format(policy_id, obj_id, cat_id, data_id)) return req @@ -108,12 +126,30 @@ def test_add_object_assignment(): assert value[id]['object_id'] == "id1" +def test_add_object_assignment_without_cat_id(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, object_assignment = add_object_assignment(client, policy_id, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty String' + + def test_delete_object_assignment(): client = utilities.register_client() policy_id = utilities.get_policy_id() - success_req = delete_object_assignment(client, policy_id) + req, object_assignment = get_object_assignment(client, policy_id) + value = object_assignment["object_assignments"] + id = list(value.keys())[0] + success_req = delete_object_assignment(client, policy_id, value[id]['object_id'], value[id]['category_id'],value[id]['assignments'][0]) assert success_req.status_code == 200 + +def test_delete_object_assignment_without_policy_id(): + client = utilities.register_client() + success_req = delete_object_assignment(client, "", "id1", "111" ,"data_id1") + assert success_req.status_code == 500 + + # --------------------------------------------------------------------------- # action_categories_test @@ -137,8 +173,8 @@ def add_action_assignment(client, policy_id, category_id): return req, action_assignment -def delete_action_assignment(client, policy_id): - req = client.delete("/policies/{}/action_assignments".format(policy_id)) +def delete_action_assignment(client, policy_id, action_id, cat_id, data_id): + req = client.delete("/policies/{}/action_assignments/{}/{}/{}".format(policy_id, action_id, cat_id, data_id)) return req @@ -165,10 +201,27 @@ def test_add_action_assignment(): assert value[id]['action_id'] == "id1" +def test_add_action_assignment_without_cat_id(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, action_assignment = add_action_assignment(client, policy_id, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty String' + + def test_delete_action_assignment(): client = utilities.register_client() policy_id = utilities.get_policy_id() - success_req = delete_action_assignment(client, policy_id) + req, action_assignment = get_action_assignment(client, policy_id) + value = action_assignment["action_assignments"] + id = list(value.keys())[0] + success_req = delete_action_assignment(client, policy_id, value[id]['action_id'], value[id]['category_id'],value[id]['assignments'][0]) assert success_req.status_code == 200 -# --------------------------------------------------------------------------- \ No newline at end of file + +def test_delete_action_assignment_without_policy_id(): + client = utilities.register_client() + success_req = delete_action_assignment(client, "", "id1", "111" ,"data_id1") + assert success_req.status_code == 500 + +# --------------------------------------------------------------------------- diff --git a/moon_manager/tests/unit_python/api/test_data.py b/moon_manager/tests/unit_python/api/test_data.py index 724f919f..f806ea2a 100644 --- a/moon_manager/tests/unit_python/api/test_data.py +++ b/moon_manager/tests/unit_python/api/test_data.py @@ -63,6 +63,28 @@ def test_delete_subject_data(): success_req = delete_subject_data(client, policy_id) assert success_req.status_code == 200 + +def test_add_subject_data_with_empty_user(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, subject_data = add_subject_data(client, "", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_subject_data_with_user_contain_space(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, subject_data = add_subject_data(client, "test user", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_delete_subject_data_without_policy_id(): + client = utilities.register_client() + success_req = delete_subject_data(client, "") + assert success_req.status_code == 500 + # --------------------------------------------------------------------------- # object_categories_test @@ -125,6 +147,27 @@ def test_delete_object_data(): success_req = delete_object_data(client, policy_id) assert success_req.status_code == 200 + +def test_add_object_data_with_empty_user(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, subject_data = add_subject_data(client, "", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_object_data_with_user_contain_space(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, object_data = add_object_data(client, "test user", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_delete_object_data_without_policy_id(): + client = utilities.register_client() + success_req = delete_object_data(client, "") + assert success_req.status_code == 500 # --------------------------------------------------------------------------- # action_categories_test @@ -183,4 +226,25 @@ def test_delete_action_data(): success_req = delete_action_data(client, policy_id) assert success_req.status_code == 200 + +def test_add_action_data_with_empty_user(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, action_data = add_action_data(client, "", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_action_data_with_user_contain_space(): + policy_id = utilities.get_policy_id() + client = utilities.register_client() + req, action_data = add_action_data(client, "test user", policy_id, "111") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_delete_action_data_without_policy_id(): + client = utilities.register_client() + success_req = delete_action_data(client, "") + assert success_req.status_code == 500 # --------------------------------------------------------------------------- diff --git a/moon_manager/tests/unit_python/api/test_import.py b/moon_manager/tests/unit_python/api/test_import.py index 3c45f8a1..12a1cc6b 100644 --- a/moon_manager/tests/unit_python/api/test_import.py +++ b/moon_manager/tests/unit_python/api/test_import.py @@ -137,8 +137,6 @@ RULES = [{"rules": [{"meta_rule": {"name": "unknown meta rule"}, "policy": {"nam {"rules": [{"meta_rule": {"name": "good meta rule"}, "policy": {"name": "test policy"}, "instructions": {"decision": "grant"}, "enabled": True, "rule": {"subject_data": [{"name": "subject data"}], "object_data": [{"name": "object data"}], "action_data": [{"name": "action data"}]}}]}] - - def test_import_models_without_new_meta_rules(): client = utilities.register_client() import_export_utilities.clean_all(client) @@ -240,7 +238,7 @@ def test_import_subject_object_action(): assert data == "Import ok !" get_elements = get_method(client) - get_elements = get_elements[type_element + "s"] + get_elements = get_elements[1][type_element + "s"] assert len(list(get_elements.keys())) == 1 values = list(get_elements.values()) diff --git a/moon_manager/tests/unit_python/api/test_pdp.py b/moon_manager/tests/unit_python/api/test_pdp.py index a2d0cb5a..fbaa6c7b 100644 --- a/moon_manager/tests/unit_python/api/test_pdp.py +++ b/moon_manager/tests/unit_python/api/test_pdp.py @@ -16,6 +16,13 @@ def add_pdp(client, data): return req, pdp +def update_pdp(client, data, pdp_id): + req = client.patch("/pdp/{}".format(pdp_id), data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + pdp = utilities.get_json(req.data) + return req, pdp + + def delete_pdp(client, key): req = client.delete("/pdp/{}".format(key)) return req @@ -60,3 +67,114 @@ def test_delete_pdp(): success_req = delete_pdp(client, key) break assert success_req.status_code == 200 + + +def test_add_pdp_with_empty_user(): + data = { + "name": "", + "security_pipeline": ["policy_id_1", "policy_id_2"], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + client = utilities.register_client() + req, models = add_pdp(client, data) + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_pdp_with_user_contain_space(): + data = { + "name": "test user", + "security_pipeline": ["policy_id_1", "policy_id_2"], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + client = utilities.register_client() + req, models = add_pdp(client, data) + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_add_pdp_without_security_pipeline(): + data = { + "name": "testuser", + "security_pipeline": [], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + client = utilities.register_client() + req, meta_rules = add_pdp(client, data) + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty Container' + + +def test_add_pdp_without_keystone(): + data = { + "name": "testuser", + "security_pipeline": ["policy_id_1", "policy_id_2"], + "keystone_project_id": "", + "description": "description of testuser" + } + client = utilities.register_client() + req, meta_rules = add_pdp(client, data) + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty String' + + +def test_update_pdp(): + data_add = { + "name": "testuser", + "security_pipeline": ["policy_id_1", "policy_id_2"], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + data_update = { + "name": "testuser", + "security_pipeline": ["policy_id_1_update", "policy_id_2_update"], + "keystone_project_id": "keystone_project_id_update", + "description": "description of testuser" + } + client = utilities.register_client() + req = add_pdp(client, data_add) + pdp_id = list(req[1]['pdps'])[0] + req_update = update_pdp(client, data_update, pdp_id) + assert req_update[0].status_code == 200 + value = list(req_update[1]["pdps"].values())[0] + assert value["keystone_project_id"] == "keystone_project_id_update" + request, pdp = get_pdp(client) + for key, value in pdp['pdps'].items(): + if value['name'] == "testuser": + delete_pdp(client, key) + break + + +def test_update_pdp_without_id(): + client = utilities.register_client() + req_update = update_pdp(client, "testuser", "") + assert req_update[0].status_code == 500 + + +def test_update_pdp_without_user(): + data = { + "name": "", + "security_pipeline": ["policy_id_1", "policy_id_2"], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + client = utilities.register_client() + req_update = update_pdp(client, data, "") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty String" + + +def test_update_pdp_without_security_pipeline(): + data = { + "name": "testuser", + "security_pipeline": [], + "keystone_project_id": "keystone_project_id", + "description": "description of testuser" + } + client = utilities.register_client() + req_update = update_pdp(client, data, "") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty Container" \ No newline at end of file diff --git a/moon_manager/tests/unit_python/api/test_perimeter.py b/moon_manager/tests/unit_python/api/test_perimeter.py index db09780f..b13bb2ed 100644 --- a/moon_manager/tests/unit_python/api/test_perimeter.py +++ b/moon_manager/tests/unit_python/api/test_perimeter.py @@ -6,11 +6,8 @@ import api.utilities as utilities def get_subjects(client): req = client.get("/subjects") - assert req.status_code == 200 subjects = utilities.get_json(req.data) - assert isinstance(subjects, dict) - assert "subjects" in subjects - return subjects + return req, subjects def add_subjects(client, name): @@ -22,58 +19,72 @@ def add_subjects(client, name): } req = client.post("/subjects", data=json.dumps(data), headers={'Content-Type': 'application/json'}) - assert req.status_code == 200 subjects = utilities.get_json(req.data) + return req, subjects + + +def delete_subject(client): + subjects = get_subjects(client) + for key, value in subjects[1]['subjects'].items(): + if value['name'] == "testuser": + req = client.delete("/subjects/{}".format(key)) + break + return req + + +def delete_subjects_without_perimeter_id(client): + req = client.delete("/subjects/{}".format("")) + return req + + +def test_perimeter_get_subject(): + client = utilities.register_client() + req, subjects = get_subjects(client) + assert req.status_code == 200 assert isinstance(subjects, dict) - key = list(subjects["subjects"].keys())[0] + assert "subjects" in subjects + + +def test_perimeter_add_subject(): + client = utilities.register_client() + req, subjects = add_subjects(client, "testuser") + assert req.status_code == 200 value = list(subjects["subjects"].values())[0] assert "subjects" in subjects - assert key == "1111111111111" - assert value['id'] == "1111111111111" - assert value['name'] == name - assert value["description"] == "description of {}".format(name) - assert value["email"] == "{}@moon".format(name) - return subjects + assert value['name'] == "testuser" + assert value["email"] == "{}@moon".format("testuser") -def add_subjects_without_name(client, name): - data = { - "name": name, - "description": "description of {}".format(name), - "password": "password for {}".format(name), - "email": "{}@moon".format(name) - } - req = client.post("/subjects", data=json.dumps(data), - headers={'Content-Type': 'application/json'}) +def test_perimeter_add_subject_without_name(): + client = utilities.register_client() + req, subjects = add_subjects(client, "") assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" -def delete_subject(client, name): - subjects = get_subjects(client) - for key, value in subjects['subjects'].items(): - if value['name'] == name: - req = client.delete("/subjects/{}".format(key)) - assert req.status_code == 200 - break - subjects = get_subjects(client) - assert name not in [x['name'] for x in subjects["subjects"].values()] +def test_perimeter_add_subject_with_name_contain_spaces(): + client = utilities.register_client() + req, subjects = add_subjects(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" -def test_subject(): +def test_perimeter_delete_subject(): client = utilities.register_client() - get_subjects(client) - add_subjects(client, "testuser") - add_subjects_without_name(client, "") - delete_subject(client, "testuser") + req = delete_subject(client) + assert req.status_code == 200 + + +def test_perimeter_delete_subjects_without_perimeter_id(): + client = utilities.register_client() + req = delete_subjects_without_perimeter_id(client) + assert req.status_code == 500 def get_objects(client): req = client.get("/objects") - assert req.status_code == 200 objects = utilities.get_json(req.data) - assert isinstance(objects, dict) - assert "objects" in objects - return objects + return req, objects def add_objects(client, name): @@ -83,42 +94,71 @@ def add_objects(client, name): } req = client.post("/objects", data=json.dumps(data), headers={'Content-Type': 'application/json'}) - assert req.status_code == 200 objects = utilities.get_json(req.data) - assert isinstance(objects, dict) - key = list(objects["objects"].keys())[0] - value = list(objects["objects"].values())[0] - assert "objects" in objects - assert value['name'] == name - assert value["description"] == "description of {}".format(name) - return objects + return req, objects -def delete_objects(client, name): +def delete_object(client): objects = get_objects(client) - for key, value in objects['objects'].items(): - if value['name'] == name: + for key, value in objects[1]['objects'].items(): + if value['name'] == "testuser": req = client.delete("/objects/{}".format(key)) - assert req.status_code == 200 break - objects = get_objects(client) - assert name not in [x['name'] for x in objects["objects"].values()] + return req + +def delete_objects_without_perimeter_id(client): + req = client.delete("/objects/{}".format("")) + return req -def test_objects(): + +def test_perimeter_get_object(): client = utilities.register_client() - get_objects(client) - add_objects(client, "testuser") - delete_objects(client, "testuser") + req, objects = get_objects(client) + assert req.status_code == 200 + assert isinstance(objects, dict) + assert "objects" in objects + + +def test_perimeter_add_object(): + client = utilities.register_client() + req, objects = add_objects(client, "testuser") + assert req.status_code == 200 + value = list(objects["objects"].values())[0] + assert "objects" in objects + assert value['name'] == "testuser" + + +def test_perimeter_add_object_without_name(): + client = utilities.register_client() + req, objects = add_objects(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_perimeter_add_object_with_name_contain_spaces(): + client = utilities.register_client() + req, objects = add_objects(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_perimeter_delete_object(): + client = utilities.register_client() + req = delete_object(client) + assert req.status_code == 200 + + +def test_perimeter_delete_objects_without_perimeter_id(): + client = utilities.register_client() + req = delete_objects_without_perimeter_id(client) + assert req.status_code == 500 def get_actions(client): req = client.get("/actions") - assert req.status_code == 200 actions = utilities.get_json(req.data) - assert isinstance(actions, dict) - assert "actions" in actions - return actions + return req, actions def add_actions(client, name): @@ -128,30 +168,63 @@ def add_actions(client, name): } req = client.post("/actions", data=json.dumps(data), headers={'Content-Type': 'application/json'}) - assert req.status_code == 200 actions = utilities.get_json(req.data) - assert isinstance(actions, dict) - key = list(actions["actions"].keys())[0] - value = list(actions["actions"].values())[0] - assert "actions" in actions - assert value['name'] == name - assert value["description"] == "description of {}".format(name) - return actions + return req, actions -def delete_actions(client, name): +def delete_actions(client): actions = get_actions(client) - for key, value in actions['actions'].items(): - if value['name'] == name: + for key, value in actions[1]['actions'].items(): + if value['name'] == "testuser": req = client.delete("/actions/{}".format(key)) - assert req.status_code == 200 break - actions = get_actions(client) - assert name not in [x['name'] for x in actions["actions"].values()] + return req + +def delete_actions_without_perimeter_id(client): + req = client.delete("/actions/{}".format("")) + return req -def test_actions(): + +def test_perimeter_get_actions(): + client = utilities.register_client() + req, actions = get_actions(client) + assert req.status_code == 200 + assert isinstance(actions, dict) + assert "actions" in actions + + +def test_perimeter_add_actions(): client = utilities.register_client() - get_actions(client) - add_actions(client, "testuser") - delete_actions(client, "testuser") + req, actions = add_actions(client, "testuser") + assert req.status_code == 200 + value = list(actions["actions"].values())[0] + assert "actions" in actions + assert value['name'] == "testuser" + + +def test_perimeter_add_actions_without_name(): + client = utilities.register_client() + req, actions = add_actions(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_perimeter_add_actions_with_name_contain_spaces(): + client = utilities.register_client() + req, actions = add_actions(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_perimeter_delete_actions(): + client = utilities.register_client() + req = delete_actions(client) + assert req.status_code == 200 + + +def test_perimeter_delete_actions_without_perimeter_id(): + client = utilities.register_client() + req = delete_actions_without_perimeter_id(client) + assert req.status_code == 500 + diff --git a/moon_manager/tests/unit_python/api/test_rules.py b/moon_manager/tests/unit_python/api/test_rules.py index 0b302494..d12b7186 100644 --- a/moon_manager/tests/unit_python/api/test_rules.py +++ b/moon_manager/tests/unit_python/api/test_rules.py @@ -23,6 +23,35 @@ def add_rules(client, policy_id): return req, rules +def add_rules_without_meta_rule_id(client, policy_id): + data = { + "meta_rule_id": "", + "rule": ["subject_data_id2", "object_data_id2", "action_data_id2"], + "instructions": ( + {"decision": "grant"}, + ), + "enabled": True + } + req = client.post("/policies/{}/rules".format(policy_id), data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + rules = utilities.get_json(req.data) + return req, rules + + +def add_rules_without_rule(client, policy_id): + data = { + "meta_rule_id": "meta_rule_id1", + "instructions": ( + {"decision": "grant"}, + ), + "enabled": True + } + req = client.post("/policies/{}/rules".format(policy_id), data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + rules = utilities.get_json(req.data) + return req, rules + + def delete_rules(client, policy_id, meta_rule_id): req = client.delete("/policies/{}/rules/{}".format(policy_id, meta_rule_id)) return req @@ -50,10 +79,37 @@ def test_add_rules(): 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 + + +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' + + +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 + + def test_delete_rules(): + client = utilities.register_client() + rules = delete_rules(client, "", "") + assert rules.status_code == 500 + + +def test_delete_rules_without_policy_id(): client = utilities.register_client() policy_id = utilities.get_policy_id() req, added_rules = get_rules(client, policy_id) id = added_rules["rules"]['rules'][0]['id'] - rules = delete_rules(client, policy_id, id) + rules = delete_rules(client, None, id) assert rules.status_code == 200 diff --git a/moon_manager/tests/unit_python/api/test_unit_models.py b/moon_manager/tests/unit_python/api/test_unit_models.py index 6f97b1ae..52cb2871 100644 --- a/moon_manager/tests/unit_python/api/test_unit_models.py +++ b/moon_manager/tests/unit_python/api/test_unit_models.py @@ -25,6 +25,42 @@ def add_models(client, name): return req, models +def update_model(client, name, model_id): + data = { + "name": name, + "description": "description of {}".format(name), + "meta_rules": ["meta_rule_id1_update", "meta_rule_id2_update"] + } + req = client.patch("/models/{}".format(model_id), data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + models = utilities.get_json(req.data) + return req, models + + +def add_model_without_meta_rules_ids(client, name): + data = { + "name": name, + "description": "description of {}".format(name), + "meta_rules": [] + } + req = client.post("/models", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + models = utilities.get_json(req.data) + return req, models + + +def update_model_without_meta_rules_ids(client, name): + data = { + "name": name, + "description": "description of {}".format(name), + "meta_rules": [] + } + req = client.patch("/models", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + models = utilities.get_json(req.data) + return req, models + + def delete_models(client, name): request, models = get_models(client) for key, value in models['models'].items(): @@ -47,6 +83,7 @@ def clean_models(): print(value) client.delete("/models/{}".format(key)) + def test_get_models(): client = utilities.register_client() req, models= get_models(client) @@ -79,3 +116,60 @@ def test_delete_models_without_id(): req = delete_models_without_id(client) assert req.status_code == 500 + +def test_add_model_with_empty_user(): + clean_models() + client = utilities.register_client() + req, models = add_models(client, "") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "Empty String" + + +def test_add_model_with_user_contain_space(): + clean_models() + client = utilities.register_client() + req, models = add_models(client, "test user") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == "String contains space" + + +def test_add_model_without_meta_rules(): + clean_models() + client = utilities.register_client() + req, meta_rules = add_model_without_meta_rules_ids(client, "testuser") + assert req.status_code == 500 + assert json.loads(req.data)["message"] == 'Empty Container' + + +def test_update_model(): + clean_models() + client = utilities.register_client() + req = add_models(client, "testuser") + model_id = list(req[1]['models'])[0] + req_update = update_model(client, "testuser", model_id) + assert req_update[0].status_code == 200 + value = list(req_update[1]["models"].values())[0] + assert value["meta_rules"][0] == "meta_rule_id1_update" + delete_models(client, "testuser") + + +def test_update_meta_rules_without_id(): + clean_models() + client = utilities.register_client() + req_update = update_model(client, "testuser", "") + assert req_update[0].status_code == 500 + + +def test_update_meta_rules_without_user(): + client = utilities.register_client() + req_update = update_model(client, "", "") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty String" + + +def test_update_meta_rules_without_meta_rules(): + client = utilities.register_client() + req_update = update_model_without_meta_rules_ids(client, "testuser") + assert req_update[0].status_code == 500 + assert json.loads(req_update[0].data)["message"] == "Empty Container" + -- cgit 1.2.3-korg