def setUp(self): super(BlockableTest, self).setUp() self.blockable_1 = test_utils.CreateBlockable() self.blockable_2 = test_utils.CreateBlockable() self.user = test_utils.CreateUser(email=_TEST_EMAIL) self.Login(self.user.email)
def testToDict_VotingProhibited(self): for state in constants.STATE.SET_VOTING_PROHIBITED: blockable = test_utils.CreateBlockable(state=state) blockable_dict = blockable.to_dict() self.assertIn('is_voting_allowed', blockable_dict) self.assertFalse(blockable_dict['is_voting_allowed']) self.assertIn('voting_prohibited_reason', blockable_dict)
def testToDict_VotingAllowedAdminOnly_User(self): for state in constants.STATE.SET_VOTING_ALLOWED_ADMIN_ONLY: blockable = test_utils.CreateBlockable(state=state) blockable_dict = blockable.to_dict() self.assertIn('is_voting_allowed', blockable_dict) self.assertFalse(blockable_dict['is_voting_allowed']) self.assertIn('voting_prohibited_reason', blockable_dict)
def testAdminPostInsert(self): """Admin posting a valid blockable.""" id_ = 'qqqqrrrrsssstttt' params = { 'type': constants.BLOCKABLE_TYPE.SANTA_BINARY, 'fileName': 'MacIIci.app', 'publisher': 'Arple' } mock_model = mock.MagicMock() mock_model.get_by_id.return_value = False test_blockable = test_utils.CreateBlockable(id=id_) mock_model.get_or_insert.return_value = test_blockable with mock.patch.object(blockables, 'model_mapping') as mock_mapping: mock_mapping.BlockableTypeModelMap.SANTA_BINARY = mock_model with self.LoggedInUser(admin=True): response = self.testapp.post('/%s' % id_, params) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertEqual(output['id'], 'qqqqrrrrsssstttt') mock_model.get_or_insert.assert_called_with( 'qqqqrrrrsssstttt', file_name='MacIIci.app', publisher='Arple', flagged=False, id_type=constants.ID_TYPE.SHA256)
def testResetState(self): blockable = test_utils.CreateBlockable(state=constants.STATE.BANNED, flagged=True) blockable.ResetState() retrieved_blockable = blockable.key.get() self.assertEqual(retrieved_blockable.state, constants.STATE.UNTRUSTED) self.assertFalse(retrieved_blockable.flagged)
def testToDict_VotingAllowedAdminOnly_Admin(self): admin_user = test_utils.CreateUser(admin=True) self.Logout() self.Login(admin_user.email) for state in constants.STATE.SET_VOTING_ALLOWED_ADMIN_ONLY: blockable = test_utils.CreateBlockable(state=state) blockable_dict = blockable.to_dict() self.assertIn('is_voting_allowed', blockable_dict) self.assertTrue(blockable_dict['is_voting_allowed']) self.assertIn('voting_prohibited_reason', blockable_dict)
def setUp(self): super(GetEventKeysToInsertTest, self).setUp() now = datetime.datetime.utcnow() self.user = test_utils.CreateUser() self.event = test_utils.CreateEvent( test_utils.CreateBlockable(), first_blocked_dt=now, last_blocked_dt=now, executing_user=self.user.nickname) self.PatchSetting('EVENT_CREATION', constants.EVENT_CREATION.EXECUTING_USER)
def testToDict_Score(self): blockable = test_utils.CreateBlockable() test_utils.CreateVote(blockable) # Recalculate the 'score' property blockable.put() # Mock out the blockable's _CalculateScore function. with mock.patch.object( blockable._properties['score'], '_func') as calc_mock: # pylint: disable=protected-access blockable_dict = blockable.to_dict() self.assertFalse(calc_mock.called) self.assertIn('score', blockable_dict) self.assertEqual(1, blockable_dict['score'])
def setUp(self, app): super(RulesTest, self).setUp(wsgi_app=app) self.blockable1 = test_utils.CreateBlockable() self.blockable2 = test_utils.CreateBlockable() self.rule_1 = base.Rule(parent=self.blockable1.key, id=123456, rule_type='BINARY', policy='WHITELIST') self.rule_2 = base.Rule(parent=self.blockable2.key, id=123457, rule_type='BINARY', policy='BLACKLIST') self.rule_3 = santa.SantaRule(parent=self.blockable2.key, id=123458, rule_type='BINARY', policy='WHITELIST') self.rule_1.put() self.rule_2.put() self.rule_3.put() self.PatchValidateXSRFToken()
def setUp(self): app = webapp2.WSGIApplication(routes=[rules.ROUTES]) super(RulesTest, self).setUp(wsgi_app=app) self.blockable1 = test_utils.CreateBlockable() self.blockable2 = test_utils.CreateBlockable() self.rule_1 = rule_models.Rule(parent=self.blockable1.key, id=123456, rule_type='BINARY', policy='WHITELIST') self.rule_2 = rule_models.Rule(parent=self.blockable2.key, id=123457, rule_type='BINARY', policy='BLACKLIST') self.rule_3 = rule_models.SantaRule(parent=self.blockable2.key, id=123458, rule_type='BINARY', policy='WHITELIST') self.rule_1.put() self.rule_2.put() self.rule_3.put() self.PatchValidateXSRFToken()
def testIsVotingAllowed_AdminOnly(self): user = test_utils.CreateUser() admin = test_utils.CreateUser(admin=True) for state in constants.STATE.SET_VOTING_ALLOWED_ADMIN_ONLY: blockable = test_utils.CreateBlockable(state=state) # Test for a regular user. allowed, reason = blockable.IsVotingAllowed(current_user=user) self.assertFalse(allowed) self.assertIsNotNone(reason) # Test for an admin. allowed, reason = blockable.IsVotingAllowed(current_user=admin) self.assertTrue(allowed) self.assertIsNone(reason)
def testAssociatedUserGet(self): """Normal user associated with a host gets it by ID.""" blockable = test_utils.CreateBlockable() with self.LoggedInUser() as user: test_utils.CreateSantaEvent(blockable, host_id=self.santa_host_1.key.id(), executing_user=user.nickname, parent=utils.ConcatenateKeys( user.key, self.santa_host_1.key, blockable.key)) self.assertTrue(self.santa_host_1.IsAssociatedWithUser(user)) response = self.testapp.get('/' + self.santa_host_1.key.id()) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertIsInstance(output, dict)
def setUp(self): super(EventTest, self).setUp() self.earlier = datetime.datetime.utcnow() self.middle = self.earlier + datetime.timedelta(seconds=1) self.later = self.earlier + datetime.timedelta(seconds=2) self.blockable = test_utils.CreateBlockable() self.user = test_utils.CreateUser() self.other_user = test_utils.CreateUser() self.event_1 = test_utils.CreateEvent( self.blockable, first_blocked_dt=self.earlier, last_blocked_dt=self.earlier, executing_user=self.user.nickname) self.event_2 = test_utils.CreateEvent( self.blockable, first_blocked_dt=self.later, last_blocked_dt=self.later, executing_user=self.other_user.nickname) self.PatchSetting('EVENT_CREATION', constants.EVENT_CREATION.EXECUTING_USER)
def testGet_AssociatedUser(self): user = test_utils.CreateUser() host = test_utils.CreateSantaHost(primary_user=user.nickname) test_utils.CreateExemption(host.key.id()) blockable = test_utils.CreateBlockable() test_utils.CreateSantaEvent( blockable, host_id=host.key.id(), executing_user=user.nickname, parent=datastore_utils.ConcatenateKeys( user.key, host.key, blockable.key)) self.assertTrue(model_utils.IsHostAssociatedWithUser(host, user)) with self.LoggedInUser(user=user): response = self.testapp.get(self.ROUTE % host.key.id()) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertIsInstance(output, dict) self.assertIn('exemption', output)
def setUp(self, app): super(BlockablesTest, self).setUp(wsgi_app=app) self.bit9_blockable = test_utils.CreateBit9Binary( id='zzzzzzzzzaaa', id_type=constants.ID_TYPE.SHA256, file_name='Mac.app.exe') self.bit9_blockable2 = test_utils.CreateBit9Binary( id='zzzzzzzzzbbb', id_type=constants.ID_TYPE.SHA256, file_name='app.exe') self.generic_blockable = test_utils.CreateBlockable( file_name='Not4Mac.exe', state=constants.STATE.SUSPECT) self.santa_blockable = test_utils.CreateSantaBlockable( publisher='Arple', product_name='New Shiny', flagged=True) self.santa_certificate = test_utils.CreateSantaCertificate( id_type=constants.ID_TYPE.SHA256, common_name='Best Cert Ever', organization='Totally Legit CA') self.PatchValidateXSRFToken()
def setUp(self): super(HostTest, self).setUp() self.host = base.Host(id='bar', hostname='foo') self.blockable = test_utils.CreateBlockable() self.user1 = test_utils.CreateUser() self.user2 = test_utils.CreateUser()
def setUp(self): super(NoteTest, self).setUp() self.blockable = test_utils.CreateBlockable()
def testToDict_ContainsIsVotingAllowed(self): with self.LoggedInUser(): blockable = test_utils.CreateBlockable() self.assertIn('is_voting_allowed', blockable.to_dict())
def testIsVotingAllowed_Allowed(self): for state in constants.STATE.SET_VOTING_ALLOWED: blockable = test_utils.CreateBlockable(state=state) allowed, reason = blockable.IsVotingAllowed() self.assertTrue(allowed) self.assertIsNone(reason)
def testIsVotingAllowed_Prohibited(self): for state in constants.STATE.SET_VOTING_PROHIBITED: blockable = test_utils.CreateBlockable(state=state) allowed, reason = blockable.IsVotingAllowed() self.assertFalse(allowed) self.assertIsNotNone(reason)
def testGetById(self): blockable = test_utils.CreateBlockable() sha256 = blockable.key.id() self.assertIsNotNone(binary_models.Blockable.get_by_id(sha256.lower())) self.assertIsNotNone(binary_models.Blockable.get_by_id(sha256.upper()))
def testIsInstance(self): blockable = test_utils.CreateBlockable() self.assertTrue(blockable.IsInstance('Blockable')) self.assertTrue(blockable.IsInstance('blockable')) self.assertTrue(blockable.IsInstance('BLOCKABLE')) self.assertFalse(blockable.IsInstance('SomethingElse'))