def test_service_params(self): service = Service(url='http://nowhere/wps', name="test_wps", purl='http://myservice/wps') assert service.params == { 'name': 'test_wps', 'public': False, 'auth': 'token', 'type': 'WPS', 'url': 'http://nowhere/wps', 'verify': True, 'purl': 'http://myservice/wps' } assert service.has_purl() is True
def fetch_by_url(self, url): """ Gets service for given ``url`` from mongodb storage. """ service = self.collection.find_one({'url': url}) if not service: raise ServiceNotFound return Service(service)
def fetch_by_name(self, name): """ Gets service for given ``name`` from mongodb storage. """ service = self.collection.find_one({'name': name}) if not service: raise ServiceNotFound return Service(service)
def list_services(self): """ Lists all services in mongodb storage. """ my_services = [] for service in self.collection.find().sort('name', pymongo.ASCENDING): my_services.append(Service(service)) return my_services
def fetch_by_name(self, name): """ Get service for given ``name`` from memory storage. """ service = self.name_index.get(name) if not service: raise ServiceNotFound return Service(service)
def list_services(self): """ Lists all services in memory storage. """ my_services = [] for service in list(self.name_index.values()): my_services.append(Service(service)) return my_services
def test_save_service_public(self): collection_mock = mock.Mock(spec=["insert_one", "find_one", "count_documents"]) collection_mock.count_documents.return_value = 0 collection_mock.find_one.return_value = self.service_public store = MongodbServiceStore(collection=collection_mock) store.save_service(Service(self.service_public)) collection_mock.insert_one.assert_called_with(self.service_public)
def register_service(self, url, data=None, overwrite=True): """ Implementation of :meth:`twitcher.api.IRegistry.register_service`. """ data = data or {} args = dict(data) args['url'] = url service = Service(**args) service = self.store.save_service(service, overwrite=overwrite) return service.params
def test_save_service_with_special_name(self): collection_mock = mock.Mock(spec=["insert_one", "find_one", "count_documents"]) collection_mock.count_documents.return_value = 0 collection_mock.find_one.return_value = self.service_special store = MongodbServiceStore(collection=collection_mock) store.save_service(Service(self.service_special)) collection_mock.insert_one.assert_called_with({ 'url': 'http://wonderload', 'type': 'wps', 'name': 'a_special_name', 'public': False, 'auth': 'token', 'verify': False, 'purl': "http://purl/wps"})
def setUp(self): self.access_token = AccessToken(token="cdefg", expires_at=expires_at(hours=1)) self.tokenstore = MemoryTokenStore() self.tokenstore.save_token(self.access_token) self.empty_tokenstore = MemoryTokenStore() self.service = Service(url='http://nowhere/wps', name='test_wps', public=False) self.servicestore = MemoryServiceStore() self.servicestore.save_service(self.service) self.security = OWSSecurity(tokenstore=self.tokenstore, servicestore=self.servicestore)
def test_check_request_public_access(self): servicestore = MemoryServiceStore() servicestore.save_service( Service(url='http://nowhere/wps', name='test_wps', public=True)) security = OWSSecurity(tokenstore=self.tokenstore, servicestore=servicestore) params = dict(request="Execute", service="WPS", version="1.0.0", token="cdefg") request = DummyRequest(params=params, path='/ows/proxy/emu') request.registry = Registry() request.registry.settings = { 'twitcher.ows_prox_protected_path': '/ows' } security.check_request(request)
def fetch_by_name(self, name, visibility=None, request=None): # noqa: F811 """ Gets service for given ``name`` from magpie. """ session = self.session_factory() try: service = MagpieService.by_service_name(name, db_session=session) if service is None: raise ServiceNotFound("Service name not found.") return Service(url=service.url, name=service.resource_name, type=service.type, verify=self.twitcher_ssl_verify) finally: session.close()
def check_request(self, request): protected_path = request.registry.settings.get('twitcher.ows_proxy_protected_path ', '/ows') if request.path.startswith(protected_path): # TODO: refactor this code try: service_name = parse_service_name(request.path, protected_path) service = self.servicestore.fetch_by_name(service_name) if service.public is True: LOGGER.warn('public access for service %s', service_name) except ServiceNotFound: # TODO: why not raising an exception? service = Service(url='unregistered', public=False, auth='token') LOGGER.warn("Service not registered.") ows_request = OWSRequest(request) if not ows_request.service_allowed(): raise OWSInvalidParameterValue( "service %s not supported" % ows_request.service, value="service") if not ows_request.public_access(): self.verify_access(request, service)
def list_services(self, request=None): # noqa: F811 """ Lists all services registered in magpie. """ # obtain admin access since 'service_url' is only provided on admin routes services = [] path = "{}{}".format(self.magpie_url, ServicesAPI.path) resp = requests.get(path, cookies=self.magpie_admin_token, headers={"Accept": CONTENT_TYPE_JSON}, verify=self.twitcher_ssl_verify) if resp.status_code != HTTPOk.code: raise resp.raise_for_status() json_body = resp.json() for service_type in json_body["services"]: for service in json_body["services"][service_type].values(): services.append( Service(url=service["service_url"], name=service["service_name"], type=service["service_type"])) return services
def save_service(self, service, overwrite=True): """ Store an OWS service in database. """ name = namesgenerator.get_sane_name(service.name) if not name: name = namesgenerator.get_random_name() if name in self.name_index: name = namesgenerator.get_random_name(retry=True) # check if service is already registered if name in self.name_index: if overwrite: self._delete(name=name) else: raise Exception("service name already registered.") self._insert( Service(name=name, url=baseurl(service.url), type=service.type, purl=service.purl, public=service.public, auth=service.auth, verify=service.verify)) return self.fetch_by_name(name=name)
def save_service(self, service, overwrite=True): """ Stores an OWS service in mongodb. """ name = namesgenerator.get_sane_name(service.name) if not name: name = namesgenerator.get_random_name() if self.collection.count_documents({'name': name}) > 0: name = namesgenerator.get_random_name(retry=True) # check if service is already registered if self.collection.count_documents({'name': name}) > 0: if overwrite: self.collection.delete_one({'name': name}) else: raise Exception("service name already registered.") self.collection.insert_one( Service(name=name, url=baseurl(service.url), type=service.type, purl=service.purl, public=service.public, auth=service.auth, verify=service.verify)) return self.fetch_by_name(name=name)
def fetch_by_url(self, url): for service in list(self.name_index.values()): if service.url == url: return Service(service) raise ServiceNotFound
def test_service_with_name(self): service = Service(url='http://nowhere/wps', name="test_wps") assert service.url == 'http://nowhere/wps' assert service.name == 'test_wps' assert service.has_purl() is False
def test_missing_url(self): with pytest.raises(TypeError): Service()
def test_service_with_url_only(self): service = Service(url='http://nowhere/wps') assert service.url == 'http://nowhere/wps' assert service.name == 'unknown' assert service.has_purl() is False