def testPost_Admin_InsertNote(self): """Admin posting a valid blockable.""" sha256 = test_utils.RandomSHA256() params = { 'notes': 'foo', 'fileName': 'bar', 'type': constants.BLOCKABLE_TYPE.SANTA_BINARY } with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % sha256, params) blockable = base.Blockable.get_by_id(sha256) self.assertEqual('bar', blockable.file_name) self.assertEntityCount(base.Note, 1) note = base.Note.query().fetch()[0] self.assertEqual(note.message, 'foo') self.assertEqual(note.key.parent(), blockable.key) self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.BINARY], reset_mock=False) # Verify the FIRST_SEEN row in BigQuery. predicate = lambda c: c[1].get('action') == 'FIRST_SEEN' calls = self.GetBigQueryCalls(predicate=predicate) self.assertLen(calls, 1)
def testPost_Admin_InsertNewBlockable(self): """Admin posting a valid blockable.""" sha256 = test_utils.RandomSHA256() params = { 'type': constants.BLOCKABLE_TYPE.SANTA_BINARY, 'fileName': 'MacIIci.app', 'publisher': 'Arple' } expected_key = ndb.Key(santa.SantaBlockable, sha256) self.assertIsNone(expected_key.get()) with self.LoggedInUser(admin=True): response = self.testapp.post(self.ROUTE % sha256, params) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertEqual(output['id'], sha256) self.assertIsNotNone(expected_key.get()) self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.BINARY], reset_mock=False) # Verify the FIRST_SEEN row in BigQuery. predicate = lambda c: c[1].get('action') == 'FIRST_SEEN' calls = self.GetBigQueryCalls(predicate=predicate) self.assertLen(calls, 1)
def testSuccess(self): self.assertEntityCount(santa.SantaCertificate, 0) self.assertEntityCount(santa.SantaRule, 0) sha256_list = [test_utils.RandomSHA256() for _ in xrange(5)] rule.EnsureCriticalRules(sha256_list) self.assertEntityCount(santa.SantaCertificate, 5) self.assertEntityCount(santa.SantaRule, 5)
def testGet_CaseInsensitiveID(self): sha256 = test_utils.RandomSHA256() test_utils.CreateSantaBlockable(id=sha256, id_type=constants.ID_TYPE.SHA256, file_name='some_binary') with self.LoggedInUser(): self.testapp.get(self.ROUTE % sha256.lower(), status=httplib.OK) self.testapp.get(self.ROUTE % sha256.upper(), status=httplib.OK)
def testPost_NoRolesError(self, mock_highest_role): mock_highest_role.side_effect = user_models.NoRolesError user = test_utils.CreateUser() self.assertIsNone(user.last_vote_dt) with self.LoggedInUser(user=user): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), status=httplib.INTERNAL_SERVER_ERROR) self.assertIsNone(user.last_vote_dt)
def testPost_Admin_InsertUnknownType(self): """Admin tries to inject a blockable of unknown type.""" sha256 = test_utils.RandomSHA256() params = {'type': 'Unknown', 'hash': sha256} with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % sha256, params, status=httplib.BAD_REQUEST) self.assertNoBigQueryInsertions()
def testPost_CaseInsensitiveID(self): sha256 = test_utils.RandomSHA256() test_utils.CreateBit9Binary(id=sha256, id_type=constants.ID_TYPE.SHA256, file_name='some_binary') with self.LoggedInUser(): self.testapp.post(self.ROUTE % sha256.lower(), {'value': True}) self.testapp.post(self.ROUTE % sha256.upper(), {'value': True}) self.assertBigQueryInsertion(constants.BIGQUERY_TABLE.BINARY)
def testDuplicateEventsFromHost(self, mock_events_skipped): events, certs = _CreateEventsAndCerts( count=100, computer_kwargs={'id': 999}, file_catalog_kwargs={'sha256': test_utils.RandomSHA256()}) expected_event_id = events[-1].id expected_cert_id = certs[0].id self._AppendMockApiResults(events, *certs) results = sync.GetEvents(0) self.assertEqual(1, len(results)) self.assertEqual(expected_event_id, results[0][0].id) self.assertEqual(expected_cert_id, results[0][1][0].id) self.assertEqual(99, mock_events_skipped.Increment.call_count)
def testPost_Admin_Reset_CaseInsensitiveID(self, mock_reset): sha256 = test_utils.RandomSHA256() test_utils.CreateBit9Binary(id=sha256, id_type=constants.ID_TYPE.SHA256, file_name='some_binary') params = {'reset': 'reset'} with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % sha256.lower(), params, status=httplib.OK) self.testapp.post(self.ROUTE % sha256.upper(), params, status=httplib.OK) self.assertEqual(2, mock_reset.call_count)
def testPost_UnsupportedPlatformError(self, mock_vote): with self.LoggedInUser(): self.testapp.post( self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.BAD_REQUEST)
def testPost_BlockableNotFoundError(self, mock_vote): with self.LoggedInUser(): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.NOT_FOUND)
def testPost_InvalidVoteWeightError(self, mock_vote): with self.LoggedInUser(): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.BAD_REQUEST)
def testPost_Admin_Reset_Exception(self, mock_reset): with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'reset': 'reset'}, status=httplib.INTERNAL_SERVER_ERROR)
def testPost_Admin_Reset_OperationNotAllowedError(self, mock_reset): with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'reset': 'reset'}, status=httplib.FORBIDDEN)
def testPost_Admin_Reset_UnsupportedPlatformError(self, mock_reset): with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'reset': 'reset'}, status=httplib.BAD_REQUEST)
def testPost_Admin_Reset_BlockableNotFoundError(self, mock_reset): with self.LoggedInUser(admin=True): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'reset': 'reset'}, status=httplib.NOT_FOUND)
def testPost_DuplicateVoteError(self, mock_vote): with self.LoggedInUser(): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.CONFLICT)
def testPost_OperationNotAllowedError(self, mock_vote): with self.LoggedInUser(): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.FORBIDDEN)
def testPost_Exception(self, mock_vote): with self.LoggedInUser(): self.testapp.post(self.ROUTE % test_utils.RandomSHA256(), params={'wasYesVote': 'true'}, status=httplib.INTERNAL_SERVER_ERROR)