Example #1
0
  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)
Example #2
0
 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)
Example #3
0
 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)
Example #5
0
    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)
Example #6
0
 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)
Example #7
0
  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)
Example #8
0
  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'])
Example #9
0
    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()
Example #10
0
    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()
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
  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)
Example #14
0
  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()
Example #16
0
 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()
Example #17
0
 def setUp(self):
   super(NoteTest, self).setUp()
   self.blockable = test_utils.CreateBlockable()
Example #18
0
 def testToDict_ContainsIsVotingAllowed(self):
     with self.LoggedInUser():
         blockable = test_utils.CreateBlockable()
         self.assertIn('is_voting_allowed', blockable.to_dict())
Example #19
0
 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)
Example #20
0
 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)
Example #21
0
 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()))
Example #22
0
 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'))