| 1 |
import osmapi |
| 2 |
import pytest |
| 3 |
import requests |
| 4 |
from unittest import mock |
| 5 |
import responses |
| 6 |
import os |
| 7 |
import re |
| 8 |
import xmltodict |
| 9 |
|
| 10 |
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) |
| 11 |
|
| 12 |
API_BASE = "http://api06.dev.openstreetmap.org" |
| 13 |
PROD_API_BASE = "https://www.openstreetmap.org" |
| 14 |
|
| 15 |
# The default `created_by` / generator string, which tracks the package version |
| 16 |
GENERATOR = f"osmapi/{osmapi.__version__}" |
| 17 |
|
| 18 |
# Changeset id used by tests that need an already-open changeset |
| 19 |
OPEN_CHANGESET_ID = 1111 |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def file_content(): |
| 24 |
def _file_content(filename): |
| 25 |
path = os.path.join(__location__, "fixtures", filename) |
| 26 |
if not os.path.exists(path): |
| 27 |
return "" |
| 28 |
with open(path) as f: |
| 29 |
return f.read() |
| 30 |
|
| 31 |
return _file_content |
| 32 |
|
| 33 |
|
| 34 |
def authenticated_session(): |
| 35 |
"""A session that carries credentials, standing in for an OAuth session. |
| 36 |
|
| 37 |
osmapi considers a session authenticated if its `auth` is set; what kind |
| 38 |
of credentials that is (an OAuth 2.0 auth handler in practice) does not |
| 39 |
matter to the library. |
| 40 |
""" |
| 41 |
session = requests.Session() |
| 42 |
session.auth = ("testuser", "testpassword") |
| 43 |
return session |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture |
| 47 |
def api(): |
| 48 |
api = osmapi.OsmApi(api=API_BASE) |
| 49 |
api._session._sleep = mock.Mock() |
| 50 |
|
| 51 |
yield api |
| 52 |
api.close() |
| 53 |
|
| 54 |
|
| 55 |
@pytest.fixture |
| 56 |
def auth_api(): |
| 57 |
api = osmapi.OsmApi(api=API_BASE, session=authenticated_session()) |
| 58 |
api._session._sleep = mock.Mock() |
| 59 |
|
| 60 |
yield api |
| 61 |
api.close() |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture |
| 65 |
def unauthenticated_api(): |
| 66 |
"""An OsmApi without a session, i.e. with no way to authenticate at all. |
| 67 |
|
| 68 |
The `request` method of the session osmapi built for itself is mocked, so |
| 69 |
a test can assert that no request was attempted. Returns an |
| 70 |
`(api, session_mock)` tuple. |
| 71 |
""" |
| 72 |
api = osmapi.OsmApi(api=API_BASE) |
| 73 |
api._session._sleep = mock.Mock() |
| 74 |
api._session._session.request = mock.Mock() |
| 75 |
|
| 76 |
yield api, api._session._session |
| 77 |
api.close() |
| 78 |
|
| 79 |
|
| 80 |
@pytest.fixture |
| 81 |
def changeset_api(auth_api): |
| 82 |
"""An authenticated api with changeset `OPEN_CHANGESET_ID` already open. |
| 83 |
|
| 84 |
Element writes require an open changeset; this skips the changeset/create |
| 85 |
round-trip for tests that are about the element operation itself. |
| 86 |
""" |
| 87 |
auth_api._current_changeset_id = OPEN_CHANGESET_ID |
| 88 |
return auth_api |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture |
| 92 |
def prod_api(): |
| 93 |
api = osmapi.OsmApi(api=PROD_API_BASE, session=authenticated_session()) |
| 94 |
api._session._sleep = mock.Mock() |
| 95 |
|
| 96 |
yield api |
| 97 |
api.close() |
| 98 |
|
| 99 |
|
| 100 |
@pytest.fixture |
| 101 |
def mocked_responses(): |
| 102 |
with responses.RequestsMock() as rsps: |
| 103 |
yield rsps |
| 104 |
|
| 105 |
|
| 106 |
@pytest.fixture |
| 107 |
def add_response(mocked_responses, file_content, request): |
| 108 |
"""Register a mocked response. |
| 109 |
|
| 110 |
`path` is relative to `/api/0.6`; use `url` for endpoints outside of it |
| 111 |
(e.g. `/api/capabilities`) or on another host. With neither, any URL on the |
| 112 |
dev API host matches — prefer naming one so the request URL is asserted too. |
| 113 |
""" |
| 114 |
|
| 115 |
def _add_response( |
| 116 |
method, path=None, filename=None, body=None, status=200, url=None |
| 117 |
): |
| 118 |
if not filename: |
| 119 |
# use testname by default |
| 120 |
filename = f"{request.node.originalname}.xml" |
| 121 |
|
| 122 |
if url is None: |
| 123 |
if path: |
| 124 |
url = f"{API_BASE}/api/0.6{path}" |
| 125 |
else: |
| 126 |
url = re.compile(r"http:\/\/api06\.dev\.openstreetmap\.org.*") |
| 127 |
|
| 128 |
if body is None: |
| 129 |
body = file_content(filename) |
| 130 |
mocked_responses.add(method, url=url, body=body, status=status) |
| 131 |
return mocked_responses |
| 132 |
|
| 133 |
return _add_response |
| 134 |
|
| 135 |
|
| 136 |
@pytest.fixture |
| 137 |
def xml_dict(): |
| 138 |
"""Parse an XML document into a dict, for whitespace-insensitive comparison.""" |
| 139 |
|
| 140 |
def _xml_dict(xml): |
| 141 |
return xmltodict.parse(xml, dict_constructor=dict) |
| 142 |
|
| 143 |
return _xml_dict |
| 144 |
|
| 145 |
|
| 146 |
@pytest.fixture |
| 147 |
def assert_request_xml(xml_dict): |
| 148 |
"""Assert that a request body carries the expected `<osm>` payload. |
| 149 |
|
| 150 |
`payload` is just the inner XML — the prolog and the `<osm>` envelope |
| 151 |
(with the generator string for the running version) are added here so |
| 152 |
tests don't have to repeat them or track the version number. |
| 153 |
""" |
| 154 |
|
| 155 |
def _assert_request_xml(request, payload): |
| 156 |
expected = ( |
| 157 |
'<?xml version="1.0" encoding="UTF-8"?>' |
| 158 |
f'<osm version="0.6" generator="{GENERATOR}">' |
| 159 |
f"{payload}" |
| 160 |
"</osm>" |
| 161 |
) |
| 162 |
assert xml_dict(request.body) == xml_dict(expected) |
| 163 |
|
| 164 |
return _assert_request_xml |
| 165 |
|
| 166 |
|
| 167 |
def make_http_response(status=200, content="test response", reason="test reason"): |
| 168 |
"""Build a minimal stand-in for a `requests` response.""" |
| 169 |
response = mock.Mock() |
| 170 |
response.status_code = status |
| 171 |
response.reason = reason |
| 172 |
response.content = content |
| 173 |
return response |
| 174 |
|
| 175 |
|
| 176 |
@pytest.fixture |
| 177 |
def http_response(): |
| 178 |
"""Factory for fake `requests` responses, for use with `mock_api`.""" |
| 179 |
return make_http_response |
| 180 |
|
| 181 |
|
| 182 |
@pytest.fixture |
| 183 |
def mock_api(): |
| 184 |
"""Factory for an OsmApi backed by a `unittest.mock` HTTP session. |
| 185 |
|
| 186 |
Unlike the `responses`-based fixtures this does not match on URL, so it is |
| 187 |
only for tests about the HTTP layer itself (status-code mapping, retries) |
| 188 |
where the response sequence matters more than the request. Returns a |
| 189 |
`(api, session_mock)` tuple. |
| 190 |
|
| 191 |
Pass `responses=[...]` to return a different response per attempt, or |
| 192 |
`side_effect=...` to raise. |
| 193 |
""" |
| 194 |
created = [] |
| 195 |
|
| 196 |
def _mock_api( |
| 197 |
status=200, |
| 198 |
content="test response", |
| 199 |
reason="test reason", |
| 200 |
auth=True, |
| 201 |
responses=None, |
| 202 |
side_effect=None, |
| 203 |
): |
| 204 |
session = mock.Mock() |
| 205 |
session.close = mock.Mock() |
| 206 |
session.auth = ("testuser", "testpassword") if auth else None |
| 207 |
|
| 208 |
if side_effect is not None: |
| 209 |
session.request = mock.Mock(side_effect=side_effect) |
| 210 |
elif responses is not None: |
| 211 |
session.request = mock.Mock(side_effect=list(responses)) |
| 212 |
else: |
| 213 |
session.request = mock.Mock( |
| 214 |
return_value=make_http_response(status, content, reason) |
| 215 |
) |
| 216 |
|
| 217 |
api = osmapi.OsmApi(api=API_BASE, session=session) |
| 218 |
api._session._sleep = mock.Mock() |
| 219 |
created.append(api) |
| 220 |
return api, session |
| 221 |
|
| 222 |
yield _mock_api |
| 223 |
for api in created: |
| 224 |
api.close() |