aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_pdp.py
blob: fbaa6c7b4e07a5c6bc47ca6f17c9c4a558e6a495 (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
import json
import api.utilities as utilities
import pytest


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():
    data = {
        "name": "testuser",
        "security_pipeline": ["policy_id_1", "policy_id_2"],
        "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)
    for key, value in pdp['pdps'].items():
        if value['name'] == "testuser":
            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"