-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_router.py
288 lines (274 loc) · 8.63 KB
/
test_router.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
# Portions of this file contributed by NIST are governed by the following
# statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. NIST assumes no
# responsibility whatsoever for its use by other parties, and makes
# no guarantees, expressed or implied, about its quality,
# reliability, or any other characteristic.
#
# We would appreciate acknowledgement if the software is used.
import logging
import os
from typing import List, Optional, Set, Tuple
import flask
import flask.testing
import pytest
import router.app
@pytest.fixture
def host_prefix() -> str:
_host_prefix = os.getenv("HOST_PREFIX")
if _host_prefix is None:
raise ValueError(
"Unable to retrieve host prefix from process environment. Please set the environment variable HOST_PREFIX before calling pytest."
)
if _host_prefix.startswith("http://"):
logging.info("Host prefix updated to use https scheme.")
_host_prefix = _host_prefix.replace("http://", "https://")
return _host_prefix
@pytest.fixture
def app() -> flask.Flask:
return router.app.app
@pytest.mark.parametrize(
[
"url_path",
"accept_content_type",
"expected_matching_file",
"expected_response_content_type",
],
[
("/co/1.0.0.rdf", None, "co/1.0.0.rdf", "application/rdf+xml"),
("/uco/vocabulary.rdf", None, "uco/vocabulary.rdf", "application/rdf+xml"),
(
"/uco/vocabulary.rdf",
"application/rdf+xml",
"uco/vocabulary.rdf",
"application/rdf+xml",
),
("/uco/vocabulary.ttl", None, "uco/vocabulary.ttl", "text/turtle"),
("/uco/vocabulary.ttl", "text/turtle", "uco/vocabulary.ttl", "text/turtle"),
],
)
def test_status_200(
app: flask.Flask,
client: flask.testing.FlaskClient,
url_path: str,
accept_content_type: Optional[str],
expected_matching_file: str,
expected_response_content_type: str,
) -> None:
headers: List[Tuple[str, str]] = []
if accept_content_type is not None:
headers.append(("Accept", accept_content_type))
response = client.get(url_path, headers=headers)
assert 200 == response.status_code
assert expected_response_content_type == response.mimetype
with open(expected_matching_file, "rb") as match_fh:
assert match_fh.read() == response.data
@pytest.mark.parametrize(
["url_path", "accept_content_type", "user_agent", "expected_location"],
[
(
"/",
None,
None,
"/documentation/index.html",
),
(
# Confirm non-umbrella ontology request is redirected to umbrella documentation index when HTML content is requested, regardless of user agent.
"/uco/uco",
"text/html",
"Java/1.8.0_332",
"/documentation/index.html",
),
(
# Confirm umbrella ontology request is redirected to RDF-XML ontology file with org.semanticweb.owlapi behavior.
"/uco/uco",
None,
"Java/1.8.0_332",
"/uco/uco.rdf",
),
(
# Confirm higher-directory ontology request is assumed to be RDF-XML request.
"/co",
None,
None,
"/co.rdf",
),
(
# Confirm higher-directory ontology versionIRI request is assumed to be RDF-XML request.
"/co/1.1.0",
None,
None,
"/co/1.1.0.rdf",
),
(
# Confirm non-umbrella ontology request is assumed to be RDF-XML request.
"/uco/action",
None,
None,
"/uco/action.rdf",
),
(
# Confirm non-umbrella ontology request is redirected to RDF-XML ontology file with org.semanticweb.owlapi behavior.
"/uco/action",
None,
"Java/1.8.0_332",
"/uco/action.rdf",
),
(
# Confirm non-umbrella ontology version request is redirected to RDF-XML ontology file with org.semanticweb.owlapi behavior.
"/uco/action/0.9.1",
None,
"Java/1.8.0_332",
"/uco/action/0.9.1.rdf",
),
(
"/uco/action",
"application/rdf+xml",
None,
"/uco/action.rdf",
),
(
"/uco/action",
"text/html",
None,
"/documentation/index.html",
),
(
"/uco/action",
"text/turtle",
None,
"/uco/action.ttl",
),
(
# Confirm prefix IRI (/namespace IRI) redirects to umbrella documentation index.
"/uco/action/",
None,
None,
"/documentation/index.html",
),
(
# Confirm version IRI request is assumed to be RDF-XML request.
"/uco/action/0.9.1",
None,
None,
"/uco/action/0.9.1.rdf",
),
(
"/uco/action/0.9.1",
"application/rdf+xml",
None,
"/uco/action/0.9.1.rdf",
),
(
# Confirm version IRI request as HTML is redirected to umbrella documentation index.
"/uco/action/0.9.1",
"text/html",
None,
"/documentation/index.html",
),
(
"/uco/action/0.9.1",
"text/turtle",
None,
"/uco/action/0.9.1.ttl",
),
(
"/uco/action/Action",
None,
None,
"/documentation/class-actionaction.html",
),
# TODO: Individual concept breakout needs to be written.
# (
# "/uco/action/Action",
# "application/rdf+xml",
# None,
# "/uco/action/Action.rdf",
# ),
(
"/uco/action/Action",
"text/html",
None,
"/documentation/class-actionaction.html",
),
# TODO: Individual concept breakout needs to be written.
# (
# "/uco/action/Action",
# "text/turtle",
# None,
# "/uco/action/Action.ttl",
# ),
(
"/uco/action/performer",
None,
None,
"/documentation/prop-actionperformer.html",
),
# TODO: Individual concept breakout needs to be written.
# (
# "/uco/action/performer",
# "application/rdf+xml",
# None,
# "/uco/action/performer.rdf",
# ),
(
"/uco/action/performer",
"text/html",
None,
"/documentation/prop-actionperformer.html",
),
# TODO: Individual concept breakout needs to be written.
# (
# "/uco/action/performer",
# "text/turtle",
# None,
# "/uco/action/performer.ttl",
# ),
],
)
def test_status_301(
app: flask.Flask,
client: flask.testing.FlaskClient,
host_prefix: str,
url_path: str,
accept_content_type: Optional[str],
user_agent: Optional[str],
expected_location: str,
) -> None:
"""
This test is intended to cover:
* Concept structural types: Ontology, classes, properties, and shapes. (CASE does not have a shape that is not also a class, so this last structural type is not currently covered.)
* Interaction of the "Accept" header with the requested URL: When none is specified, or Turtle or RDF is requested.
"""
headers: List[Tuple[str, str]] = []
if accept_content_type is not None:
headers.append(("Accept", accept_content_type))
if user_agent is not None:
headers.append(("User-Agent", user_agent))
response = client.get(url_path, headers=headers)
assert 301 == response.status_code
assert host_prefix + expected_location == response.location
@pytest.mark.parametrize(
["url_path", "expected_status_codes"],
[
(".git", {403, 404}),
],
)
def test_status_40x(
app: flask.Flask,
client: flask.testing.FlaskClient,
url_path: str,
expected_status_codes: Set[int],
) -> None:
"""
Test starting source:
https://pytest-flask.readthedocs.io/en/latest/features.html#feature-reference
"""
logging.debug(client)
assert client.get(url_path).status_code in expected_status_codes