def handler_user(zmq_name, jsondata): logger.info('Handling user') action = jsondata['action'] json_user = jsondata['User'] json_org = jsondata['Organisation'] org = json_org['name'] if action == 'login': #only consider user login timestamp = int(time.time()) users_helper.add_user_login(timestamp, org) else: pass
def handler_audit(zmq_name, jsondata): action = jsondata.get('action', None) jsonlog = jsondata.get('Log', None) if action is None or jsonlog is None: return # consider login operations if action == 'log': # audit is related to log logAction = jsonlog.get('action', None) if logAction == 'login': # only consider user login timestamp = int(time.time()) email = jsonlog.get('email', '') org = jsonlog.get('org', '') users_helper.add_user_login(timestamp, org, email) else: pass
def test(): flag_error = False now = int(time.time()) today = datetime.datetime.fromtimestamp(now) twoDayAgo = today - datetime.timedelta(days=2) DAY = 60 * 60 * 24 org = 'TEST_ORG' org2 = 'TEST_ORG2' # logged in dates users_helper.add_user_login(now, org) users_helper.add_user_login(now + 5, org) users_helper.add_user_login(now - DAY * 2, org) expected_result = [ datetime.datetime.fromtimestamp(now - DAY * 2), datetime.datetime.fromtimestamp(now + 5), datetime.datetime.fromtimestamp(now) ] rep = users_helper.getDates(org) if not checkArrayEquality(rep, expected_result): print('getDates result not matching for all dates') flag_error = True expected_result = [ datetime.datetime.fromtimestamp(now + 5), datetime.datetime.fromtimestamp(now) ] rep = users_helper.getDates(org, datetime.datetime.now()) if not checkArrayEquality(rep, expected_result): print('getDates result not matching for query 1') flag_error = True expected_result = [] rep = users_helper.getDates( org, datetime.datetime.now() - datetime.timedelta(days=7)) if not checkArrayEquality(rep, expected_result): print('getDates result not matching for query 2') flag_error = True # all logged orgs users_helper.add_user_login(now, org2) expected_result = [ datetime.datetime.fromtimestamp(now + 5), datetime.datetime.fromtimestamp(now), datetime.datetime.fromtimestamp(now) ] rep = users_helper.getUserLogins(datetime.datetime.now()) if not checkArrayEquality(rep, expected_result): print('getUserLogins result not matching') flag_error = True # all logged in org expected_result = [org, org2] rep = users_helper.getAllLoggedInOrgs(datetime.datetime.fromtimestamp(now + 5), prev_days=7) if not checkArrayEquality(rep, expected_result): print('getAllLoggedInOrgs result not matching') flag_error = True # punchcard expected_result = [[0 for x in range(24)] for y in range(7)] # set correct values day = today.weekday() hour = today.hour expected_result[day][hour] = 3 day = twoDayAgo.weekday() hour = twoDayAgo.hour expected_result[day][hour] = 1 # swap: punchcard day starts on sunday expected_result = [expected_result[6]] + expected_result[:6] rep = users_helper.getUserLoginsForPunchCard( datetime.datetime.fromtimestamp(now), org=None, prev_days=6) if not checkArrayEquality(rep, expected_result): print('getUserLoginsForPunchCard result not matching') flag_error = True # overtime rep = users_helper.getUserLoginsAndContribOvertime( datetime.datetime.fromtimestamp(now), org=None, prev_days=6) t1 = all([tab[1] == 0 for tab in rep['contrib']]) # no contribution t2 = [True for tab in rep['login'] if tab[1] == 3] t2 = t2[0] and len(t2) == 1 # one login at 3, others at 0 if not (t1 and t2): print('getUserLoginsAndContribOvertime result not matching') flag_error = True return flag_error