Пример #1
0
 def test_config_file_location(self):
     # test the location of the config file when the host is specified
     myhost = 'myhost.ca'
     service = 'myservice'
     resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
     client = Mock(resource_id=resource_id)
     with patch('cadcutils.net.ws.os.makedirs') as makedirs_mock:
         wscap = ws.WsCapabilities(client, host=myhost)
     expected_location = os.path.join(ws.CACHE_LOCATION, 'alt-domains',
                                      myhost)
     makedirs_mock.check_called_with(expected_location)
     self.assertEqual(os.path.join(expected_location, 'resource-caps'),
                      wscap.reg_file)
     self.assertEqual(
         os.path.join(expected_location,
                      urlparse(resource_id).netloc, '.{}'.format(service)),
         wscap.caps_file)
Пример #2
0
    def test_get_caps(self, get_mock, file_modtime_mock):
        """
        Tests the capabilities part of WsCapabilities
        """
        # test when registry information is read from the server
        # (cache is outdated)
        service = 'myservice'
        resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
        resource_cap_url = 'www.canfar.net/myservice'
        # set the modified time of the cache file to 0 to make sure the
        # info is retrieved from server
        file_modtime_mock.return_value = 0
        # test anonymous access
        expected_content = capabilities__content.replace(
            'WS_URL', resource_cap_url)
        response = Mock(text=expected_content)
        get_mock.return_value = response
        caps = ws.WsCapabilities(
            Mock(resource_id=resource_id, subject=auth.Subject()))

        # mock _get_capability_url to return some url without attempting
        # to access the server
        def get_url():
            return 'http://some.url/capabilities'

        caps._get_capability_url = get_url
        # remove the cached file if exists
        if os.path.isfile(caps.caps_file):
            os.remove(caps.caps_file)
        caps.caps_urls[service] = '{}/capabilities'.format(resource_cap_url)
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'http://{}/pub'.format(resource_cap_url),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))
        actual_content = open(caps.caps_file, 'r').read()
        self.assertEqual(expected_content, actual_content)

        # mock _get_capability_url to return a subservice
        service = 'myservice/mysubservice'
        resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
        resource_cap_url = 'www.canfar.net/myservice/mysubservice'
        # set the modified time of the cache file to 0 to make sure the
        # info is retrieved from server
        file_modtime_mock.return_value = 0
        expected_content = capabilities__content.replace(
            'WS_URL', resource_cap_url)
        # test anonymous access
        response = Mock(text=expected_content)
        get_mock.return_value = response
        caps = ws.WsCapabilities(
            Mock(resource_id=resource_id, subject=auth.Subject()))
        # remove the cached file if exists
        if os.path.isfile(caps.caps_file):
            os.remove(caps.caps_file)
        caps._get_capability_url = get_url
        caps.caps_urls[service] = '{}/capabilities'.format(resource_cap_url)
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'http://{}/pub'.format(resource_cap_url),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))
        actual_content = open(caps.caps_file, 'r').read()
        self.assertEqual(expected_content, actual_content)

        # repeat for basic auth subject. Mock the netrc library to
        # prevent a lookup for $HOME/.netrc
        with patch('cadcutils.net.auth.netrclib'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(netrc=True))
        client.get.return_value = response
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # capabilities works even if it has only one anonymous interface
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        # same for availability
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))

        # repeat for https
        with patch('os.path.isfile'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(certificate='somecert.pem'))
        client.get.return_value = response
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # capabilities works even if it has only one anonymous interface
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        # same for availability
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'https://{}'.format(resource_cap_url),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))

        # test when capabilities information is retrieved from the cache file
        get_mock.reset_mock()
        get_mock.return_value = None
        file_modtime_mock.reset_mock()
        service = 'myservice2'
        resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
        resource_cap_url2 = 'canfar.phys.uvic.ca/myservice2'
        expected_content = capabilities__content.replace(
            'WS_URL', resource_cap_url2)
        file_modtime_mock.return_value = time.time()
        client = Mock(resource_id=resource_cap_url2, subject=auth.Subject())
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        caps.caps_urls[service] = '{}/capabilities'.format(resource_cap_url2)
        # manually write the content
        with open(caps.caps_file, 'w') as f:
            f.write(expected_content)
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'http://{}/pub'.format(resource_cap_url2),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))
        actual_content = open(caps.caps_file, 'r').read()
        self.assertEqual(expected_content, actual_content)

        # repeat for basic auth subject. Mock the netrc library to prevent a
        # lookup for $HOME/.netrc
        with patch('cadcutils.net.auth.netrclib'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(netrc=True))
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # does not work with user-password of the subject set above
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'http://{}/auth'.format(resource_cap_url2),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))

        # repeat for https
        with patch('os.path.isfile'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(certificate='somecert.pem'))
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # does not work with user-password of the subject set above
        self.assertEqual(
            'http://{}/capabilities'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual(
            'http://{}/availability'.format(resource_cap_url2),
            caps.get_access_url('ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual(
            'https://{}'.format(resource_cap_url2),
            caps.get_access_url('vos://cadc.nrc.ca~service/CADC/mystnd01'))
Пример #3
0
    def test_get_reg(self, get_mock, file_mock, file_modtime_mock):
        """
        Tests the registry part of WsCapabilities
        """
        # test when registry information is read from the server
        # (cache is outdated)
        service = 'myservice'
        resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
        resource_cap_url = 'http://www.canfar.net/myservice'
        cadcreg_content = ('#test content\n {} = {} \n'
                           'ivo://some.provider/service = '
                           'http://providerurl.test/service'). \
            format(resource_id, resource_cap_url)
        response = Mock(text=cadcreg_content)
        get_mock.return_value = response
        # set the modified time of the cache file to 0 to make sure the info
        # is retrieved from server
        file_modtime_mock.return_value = 0
        # test anonymous access
        fh_mock = Mock()
        file_mock.write = fh_mock
        client = Mock(resource_id=resource_id)
        caps = ws.WsCapabilities(client)
        self.assertEqual(os.path.join(ws.CACHE_LOCATION, ws.REGISTRY_FILE),
                         caps.reg_file)
        self.assertEqual(
            os.path.join(ws.CACHE_LOCATION, 'canfar.phys.uvic.ca',
                         '.{}'.format(service)), caps.caps_file)
        self.assertEqual(resource_cap_url, caps._get_capability_url())
        file_mock.assert_called_once_with(
            os.path.join(ws.CACHE_LOCATION, ws.REGISTRY_FILE), 'w')
        # TODO not sure why need to access write this way
        file_mock().__enter__.return_value.write.assert_called_once_with(
            cadcreg_content)

        # test when registry information is retrieved from the cache file
        get_mock.reset_mock()
        get_mock.return_value = None
        file_modtime_mock.reset_mock()
        file_mock.reset_mock()
        resource_cap_url2 = 'http://www.canfar.net/myservice2'
        cache_content2 = ('#test content\n {} = {} \n'
                          'ivo://some.provider/service = '
                          'http://providerurl.test/service'). \
            format(resource_id, resource_cap_url2)
        file_modtime_mock.return_value = time.time()
        file_mock().__enter__.return_value.read.return_value = cache_content2
        caps = ws.WsCapabilities(client)
        self.assertEqual(resource_cap_url2, caps._get_capability_url())

        # test when registry information is outdated but there are
        # errors retrieving it from the CADC registry
        # so in the end go back and use the cache version
        file_modtime_mock.reset_mock()
        file_mock.reset_mock()
        file_modtime_mock.return_value = 0
        file_mock().__enter__.return_value.read.return_value = cache_content2
        get_mock.side_effect = [exceptions.HttpException()]
        client.get.side_effect = [exceptions.HttpException]
        caps = ws.WsCapabilities(client)
        self.assertEqual(resource_cap_url2, caps._get_capability_url())
Пример #4
0
    def test_get_caps(self, get_mock, file_mock, file_modtime_mock):
        """
        Tests the capabilities part of WsCapabilities
        """
        # test when registry information is read from the server
        # (cache is outdated)
        service = 'myservice'
        resource_id = 'ivo://canfar.phys.uvic.ca/{}'.format(service)
        resource_cap_url = 'www.canfar.net/myservice'
        # set the modified time of the cache file to 0 to make sure the
        # info is retrieved from server
        file_modtime_mock.return_value = 0
        # test anonymous access
        fh_mock = Mock()
        file_mock.write = fh_mock
        response = Mock(
            text=capabilities__content.replace('WS_URL', resource_cap_url))
        get_mock.return_value = response
        caps = ws.WsCapabilities(Mock(resource_id=resource_id,
                                      subject=auth.Subject()))

        # mock _get_capability_url to return some url without attempting
        # to access the server
        def get_url():
            return 'http://some.url/capabilities'

        caps._get_capability_url = get_url
        caps.caps_urls[service] = '{}/capabilities'.format(resource_cap_url)
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual('http://{}/availability'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual('http://{}/pub'.format(resource_cap_url),
                         caps.get_access_url(
                             'vos://cadc.nrc.ca~service/CADC/mystnd01'))
        resource_url = urlparse(resource_id)
        file_mock.assert_called_once_with(os.path.join(ws.CACHE_LOCATION,
                                                       resource_url.netloc,
                                                       resource_url.path.strip(
                                                           '/')), 'w')
        # TODO not sure why need to access write this way
        file_mock().__enter__.return_value.write.assert_called_once_with(
            capabilities__content.replace('WS_URL', resource_cap_url))

        # repeat for basic auth subject. Mock the netrc library to
        # prevent a lookup for $HOME/.netrc
        with patch('cadcutils.net.auth.netrclib'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(netrc=True))
        client.get.return_value = response
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # capabilities works even if it has only one anonymous interface
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        # same for availability
        self.assertEqual('http://{}/availability'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))

        # repeat for https
        with patch('os.path.isfile'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(certificate='somecert.pem'))
        client.get.return_value = response
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # capabilities works even if it has only one anonymous interface
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        # same for availability
        self.assertEqual('http://{}/availability'.format(resource_cap_url),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual('https://{}'.format(resource_cap_url),
                         caps.get_access_url(
                             'vos://cadc.nrc.ca~service/CADC/mystnd01'))

        # test when capabilities information is retrieved from the cache file
        get_mock.reset_mock()
        get_mock.return_value = None
        file_modtime_mock.reset_mock()
        file_mock.reset_mock()
        resource_cap_url2 = 'http://www.canfar.net/myservice2'
        cache_content2 = capabilities__content.replace('WS_URL',
                                                       resource_cap_url2)
        file_modtime_mock.return_value = time.time()
        file_mock().__enter__.return_value.read.return_value = cache_content2
        client = Mock(resource_id=resource_id, subject=auth.Subject())
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        caps.caps_urls[service] = '{}/capabilities'.format(resource_cap_url2)
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual('http://{}/availability'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual('http://{}/pub'.format(resource_cap_url2),
                         caps.get_access_url(
                             'vos://cadc.nrc.ca~service/CADC/mystnd01'))
        resource_url = urlparse(resource_id)
        file_mock().__enter__.return_value.read.return_value = \
            capabilities__content.replace('WS_URL', resource_cap_url2)

        # repeat for basic auth subject. Mock the netrc library to prevent a
        # lookup for $HOME/.netrc
        with patch('cadcutils.net.auth.netrclib'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(netrc=True))
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # does not work with user-password of the subject set above
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual('http://{}/availability'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual('http://{}/auth'.format(resource_cap_url2),
                         caps.get_access_url(
                             'vos://cadc.nrc.ca~service/CADC/mystnd01'))

        # repeat for https
        with patch('os.path.isfile'):
            client = Mock(resource_id=resource_id,
                          subject=auth.Subject(certificate='somecert.pem'))
        caps = ws.WsCapabilities(client)
        caps._get_capability_url = get_url
        # does not work with user-password of the subject set above
        self.assertEqual('http://{}/capabilities'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#capabilities'))
        self.assertEqual('http://{}/availability'.format(resource_cap_url2),
                         caps.get_access_url(
                             'ivo://ivoa.net/std/VOSI#availability'))
        self.assertEqual('https://{}'.format(resource_cap_url2),
                         caps.get_access_url(
                             'vos://cadc.nrc.ca~service/CADC/mystnd01'))