def test_do_alert_when_travel_impossible(self): evts = [ locality.Locality( sourceipaddress='1.2.3.123', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(minutes=5), latitude=43.6529, longitude=-79.3849, radius=50), locality.Locality( sourceipaddress='123.3.2.1', city='San Francisco', country='US', lastaction=toUTC(datetime.now()) - timedelta(hours=1), latitude=37.773972, longitude=-122.431297, radius=50) ] alert_produced = alert('testuser', evts, [], 'INFO') assert alert_produced is not None assert alert_produced.username == 'testuser' assert alert_produced.hops[0].origin.city == 'San Francisco' assert alert_produced.hops[0].destination.city == 'Toronto'
def test_update_localities(self): from_es = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) from_events = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=43.6529, longitude=-79.3849, radius=50) ]) update = locality.update(from_es, from_events) last_action = update.state.localities[0].lastaction hour_ago = toUTC(datetime.now()) - timedelta(hours=1) assert update.did_update assert update.state.username == 'user1' assert last_action > hour_ago
def test_journaling(self): journal = locality.wrap_journal(self.es_client) test_entry = locality.Entry( 'testingid1', locality.State('locality', 't1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50), locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=52.520008, longitude=13.404954, radius=50) ])) journal(test_entry, self.event_index_name) self.refresh(self.event_index_name) query_iface = locality.wrap_query(self.es_client) loc_cfg = config.Localities(self.event_index_name, 30, 50.0) entry = locality.find(query_iface, 't1', loc_cfg.es_index) assert entry is not None assert entry.state.username == 't1'
def test_remove_outdated_removes_old_localities(self): test_state = locality.State('locality', 'tester1', [ locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(days=10), latitude=52.520008, longitude=13.404954, radius=50), locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) update = locality.remove_outdated(test_state, 5) assert update.did_update assert len(test_state.localities) == 2 assert len(update.state.localities) == 1 assert update.state.localities[0].city == 'Toronto'
def test_update_records_new_localities(self): from_es = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) from_events = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=52.520008, longitude=13.404954, radius=50) ]) update = locality.update(from_es, from_events) cities = [loc.city for loc in update.state.localities] assert update.did_update assert sorted(cities) == ['Berlin', 'Toronto']
def test_do_not_alert_when_travel_possible(self): evts = [ locality.Locality(sourceipaddress='1.2.3.123', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(minutes=5), latitude=43.6529, longitude=-79.3849, radius=50), locality.Locality(sourceipaddress='123.3.2.1', city='San Francisco', country='US', lastaction=toUTC(datetime.now()) - timedelta(hours=10), latitude=37.773972, longitude=-122.431297, radius=50) ] alert_produced = alert('tester1', evts, []) assert alert_produced is None
def test_alerts_include_all_impossible_hops(self): evts = [ locality.Locality( sourceipaddress='1.2.3.123', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(minutes=5), latitude=43.6529, longitude=-79.3849, radius=50), locality.Locality( sourceipaddress='52.32.127.3', city='Saint Petersburg', country='RU', lastaction=toUTC(datetime.now()) - timedelta(minutes=15), latitude=59.9343, longitude=30.3351, radius=50), locality.Locality( sourceipaddress='123.3.2.1', city='San Francisco', country='US', lastaction=toUTC(datetime.now()) - timedelta(hours=1), latitude=37.773972, longitude=-122.431297, radius=50) ] alert_produced = alert('tester', evts, [], 'INFO') assert alert_produced is not None assert len(alert_produced.hops) == 2 assert alert_produced.hops[0].origin.city == 'San Francisco' assert alert_produced.hops[0].destination.city == 'Saint Petersburg' assert alert_produced.hops[1].origin.city == 'Saint Petersburg' assert alert_produced.hops[1].destination.city == 'Toronto'
def locality(cfg): return geomodel.Locality(**cfg)