def test_auth(): # the Cadc() will cause a remote data call to TAP service capabilities # To avoid this, use an anonymous session and replace it with an # auth session later cadc = Cadc(auth_session=requests.Session()) cadc.cadctap._session = authsession.AuthSession() user = '******' password = '******' cert = 'cert' with pytest.raises(AttributeError): cadc.login(None, None, None) with pytest.raises(AttributeError): cadc.login(user=user) with pytest.raises(AttributeError): cadc.login(password=password) cadc.login(certificate_file=cert) assert cadc.cadctap._session.credentials.get( 'ivo://ivoa.net/sso#tls-with-certificate').cert == cert # reset and try with user password/cookies cadc.cadctap._session = authsession.AuthSession() post_mock = Mock() cookie = 'ABC' mock_resp = Mock() mock_resp.text = cookie post_mock.return_value.cookies = requests.cookies.RequestsCookieJar() post_mock.return_value = mock_resp cadc._request = post_mock cadc.login(user=user, password=password) assert cadc.cadctap._session.credentials.get( 'ivo://ivoa.net/sso#cookie').cookies[cadc_core.CADC_COOKIE_PREFIX] == \ '"{}"'.format(cookie)
def test_get_data_urls(): def get(*args, **kwargs): class CapsResponse: def __init__(self): self.status_code = 200 self.content = b'' def raise_for_status(self): pass return CapsResponse() class Result: pass file1 = Mock() file1.semantics = '#this' file1.access_url = 'https://get.your.data/path' file2 = Mock() file2.semantics = '#aux' file2.access_url = 'https://get.your.data/auxpath' file3 = Mock() file3.semantics = '#preview' file3.access_url = 'https://get.your.data/previewpath' # add the package file that should be filtered out package_file_old = Mock() package_file_old.semantics = 'http://www.opencadc.org/caom2#pkg' package_file = Mock() package_file.semantics = '#package' result = [file1, file2, file3, package_file_old, package_file] with patch('pyvo.dal.adhoc.DatalinkResults.from_result_url') as \ dl_results_mock: dl_results_mock.return_value = result cadc = Cadc() cadc._request = get # mock the request assert [file1.access_url] == \ cadc.get_data_urls({'publisherID': ['ivo://cadc.nrc.ca/foo']}) assert [file1.access_url, file2.access_url, file3.access_url] == \ cadc.get_data_urls({'publisherID': ['ivo://cadc.nrc.ca/foo']}, include_auxiliaries=True) with pytest.raises(AttributeError): cadc.get_data_urls(None) with pytest.raises(AttributeError): cadc.get_data_urls({'noPublisherID': 'test'})
def test_get_data_urls(monkeypatch): monkeypatch.setattr(cadc_core, 'get_access_url', get_access_url_mock) def get(*args, **kwargs): class CapsResponse(object): def __init__(self): self.status_code = 200 self.content = b'' def raise_for_status(self): pass return CapsResponse() class Result(object): pass vot_result = Result() vot_result.array = [{'semantics': b'#this', 'access_url': b'https://get.your.data/path'}, {'semantics': b'#aux', 'access_url': b'https://get.your.data/auxpath'}] monkeypatch.setattr(cadc_core, 'parse_single_table', lambda x: vot_result) dummyTapHandler = DummyTapHandler() cadc = Cadc(tap_plus_handler=dummyTapHandler) cadc._request = get # mock the request assert [vot_result.array[0]['access_url'].decode('ascii')] == \ cadc.get_data_urls({'caomPublisherID': ['ivo://cadc.nrc.ca/foo']}) assert [vot_result.array[0]['access_url'].decode('ascii'), vot_result.array[1]['access_url'].decode('ascii')] == \ cadc.get_data_urls({'caomPublisherID': ['ivo://cadc.nrc.ca/foo']}, include_auxiliaries=True) with pytest.raises(AttributeError): cadc.get_data_urls(None) with pytest.raises(AttributeError): cadc.get_data_urls({'noPublisherID': 'test'})
def test_get_image_list(): def get(*args, **kwargs): class CapsResponse(object): def __init__(self): self.status_code = 200 self.content = b'' def raise_for_status(self): pass return CapsResponse() class Params(object): def __init__(self, **param_dict): self.__dict__.update(param_dict) coords = '08h45m07.5s +54d18m00s' coords_ra = parse_coordinates(coords).fk5.ra.degree coords_dec = parse_coordinates(coords).fk5.dec.degree radius = 0.1*u.deg uri = 'im_an_ID' run_id = 'im_a_RUNID' pos = 'CIRCLE {} {} {}'.format(coords_ra, coords_dec, radius.value) service_def1 = Mock() service_def1.access_url = \ b'https://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/caom2ops/sync' service_def1.input_params = [Params(name='ID', value=uri), Params(name='RUNID', value=run_id)] service_def2 = Mock() service_def2.access_url = \ b'https://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/caom2ops/async' service_def2.input_params = [Params(name='ID', value=uri), Params(name='RUNID', value=run_id)] result = Mock() service_def_list = [service_def1, service_def2] result.bysemantics.return_value = service_def_list with patch('pyvo.dal.adhoc.DatalinkResults.from_result_url') as dl_results_mock: dl_results_mock.return_value = result cadc = Cadc() cadc._request = get # Mock the request url_list = cadc.get_image_list( {'publisherID': ['ivo://cadc.nrc.ca/foo']}, coords, radius) assert len(url_list) == 1 params = parse_qs(urlsplit(url_list[0]).query) assert params['ID'][0] == uri assert params['RUNID'][0] == run_id assert params['POS'][0] == pos with pytest.raises(TypeError): cadc.get_image_list(None) with pytest.raises(AttributeError): cadc.get_image_list(None, coords, radius) with pytest.raises(TypeError): cadc.get_image_list({'publisherID': [ 'ivo://cadc.nrc.ca/foo']}, coords, 0.1)