def test_response_iterator(self): """ test if request context gets reinstated in sendResultIterator """ def request_context_test_iterator(): # this will raise an error if it is called # outside of request_context_safety yield request_context.get('foo') # we need to enclose bar into double qoutes, # because the json is assembled manually request_context_copy = {'foo': '"bar"'} try: res = sendResultIterator(request_context_test_iterator(), request_context_copy=request_context_copy) except ProgrammingError: assert False, 'request_context was used outside' \ 'of request_context_safety' result = "" for chunk in res: result += chunk result_dict = json.loads(result) value = result_dict.get('result', {}).get('value') assert 'bar' in value
def test_response_iterator_request_context(self): """ test if request context gets reinstated in sendResultIterator """ def request_context_test_iterator(): # this will raise an error if it is called # outside of request_context_safety yield request_context.get('foo') # we need to enclose bar into double qoutes, # because the json is assembled manually request_context_copy = {'foo': '"bar"'} try: res = sendResultIterator(request_context_test_iterator(), request_context_copy=request_context_copy) except ProgrammingError: self.assertTrue(False, 'request_context was used outside' 'of request_context_safety') result = "" for chunk in res: result += chunk result_dict = json.loads(result) value = result_dict.get('result', {}).get('value') self.assertIn(u'bar', value)
def test_response_iterator(self): """test if request context gets reinstated in sendResultIterator""" # we need to enclose bar into double qoutes, # because the json is assembled manually request_context_copy = {"foo": '"bar"'} def request_context_test_iterator(): # this will raise an error if it is called # outside of request_context_safety res = request_context_copy.get("foo") yield res try: res = sendResultIterator(obj=request_context_test_iterator()) except ProgrammingError: assert ( False, "request_context was used outside of request_context_safety", ) result = "" for chunk in res: result += chunk result_dict = json.loads(result) value = result_dict.get("result", {}).get("value") assert "bar" in value
def show(self): """ method: reporting/show description: show entries from the reporting database table arguments: * date - optional: only show entries which are newer than date; date must be given in format 'yyyy-mm-dd' if no date is given, all entries are shown * realms - optional: takes realms, only the reporting entries from this realm are shown * status - optional: filters reporting entries by status like 'assigned' or 'inactive' * sortby - optional: sort the output by column * sortdir - optional: asc/desc * page - optional: reqeuest a certain page * pagesize - optional: limit the number of returned tokens * outform - optional: if set to "csv", the output will be a .csv file returns: a json result with: { "head": [], "data": [ [row1] , [row2] , [row3] .. ] } in case of csv: first line: header of columns other lines: column values exception: if an error occurs an exception is serialized and returned """ try: param = request.params page = param.get('page') sort = param.get('sortby') sortdir = param.get('sortdir') psize = param.get('pagesize') output_format = param.get('outform', 'json') request_realms = param.get('realms', '').split(',') status = param.get('status', []) border_day = param.get('date') if border_day: # this may throw ValueError if date is in wrong format datetime.strptime(border_day, "%Y-%m-%d") realm_whitelist = [] policies = getAdminPolicies('show', scope='reporting.access') if policies['active'] and policies['realms']: realm_whitelist = policies.get('realms') # if there are no policies for us, we are allowed to see all realms if not realm_whitelist or '*' in realm_whitelist: realm_whitelist = request_context['Realms'].keys() realms = match_realms(request_realms, realm_whitelist) reports = ReportingIterator(realms=realms, status=status, date=None, page=page, psize=psize, sort=sort, sortdir=sortdir) info = reports.getResultSetInfo() c.audit['success'] = True Session.commit() if output_format == 'csv': response.content_type = "application/force-download" response.headers['Content-disposition'] = \ 'attachment; filename=linotp-reports.csv' return sendCSVIterator(reports.iterate_reports()) else: response.content_type = 'application/json' return sendResultIterator(reports.iterate_reports(), opt=info) except PolicyException as policy_exception: log.exception(policy_exception) Session.rollback() return sendError(response, unicode(policy_exception), 1) except ValueError as value_error: log.exception(value_error) Session.rollback() return sendError(response, unicode(value_error), 1) except Exception as exc: log.exception(exc) Session.rollback() return sendError(response, exc) finally: Session.close()
def show(self): """ method: reporting/show description: show entries from the reporting database table arguments: * date - optional: only show entries which are newer than date; date must be given in format 'yyyy-mm-dd' if no date is given, all entries are shown * realms - optional: takes realms, only the reporting entries from this realm are shown * status - optional: filters reporting entries by status like 'assigned' or 'inactive' * sortby - optional: sort the output by column * sortdir - optional: asc/desc * page - optional: reqeuest a certain page * pagesize - optional: limit the number of returned tokens * outform - optional: if set to "csv", the output will be a .csv file returns: a json result with: { "head": [], "data": [ [row1] , [row2] , [row3] .. ] } in case of csv: first line: header of columns other lines: column values exception: if an error occurs an exception is serialized and returned """ try: param = self.request_params page = param.get("page") sort = param.get("sortby") sortdir = param.get("sortdir") psize = param.get("pagesize") output_format = param.get("outform", "json") request_realms = param.get("realms", "").split(",") status = param.get("status", []) start_day = None if "date" in param: start_day = convert_to_datetime(param.get("date"), TIME_FMTS) realm_whitelist = [] policies = getAdminPolicies("show", scope="reporting.access") if policies["active"] and policies["realms"]: realm_whitelist = policies.get("realms") # if there are no policies for us, we are allowed to see all realms if not realm_whitelist or "*" in realm_whitelist: realm_whitelist = list(request_context["Realms"].keys()) realms = match_realms(request_realms, realm_whitelist) reports = ReportingIterator( realms=realms, status=status, date=start_day, page=page, psize=psize, sort=sort, sortdir=sortdir, ) info = reports.getResultSetInfo() g.audit["success"] = True db.session.commit() if output_format == "csv": headers = Headers() headers.add( "Content-Disposition", "attachment", filename="linotp-reports.csv", ) return Response( stream_with_context( sendCSVIterator(reports.iterate_reports())), mimetype="text/csv", headers=headers, ) else: return Response( stream_with_context( sendResultIterator(reports.iterate_reports(), opt=info)), mimetype="application/json", ) except PolicyException as policy_exception: log.error(policy_exception) db.session.rollback() return sendError(response, policy_exception, 1) except ValueError as value_error: log.error(value_error) db.session.rollback() return sendError(response, value_error, 1) except Exception as exc: log.error(exc) db.session.rollback() return sendError(response, exc)
def show(self): """ method: reporting/show description: show entries from the reporting database table arguments: * date - optional: only show entries which are newer than date; date must be given in format 'yyyy-mm-dd' if no date is given, all entries are shown * realms - optional: takes realms, only the reporting entries from this realm are shown * status - optional: filters reporting entries by status like 'assigned' or 'inactive' * sortby - optional: sort the output by column * sortdir - optional: asc/desc * page - optional: reqeuest a certain page * pagesize - optional: limit the number of returned tokens * outform - optional: if set to "csv", the output will be a .csv file returns: a json result with: { "head": [], "data": [ [row1] , [row2] , [row3] .. ] } in case of csv: first line: header of columns other lines: column values exception: if an error occurs an exception is serialized and returned """ try: param = self.request_params page = param.get('page') sort = param.get('sortby') sortdir = param.get('sortdir') psize = param.get('pagesize') output_format = param.get('outform', 'json') request_realms = param.get('realms', '').split(',') status = param.get('status', []) border_day = param.get('date') if border_day: # this may throw ValueError if date is in wrong format datetime.strptime(border_day, "%Y-%m-%d") realm_whitelist = [] policies = getAdminPolicies('show', scope='reporting.access') if policies['active'] and policies['realms']: realm_whitelist = policies.get('realms') # if there are no policies for us, we are allowed to see all realms if not realm_whitelist or '*' in realm_whitelist: realm_whitelist = request_context['Realms'].keys() realms = match_realms(request_realms, realm_whitelist) reports = ReportingIterator(realms=realms, status=status, date=None, page=page, psize=psize, sort=sort, sortdir=sortdir) info = reports.getResultSetInfo() c.audit['success'] = True Session.commit() if output_format == 'csv': response.content_type = "application/force-download" response.headers['Content-disposition'] = \ 'attachment; filename=linotp-reports.csv' return sendCSVIterator(reports.iterate_reports()) else: response.content_type = 'application/json' return sendResultIterator(reports.iterate_reports(), opt=info) except PolicyException as policy_exception: log.exception(policy_exception) Session.rollback() return sendError(response, unicode(policy_exception), 1) except ValueError as value_error: log.exception(value_error) Session.rollback() return sendError(response, unicode(value_error), 1) except Exception as exc: log.exception(exc) Session.rollback() return sendError(response, exc) finally: Session.close()