aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/import_export_utilities.py
blob: 3c921e21b96050e669bfc6bd086ec7a9b29a6b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright 2018 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import api.utilities as utilities
import api.test_unit_models as test_models
import api.test_policies as test_policies
import api.test_perimeter as test_perimeter
import api.meta_data_test as test_categories
import api.test_data as test_data
import api.meta_rules_test as test_meta_rules
import api.test_assignemnt as test_assignments
import api.test_rules as test_rules
import logging

logger = logging.getLogger("moon.manager.test.api." + __name__)


def clean_models(client):
    req, models = test_models.get_models(client)
    for key in models["models"]:
        client.delete("/models/{}".format(key))
        print("deleted model with id {}".format(key))


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
        print("deleted policy with id {}".format(key))


def clean_subjects(client):
    subjects = test_perimeter.get_subjects(client)
    logger.info("subjects {}".format(subjects))
    for key in subjects["subjects"]:
        subject = subjects["subjects"][key]
        policy_keys = subject["policy_list"]
        logger.info("subjects policy_keys {}".format(policy_keys))
        for policy_key in policy_keys:
            client.delete("/policies/{}/subjects/{}".format(policy_key,key))
        client.delete("/subjects/{}".format(key))
        print("deleted subject with id {}".format(key))


def clean_objects(client):
    objects = test_perimeter.get_objects(client)
    logger.info("objects {}".format(objects))
    for key in objects["objects"]:
        object_ = objects["objects"][key]
        policy_keys = object_["policy_list"]
        logger.info("objects policy_keys {}".format(policy_keys))
        for policy_key in policy_keys:
            print("/policies/{}/objects/{}".format(policy_key, key))
            req = client.delete("/policies/{}/objects/{}".format(policy_key, key))
        client.delete("/objects/{}".format(key))
        print("deleted object with id {}".format(key))


def clean_actions(client):
    actions = test_perimeter.get_actions(client)
    logger.info("objects {}".format(actions))
    for key in actions["actions"]:
        action = actions["actions"][key]
        policy_keys = action["policy_list"]
        logger.info("action policy_keys {}".format(policy_keys))
        for policy_key in policy_keys:
            client.delete("/policies/{}/actions/{}".format(policy_key, key))
        client.delete("/actions/{}".format(key))
        print("deleted action with id {}".format(key))


def clean_subject_categories(client):
    req, categories = test_categories.get_subject_categories(client)
    logger.info(categories)
    for key in categories["subject_categories"]:
        client.delete("/subject_categories/{}".format(key))


def clean_object_categories(client):
    req, categories = test_categories.get_object_categories(client)
    logger.info(categories)
    for key in categories["object_categories"]:
        client.delete("/object_categories/{}".format(key))


def clean_action_categories(client):
    req, categories = test_categories.get_action_categories(client)
    logger.info(categories)
    for key in categories["action_categories"]:
        client.delete("/action_categories/{}".format(key))


def clean_subject_data(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, data = test_data.get_subject_data(client, policy_id=policy_key)
        print(data)
        for key in data["subject_data"]:
            client.delete("/policies/{}/subject_data/{}".format(policy_key, key))


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)
        print(data)
        for key in data["object_data"]:
            client.delete("/policies/{}/object_data/{}".format(policy_key, key))


def clean_action_data(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, data = test_data.get_action_data(client, policy_id=policy_key)
        for key in data["action_data"]:
            client.delete("/policies/{}/action_data/{}".format(policy_key, key))


def clean_meta_rule(client):
    req, meta_rules = test_meta_rules.get_meta_rules(client)
    meta_rules = meta_rules["meta_rules"]
    for meta_rule_key in meta_rules:
        print(meta_rule_key)
        client.delete("/meta_rules/{}".format(meta_rule_key))


def clean_subject_assignments(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, assignments = test_assignments.get_subject_assignment(client, policy_key)
        for key in assignments["subject_assignments"]:
            subject_key = assignments["subject_assignments"][key]["subject_id"]
            cat_key = assignments["subject_assignments"][key]["category_id"]
            data_keys = assignments["subject_assignments"][key]["assignments"]
            for data_key in data_keys:
                req = client.delete("/policies/{}/subject_assignments/{}/{}/{}".format(policy_key, subject_key, cat_key, data_key))


def clean_object_assignments(client):
    req, policies = test_policies.get_policies(client)
    for policy_key in policies["policies"]:
        req, assignments = test_assignments.get_object_assignment(client, policy_key)
        for key in assignments["object_assignments"]:
            object_key = assignments["object_assignments"][key]["object_id"]
            cat_key = assignments["object_assignments"][key]["category_id"]
            data_keys = assignments["object_assignments"][key]["assignments"]
            for data_key in data_keys:
                req = client.delete("/policies/{}/object_assignments/{}/{}/{}".format(policy_key, object_key, cat_key, data_key))


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:
                req = client.delete("/policies/{}/action_assignments/{}/{}/{}".format(policy_key, action_key, cat_key, data_key))


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)
        print(rules)
        rules = rules["rules"]
        rules = rules["rules"]
        for rule_key in rules:
            client.delete("/policies/{}/rules/{}".format(policy_key, rule_key))


def clean_all(client):
    clean_rules(client)

    clean_subject_assignments(client)
    clean_object_assignments(client)
    clean_action_assignments(client)

    clean_meta_rule(client)

    clean_subject_categories(client)
    clean_object_categories(client)
    clean_action_categories(client)

    clean_subject_data(client)
    clean_object_data(client)
    clean_action_data(client)

    clean_actions(client)
    clean_objects(client)
    clean_subjects(client)

    clean_policies(client)
    clean_models(client)