Esempio n. 1
0
def test_model():
  ns = Namespace( name=None, version='0.0', root_path='/api/', converter=None )

  model1 = Model( name='model1', transaction_class=TestTransaction, field_list=[] )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( model1.describe( ns.converter ).data ) == { 'name': 'model1', 'path': None, 'fields': [], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }
  ns.addElement( model1 )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [ '/api/model1' ], 'multi-uri-max': 100 }
  assert sort_dsc( model1.describe( model1.parent.converter ).data ) == { 'name': 'model1', 'path': '/api/model1', 'fields': [], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }

  field_list = []
  field_list.append( Field( name='field1', type='String', length=50 ) )
  field_list.append( Field( name='field2', type='Integer', mode='RO' ) )

  model2 = Model( name='model2', transaction_class=TestTransaction, field_list=field_list )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [ '/api/model1' ], 'multi-uri-max': 100 }
  assert sort_dsc( model1.describe( model1.parent.converter ).data ) == { 'name': 'model1', 'path': '/api/model1', 'fields': [], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }
  assert sort_dsc( model2.describe( ns.converter ).data ) == { 'name': 'model2', 'path': None, 'fields': [ { 'type': 'String', 'length': 50, 'name': 'field1', 'mode': 'RW', 'required': True, }, { 'type': 'Integer', 'name': 'field2', 'mode': 'RO', 'required': True } ], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }
  ns.addElement( model2 )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [ '/api/model1', '/api/model2' ], 'multi-uri-max': 100 }
  assert sort_dsc( model1.describe( model1.parent.converter ).data ) == { 'name': 'model1', 'path': '/api/model1', 'fields': [], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }
  assert sort_dsc( model2.describe( model2.parent.converter ).data ) == { 'name': 'model2', 'path': '/api/model2', 'fields': [ { 'type': 'String', 'length': 50, 'name': 'field1', 'mode': 'RW', 'required': True }, { 'type': 'Integer', 'name': 'field2', 'mode': 'RO', 'required': True } ], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] }

  assert model2.describe( model2.parent.converter ).header_map == { 'Cache-Control': 'max-age=0', 'Verb': 'DESCRIBE', 'Type': 'Model' }

  assert model2.options().header_map == { 'Allow': 'OPTIONS, DESCRIBE, GET, LIST, CREATE, UPDATE, DELETE' }
  assert model2.options().data is None
Esempio n. 2
0
def test_werkzeug_server():
    server = WerkzeugServer(root_path='/api/',
                            root_version='0.0',
                            debug=True,
                            get_user=getUser)
    ns = Namespace(name='ns1', version='0.1', converter=None)
    ns.addElement(Model(name='model1', field_list=[], transaction_class=None))
    server.registerNamespace('/', ns)

    env = {
        'PATH_INFO': '/api/',
        'HTTP_CINP_VERSION': '1.0',
        'REQUEST_METHOD': 'DESCRIBE',
        'wsgi.url_scheme': 'http',
        'wsgi.input_terminated': True,
        'wsgi.input': FakeBody('')
    }
    wresp = server.handle(env)
    assert wresp.status_code == 200
    assert wresp.headers == Headers([
        ('Cache-Control', 'max-age=0'), ('Cinp-Version', '1.0'),
        ('Content-Type', 'application/json;charset=utf-8'),
        ('Content-Length', '120'), ('Verb', 'DESCRIBE'), ('Type', 'Namespace')
    ])
    assert json.loads(str(wresp.data, 'utf-8')) == {
        'multi-uri-max': 100,
        'api-version': '0.0',
        'path': '/api/',
        'namespaces': ['/api/ns1/'],
        'models': [],
        'name': 'root'
    }
Esempio n. 3
0
  def getNamespace( self, uri ):
    namespace = Namespace( name=self.name, version=self.version, doc=self.doc, converter=NullConverter( uri ) )
    namespace.checkAuth = checkAuth_true
    for model in self.model_list:
      check_auth = self.check_auth_map.get( model.name, None )
      if check_auth is None:
        check_auth = checkAuth_false

      namespace.addElement( model )
      model.checkAuth = check_auth
      for action in self.action_map.get( model.name, [] ):
        action.checkAuth = eval( 'lambda user, verb, id_list: check_auth( user, verb, id_list, "{0}" )'.format( action.name ), { 'check_auth': check_auth } )  # TODO: eval ew, find a better way
        model.addAction( action )

    return namespace
