def groups(): size = int(request.args.get('results_per_page', 20)) cookie = request.headers.get('X-FasJson-Cookie', '') msgid = request.headers.get('X-Fasjson-Previous-Msgid') l = ldaputils.singleton(current_app.config['FASJSON_LDAP_URI']) while True: try: rmsgid, rsize, rcookie, rdata = l.get_groups(size=size, cookie=cookie) except ldap.LOCAL_ERROR as e: raise errors.WebApiError('LDAP local error', 500, data={'exception': str(e)}) if msgid is None: break if int(msgid) != int(rmsgid): break if len(rdata) == 0: raise errors.WebApiError('0 groups found', 404) c = rcookie.decode() if rcookie else None output = {'result': {'data': rdata, 'size': rsize}} headers = {'X-FasJson-MsgId': rmsgid} if c: headers['X-FasJson-Cookie'] = c return output, 200, headers
def user(username): l = ldaputils.singleton(current_app.config['FASJSON_LDAP_URI']) try: res = l.get_user(username) except ldap.LOCAL_ERROR as e: raise errors.WebApiError('LDAP local error', 500, data={'exception': str(e)}) if not res: raise errors.WebApiError('user not found', 404, data={'username': username}) output = {'result': res} return output, 200
def test_webapierror_as_dict_full(): err = errors.WebApiError('foobar', 404, data={'foo': 'bar'}) assert { 'error': { 'data': { 'foo': 'bar' }, 'message': 'foobar' } } == err.as_dict()
def test_webapierror_as_json(): err = errors.WebApiError('foobar', 404, data={'foo': 'bar'}) jsonstr = err.as_json() assert { 'error': { 'data': { 'foo': 'bar' }, 'message': 'foobar' } } == json.loads(jsonstr)
def me(): try: l = ldaputils.singleton(current_app.config['FASJSON_LDAP_URI']) raw, parsed = l.whoami() except (ldap.LOCAL_ERROR, ldap.SERVER_DOWN) as e: raise errors.WebApiError('LDAP local error', 500, data={'exception': str(e)}) output = {'result': {'raw': raw, 'info': parsed}} return output, 200
def root(): raise errors.WebApiError('method not implemented', 405, data={'reason': 'method not implemented'})
def test_webapierror_as_dict(): err = errors.WebApiError('foobar', 404) assert {'error': {'data': None, 'message': 'foobar'}} == err.as_dict()