Skip to content

Commit

Permalink
NXPY-258: File download from aws S3 with auto-redirect not working in…
Browse files Browse the repository at this point in the history
… case of OAuth (#305)

* NXPY-258: File download from aws S3 with auto-redirect not working in case of OAuth
  • Loading branch information
gitofanindya authored Apr 17, 2024
1 parent dd53b7a commit 8a1fec9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release date: ``2024-xx-xx``
- `NXPY-254 <https://jira.nuxeo.com/browse/NXPY-254>`__: Authorization Error for OAuth
- `NXPY-253 <https://jira.nuxeo.com/browse/NXPY-253>`__: Restore capability to use the client with a local HTTP server
- `NXPY-255 <https://jira.nuxeo.com/browse/NXPY-255>`__: Fix Test cases for moto3
- `NXPY-258 <https://jira.nuxeo.com/browse/NXPY-258>`__: File download from aws S3 with auto-redirect not working in case of OAuth

Technical changes
-----------------
Expand Down
25 changes: 25 additions & 0 deletions nuxeo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,34 @@ def request(
headers=headers,
auth=auth,
data=data,
allow_redirects=False,
verify=ssl_verify,
**kwargs,
)
if resp.status_code in range(301, 309):
try:
redirect_url = resp.headers["Location"]
except Exception:
redirect_url = ""
if "amazonaws.com" in redirect_url:
resp = requests.get(
url=resp.headers["Location"],
headers=resp.headers,
data=data,
allow_redirects=True,
verify=ssl_verify,
**kwargs,
)
else:
resp = self._session.request(
method,
url,
headers=headers,
auth=auth,
data=data,
verify=ssl_verify,
**kwargs,
)
resp.raise_for_status()
except Exception as exc:
if default is object:
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def test_request(server):

import requests
from unittest.mock import patch, Mock
from ..constants import NUXEO_SERVER_URL

flag = True

def _request_no_aws(*args, **kwargs):
nonlocal flag
resp = Mock()
resp.status_code = 302
resp.headers = {"Location": f"{NUXEO_SERVER_URL}/api/v1/drive/configuration"}
if flag:
flag = False
return resp
resp = Mock()
resp.status_code = 200
flag = True
return resp

def _request_aws(*args, **kwargs):
nonlocal flag
resp = Mock()
resp.status_code = 302
resp.headers = {"Location": "amazonaws.com"}
if flag:
flag = False
return resp
resp = Mock()
resp.status_code = 200
flag = True
return resp

with patch.object(requests.sessions.Session, "request", new=_request_no_aws):
resp = server.client.request("GET", "json/cmis")
assert resp.status_code == 200

with patch.object(requests.sessions.Session, "request", new=_request_aws):
resp = server.client.request("GET", "json/cmis")
assert resp.status_code == 200

0 comments on commit 8a1fec9

Please sign in to comment.