-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_api.py
151 lines (114 loc) · 5.76 KB
/
test_api.py
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
import pytest
import json
from pathlib import Path
from app import create_app
base_url = "/api"
@pytest.fixture
def app():
app = create_app()
app.config.update({"TESTING": True})
yield app
@pytest.fixture
def client(app):
return app.test_client()
def assert_response_status(response, expected_status):
assert response.status_code == expected_status
def assert_response_json_types(response, key_type_pairs):
json_data = response.json
for key, data_type in key_type_pairs:
assert isinstance(json_data.get(key), data_type)
class TestFlowers:
def test_create_flower(self, client):
response = client.post(f"{base_url}/flowers", json={})
assert_response_status(response, 200)
def test_share_flower(self, client):
json_path = Path('generated/1.json')
with json_path.open('r') as file:
flower_data = json.load(file)
response = client.post(f"{base_url}/flowers", json=flower_data)
assert_response_status(response, 200)
def test_share_flower_missing_node_chromosome(self, client):
json_path = Path('generated/1.json')
with json_path.open('r') as file:
flower_data = json.load(file)
del flower_data['Flower']['dna']['genomes'][1]['nodeChromosomes']
response = client.post(f"{base_url}/flowers", json=flower_data)
assert_response_status(response, 400)
def test_get_flowers(self, client):
response = client.get(f"{base_url}/flowers")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int), ("flowers", list)])
def test_get_flowers_count(self, client):
response = client.get(f"{base_url}/flowers?count=1")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int)])
def test_get_flower_by_id(self, client):
response = client.get(f"{base_url}/flowers/1")
assert_response_status(response, 200)
assert_response_json_types(response, [("id", int), ("genome", str), ("image", str)])
def test_get_flower_by_invalid_id(self, client):
response = client.get(f"{base_url}/flowers/999999")
assert_response_status(response, 404)
class TestMutations:
def test_create_mutation(self, client):
response = client.post(f"{base_url}/mutations", json={"original": 1})
assert_response_status(response, 200)
def test_create_mutation_invalid_data(self, client):
response = client.post(f"{base_url}/mutations", json={"original": "invalid"})
assert_response_status(response, 400)
def test_get_mutations(self, client):
response = client.get(f"{base_url}/mutations")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int), ("mutations", list)])
def test_get_mutations_count(self, client):
response = client.get(f"{base_url}/mutations?count=1")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int)])
def test_get_mutations_by_original(self, client):
response = client.get(f"{base_url}/mutations/1")
assert_response_status(response, 200)
assert isinstance(response.json, list)
def test_get_mutations_by_invalid_id(self, client):
response = client.get(f"{base_url}/mutations/999999")
assert_response_status(response, 404)
def test_create_mutation_xss(self, client):
response = client.post(f"{base_url}/mutations", json={"original": "<script>alert('XSS')</script>"})
assert_response_status(response, 400)
class TestAncestors:
def test_create_ancestor(self, client):
response = client.post(f"{base_url}/ancestors", json={"father": 1, "mother": 2})
assert_response_status(response, 200)
def test_create_ancestor_invalid_data(self, client):
response = client.post(f"{base_url}/ancestors", json={"father": "invalid", "mother": "invalid"})
assert_response_status(response, 400)
def test_create_ancestor_xss(self, client):
response = client.post(f"{base_url}/ancestors", json={"father": "<script>alert('XSS')</script>", "mother": 2})
assert_response_status(response, 400)
def test_get_ancestors(self, client):
response = client.get(f"{base_url}/ancestors")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int), ("ancestors", list)])
def test_get_ancestors_count(self, client):
response = client.get(f"{base_url}/ancestors?count=1")
assert_response_status(response, 200)
assert_response_json_types(response, [("count", int)])
def test_get_ancestors_by_father(self, client):
response = client.get(f"{base_url}/ancestors/1")
assert_response_status(response, 200)
assert isinstance(response.json, list)
def test_get_ancestors_by_father_and_mother(self, client):
response = client.get(f"{base_url}/ancestors/1/2")
assert_response_status(response, 200)
assert isinstance(response.json, list)
def test_get_ancestors_by_invalid_father(self, client):
response = client.get(f"{base_url}/ancestors/999999")
assert_response_status(response, 404)
def test_get_ancestors_by_invalid_father_and_valid_mother(self, client):
response = client.get(f"{base_url}/ancestors/999999/2")
assert_response_status(response, 404)
def test_get_ancestors_by_valid_father_and_invalid_mother(self, client):
response = client.get(f"{base_url}/ancestors/1/9999999")
assert_response_status(response, 404)
def test_get_ancestors_by_invalid_father_and_mother(self, client):
response = client.get(f"{base_url}/ancestors/9999999/999999")
assert_response_status(response, 404)