Skip to content

Commit

Permalink
Catch 403 Error for Artifactory backend paths (#27)
Browse files Browse the repository at this point in the history
* DEBUG dohq-artifactory

* Add tests

* Except requests.exceptions.HTTPError
  • Loading branch information
hagenw authored Sep 28, 2021
1 parent 998e871 commit 1c66829
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
25 changes: 22 additions & 3 deletions audbackend/core/artifactory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import requests
import typing

import audfactory
Expand Down Expand Up @@ -53,9 +54,12 @@ def _exists(
) -> bool:
r"""Check if file exists on backend."""
try:
# Can lead to RuntimeError: 404 page not found
# Can lead to
# RuntimeError: 404 page not found
# or
# requests.exceptions.HTTPError: 403 Client Error
return audfactory.path(path).exists()
except RuntimeError: # pragma: nocover
except self._non_existing_path_error: # pragma: nocover
return False

def _get_file(
Expand Down Expand Up @@ -84,7 +88,7 @@ def _glob(
path = audfactory.path(url)
try:
result = [str(x) for x in path.glob(pattern)]
except RuntimeError: # pragma: no cover
except self._non_existing_path_error: # pragma: nocover
result = []
return result

Expand Down Expand Up @@ -123,3 +127,18 @@ def _versions(
r"""Versions of a file."""
group_id = audfactory.path_to_group_id(folder)
return audfactory.versions(self.host, self.repository, group_id, name)

_non_existing_path_error = (RuntimeError, requests.exceptions.HTTPError)
r"""Error expected for non-existing paths.
If a user has no permission to a given path
or the path does not exists :func:`audfactory.path`
might return a
``RuntimeError: 404 page not found``
or
``requests.exceptions.HTTPError: 403 Client Error``
error,
which might depend on the instaleld ``dohq-artifactory``
version. So we better catch both of them.
"""
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ packages = find:
install_requires =
audeer >=1.12.0
audfactory >=1.0.3
dohq-artifactory ==0.7.742
setup_requires =
setuptools_scm

Expand Down
38 changes: 38 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,36 @@ def test_errors(tmpdir, backend):
)


@pytest.mark.parametrize(
'backend',
[
audbackend.FileSystem(
pytest.FILE_SYSTEM_HOST,
pytest.REPOSITORY_NAME,
),
audbackend.Artifactory(
pytest.ARTIFACTORY_HOST,
pytest.REPOSITORY_NAME,
),
]
)
def test_exists(tmpdir, backend):
file = 'test.txt'
version = '1.0.0'
local_file = os.path.join(tmpdir, file)
audeer.mkdir(os.path.dirname(local_file))
with open(local_file, 'w'):
pass
remote_file = backend.join(
pytest.ID,
'test_exists',
file,
)
backend.put_file(local_file, remote_file, version)
assert backend.exists(remote_file, version)
assert not backend.exists('non-existing-file.txt', version)


@pytest.mark.parametrize(
'local_file, remote_file, version, ext',
[
Expand Down Expand Up @@ -277,6 +307,14 @@ def test_file(tmpdir, local_file, remote_file, version, ext, backend):
f'{pytest.ID}/test_glob/path/to',
['path/to/file.ext'],
),
# Test nion-existing path on server
(
[],
f'{pytest.ID}/test_non-existing-path/**/*.ext',
None,
[],
),
],
)
@pytest.mark.parametrize(
Expand Down

0 comments on commit 1c66829

Please sign in to comment.