| 1 |
import datetime |
| 2 |
|
| 3 |
import osmapi |
| 4 |
import pytest |
| 5 |
from requests.auth import HTTPBasicAuth |
| 6 |
from responses import DELETE, GET, PUT |
| 7 |
|
| 8 |
from .conftest import API_BASE, OPEN_CHANGESET_ID |
| 9 |
|
| 10 |
TEST_NODE = { |
| 11 |
"lat": 47.287, |
| 12 |
"lon": 8.765, |
| 13 |
"tag": {"amenity": "place_of_worship", "religion": "pastafarian"}, |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
def test_node_get(api, add_response): |
| 18 |
resp = add_response(GET, "/node/123") |
| 19 |
|
| 20 |
result = api.node_get(123) |
| 21 |
|
| 22 |
assert resp.calls[0].request.method == "GET" |
| 23 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/123" |
| 24 |
assert result == { |
| 25 |
"id": 123, |
| 26 |
"changeset": 15293, |
| 27 |
"uid": 605, |
| 28 |
"timestamp": datetime.datetime(2012, 4, 18, 11, 14, 26), |
| 29 |
"lat": 51.8753146, |
| 30 |
"lon": -1.4857118, |
| 31 |
"visible": True, |
| 32 |
"version": 8, |
| 33 |
"user": "freundchen", |
| 34 |
"tag": {"amenity": "school", "foo": "bar", "name": "Berolina & Schule"}, |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
def test_node_get_with_version(api, add_response): |
| 39 |
resp = add_response(GET, "/node/123/2") |
| 40 |
|
| 41 |
result = api.node_get(123, node_version=2) |
| 42 |
|
| 43 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/123/2" |
| 44 |
assert result == { |
| 45 |
"id": 123, |
| 46 |
"changeset": 4152, |
| 47 |
"uid": 605, |
| 48 |
"timestamp": datetime.datetime(2011, 4, 18, 11, 14, 26), |
| 49 |
"lat": 51.8753146, |
| 50 |
"lon": -1.4857118, |
| 51 |
"visible": True, |
| 52 |
"version": 2, |
| 53 |
"user": "freundchen", |
| 54 |
"tag": {"amenity": "school"}, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def test_node_get_invalid_response(api, add_response): |
| 59 |
add_response(GET, "/node/987") |
| 60 |
|
| 61 |
with pytest.raises(osmapi.XmlResponseInvalidError): |
| 62 |
api.node_get(987) |
| 63 |
|
| 64 |
|
| 65 |
def test_node_create(changeset_api, add_response, assert_request_xml): |
| 66 |
resp = add_response(PUT, "/node/create") |
| 67 |
|
| 68 |
result = changeset_api.node_create(dict(TEST_NODE)) |
| 69 |
|
| 70 |
assert resp.calls[0].request.method == "PUT" |
| 71 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/create" |
| 72 |
assert_request_xml( |
| 73 |
resp.calls[0].request, |
| 74 |
f'<node lat="47.287" lon="8.765" visible="true" ' |
| 75 |
f'changeset="{OPEN_CHANGESET_ID}">' |
| 76 |
'<tag k="amenity" v="place_of_worship"/>' |
| 77 |
'<tag k="religion" v="pastafarian"/>' |
| 78 |
"</node>", |
| 79 |
) |
| 80 |
|
| 81 |
assert result["id"] == 9876 |
| 82 |
assert result["version"] == 1 |
| 83 |
assert result["changeset"] == OPEN_CHANGESET_ID |
| 84 |
assert result["lat"] == TEST_NODE["lat"] |
| 85 |
assert result["lon"] == TEST_NODE["lon"] |
| 86 |
assert result["tag"] == TEST_NODE["tag"] |
| 87 |
|
| 88 |
|
| 89 |
def test_node_create_wo_changeset(auth_api): |
| 90 |
with pytest.raises(osmapi.NoChangesetOpenError, match="need to open a changeset"): |
| 91 |
auth_api.node_create(dict(TEST_NODE)) |
| 92 |
|
| 93 |
|
| 94 |
def test_node_create_existing_node(changeset_api): |
| 95 |
test_node = dict(TEST_NODE, id=123) |
| 96 |
|
| 97 |
with pytest.raises( |
| 98 |
osmapi.OsmTypeAlreadyExistsError, match="This node already exists" |
| 99 |
): |
| 100 |
changeset_api.node_create(test_node) |
| 101 |
|
| 102 |
|
| 103 |
def test_node_create_wo_auth(api): |
| 104 |
api._current_changeset_id = OPEN_CHANGESET_ID |
| 105 |
|
| 106 |
with pytest.raises( |
| 107 |
osmapi.AuthenticationMissingError, match="Authentication missing" |
| 108 |
): |
| 109 |
api.node_create(dict(TEST_NODE)) |
| 110 |
|
| 111 |
|
| 112 |
def test_node_create_unauthorized(changeset_api, add_response): |
| 113 |
add_response(PUT, "/node/create", body="", status=401) |
| 114 |
|
| 115 |
with pytest.raises(osmapi.UnauthorizedApiError): |
| 116 |
changeset_api.node_create(dict(TEST_NODE)) |
| 117 |
|
| 118 |
|
| 119 |
def test_node_create_with_session_auth(mock_api): |
| 120 |
api, session = mock_api(auth=False, content="3322") |
| 121 |
session.auth = HTTPBasicAuth("user", "pass") |
| 122 |
api = osmapi.OsmApi(api=API_BASE, session=session) |
| 123 |
api._current_changeset_id = OPEN_CHANGESET_ID |
| 124 |
|
| 125 |
result = api.node_create(dict(TEST_NODE)) |
| 126 |
|
| 127 |
assert result["id"] == 3322 |
| 128 |
|
| 129 |
|
| 130 |
def test_node_create_with_exception(mock_api): |
| 131 |
api, session = mock_api(side_effect=Exception) |
| 132 |
api._current_changeset_id = OPEN_CHANGESET_ID |
| 133 |
|
| 134 |
with pytest.raises( |
| 135 |
osmapi.MaximumRetryLimitReachedError, match="Give up after 5 retries" |
| 136 |
): |
| 137 |
api.node_create(dict(TEST_NODE)) |
| 138 |
|
| 139 |
|
| 140 |
def test_node_create_drops_timestamp(changeset_api, add_response, assert_request_xml): |
| 141 |
"""`_do` strips a client-supplied timestamp, the server assigns its own.""" |
| 142 |
resp = add_response(PUT, "/node/create", filename="test_node_create.xml") |
| 143 |
test_node = dict(TEST_NODE, timestamp=datetime.datetime(2012, 4, 18, 11, 14, 26)) |
| 144 |
|
| 145 |
result = changeset_api.node_create(test_node) |
| 146 |
|
| 147 |
assert "timestamp" not in resp.calls[0].request.body.decode("utf-8") |
| 148 |
assert "timestamp" not in result |
| 149 |
assert_request_xml( |
| 150 |
resp.calls[0].request, |
| 151 |
f'<node lat="47.287" lon="8.765" visible="true" ' |
| 152 |
f'changeset="{OPEN_CHANGESET_ID}">' |
| 153 |
'<tag k="amenity" v="place_of_worship"/>' |
| 154 |
'<tag k="religion" v="pastafarian"/>' |
| 155 |
"</node>", |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
def test_node_update(changeset_api, add_response, assert_request_xml): |
| 160 |
resp = add_response(PUT, "/node/7676") |
| 161 |
test_node = { |
| 162 |
"id": 7676, |
| 163 |
"lat": 47.287, |
| 164 |
"lon": 8.765, |
| 165 |
"tag": {"amenity": "place_of_worship", "name": "christian"}, |
| 166 |
} |
| 167 |
|
| 168 |
result = changeset_api.node_update(test_node) |
| 169 |
|
| 170 |
assert resp.calls[0].request.method == "PUT" |
| 171 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/7676" |
| 172 |
assert_request_xml( |
| 173 |
resp.calls[0].request, |
| 174 |
f'<node id="7676" lat="47.287" lon="8.765" visible="true" ' |
| 175 |
f'changeset="{OPEN_CHANGESET_ID}">' |
| 176 |
'<tag k="amenity" v="place_of_worship"/>' |
| 177 |
'<tag k="name" v="christian"/>' |
| 178 |
"</node>", |
| 179 |
) |
| 180 |
|
| 181 |
assert result["id"] == 7676 |
| 182 |
assert result["version"] == 3 |
| 183 |
assert result["lat"] == test_node["lat"] |
| 184 |
assert result["lon"] == test_node["lon"] |
| 185 |
assert result["tag"] == test_node["tag"] |
| 186 |
|
| 187 |
|
| 188 |
def test_node_update_when_changeset_is_closed(changeset_api, add_response): |
| 189 |
add_response(PUT, "/node/7676", status=409) |
| 190 |
test_node = {"id": 7676, "lat": 47.287, "lon": 8.765, "tag": {}} |
| 191 |
|
| 192 |
with pytest.raises(osmapi.ChangesetClosedApiError) as execinfo: |
| 193 |
changeset_api.node_update(test_node) |
| 194 |
|
| 195 |
assert execinfo.value.status == 409 |
| 196 |
assert ( |
| 197 |
execinfo.value.payload |
| 198 |
== b"The changeset 2222 was closed at 2021-11-20 09:42:47 UTC." |
| 199 |
) |
| 200 |
assert ( |
| 201 |
execinfo.value.payload_str |
| 202 |
== "The changeset 2222 was closed at 2021-11-20 09:42:47 UTC." |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
def test_node_update_conflict(changeset_api, add_response): |
| 207 |
add_response(PUT, "/node/7676", status=409) |
| 208 |
test_node = {"id": 7676, "lat": 47.287, "lon": 8.765, "tag": {}} |
| 209 |
|
| 210 |
with pytest.raises(osmapi.VersionMismatchApiError) as execinfo: |
| 211 |
changeset_api.node_update(test_node) |
| 212 |
|
| 213 |
assert execinfo.value.status == 409 |
| 214 |
assert ( |
| 215 |
execinfo.value.payload |
| 216 |
== b"Version does not match the current database version of the element" |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
def test_node_delete(changeset_api, add_response, assert_request_xml): |
| 221 |
resp = add_response(DELETE, "/node/7676") |
| 222 |
|
| 223 |
result = changeset_api.node_delete({"id": 7676}) |
| 224 |
|
| 225 |
assert resp.calls[0].request.method == "DELETE" |
| 226 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/7676" |
| 227 |
assert_request_xml( |
| 228 |
resp.calls[0].request, |
| 229 |
f'<node id="7676" visible="true" changeset="{OPEN_CHANGESET_ID}"/>', |
| 230 |
) |
| 231 |
|
| 232 |
assert result["id"] == 7676 |
| 233 |
assert result["version"] == 4 |
| 234 |
# a deleted element is reported back as no longer visible |
| 235 |
assert result["visible"] is False |
| 236 |
|
| 237 |
|
| 238 |
def test_node_delete_when_changeset_is_closed(changeset_api, add_response): |
| 239 |
add_response(DELETE, "/node/7676", status=409) |
| 240 |
|
| 241 |
with pytest.raises(osmapi.ChangesetClosedApiError) as execinfo: |
| 242 |
changeset_api.node_delete({"id": 7676}) |
| 243 |
|
| 244 |
assert execinfo.value.status == 409 |
| 245 |
assert ( |
| 246 |
execinfo.value.payload_str |
| 247 |
== "The changeset 1111 was closed at 2024-03-01 10:00:00 UTC." |
| 248 |
) |
| 249 |
|
| 250 |
|
| 251 |
def test_node_delete_conflict(changeset_api, add_response): |
| 252 |
add_response(DELETE, "/node/7676", status=409) |
| 253 |
|
| 254 |
with pytest.raises(osmapi.VersionMismatchApiError) as execinfo: |
| 255 |
changeset_api.node_delete({"id": 7676}) |
| 256 |
|
| 257 |
assert execinfo.value.status == 409 |
| 258 |
assert ( |
| 259 |
execinfo.value.payload_str |
| 260 |
== "Version does not match the current database version of the element" |
| 261 |
) |
| 262 |
|
| 263 |
|
| 264 |
def test_node_delete_precondition_failed(changeset_api, add_response): |
| 265 |
add_response(DELETE, "/node/7676", status=412) |
| 266 |
|
| 267 |
with pytest.raises(osmapi.PreconditionFailedApiError) as execinfo: |
| 268 |
changeset_api.node_delete({"id": 7676}) |
| 269 |
|
| 270 |
assert execinfo.value.status == 412 |
| 271 |
assert execinfo.value.payload_str == "Node 7676 is still used by ways 1234." |
| 272 |
|
| 273 |
|
| 274 |
def test_node_delete_forbidden(changeset_api, add_response): |
| 275 |
"""A status the write-error translation does not special-case is re-raised.""" |
| 276 |
add_response(DELETE, "/node/7676", status=403) |
| 277 |
|
| 278 |
with pytest.raises(osmapi.ApiError) as execinfo: |
| 279 |
changeset_api.node_delete({"id": 7676}) |
| 280 |
|
| 281 |
assert type(execinfo.value) is osmapi.ApiError |
| 282 |
assert execinfo.value.status == 403 |
| 283 |
|
| 284 |
|
| 285 |
def test_node_create_when_changeset_is_closed(changeset_api, add_response): |
| 286 |
add_response(PUT, "/node/create", status=409) |
| 287 |
|
| 288 |
with pytest.raises(osmapi.ChangesetClosedApiError) as execinfo: |
| 289 |
changeset_api.node_create(dict(TEST_NODE)) |
| 290 |
|
| 291 |
assert execinfo.value.status == 409 |
| 292 |
assert ( |
| 293 |
execinfo.value.payload_str |
| 294 |
== "The changeset 1111 was closed at 2024-03-01 10:00:00 UTC." |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
def test_node_create_precondition_failed(changeset_api, add_response): |
| 299 |
add_response(PUT, "/node/create", status=412) |
| 300 |
|
| 301 |
with pytest.raises(osmapi.PreconditionFailedApiError) as execinfo: |
| 302 |
changeset_api.node_create(dict(TEST_NODE)) |
| 303 |
|
| 304 |
assert execinfo.value.status == 412 |
| 305 |
|
| 306 |
|
| 307 |
def test_node_history(api, add_response): |
| 308 |
resp = add_response(GET, "/node/123/history") |
| 309 |
|
| 310 |
result = api.node_history(123) |
| 311 |
|
| 312 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/123/history" |
| 313 |
assert len(result) == 8 |
| 314 |
assert result[4]["id"] == 123 |
| 315 |
assert result[4]["version"] == 4 |
| 316 |
assert result[4]["lat"] == 51.8753146 |
| 317 |
assert result[4]["lon"] == -1.4857118 |
| 318 |
assert result[4]["tag"] == {"empty": "", "foo": "bar"} |
| 319 |
|
| 320 |
|
| 321 |
def test_node_ways(api, add_response): |
| 322 |
resp = add_response(GET, "/node/234/ways") |
| 323 |
|
| 324 |
result = api.node_ways(234) |
| 325 |
|
| 326 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/234/ways" |
| 327 |
assert len(result) == 1 |
| 328 |
assert result[0]["id"] == 60 |
| 329 |
assert result[0]["changeset"] == 61 |
| 330 |
assert result[0]["tag"] == {"highway": "path", "name": "Dog walking path"} |
| 331 |
|
| 332 |
|
| 333 |
def test_node_ways_not_exists(api, add_response): |
| 334 |
resp = add_response(GET, "/node/404/ways") |
| 335 |
|
| 336 |
result = api.node_ways(404) |
| 337 |
|
| 338 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/404/ways" |
| 339 |
assert result == [] |
| 340 |
|
| 341 |
|
| 342 |
def test_node_relations(api, add_response): |
| 343 |
resp = add_response(GET, "/node/4295668179/relations") |
| 344 |
|
| 345 |
result = api.node_relations(4295668179) |
| 346 |
|
| 347 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/4295668179/relations" |
| 348 |
assert len(result) == 1 |
| 349 |
assert result[0]["id"] == 4294968148 |
| 350 |
assert result[0]["changeset"] == 23123 |
| 351 |
assert result[0]["member"][1] == { |
| 352 |
"role": "point", |
| 353 |
"ref": 4295668179, |
| 354 |
"type": "node", |
| 355 |
} |
| 356 |
assert result[0]["tag"] == {"type": "fancy"} |
| 357 |
|
| 358 |
|
| 359 |
def test_node_relations_unused_element(api, add_response): |
| 360 |
resp = add_response(GET, "/node/4295668179/relations") |
| 361 |
|
| 362 |
result = api.node_relations(4295668179) |
| 363 |
|
| 364 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/node/4295668179/relations" |
| 365 |
assert result == [] |
| 366 |
|
| 367 |
|
| 368 |
def test_nodes_get(api, add_response): |
| 369 |
resp = add_response(GET, "/nodes") |
| 370 |
|
| 371 |
result = api.nodes_get([123, 345]) |
| 372 |
|
| 373 |
assert resp.calls[0].request.url == f"{API_BASE}/api/0.6/nodes?nodes=123,345" |
| 374 |
assert len(result) == 2 |
| 375 |
assert result[123] == { |
| 376 |
"id": 123, |
| 377 |
"changeset": 15293, |
| 378 |
"uid": 605, |
| 379 |
"timestamp": datetime.datetime(2012, 4, 18, 11, 14, 26), |
| 380 |
"lat": 51.8753146, |
| 381 |
"lon": -1.4857118, |
| 382 |
"visible": True, |
| 383 |
"version": 8, |
| 384 |
"user": "freundchen", |
| 385 |
"tag": {"amenity": "school", "foo": "bar", "name": "Berolina & Schule"}, |
| 386 |
} |
| 387 |
assert result[345] == { |
| 388 |
"id": 345, |
| 389 |
"changeset": 244, |
| 390 |
"timestamp": datetime.datetime(2009, 9, 12, 3, 22, 59), |
| 391 |
"uid": 1, |
| 392 |
"visible": False, |
| 393 |
"version": 2, |
| 394 |
"user": "guggis", |
| 395 |
"tag": {}, |
| 396 |
} |