Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Add a fixture enabling SRTM login using envvars. #1145

Merged
merged 2 commits into from
Nov 12, 2018
Merged
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
7 changes: 4 additions & 3 deletions lib/cartopy/io/srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,12 @@ def __init__(self,
Downloader.__init__(self, None,
target_path_template,
pre_downloaded_path_template)
warnings.warn('SRTM requires an account set up and log in to access.'
'use of this class is likely to fail with'
' HTTP 401 errors.')

def url(self, format_dict):
warnings.warn('SRTM requires an account set up and log in to access. '
'Use of this Downloader is likely to fail with HTTP 401 '
'errors.')

# override the url method, looking up the url from the
# ``SRTMDownloader._SRTM_LOOKUP_MASK`` array
lat = int(format_dict['y'][1:])
Expand Down
36 changes: 34 additions & 2 deletions lib/cartopy/tests/io/test_srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,40 @@
from .test_downloaders import download_to_temp # noqa: F401 (used as fixture)


pytestmark = [pytest.mark.skip('SRTM login not supported'),
pytest.mark.network]
pytestmark = [pytest.mark.network,
pytest.mark.filterwarnings('ignore:SRTM requires an account'),
pytest.mark.usefixtures('srtm_login_or_skip')]


@pytest.fixture
def srtm_login_or_skip(monkeypatch):
import os
try:
srtm_username = os.environ['SRTM_USERNAME']
except KeyError:
pytest.skip('SRTM_USERNAME environment variable is unset.')
try:
srtm_password = os.environ['SRTM_PASSWORD']
except KeyError:
pytest.skip('SRTM_PASSWORD environment variable is unset.')

from six.moves.urllib.request import (HTTPBasicAuthHandler,
HTTPCookieProcessor,
HTTPPasswordMgrWithDefaultRealm,
build_opener)
from six.moves.http_cookiejar import CookieJar

password_manager = HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None,
"https://urs.earthdata.nasa.gov",
srtm_username,
srtm_password)
cookie_jar = CookieJar()
opener = build_opener(HTTPBasicAuthHandler(password_manager),
HTTPCookieProcessor(cookie_jar))

monkeypatch.setattr(cartopy.io, 'urlopen', opener.open)


class TestRetrieve(object):
Expand Down