aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_pdp.py
blob: 53a87b21f82e294ef7e6471cdd74a44fd1f1b9d4 (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
import json
import api.utilities as utilities
from helpers import data_builder as builder
from uuid import uuid4


def get_pdp(client):
    req = client.get("/pdp")
    pdp = utilities.get_json(req.data)
    return req, pdp


def add_pdp(client, data):
    req = client.post("/pdp", data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    pdp = utilities.get_json(req.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


def delete_pdp_without_id(client):
    req = client.delete("/pdp/{}".format(""))
    return req


def test_get_pdp():
    client = utilities.register_client()
    req, pdp = get_pdp(client)
    assert req.status_code == 200
    assert isinstance(pdp, dict)
    assert "pdps" in pdp


def test_add_pdp():
    subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy(
        subject_category_name="subject_category1" + uuid4().hex,
        object_category_name="object_category1" + uuid4().hex,
        action_category_name="action_category1" + uuid4().hex,
        meta_rule_name="meta_rule_1" + uuid4().hex,
        model_name="model1" + uuid4().hex)
    data = {
        "name": "testuser",
        "security_pipeline": [policy_id],
        "keystone_project_id": "keystone_project_id",
        "description": "description of testuser"
    }
    client = utilities.register_client()
    req, pdp = add_pdp(client, data)
    assert req.status_code == 200
    assert isinstance(pdp, dict)
    value = list(pdp["pdps"].values())[0]
    assert "pdps" in pdp
    assert value['name'] == "testuser"
    assert value["description"] == "description of {}".format("testuser")
    assert value["keystone_project_id"] == "keystone_project_id"


def test_delete_pdp():
    client = utilities.register_client()
    request, pdp = get_pdp(client)
    success_req = None
    for key, value in pdp['pdps'].items():
        if value['name'] == "testuser":
            success_req = delete_pdp(client, key)
            break
    assert success_req
    assert success_req.status_code == 200


def test_add_pdp_with_forbidden_char_in_user():
    data = {
        "name": "<a>",
        "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 == 400
    assert json.loads(req.data)["message"] == "Key: 'name', [Forbidden characters in string]"


def test_add_pdp_with_forbidden_char_in_keystone():
    data = {
        "name": "testuser",
        "security_pipeline": ["policy_id_1", "policy_id_2"],
        "keystone_project_id": "<a>",
        "description": "description of testuser"
    }
    client = utilities.register_client()
    req, meta_rules = add_pdp(client, data)
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == "Key: 'keystone_project_id', [Forbidden characters in string]"


def test_update_pdp():
    subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id = builder.create_new_policy(
        subject_category_name="subject_category1"+uuid4().hex,
        object_category_name="object_category1"+uuid4().hex,
        action_category_name="action_category1"+uuid4().hex,
        meta_rule_name="meta_rule_1"+uuid4().hex,
        model_name="model1"+uuid4().hex)
    data_add = {
        "name": "testuser",
        "security_pipeline": [policy_id],
        "keystone_project_id": "keystone_project_id",
        "description": "description of testuser"
    }

    subject_category_id, object_category_id, action_category_id, meta_rule_id, policy_id_update = builder.create_new_policy(
        subject_category_name="subject_category1" + uuid4().hex,
        object_category_name="object_category1" + uuid4().hex,
        action_category_name="action_category1" + uuid4().hex,
        meta_rule_name="meta_rule_1" + uuid4().hex,
        model_name="model1" + uuid4().hex)
    data_update = {
        "name": "testuser",
        "security_pipeline": [policy_id_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 == 400
    assert json.loads(req_update[0].data)["message"] == 'Invalid Key :name not found'


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, "<a>")
    assert req_update[0].status_code == 400
    assert json.loads(req_update[0].data)["message"] == "Forbidden characters in string"