Esempio n. 4
0
def test_user():
  server = Server( root_path='/api/', root_version='0.0', debug=True )
  server.getUser = getUser
  ns1 = Namespace( name='ns1', version='0.1', converter=None )
  ns1.checkAuth = checkAuth
  field_list = []
  model1 = Model( name='model1', field_list=field_list, transaction_class=TestTransaction )
  model1.checkAuth = checkAuth
  action1 = Action( name='act', return_paramater=Paramater( type='String' ), func=fake_func )
  action1.checkAuth = checkAuth
  model1.addAction( action1 )
  ns1.addElement( model1 )
  server.registerNamespace( '/', ns1 )

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'nope', 'AUTH-TOKEN': 'nope' } )
  res = server.handle( req )
  assert res.http_code == 401

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'good', 'AUTH-TOKEN': 'nope' } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'nope', 'AUTH-TOKEN': 'bad' } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'super', 'AUTH-TOKEN': 'super' } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'GET', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'me', 'AUTH-TOKEN': 'me' } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'GET', '/api/ns1/model1:me:', { 'CINP-VERSION': __CINP_VERSION__, 'AUTH-ID': 'me', 'AUTH-TOKEN': 'me' } )
  res = server.handle( req )
  assert res.http_code == 200
Esempio n. 5
0
def test_action():
  ns = Namespace( name=None, version='0.0', root_path='/api/', converter=None )
  model = Model( name='model1', transaction_class=TestTransaction, field_list=[] )
  ns.addElement( model )

  action1 = Action( name='act1', return_paramater=Paramater( type='String' ), func=fake_func )
  assert sort_dsc( action1.describe( ns.converter ).data ) == { 'name': 'act1', 'return-type': { 'type': 'String' }, 'paramaters': [], 'static': True, 'path': None }
  model.addAction( action1 )
  assert sort_dsc( action1.describe( action1.parent.parent.converter ).data ) == { 'name': 'act1', 'return-type': { 'type': 'String' }, 'paramaters': [], 'static': True, 'path': '/api/model1(act1)' }

  action2 = Action( name='act2', return_paramater=Paramater( type='Integer' ), static=False, func=fake_func )
  assert sort_dsc( action2.describe( ns.converter ).data ) == { 'name': 'act2', 'return-type': { 'type': 'Integer' }, 'paramaters': [], 'static': False, 'path': None }

  action3 = Action( name='act3', return_paramater=Paramater( type='Boolean' ), paramater_list=[ Paramater( name='bob', type='Float' ) ], static=False, func=fake_func )
  model.addAction( action3 )
  assert sort_dsc( action3.describe( action3.parent.parent.converter ).data ) == { 'name': 'act3', 'return-type': { 'type': 'Boolean' }, 'paramaters': [ { 'name': 'bob', 'type': 'Float' } ], 'static': False, 'path': '/api/model1(act3)' }

  assert action3.describe( action3.parent.parent.converter ).header_map == { 'Cache-Control': 'max-age=0', 'Verb': 'DESCRIBE', 'Type': 'Action' }

  assert action3.options().header_map == { 'Allow': 'OPTIONS, DESCRIBE, CALL' }
  assert action3.options().data is None
