diff --git a/osmapi/http.py b/osmapi/http.py index 9c2bd80..d368401 100644 --- a/osmapi/http.py +++ b/osmapi/http.py @@ -68,7 +68,8 @@ def _http_request( # noqa: C901 `return_value` indicates wheter this request should return any data or not. - If the username or password is missing, + If the username or password is missing + and no Authorization header is present, `OsmApi.UsernamePasswordMissingError` is raised. If the requested element has been deleted, @@ -85,8 +86,10 @@ def _http_request( # noqa: C901 # Add API base URL to path path = self._api + path - if auth and not self._auth: - raise errors.UsernamePasswordMissingError("Username/Password missing") + if auth and not (self._auth or self._session.headers.get("Authorization")): + raise errors.UsernamePasswordMissingError( + "No username/password or 'Authorization' header provided" + ) try: response = self._session.request( diff --git a/tests/changeset_test.py b/tests/changeset_test.py index 67125ad..1553781 100644 --- a/tests/changeset_test.py +++ b/tests/changeset_test.py @@ -505,7 +505,9 @@ def test_changeset_upload_no_auth(api): with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo: api.changeset_upload(changesdata) - assert str(execinfo.value) == "Username/Password missing" + assert ( + str(execinfo.value) == "No username/password or 'Authorization' header provided" + ) def test_changeset_download(api, add_response): @@ -707,10 +709,33 @@ def test_changeset_comment(auth_api, add_response): } +def test_changeset_comment_with_session_authorization_header(add_response): + resp = add_response( + POST, + "/changeset/123/comment", + filename="test_ChangesetComment.xml", + ) + + session = requests.Session() + session.headers.update({"Authorization": "Bearer test-token"}) + api = osmapi.OsmApi(api="http://api06.dev.openstreetmap.org", session=session) + + try: + result = api.ChangesetComment(123, comment="test comment") + finally: + api.close() + + assert len(resp.calls) == 1 + assert resp.calls[0].request.body == "text=test+comment" + assert result["id"] == 123 + + def test_changeset_comment_no_auth(api): with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo: api.changeset_comment(123, comment="test comment") - assert str(execinfo.value) == "Username/Password missing" + assert ( + str(execinfo.value) == "No username/password or 'Authorization' header provided" + ) def test_changeset_subscribe(auth_api, add_response): @@ -763,7 +788,9 @@ def test_changeset_subscribe_when_already_subscribed(auth_api, add_response): def test_changeset_subscribe_no_auth(api): with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo: api.changeset_subscribe(45627) - assert str(execinfo.value) == "Username/Password missing" + assert ( + str(execinfo.value) == "No username/password or 'Authorization' header provided" + ) def test_changeset_unsubscribe(auth_api, add_response): @@ -803,4 +830,6 @@ def test_changeset_unsubscribe_when_not_subscribed(auth_api, add_response): def test_changeset_unsubscribe_no_auth(api): with pytest.raises(osmapi.UsernamePasswordMissingError) as execinfo: api.changeset_unsubscribe(45627) - assert str(execinfo.value) == "Username/Password missing" + assert ( + str(execinfo.value) == "No username/password or 'Authorization' header provided" + ) diff --git a/tests/node_test.py b/tests/node_test.py index 8226ac0..d8f486b 100644 --- a/tests/node_test.py +++ b/tests/node_test.py @@ -120,7 +120,7 @@ def test_node_create_existing_node(self): def test_node_create_wo_auth(self): self._session_mock() - + # setup mock self.api.changeset_create = mock.Mock(return_value=1111) self.api._current_changeset_id = 1111 @@ -131,7 +131,8 @@ def test_node_create_wo_auth(self): } with self.assertRaisesRegex( - osmapi.UsernamePasswordMissingError, "Username/Password missing" + osmapi.UsernamePasswordMissingError, + "No username/password or 'Authorization' header provided", ): self.api.node_create(test_node) diff --git a/tests/osmapi_test.py b/tests/osmapi_test.py index 0ff6bff..a4ffdc2 100644 --- a/tests/osmapi_test.py +++ b/tests/osmapi_test.py @@ -32,6 +32,7 @@ def _session_mock(self, auth=False, filenames=None, status=200): self.session_mock = mock.Mock() self.session_mock.request = mock.Mock(return_value=response_mock) self.session_mock.auth = None + self.session_mock.headers = {"Authorization": None} if auth: self.api = OsmApi(