def testNormal(self): expected = 3 for i in xrange(expected): test_utils.CreateSantaRule(self.santa_blockable.key, policy=constants.RULE_POLICY.WHITELIST, host_id='host%s' % i) test_utils.CreateSantaRule(self.santa_blockable.key, policy=constants.RULE_POLICY.BLACKLIST) test_utils.CreateSantaRule(self.santa_blockable.key, policy=constants.RULE_POLICY.WHITELIST, in_effect=False) with self.LoggedInUser(admin=True): response = self.testapp.get('/%s' % self.santa_blockable.key.id()) output = response.json self.assertEqual(expected, output)
def testPackage(self): blockable = test_utils.CreateSantaBlockable() bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable]) rule = test_utils.CreateSantaRule( bundle.key, rule_type=constants.RULE_TYPE.PACKAGE) self.assertSameElements([blockable.key.id()], model_utils.GetBundleBinaryIdsForRule(rule))
def testToDict_Package(self): blockable = test_utils.CreateSantaBlockable() bundle = test_utils.CreateSantaBundle(bundle_binaries=[blockable]) rule = test_utils.CreateSantaRule( bundle.key, rule_type=constants.RULE_TYPE.PACKAGE) self.assertSameElements([blockable.key.id()], rule.to_dict()['binary_ids'])
def testInsertBigQueryRow_GlobalRule(self): blockable_key = test_utils.CreateSantaBlockable().key global_rule = test_utils.CreateSantaRule(blockable_key) global_rule.InsertBigQueryRow() self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.RULE], reset_mock=False) calls = self.GetBigQueryCalls() self.assertLen(calls, 1) self.assertEqual(constants.RULE_SCOPE.GLOBAL, calls[0][1].get('scope'))
def testIsVotingAllowed_CertIsBlacklisted(self): """IsVotingAllowed() called on blockable signed by a blacklisted cert.""" blockable_cert = test_utils.CreateSantaBlockable() blockable = test_utils.CreateSantaBlockable( cert_key=blockable_cert.key) test_utils.CreateSantaRule(blockable_cert.key, rule_type=constants.RULE_TYPE.CERTIFICATE, policy=constants.RULE_POLICY.BLACKLIST) with self.LoggedInUser(): allowed, reason = blockable.IsVotingAllowed() self.assertFalse(allowed) self.assertIsNotNone(reason)
def testIsVotingAllowed_Admin_CertIsBlacklisted(self): """IsVotingAllowed() called on blockable signed by a blacklisted cert.""" blockable_cert = test_utils.CreateSantaBlockable() blockable = test_utils.CreateSantaBlockable( cert_key=blockable_cert.key) test_utils.CreateSantaRule(blockable_cert.key, rule_type=constants.RULE_TYPE.CERTIFICATE, policy=constants.RULE_POLICY.BLACKLIST) with self.LoggedInUser(admin=True): _, reason = blockable.IsVotingAllowed() # Ensure voting isn't disabled because of the blacklisted cert. self.assertNotEqual( constants.VOTING_PROHIBITED_REASONS.BLACKLISTED_CERT, reason)
def testGetRules(self): self.assertEqual(0, len(self.blockable_1.GetRules())) self.assertEqual(0, len(self.blockable_2.GetRules())) test_utils.CreateSantaRule(self.blockable_1.key) test_utils.CreateSantaRule(self.blockable_1.key) test_utils.CreateSantaRule(self.blockable_1.key, **{'in_effect': False}) test_utils.CreateSantaRule(self.blockable_2.key) test_utils.CreateSantaRule(self.blockable_2.key) test_utils.CreateSantaRule(self.blockable_2.key) test_utils.CreateSantaRule(self.blockable_2.key, **{'in_effect': False}) self.assertEqual(2, len(self.blockable_1.GetRules())) self.assertEqual(3, len(self.blockable_2.GetRules())) self.assertEqual(3, len(self.blockable_1.GetRules(in_effect=False))) self.assertEqual(4, len(self.blockable_2.GetRules(in_effect=False)))
def testInsertBigQueryRow_LocalRule_UserKeyMissing(self): """Verifies that a LOCAL row is inserted, even if user_key is missing. The host_id and user_key columns have to be NULLABLE in order to support GLOBAL rows (which will lack values for both of these columns). If user_key is mistakenly omitted, we should still insert a LOCAL row with the values we have. """ blockable_key = test_utils.CreateSantaBlockable().key local_rule = test_utils.CreateSantaRule(blockable_key, host_id='12345') local_rule.InsertBigQueryRow() self.assertBigQueryInsertions([constants.BIGQUERY_TABLE.RULE], reset_mock=False) calls = self.GetBigQueryCalls() self.assertLen(calls, 1) self.assertEqual(constants.RULE_SCOPE.LOCAL, calls[0][1].get('scope'))
def testPost_Admin_Recount_Success(self): """Admin requesting a recount for a blockable.""" # Create an anomalous global blacklist rule that should be deactivated by # the recount. rule = test_utils.CreateSantaRule(self.santa_blockable.key) self.assertTrue(rule.in_effect) id_ = self.santa_blockable.key.id() params = {'recount': 'recount'} with self.LoggedInUser(admin=True): response = self.testapp.post(self.ROUTE % id_, params) self.assertFalse(rule.key.get().in_effect) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertIsInstance(output, dict) self.assertEqual(output['fileName'], self.santa_blockable.file_name) self.assertIn('Blockable', output['class_'])
def testNotPackage(self): blockable = test_utils.CreateSantaBlockable() rule = test_utils.CreateSantaRule(blockable.key, rule_type=constants.RULE_TYPE.BINARY) self.assertListEqual([], model_utils.GetBundleBinaryIdsForRule(rule))
def testToDict_NotPackage(self): blockable = test_utils.CreateSantaBlockable() rule = test_utils.CreateSantaRule(blockable.key, rule_type=constants.RULE_TYPE.BINARY) self.assertNotIn('binary_ids', rule.to_dict())