|
12 | 12 | from astropy.io import fits
|
13 | 13 |
|
14 | 14 | import astropy.units as u
|
| 15 | +from requests import HTTPError, Response |
15 | 16 |
|
16 | 17 | from astroquery.mast.services import _json_to_table
|
17 | 18 | from astroquery.utils.mocks import MockResponse
|
@@ -147,6 +148,11 @@ def request_mockreturn(url, params={}):
|
147 | 148 |
|
148 | 149 |
|
149 | 150 | def download_mockreturn(*args, **kwargs):
|
| 151 | + if 'unauthorized' in args[0]: |
| 152 | + response = Response() |
| 153 | + response.reason = 'Unauthorized' |
| 154 | + response.status_code = 401 |
| 155 | + raise HTTPError(response=response) |
150 | 156 | return ('COMPLETE', None, None)
|
151 | 157 |
|
152 | 158 |
|
@@ -376,6 +382,30 @@ def test_missions_download_products(patch_post, tmp_path):
|
376 | 382 | extension='jpg',
|
377 | 383 | download_dir=tmp_path)
|
378 | 384 |
|
| 385 | + |
| 386 | +def test_missions_download_no_auth(patch_post, caplog): |
| 387 | + # Exclusive access products should not be downloaded if user is not authenticated |
| 388 | + # User is not authenticated |
| 389 | + uri = 'unauthorized.fits' |
| 390 | + result = mast.MastMissions.download_file(uri) |
| 391 | + assert result[0] == 'ERROR' |
| 392 | + assert 'HTTPError' in result[1] |
| 393 | + with caplog.at_level('WARNING', logger='astroquery'): |
| 394 | + assert 'You are not authorized to download' in caplog.text |
| 395 | + assert 'Please authenticate yourself' in caplog.text |
| 396 | + caplog.clear() |
| 397 | + |
| 398 | + # User is authenticated, but doesn't have proper permissions |
| 399 | + test_token = "56a9cf3df4c04052atest43feb87f282" |
| 400 | + mast.MastMissions.login(token=test_token) |
| 401 | + result = mast.MastMissions.download_file(uri) |
| 402 | + assert result[0] == 'ERROR' |
| 403 | + assert 'HTTPError' in result[1] |
| 404 | + with caplog.at_level('WARNING', logger='astroquery'): |
| 405 | + assert 'You are not authorized to download' in caplog.text |
| 406 | + assert 'Please check your authentication token' in caplog.text |
| 407 | + |
| 408 | + |
379 | 409 | ###################
|
380 | 410 | # MastClass tests #
|
381 | 411 | ###################
|
|
0 commit comments