def get_app(debug): app = WerkzeugServer(root_path='/api/v1/', root_version='0.9', debug=debug, get_user=getUser, cors_allow_list=['*']) config = Model(name='config', field_list=[], transaction_class=BlankTransaction) config.checkAuth = lambda user, verb, id_list: True app.root_namespace.addElement(config) info = Action(name='getContractorInfo', return_paramater=Paramater(type='Map'), func=contractorInfo) info.checkAuth = lambda user, verb, id_list: True config.addAction(info) app.registerNamespace('/', 'mcp.User') app.registerNamespace('/', 'mcp.Resource') app.registerNamespace('/', 'mcp.Project') app.registerNamespace('/', 'mcp.Processor') app.validate() return app
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' }
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
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' }
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