aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_perimeter.py
blob: d255ae659f7be52110d4e3683ff9601f2aea79b2 (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
# import moon_manager
# import moon_manager.api
import json


def get_json(data):
    return json.loads(data.decode("utf-8"))


def get_subjects(client):
    req = client.get("/subjects")
    assert req.status_code == 200
    subjects = get_json(req.data)
    assert isinstance(subjects, dict)
    assert "subjects" in subjects
    return 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'})
    assert req.status_code == 200
    subjects = get_json(req.data)
    assert isinstance(subjects, dict)
    key = list(subjects["subjects"].keys())[0]
    value = list(subjects["subjects"].values())[0]
    assert "subjects" in subjects
    assert key == "1111111111111"
    assert value['id'] == "1111111111111"
    assert value['name'] == name
    assert value["description"] == "description of {}".format(name)
    assert value["email"] == "{}@moon".format(name)
    return subjects


def add_subjects_without_name(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'})
    assert req.status_code == 500


def delete_subject(client, name):
    subjects = get_subjects(client)
    for key, value in subjects['subjects'].items():
        if value['name'] == name:
            req = client.delete("/subjects/{}".format(key))
            assert req.status_code == 200
            break
    subjects = get_subjects(client)
    assert name not in [x['name'] for x in subjects["subjects"].values()]


def test_subject():
    import moon_manager.server
    server = moon_manager.server.main()
    client = server.app.test_client()
    get_subjects(client)
    add_subjects(client, "testuser")
    add_subjects_without_name(client, "")
    delete_subject(client, "testuser")


def get_objects(client):
    req = client.get("/objects")
    assert req.status_code == 200
    objects = get_json(req.data)
    assert isinstance(objects, dict)
    assert "objects" in objects
    return 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'})
    assert req.status_code == 200
    objects = get_json(req.data)
    assert isinstance(objects, dict)
    key = list(objects["objects"].keys())[0]
    value = list(objects["objects"].values())[0]
    assert "objects" in objects
    assert value['name'] == name
    assert value["description"] == "description of {}".format(name)
    return objects


def delete_objects(client, name):
    objects = get_objects(client)
    for key, value in objects['objects'].items():
        if value['name'] == name:
            req = client.delete("/objects/{}".format(key))
            assert req.status_code == 200
            break
    objects = get_objects(client)
    assert name not in [x['name'] for x in objects["objects"].values()]


def test_objects():
    import moon_manager.server
    server = moon_manager.server.main()
    client = server.app.test_client()
    get_objects(client)
    add_objects(client, "testuser")
    delete_objects(client, "testuser")


def get_actions(client):
    req = client.get("/actions")
    assert req.status_code == 200
    actions = get_json(req.data)
    assert isinstance(actions, dict)
    assert "actions" in actions
    return 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'})
    assert req.status_code == 200
    actions = get_json(req.data)
    assert isinstance(actions, dict)
    key = list(actions["actions"].keys())[0]
    value = list(actions["actions"].values())[0]
    assert "actions" in actions
    assert value['name'] == name
    assert value["description"] == "description of {}".format(name)
    return actions


def delete_actions(client, name):
    actions = get_actions(client)
    for key, value in actions['actions'].items():
        if value['name'] == name:
            req = client.delete("/actions/{}".format(key))
            assert req.status_code == 200
            break
    actions = get_actions(client)
    assert name not in [x['name'] for x in actions["actions"].values()]


def test_actions():
    import moon_manager.server
    server = moon_manager.server.main()
    client = server.app.test_client()
    get_actions(client)
    add_actions(client, "testuser")
    delete_actions(client, "testuser")