def testSuccess(self, mock_metric): non_expired_dt = datetime.datetime.utcnow() + datetime.timedelta( days=1) test_utils.CreateExemption( 'non_expired_host_id', deactivation_dt=non_expired_dt, initial_state=constants.EXEMPTION_STATE.APPROVED) expired_dt = datetime.datetime.utcnow() - datetime.timedelta(days=1) exm_count = 7 expired_exm_keys = [] for host_id in test_utils.RandomStrings(exm_count): exm_key = test_utils.CreateExemption( host_id, deactivation_dt=expired_dt, initial_state=constants.EXEMPTION_STATE.APPROVED) expired_exm_keys.append(exm_key) self.testapp.get(self.ROUTE, headers={'X-AppEngine-Cron': 'true'}, status=httplib.OK) tasks = self.UnpackTaskQueue( queue_name=constants.TASK_QUEUE.EXEMPTIONS) # keys = {args[0] for func, args, kwargs in tasks} actual_exm_keys = {args[0] for func, args, kwargs in tasks} self.assertSameElements(expired_exm_keys, actual_exm_keys) self.assertNoBigQueryInsertions() mock_metric.Set.assert_called_once_with(exm_count)
def testUpdateHostUsers(self): old_users = test_utils.RandomStrings(2) new_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_db.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *new_users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_db.Bit9Host.get_by_id('12345') self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 3) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1) self.assertEntityCount(bigquery_db.UserRow, 2)
def testNewHost(self): users = test_utils.RandomStrings(2) host = bit9_test_utils.CreateComputer( users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() self.assertEntityCount(bit9_models.Bit9Host, 0) sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_models.Bit9Host, 1) self.assertBigQueryInsertions( [constants.BIGQUERY_TABLE.HOST] + [constants.BIGQUERY_TABLE.USER] * 2)
def testUpdateHostUsers_People(self): old_users = test_utils.RandomStrings(2) new_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *new_users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() # Verify that the users were updated in Datastore. host = bit9_models.Bit9Host.get_by_id('12345') self.assertSameElements(new_users, host.users) # Verify all BigQuery row persistence. self.assertBigQueryInsertions( [constants.BIGQUERY_TABLE.HOST] + [constants.BIGQUERY_TABLE.USER] * 2)
def testUpdateHostUsers_NoIncomingUsers(self): old_users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='12345', users=old_users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=22222, users='') occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() # Verify that the users weren't updated in Datastore. host = bit9_models.Bit9Host.get_by_id('12345') self.assertSameElements(old_users, host.users)
def testNewHost(self): users = test_utils.RandomStrings(2) host = bit9_test_utils.CreateComputer( users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() self.assertEntityCount(bit9_db.Bit9Host, 0) self.assertEntityCount(bigquery_db.HostRow, 0) self.assertEntityCount(bigquery_db.UserRow, 0) sync._PersistBit9Host(host, occurred_dt).wait() self.assertEntityCount(bit9_db.Bit9Host, 1) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 3) self.DrainTaskQueue(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1) self.assertEntityCount(bigquery_db.UserRow, 2)
def testUpdatePolicyKey(self): users = test_utils.RandomStrings(2) old_policy_key = ndb.Key(bit9_models.Bit9Policy, '22222') test_utils.CreateBit9Host( id='11111', policy_key=old_policy_key, users=users) host = bit9_test_utils.CreateComputer( id=11111, policy_id=33333, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_host = bit9_models.Bit9Host.get_by_id('11111') new_policy_key = ndb.Key(bit9_models.Bit9Policy, '33333') self.assertEqual(new_policy_key, bit9_host.policy_key) self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.HOST])
def testUpdateHostname(self): users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '100') test_utils.CreateBit9Host( id='12345', hostname=bit9_utils.ExpandHostname('hostname1'), users=users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, name='hostname2', policy_id=100, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_host = bit9_models.Bit9Host.get_by_id('12345') self.assertEqual(bit9_utils.ExpandHostname('hostname2'), bit9_host.hostname) self.assertNoBigQueryInsertions()
def testUpdateLastEventDt(self): now_dt = datetime.datetime.utcnow() earlier_dt = now_dt - datetime.timedelta(days=7) users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_models.Bit9Policy, '100') test_utils.CreateBit9Host( id='12345', last_event_dt=earlier_dt, users=users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, policy_id=100, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) sync._PersistBit9Host(host, now_dt).wait() bit9_host = bit9_models.Bit9Host.get_by_id('12345') self.assertEqual(now_dt, bit9_host.last_event_dt) self.assertNoBigQueryInsertions()
def testUpdateHostname(self): users = test_utils.RandomStrings(2) policy_key = ndb.Key(bit9_db.Bit9Policy, '100') test_utils.CreateBit9Host( id='12345', hostname=utils.ExpandHostname('hostname1'), users=users, policy_key=policy_key) host = bit9_test_utils.CreateComputer( id=12345, name='hostname2', policy_id=100, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_host = bit9_db.Bit9Host.get_by_id('12345') self.assertEqual(utils.ExpandHostname('hostname2'), bit9_host.hostname) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 0)
def testUpdatePolicyKey(self): users = test_utils.RandomStrings(2) old_policy_key = ndb.Key(bit9_db.Bit9Policy, '22222') test_utils.CreateBit9Host( id='11111', policy_key=old_policy_key, users=users) host = bit9_test_utils.CreateComputer( id=11111, policy_id=33333, users='{0}\\{1},{0}\\{2}'.format(settings.AD_DOMAIN, *users)) occurred_dt = datetime.datetime.utcnow() sync._PersistBit9Host(host, occurred_dt).wait() bit9_host = bit9_db.Bit9Host.get_by_id('11111') new_policy_key = ndb.Key(bit9_db.Bit9Policy, '33333') self.assertEqual(new_policy_key, bit9_host.policy_key) self.assertTaskCount(constants.TASK_QUEUE.BQ_PERSISTENCE, 1) self.RunDeferredTasks(constants.TASK_QUEUE.BQ_PERSISTENCE) self.assertEntityCount(bigquery_db.HostRow, 1)