Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions osmapi/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
)
Comment thread
gy-mate marked this conversation as resolved.
Comment thread
gy-mate marked this conversation as resolved.

try:
response = self._session.request(
Expand Down
37 changes: 33 additions & 4 deletions tests/changeset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -707,10 +709,33 @@ def test_changeset_comment(auth_api, add_response):
}


def test_changeset_comment_with_session_authorization_header(add_response):
Comment thread
gy-mate marked this conversation as resolved.
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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"
)
5 changes: 3 additions & 2 deletions tests/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_node_create_existing_node(self):

def test_node_create_wo_auth(self):
self._session_mock()

# setup mock
Comment thread
gy-mate marked this conversation as resolved.
self.api.changeset_create = mock.Mock(return_value=1111)
self.api._current_changeset_id = 1111
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions tests/osmapi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down