Esempio n. 6
0
def test_multi():
  server = Server( root_path='/api/', root_version='0.0', debug=True )
  ns1 = Namespace( name='ns1', version='0.1', converter=None )
  ns1.checkAuth = lambda user, verb, id_list: True
  model1 = Model( name='model1', field_list=[], transaction_class=TestTransaction )
  model1.checkAuth = lambda user, verb, id_list: True
  ns1.addElement( model1 )
  server.registerNamespace( '/', ns1 )

  req = Request( 'GET', '/api/ns1/model1:abc:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200
  assert res.header_map == { 'Cache-Control': 'no-cache', 'Cinp-Version': '0.9', 'Verb': 'GET', 'Multi-Object': 'False' }
  assert res.data == { '_extra_': 'get "abc"' }

  req = Request( 'GET', '/api/ns1/model1:abc:def:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200
  assert res.header_map == { 'Cache-Control': 'no-cache', 'Cinp-Version': '0.9', 'Verb': 'GET', 'Multi-Object': 'True' }
  assert res.data == { '/api/ns1/model1:abc:': { '_extra_': 'get "abc"' }, '/api/ns1/model1:def:': { '_extra_': 'get "def"' } }

  req = Request( 'GET', '/api/ns1/model1:abc:', { 'CINP-VERSION': __CINP_VERSION__, 'MULTI-OBJECT': 'true' } )
  res = server.handle( req )
  assert res.http_code == 200
  assert res.header_map == { 'Cache-Control': 'no-cache', 'Cinp-Version': '0.9', 'Verb': 'GET', 'Multi-Object': 'True' }
  assert res.data == { '/api/ns1/model1:abc:': { '_extra_': 'get "abc"' } }

  req = Request( 'GET', '/api/ns1/model1:abc:def:', { 'CINP-VERSION': __CINP_VERSION__, 'MULTI-OBJECT': 'true' } )
  res = server.handle( req )
  assert res.http_code == 200
  assert res.header_map == { 'Cache-Control': 'no-cache', 'Cinp-Version': '0.9', 'Verb': 'GET', 'Multi-Object': 'True' }
  assert res.data == { '/api/ns1/model1:abc:': { '_extra_': 'get "abc"' }, '/api/ns1/model1:def:': { '_extra_': 'get "def"' } }

  req = Request( 'GET', '/api/ns1/model1:abc:def:', { 'CINP-VERSION': __CINP_VERSION__, 'MULTI-OBJECT': 'false' } )
  res = server.handle( req )
  assert res.http_code == 400
  assert res.header_map == { 'Cinp-Version': '0.9' }
  assert res.data == { 'message': 'requested non multi-object, however multiple ids where sent' }
Esempio n. 7
0
def test_server():
  server = Server( root_path='/api/', root_version='0.0', debug=True )
  ns1 = Namespace( name='ns1', version='0.1', converter=None )
  ns1.checkAuth = lambda user, verb, id_list: True
  model1 = Model( name='model1', field_list=[], transaction_class=TestTransaction )
  model1.checkAuth = lambda user, verb, id_list: True
  ns1.addElement( model1 )
  model2 = Model( name='model2', field_list=[], transaction_class=TestTransaction )
  model2.checkAuth = lambda user, verb, id_list: True
  ns1.addElement( model2 )
  server.registerNamespace( '/', ns1 )

  ns2 = Namespace( name='ns2', version='0.2', converter=None )
  server.registerNamespace( '/api/', ns2 )

  req = Request( 'OPTIONS', '/api/', {} )
  res = server.handle( req )
  assert res.http_code == 200
  assert res.header_map == { 'Allow': 'OPTIONS, DESCRIBE', 'Cache-Control': 'max-age=0', 'Cinp-Version': '0.9' }

  path = '/api/'
  desc_ref = sort_dsc( { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns1/', '/api/ns2/' ], 'models': [], 'multi-uri-max': 100 } )
  assert sort_dsc( server.root_namespace.getElement( server.uri.split( path ) ).describe( ns1.converter ).data ) == desc_ref
  req = Request( 'DESCRIBE', path, { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200
  assert sort_dsc( res.data ) == desc_ref
  assert res.header_map == { 'Type': 'Namespace', 'Verb': 'DESCRIBE', 'Cache-Control': 'max-age=0', 'Cinp-Version': '0.9' }

  path = '/api/ns1/'
  desc_ref = sort_dsc( { 'name': 'ns1', 'path': '/api/ns1/', 'api-version': '0.1', 'namespaces': [], 'models': [ '/api/ns1/model1', '/api/ns1/model2' ], 'multi-uri-max': 100 } )
  assert sort_dsc( server.root_namespace.getElement( server.uri.split( path ) ).describe( ns1.converter ).data ) == desc_ref
  req = Request( 'DESCRIBE', path, { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200
  assert sort_dsc( res.data ) == desc_ref
  assert res.header_map == { 'Type': 'Namespace', 'Verb': 'DESCRIBE', 'Cache-Control': 'max-age=0', 'Cinp-Version': '0.9' }

  path = '/api/ns1/model1'
  desc_ref = sort_dsc( { 'name': 'model1', 'path': '/api/ns1/model1', 'fields': [], 'actions': [], 'constants': {}, 'list-filters': {}, 'not-allowed-methods': [] } )
  assert sort_dsc( server.root_namespace.getElement( server.uri.split( path ) ).describe( ns1.converter ).data ) == desc_ref
  req = Request( 'DESCRIBE', path, { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200
  assert sort_dsc( res.data ) == desc_ref
  assert res.header_map == { 'Type': 'Model', 'Verb': 'DESCRIBE', 'Cache-Control': 'max-age=0', 'Cinp-Version': '0.9' }
Esempio n. 8
0
    def registerNamespace(self, path, module=None, name=None, version=None):
        if module is None:
            if name is None or version is None:
                raise ValueError(
                    'name and version must be specified if no module is specified'
                )

            namespace = Namespace(name=name,
                                  version=version,
                                  converter=Converter(self.uri))

        else:
            if isinstance(module, Namespace):
                namespace = module

            else:
                module = import_module('{0}.models'.format(module))
                if not hasattr(module, 'cinp'):
                    raise NoCINP('module "{0}" missing cinp'.format(module))

                namespace = module.cinp.getNamespace(self.uri)

        super().registerNamespace(path, namespace)
Esempio n. 9
0
def test_not_allowed_verbs():
  server = Server( root_path='/api/', root_version='0.0', debug=True )
  ns1 = Namespace( name='ns1', version='0.1', converter=Converter( URI( '/api/' ) ) )
  ns1.checkAuth = lambda user, verb, id_list: True
  field_list = []
  field_list.append( Field( name='field1', type='String', length=50 ) )
  model1 = Model( name='model1', field_list=field_list, not_allowed_verb_list=[], transaction_class=TestTransaction )
  model2 = Model( name='model2', field_list=field_list, not_allowed_verb_list=[ 'GET', 'LIST', 'CALL', 'CREATE', 'UPDATE', 'DELETE', 'DESCRIBE' ], transaction_class=TestTransaction )
  model1.checkAuth = lambda user, verb, id_list: True
  model2.checkAuth = lambda user, verb, id_list: True
  action1 = Action( name='act', return_paramater=Paramater( type='String' ), func=fake_func )
  action2 = Action( name='act', return_paramater=Paramater( type='String' ), func=fake_func )
  action1.checkAuth = lambda user, verb, id_list: True
  action2.checkAuth = lambda user, verb, id_list: True
  model1.addAction( action1 )
  model2.addAction( action2 )
  ns1.addElement( model1 )
  ns1.addElement( model2 )
  server.registerNamespace( '/', ns1 )

  with pytest.raises( ValueError ):
    Model( name='modelX', field_list=[], not_allowed_verb_list=[ 'OPTIONS' ], transaction_class=TestTransaction )

  with pytest.raises( ValueError ):
    Model( name='modelX', field_list=[], not_allowed_verb_list=[ 'ASDF' ], transaction_class=TestTransaction )

  req = Request( 'OPTIONS', '/api/ns1/model1', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'OPTIONS', '/api/ns1/model2', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'DESCRIBE', '/api/ns1/model1', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'DESCRIBE', '/api/ns1/model2', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'GET', '/api/ns1/model1:asd:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'GET', '/api/ns1/model2:asd:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'LIST', '/api/ns1/model1', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'LIST', '/api/ns1/model2', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'CREATE', '/api/ns1/model1', { 'CINP-VERSION': __CINP_VERSION__ } )
  req.data = { 'field1': 'stuff' }
  res = server.handle( req )
  assert res.http_code == 201

  req = Request( 'CREATE', '/api/ns1/model2', { 'CINP-VERSION': __CINP_VERSION__ } )
  req.data = { 'field1': 'stuff' }
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'UPDATE', '/api/ns1/model1:sdf:', { 'CINP-VERSION': __CINP_VERSION__ } )
  req.data = { 'field1': 'stuff' }
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'UPDATE', '/api/ns1/model2:sdf:', { 'CINP-VERSION': __CINP_VERSION__ } )
  req.data = { 'field1': 'stuff' }
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'DELETE', '/api/ns1/model1:asd:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'DELETE', '/api/ns1/model2:asd:', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403

  req = Request( 'CALL', '/api/ns1/model1(act)', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 200

  req = Request( 'CALL', '/api/ns1/model2(act)', { 'CINP-VERSION': __CINP_VERSION__ } )
  res = server.handle( req )
  assert res.http_code == 403
Esempio n. 10
0
def test_saninity_checks():
  server = Server( root_path='/api/', root_version='0.0' )
  ns = Namespace( name='ns', version='0.1', converter=None )
  model = Model( name='model', field_list=[], transaction_class=TestTransaction )
  ns.addElement( model )
  action = Action( name='action', return_paramater=Paramater( type='String' ), func=fake_func )
  model.addAction( action )
  server.registerNamespace( '/', ns )

  res = server.dispatch( Request( 'BOB', '/api/', { 'CINP-VERSION': '0.9' } ) )
  assert res.http_code == 400
  assert res.data == { 'message': 'Invalid Verb (HTTP Method) "BOB"' }

  res = server.dispatch( Request( 'DESCRIBE', 'invalid', { 'CINP-VERSION': '0.9' } ) )
  assert res.http_code == 400
  assert res.data == { 'message': 'Unable to Parse "invalid"' }

  res = server.dispatch( Request( 'DESCRIBE', '/api/ns/model:' + ':'.join( 'id' * 101 ) + ':', { 'CINP-VERSION': '0.9' } ) )
  assert res.http_code == 400
  assert res.data == { 'message': 'id_list longer than supported length of "100"' }

  res = server.dispatch( Request( 'DESCRIBE', '/api/nope', { 'CINP-VERSION': '0.9' } ) )
  assert res.http_code == 404
  assert res.data == { 'message': 'path not found "/api/nope"' }

  res = server.handle( Request( 'DESCRIBE', '/api/', {} ) )
  assert res.http_code == 400
  assert res.data == { 'message': 'Invalid CInP Protocol Version' }

  res = server.handle( Request( 'DESCRIBE', '/api/', { 'Cinp-Version': '0' } ) )
  assert res.http_code == 400
  assert res.data == { 'message': 'Invalid CInP Protocol Version' }

  with pytest.raises( ValueError ):  # checkAuth not implemented, for this round of tests we call good
    server.dispatch( Request( 'DESCRIBE', '/api/ns/model(action)', { 'CINP-VERSION': '0.9' } ) )

  for verb in ( 'GET', 'LIST', 'CREATE', 'UPDATE', 'DELETE' ):
    res = server.dispatch( Request( verb, '/api/ns/model(action)', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Invalid verb "{0}" for request with action'.format( verb ) }

  for verb in ( 'GET', 'LIST', 'CREATE', 'UPDATE', 'DELETE' ):
    res = server.dispatch( Request( verb, '/api/ns/model:id:(action)', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Invalid verb "{0}" for request with action'.format( verb ) }

  for uri in ( '/api/', '/api/ns/', '/api/ns/model', '/api/ns/model:id:' ):
    res = server.dispatch( Request( 'CALL', uri, { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Verb "CALL" requires action'.format( verb ) }

  for verb in ( 'LIST', 'CREATE', 'DESCRIBE' ):
    res = server.dispatch( Request( verb, '/api/ns/model:id:', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Invalid Verb "{0}" for request with id'.format( verb ) }

  for verb in ( 'GET', 'UPDATE', 'DELETE' ):
    res = server.dispatch( Request( verb, '/api/ns/model', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Verb "{0}" requires id'.format( verb ) }

  for verb in ( 'GET', 'DELETE' ):
    req = Request( verb, '/api/ns/model:d:', { 'CINP-VERSION': '0.9' } )
    req.data = { 'some': 'data' }
    res = server.dispatch( req )
    assert res.http_code == 400
    assert res.data == { 'message': 'Invalid verb "{0}" for request with data'.format( verb ) }

  for verb in ( 'DESCRIBE', ):
    req = Request( verb, '/api/ns/model', { 'CINP-VERSION': '0.9' } )
    req.data = { 'some': 'data' }
    res = server.dispatch( req )
    assert res.http_code == 400
    assert res.data == { 'message': 'Invalid verb "{0}" for request with data'.format( verb ) }

  for verb in ( 'UPDATE', ):
    res = server.dispatch( Request( verb, '/api/ns/model:id:', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Verb "{0}" requires data'.format( verb ) }

  for verb in ( 'CREATE', ):
    res = server.dispatch( Request( verb, '/api/ns/model', { 'CINP-VERSION': '0.9' } ) )
    assert res.http_code == 400
    assert res.data == { 'message': 'Verb "{0}" requires data'.format( verb ) }

  for verb in ( 'LIST', 'CREATE' ):  # also 'GET', 'UPDATE', 'DELETE' which also requires an Id which requires a model so already covered
    req = Request( verb, '/api/ns/', { 'CINP-VERSION': '0.9' } )
    req.data = { 'some': 'data' }
    res = server.dispatch( req )
    assert res.http_code == 400
    assert res.data == { 'message': 'Verb "{0}" requires model'.format( verb ) }
Esempio n. 11
0
def test_getElement():
  uri = URI( root_path='/api/' )
  root_ns = Namespace( name=None, version='0.0', root_path='/api/', converter=None )
  ns2 = Namespace( name='ns2', version='0.1', converter=None )
  ns3 = Namespace( name='ns3', version='0.2', converter=None )
  ns2_2 = Namespace( name='ns2_2', version='0.1', converter=None )
  root_ns.addElement( ns2 )
  root_ns.addElement( ns3 )
  ns2.addElement( ns2_2 )
  mdl1 = Model( name='mdl1', field_list={}, transaction_class=TestTransaction )
  mdl2 = Model( name='mdl2', field_list={}, transaction_class=TestTransaction )
  root_ns.addElement( mdl1 )
  ns2_2.addElement( mdl2 )
  act1 = Action( name='act1', return_paramater=Paramater( type='String' ), paramater_list=[ Paramater( name='bob', type='Float' ) ], static=False, func=fake_func )
  act2 = Action( name='act2', return_paramater=Paramater( type='String' ), paramater_list=[ Paramater( name='stuff', type='Boolean' ) ], func=fake_func )
  mdl1.addAction( act1 )
  mdl2.addAction( act2 )

  assert root_ns.getElement( uri.split( '/api/', root_optional=True ) ) == root_ns
  assert root_ns.getElement( uri.split( '/api/ns2/', root_optional=True ) ) == ns2
  assert root_ns.getElement( uri.split( '/api/ns3/', root_optional=True ) ) == ns3
  assert root_ns.getElement( uri.split( '/api/ns2/ns2_2/', root_optional=True ) ) == ns2_2
  assert root_ns.getElement( uri.split( '/', root_optional=True ) ) == root_ns
  assert root_ns.getElement( uri.split( '/ns2/', root_optional=True ) ) == ns2
  assert root_ns.getElement( uri.split( '/ns3/', root_optional=True ) ) == ns3
  assert root_ns.getElement( uri.split( '/ns2/ns2_2/', root_optional=True ) ) == ns2_2
  assert root_ns.getElement( uri.split( '/api/mdl1', root_optional=True ) ) == mdl1
  assert root_ns.getElement( uri.split( '/api/ns2/ns2_2/mdl2', root_optional=True ) ) == mdl2
  assert root_ns.getElement( uri.split( '/mdl1', root_optional=True ) ) == mdl1
  assert root_ns.getElement( uri.split( '/ns2/ns2_2/mdl2', root_optional=True ) ) == mdl2
  assert root_ns.getElement( uri.split( '/api/mdl1(act1)', root_optional=True ) ) == act1
  assert root_ns.getElement( uri.split( '/api/ns2/ns2_2/mdl2(act2)', root_optional=True ) ) == act2
  assert root_ns.getElement( uri.split( '/mdl1(act1)', root_optional=True ) ) == act1
  assert root_ns.getElement( uri.split( '/ns2/ns2_2/mdl2(act2)', root_optional=True ) ) == act2

  with pytest.raises( ValueError ):
    root_ns.getElement( '/api/' )

  assert root_ns.getElement( uri.split( '/api/nsX/' ) ) is None
  assert root_ns.getElement( uri.split( '/api/ns2/mdlX' ) ) is None
  assert root_ns.getElement( uri.split( '/api/mdl1(actX)' ) ) is None
Esempio n. 12
0
def test_namespace():
  ns = Namespace( name=None, version='0.0', root_path='/api/', converter=None )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }

  with pytest.raises( ValueError ):
    Namespace( name='root', version='0.0', converter=None )

  with pytest.raises( ValueError ):
    Namespace( name=None, version='0.0', converter=None )

  with pytest.raises( ValueError ):
    Namespace( name=None, version='0.0', root_path='/asd', converter=None )

  ns2 = Namespace( name='ns2', version='2.0', converter=None )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': None, 'api-version': '2.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  ns.addElement( ns2 )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns2/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': '/api/ns2/', 'api-version': '2.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }

  ns3 = Namespace( name='ns3', version='3.0', converter=None )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns2/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': '/api/ns2/', 'api-version': '2.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns3.describe( ns3.converter ).data ) == { 'name': 'ns3', 'path': None, 'api-version': '3.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  ns.addElement( ns3 )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns2/', '/api/ns3/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': '/api/ns2/', 'api-version': '2.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns3.describe( ns3.converter ).data ) == { 'name': 'ns3', 'path': '/api/ns3/', 'api-version': '3.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }

  ns2_1 = Namespace( name='ns2_1', version='2.1', converter=None, doc='stuff' )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns2/', '/api/ns3/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': '/api/ns2/', 'api-version': '2.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns3.describe( ns3.converter ).data ) == { 'name': 'ns3', 'path': '/api/ns3/', 'api-version': '3.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2_1.describe( ns2_1.converter ).data ) == { 'name': 'ns2_1', 'path': None, 'api-version': '2.1', 'namespaces': [], 'models': [], 'multi-uri-max': 100, 'doc': 'stuff' }
  ns2.addElement( ns2_1 )
  assert sort_dsc( ns.describe( ns.converter ).data ) == { 'name': 'root', 'path': '/api/', 'api-version': '0.0', 'namespaces': [ '/api/ns2/', '/api/ns3/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2.describe( ns2.converter ).data ) == { 'name': 'ns2', 'path': '/api/ns2/', 'api-version': '2.0', 'namespaces': [ '/api/ns2/ns2_1/' ], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns3.describe( ns3.converter ).data ) == { 'name': 'ns3', 'path': '/api/ns3/', 'api-version': '3.0', 'namespaces': [], 'models': [], 'multi-uri-max': 100 }
  assert sort_dsc( ns2_1.describe( ns2_1.converter ).data ) == { 'name': 'ns2_1', 'path': '/api/ns2/ns2_1/', 'api-version': '2.1', 'namespaces': [], 'models': [], 'multi-uri-max': 100, 'doc': 'stuff' }

  assert ns.describe( ns.converter ).header_map == { 'Cache-Control': 'max-age=0', 'Verb': 'DESCRIBE', 'Type': 'Namespace' }

  assert ns.options().header_map == { 'Allow': 'OPTIONS, DESCRIBE', 'Cache-Control': 'max-age=0' }
  assert ns.options().data is None