def test_args(fconfig: Config, db: SQLAlchemy): """Tests the handling of query arguments in the URL.""" DeviceDef, *_ = fconfig.RESOURCE_DEFINITIONS # type: Tuple[ResourceDef] class FindArgsFoo(DeviceDef.VIEW.FindArgs): foo = Integer() DeviceDef.VIEW.FindArgs = FindArgsFoo def find(args: dict): assert args == {'foo': 25} return Response(status=200) DeviceDef.VIEW.find = MagicMock(side_effect=find) client = Teal(config=fconfig, db=db).test_client() # type: Client # Ok client.get(res=DeviceDef.type, query=[('foo', 25)]) # Extra not needed data client.get(res=DeviceDef.type, query=[('foo', 25), ('bar', 'nope')]) # Wrong data r, _ = client.get(res=DeviceDef.type, query=[('foo', 'nope')], status=UnprocessableEntity)
def test_http_exception(fconfig: Config, db: SQLAlchemy): """Tests correct handling of HTTP exceptions.""" DeviceDef, *_ = fconfig.RESOURCE_DEFINITIONS # type: Tuple[ResourceDef] DeviceDef.VIEW.get = MagicMock(side_effect=NotFound) client = Teal(config=fconfig, db=db).test_client() # type: Client d, _ = client.get(res=DeviceDef.type, status=NotFound) assert d['code'] == 404
def test_inheritance_access(fconfig: Config, db: SQLAlchemy): """ Tests that the right endpoint is called when accessing sub-resources. """ DeviceDef, ComponentDef, ComputerDef = fconfig.RESOURCE_DEFINITIONS # type: Tuple[ResourceDef] DUMMY_DICT = {'ok': 'yes'} def foo(*args, **kw): return jsonify(DUMMY_DICT) DeviceDef.VIEW.get = MagicMock(side_effect=foo) ComponentDef.VIEW.get = MagicMock(side_effect=foo) ComputerDef.VIEW.get = MagicMock(side_effect=foo) client = Teal(config=fconfig, db=db).test_client() # type: Client # Access any non-defined URI client.get(uri='/this-does-not-exist', status=NotFound) assert DeviceDef.VIEW.get.call_count == \ ComponentDef.VIEW.get.call_count == \ ComputerDef.VIEW.get.call_count == 0 # Access to a non-defined method for a resource client.post(res=DeviceDef.type, status=MethodNotAllowed, data=dict()) assert DeviceDef.VIEW.get.call_count == \ ComponentDef.VIEW.get.call_count == \ ComputerDef.VIEW.get.call_count == 0 # Get top resource Device # Only device endpoint is called d, _ = client.get(res=DeviceDef.type) assert d == DUMMY_DICT DeviceDef.VIEW.get.assert_called_once_with(id=None) assert ComponentDef.VIEW.get.call_count == 0 assert ComputerDef.VIEW.get.call_count == 0 # Get computer # Only component endpoint is called d, _ = client.get(res=ComputerDef.type) assert d == DUMMY_DICT assert DeviceDef.VIEW.get.call_count == 1 # from before assert ComponentDef.VIEW.get.call_count == 0 ComputerDef.VIEW.get.assert_called_once_with(id=None)
def test_cors(fconfig: Config, db: SchemaSQLAlchemy): DeviceDef, *_ = fconfig.RESOURCE_DEFINITIONS # type: Tuple[ResourceDef] def foo(*args, **kw): return Response(status=200) DeviceDef.VIEW.get = MagicMock(side_effect=foo) client = Teal(config=fconfig, db=db).test_client() # type: Client _, response = client.get('/devices/') headers = response.headers.to_list() assert ('Access-Control-Expose-Headers', 'Authorization') in headers assert ('Access-Control-Allow-Origin', '*') in headers
def test_http_exception(fconfig: Config, db: SQLAlchemy): """Tests correct handling of HTTP exceptions.""" DeviceDef, *_ = fconfig.RESOURCE_DEFINITIONS # type: Tuple[ResourceDef] DeviceDef.VIEW.get = MagicMock(side_effect=NotFound) client = Teal(config=fconfig, db=db).test_client() # type: Client d, _ = client.get(res=DeviceDef.type, status=NotFound) assert d == { 'code': 404, 'message': '404 Not Found: The requested URL was not found on the server. ' 'If you entered the URL manually please check your spelling and try again.', 'type': 'NotFound' }