Skip to content

Commit 2efa9e9

Browse files
authored
fix(plugins): FileNotFoundError on usgs auth attempt failure (#1550)
1 parent 643cb3c commit 2efa9e9

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

eodag/plugins/apis/usgs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def authenticate(self) -> None:
128128
api.logout()
129129
continue
130130
except USGSError as e:
131-
if i == 0:
131+
if i == 0 and os.path.isfile(api.TMPFILE):
132132
# `.usgs` API file key might be obsolete
133133
# Remove it and try again
134134
os.remove(api.TMPFILE)

tests/units/test_apis_plugins.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,21 +485,28 @@ def test_plugins_apis_usgs_authenticate(self, mock_api_logout, mock_api_login):
485485
USGSError("USGS error"),
486486
None,
487487
]
488-
with mock.patch("os.remove", autospec=True) as mock_os_remove:
488+
with (
489+
mock.patch("os.remove", autospec=True) as mock_os_remove,
490+
mock.patch("os.path.isfile", autospec=True) as mock_isfile,
491+
):
489492
self.api_plugin.authenticate()
490493
self.assertEqual(mock_api_login.call_count, 2)
491494
self.assertEqual(mock_api_logout.call_count, 0)
495+
mock_isfile.assert_called_once_with(USGS_TMPFILE)
492496
mock_os_remove.assert_called_once_with(USGS_TMPFILE)
493497
mock_api_login.reset_mock()
494498
mock_api_logout.reset_mock()
495499

496500
# with invalid credentials / USGSError
497501
mock_api_login.side_effect = USGSError()
498-
with mock.patch("os.remove", autospec=True) as mock_os_remove:
499-
with self.assertRaises(AuthenticationError):
500-
self.api_plugin.authenticate()
501-
self.assertEqual(mock_api_login.call_count, 2)
502-
mock_api_logout.assert_not_called()
502+
with (
503+
mock.patch("os.remove", autospec=True),
504+
mock.patch("os.path.isfile", autospec=True),
505+
self.assertRaises(AuthenticationError),
506+
):
507+
self.api_plugin.authenticate()
508+
self.assertEqual(mock_api_login.call_count, 2)
509+
mock_api_logout.assert_not_called()
503510

504511
@mock.patch("usgs.api.login", autospec=True)
505512
@mock.patch("usgs.api.logout", autospec=True)
@@ -635,7 +642,6 @@ def test_plugins_apis_usgs_download(
635642

636643
@responses.activate
637644
def run():
638-
639645
product = EOProduct(
640646
"peps",
641647
dict(

0 commit comments

Comments
 (0)