예제 #1
0
def clean_rules(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, rules = test_rules.get_rules(client, policy_key)
        rules = rules["rules"]["rules"]
        for rule_key in rules:
            req = client.delete("/policies/{}/rules/{}".format(policy_key, rule_key["id"]))
예제 #2
0
def test_import_policies():
    client = utilities.register_client()
    import_export_utilities.clean_all(client)
    counter = -1
    for policy_description in POLICIES:
        counter = counter + 1
        req = client.post("/import",
                          content_type='application/json',
                          data=json.dumps(policy_description))
        try:
            data = utilities.get_json(req.data)
            assert data == "Import ok !"
        except Exception:
            assert counter == 2  # this is an expected failure
            continue

        req, policies = test_policies.get_policies(client)
        policies = policies["policies"]
        assert len(list(policies.keys())) == 1
        values = list(policies.values())
        assert values[0]["name"] == "test policy"
        if counter < 3:
            assert values[0]["genre"] == "authz"
            assert values[0]["description"] == "description"
        else:
            assert values[0]["genre"] == "not authz ?"
            assert values[0]["description"] == "changes taken into account"
            assert len(values[0]["model_id"]) > 0
    import_export_utilities.clean_all(client)
예제 #3
0
def clean_object_data(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, data = test_data.get_object_data(client, policy_id=policy_key)
        for data_item in data["object_data"]:
            if data_item["data"]:
                for data_id in data_item["data"]:
                    logger.info("============= object_data {}/{}".format(policy_key, data_id))
                    client.delete("/policies/{}/object_data/{}/{}".format(policy_key, data_item['category_id'], data_id))
예제 #4
0
def clean_subject_data(client):
    req, policies = test_policies.get_policies(client)
    logger.info("clean_subject_data on {}".format(policies))
    for policy_key in policies["policies"]:
        req, data = test_data.get_subject_data(client, policy_id=policy_key)
        logger.info("============= data {}".format(data))
        for data_item in data["subject_data"]:
            if data_item["data"]:
                for data_id in data_item["data"]:
                    logger.info("============= Deleting {}/{}".format(policy_key, data_id))
                    client.delete("/policies/{}/subject_data/{}/{}".format(policy_key, data_item['category_id'], data_id))
예제 #5
0
def clean_action_assignments(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, assignments = test_assignments.get_action_assignment(client, policy_key)
        for key in assignments["action_assignments"]:
            action_key = assignments["action_assignments"][key]["action_id"]
            cat_key = assignments["action_assignments"][key]["category_id"]
            data_keys = assignments["action_assignments"][key]["assignments"]
            for data_key in data_keys:
                client.delete("/policies/{}/action_assignments/{}/{}/{}".format(policy_key, action_key,
                                                                                cat_key, data_key))
예제 #6
0
def test_import_subject_object_action_assignments():
    client = utilities.register_client()
    import_export_utilities.clean_all(client)

    req = client.post("/import",
                      content_type='application/json',
                      data=json.dumps(PRE_ASSIGNMENTS))
    data = utilities.get_json(req.data)
    assert data == "Import ok !"

    type_elements = ["subject", "object", "action"]

    for type_element in type_elements:
        counter = -1
        if type_element == "subject":
            datas = SUBJECT_ASSIGNMENTS
            get_method = test_assignments.get_subject_assignment
        elif type_element == "object":
            datas = OBJECT_ASSIGNMENTS
            get_method = test_assignments.get_object_assignment
        else:
            datas = ACTION_ASSIGNMENTS
            get_method = test_assignments.get_action_assignment

        for assignments in datas:
            counter = counter + 1
            req = client.post("/import",
                              content_type='application/json',
                              data=json.dumps(assignments))
            if counter != 3:
                assert req.status_code == 500
                continue
            else:
                assert data == "Import ok !"
                assert req.status_code == 200
                req, policies = test_policies.get_policies(client)
                for policy_key in policies["policies"]:
                    req, get_assignments = get_method(client, policy_key)
                    get_assignments = get_assignments[type_element +
                                                      "_assignments"]
                    assert len(get_assignments) == 1
예제 #7
0
def clean_policies(client):
    req, policies = test_policies.get_policies(client)
    for key in policies["policies"]:
        req = client.delete("/policies/{}".format(key))
        assert req.status_code == 200
예제 #8
0
def test_import_subject_object_action_data():
    client = utilities.register_client()
    type_elements = ["subject", "object", "action"]

    for type_element in type_elements:
        import_export_utilities.clean_all(client)
        req = client.post("/import",
                          content_type='application/json',
                          data=json.dumps(PRE_DATA))
        counter = -1
        # set the getters and the comparison values
        if type_element == "subject":
            elements = SUBJECT_DATA
            get_method = test_data.get_subject_data
            get_categories = test_categories.get_subject_categories
        elif type_element == "object":
            elements = OBJECT_DATA
            get_method = test_data.get_object_data
            get_categories = test_categories.get_object_categories
        else:
            elements = ACTION_DATA
            get_method = test_data.get_action_data
            get_categories = test_categories.get_action_categories

        for element in elements:
            req = client.post("/import",
                              content_type='application/json',
                              data=json.dumps(element))
            counter = counter + 1
            if counter == 0 or counter == 1:
                assert req.status_code == 500
                continue
            assert req.status_code == 200
            data = utilities.get_json(req.data)
            assert data == "Import ok !"

            req, policies = test_policies.get_policies(client)
            policies = policies["policies"]
            req, categories = get_categories(client)
            categories = categories[type_element + "_categories"]
            case_tested = False
            for policy_key in policies.keys():
                policy = policies[policy_key]
                for category_key in categories:
                    req, get_elements = get_method(client,
                                                   policy_id=policy_key,
                                                   category_id=category_key)
                    if len(get_elements[type_element + "_data"]) == 0:
                        continue

                    # do this because the backend gives an element with empty data if the policy_key,
                    # category_key couple does not have any data...
                    get_elements = get_elements[type_element + "_data"]
                    if len(get_elements[0]["data"]) == 0:
                        continue

                    if policy["name"] == "test policy":
                        assert len(get_elements) == 1
                        el = get_elements[0]
                        assert isinstance(el["data"], dict)
                        if counter == 2:
                            assert len(el["data"].keys()) == 1
                            el = el["data"][list(el["data"].keys())[0]]
                            if "value" in el:
                                el = el["value"]
                            assert el[
                                "name"] == "one valid " + type_element + " data"
                        if counter == 3:
                            assert len(el["data"].keys()) == 2
                            el1 = el["data"][list(el["data"].keys())[0]]
                            el2 = el["data"][list(el["data"].keys())[1]]
                            if "value" in el1:
                                el1 = el1["value"]
                                el2 = el2["value"]
                            assert (el1["name"] == "one valid " +
                                    type_element + " data" and el2["name"]
                                    == "valid " + type_element + " data") or (
                                        el2["name"] == "one valid " +
                                        type_element + " data" and el1["name"]
                                        == "valid " + type_element + " data")
                            assert el1["description"] == "description"
                            assert el2["description"] == "description"

                        case_tested = True

                    if policy["name"] == "test other policy":
                        if counter == 4:
                            assert len(get_elements) == 1
                            el = get_elements[0]
                            assert isinstance(el["data"], dict)
                            assert len(el["data"].keys()) == 1
                            el = el["data"][list(el["data"].keys())[0]]
                            if "value" in el:
                                el = el["value"]
                            assert el[
                                "name"] == "valid " + type_element + " data"
                            assert el["description"] == "new description"
                            case_tested = True

            assert case_tested is True