aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_perimeter.py
blob: b13bb2eda20c1b89cb62350adfa25124b8cfcb95 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# import moon_manager
# import moon_manager.api
import json
import api.utilities as utilities


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


def add_subjects(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'})
    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)
    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 value['name'] == "testuser"
    assert value["email"] == "{}@moon".format("testuser")


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 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_perimeter_delete_subject():
    client = utilities.register_client()
    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")
    objects = utilities.get_json(req.data)
    return req, objects


def add_objects(client, name):
    data = {
        "name": name,
        "description": "description of {}".format(name),
    }
    req = client.post("/objects", data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    objects = utilities.get_json(req.data)
    return req, objects


def delete_object(client):
    objects = get_objects(client)
    for key, value in objects[1]['objects'].items():
        if value['name'] == "testuser":
            req = client.delete("/objects/{}".format(key))
            break
    return req


def delete_objects_without_perimeter_id(client):
    req = client.delete("/objects/{}".format(""))
    return req


def test_perimeter_get_object():
    client = utilities.register_client()
    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")
    actions = utilities.get_json(req.data)
    return req, actions


def add_actions(client, name):
    data = {
        "name": name,
        "description": "description of {}".format(name),
    }
    req = client.post("/actions", data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    actions = utilities.get_json(req.data)
    return req, actions


def delete_actions(client):
    actions = get_actions(client)
    for key, value in actions[1]['actions'].items():
        if value['name'] == "testuser":
            req = client.delete("/actions/{}".format(key))
            break
    return req


def delete_actions_without_perimeter_id(client):
    req = client.delete("/actions/{}".format(""))
    return req


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()
    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