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
|
"""Mirrors tests/envelope.rs and the envelope check of tests/openapi.rs:
the success envelope (`data` next to `meta`, `notes` and `next`) on
every endpoint except `/` and `/openapi.json`, and that every `next`
href resolves. The `/version` and OpenAPI checks need nothing else; the
`next` crawl needs a live MariaDB and a token (see docs/gn-auth.md) and
skips with a message when either is missing.
"""
import http.client
import json
import unittest
from auth_helpers import TOKEN_ENV, auth_token
from server_test_case import HOST, PORT, ServerTestCase, database_reachable
# Hrefs not followed: lists of thousands of rows (every trait of a
# group, every probeset of a dataset).
TOO_LARGE_SUFFIXES = ("/traits", "/probesets")
SEEDS = [
"/species",
"/species/mouse",
"/species/mouse/datasets",
"/groups",
"/groups/BXD",
"/groups/BXD/strains",
"/groups/BXD/case-attributes",
"/groups/BXD/datasets",
"/mapping-methods",
"/accessions/GN602",
"/accessions/GN1",
"/groups/BXD/traits?abbreviation=5HT_MB_M",
"/groups/BXD/traits/17985",
"/groups/BXD/traits/17985/values",
"/datasets/Br_U_0803_M",
"/datasets/Br_U_0803_M/probesets/100001_at",
"/datasets/Br_U_0803_M/probesets/100001_at/values",
"/traits/BXDPublish/17985",
"/traits/BXDPublish/17985/values",
"/publications/24102824/traits",
]
class EnvelopeTest(ServerTestCase):
EXTRA_ENV = TOKEN_ENV
def get(self, path, token=None, timeout=60):
conn = http.client.HTTPConnection(HOST, int(PORT), timeout=timeout)
try:
headers = {"Authorization": f"Bearer {token}"} if token else {}
conn.request("GET", path, headers=headers)
response = conn.getresponse()
return response, response.read()
finally:
conn.close()
def test_version_is_wrapped_in_the_envelope(self):
response, body = self.get("/version")
self.assertEqual(response.status, 200)
envelope = json.loads(body)
self.assertEqual(envelope["data"]["name"], "GeneNetwork4")
self.assertEqual(envelope["meta"]["state"], "complete")
self.assertIsInstance(envelope["meta"]["took_ms"], int)
self.assertEqual(
envelope["meta"]["request_id"], response.getheader("x-request-id")
)
self.assertEqual(envelope["notes"], [])
self.assertEqual(envelope["next"], [])
def test_errors_carry_the_request_id_and_took_ms(self):
# The token extractor's 401: needs no database or gn-auth.
response, body = self.get("/species")
self.assertEqual(response.status, 401)
error = json.loads(body)
self.assertEqual(error["code"], "invalid_token")
self.assertEqual(error["request_id"], response.getheader("x-request-id"))
self.assertIsInstance(error["took_ms"], int)
def test_the_greeting_and_the_openapi_document_are_not_wrapped(self):
response, body = self.get("/")
self.assertEqual(response.status, 200)
self.assertEqual(body, b"Hello, world!")
response, body = self.get("/openapi.json")
doc = json.loads(body)
self.assertIn("openapi", doc)
self.assertNotIn("meta", doc)
def test_every_json_success_response_is_an_envelope(self):
_, body = self.get("/openapi.json")
checked = 0
for path, item in json.loads(body)["paths"].items():
for method, operation in item.items():
content = operation["responses"].get("200", {}).get("content", {})
body_schema = content.get("application/json")
if body_schema is None:
continue
schema = body_schema["schema"].get("$ref", "")
self.assertTrue(
schema.startswith("#/components/schemas/Envelope_"),
f"{method} {path}: 200 response is {schema!r}, not an envelope",
)
checked += 1
self.assertGreaterEqual(checked, 20)
def test_next_links_resolve(self):
if not database_reachable():
self.skipTest("no database on 127.0.0.1:3306")
token = auth_token()
if not token:
self.skipTest("no token (see docs/gn-auth.md)")
followed = 0
for seed in SEEDS:
response, body = self.get(seed, token)
self.assertEqual(response.status, 200, f"GET {seed}: {body[:200]}")
envelope = json.loads(body)
self.assertEqual(
envelope["meta"]["request_id"], response.getheader("x-request-id"), seed
)
self.assertIsInstance(envelope["meta"]["took_ms"], int, seed)
for nxt in envelope["next"]:
href = nxt["href"]
self.assertTrue(href.startswith("/"), f"{seed}: relative href {href}")
self.assertIsInstance(nxt["description"], str)
if href.endswith(TOO_LARGE_SUFFIXES):
continue
response, linked = self.get(href, token)
self.assertEqual(
response.status, 200, f"{seed} suggests {href}: {linked[:200]}"
)
self.assertIn("state", json.loads(linked)["meta"])
followed += 1
self.assertGreaterEqual(followed, 20)
if __name__ == "__main__":
unittest.main()
|