Beispiel #1
0
    def testIsVotingAllowed_DisableHasFlaggedChecks(self):
        blockables = test_utils.CreateSantaBlockables(26)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=blockables)

        # Flag one of the binaries.
        blockables[0].flagged = True
        blockables[0].put()

        with self.LoggedInUser():
            # Ensure that the normal call succeeds in finding the flagged binary.
            allowed, reason = bundle.IsVotingAllowed()
            self.assertFalse(allowed)
            self.assertEqual(
                constants.VOTING_PROHIBITED_REASONS.FLAGGED_BINARY, reason)

            # In a transaction, the 26 searched blockables should exceed the allowed
            # limit of 25.
            with self.assertRaises(db.BadRequestError):
                ndb.transaction(
                    lambda: bundle.IsVotingAllowed(enable_flagged_checks=True),
                    xg=True)

            # With the checks disabled, IsVotingAllowed shouldn't raise an exception.
            def Test():
                allowed, reason = bundle.IsVotingAllowed(
                    enable_flagged_checks=False)
                self.assertTrue(allowed)
                self.assertIsNone(reason)

            ndb.transaction(Test, xg=True)
Beispiel #2
0
    def testHasFlaggedCert_No(self):

        cert = test_utils.CreateSantaCertificate(flagged=False)
        bundle_binaries = test_utils.CreateSantaBlockables(10,
                                                           cert_key=cert.key)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertFalse(bundle.HasFlaggedCert())
Beispiel #3
0
    def testHasFlaggedBinary_Yes(self):

        bundle_binaries = test_utils.CreateSantaBlockables(10, flagged=False)
        bundle_binaries[-1].flagged = True
        bundle_binaries[-1].put()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertTrue(bundle.HasFlaggedBinary())
Beispiel #4
0
    def testHasFlaggedCert_Yes(self):

        cert = test_utils.CreateSantaCertificate(flagged=True)
        bundle_binaries = test_utils.CreateSantaBlockables(10)
        bundle_binaries[-1].cert_key = cert.key
        bundle_binaries[-1].put()
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertTrue(bundle.HasFlaggedCert())
Beispiel #5
0
def CreateTestEntities(email_addr):
    """Create some test Datastore data if specified, but only if running locally.

  Note that this code doesn't (and shouldn't) delete any existing entities.
  The risk of such code being accidentally triggered in prod is too great, so
  if local entities need to be deleted, use the local Datastore viewer (e.g.
  http://127.0.0.1:8000/datastore).

  Args:
    email_addr: Email address of the local users for whom test data should
        be created.

  Raises:
    NotRunningLocally: if called anywhere other than a local deployment.
  """
    if not utils.RunningLocally():
        raise NotRunningLocally

    # Create a user entity with all available roles.
    user = base.User.GetOrInsert(email_addr=email_addr)
    base.User.SetRoles(email_addr, constants.USER_ROLE.SET_ALL)

    username = user_map.EmailToUsername(email_addr)

    # Create associated SantaHosts for the user.
    santa_hosts = test_utils.CreateSantaHosts(2, primary_user=username)

    # For each SantaHost, create some SantaEvents.
    for santa_host in santa_hosts:
        for santa_blockable in test_utils.CreateSantaBlockables(5):

            parent_key = model_utils.ConcatenateKeys(user.key, santa_host.key,
                                                     santa_blockable.key)
            test_utils.CreateSantaEvent(
                santa_blockable,
                executing_user=username,
                event_type=constants.EVENT_TYPE.BLOCK_BINARY,
                host_id=santa_host.key.id(),
                parent=parent_key)

    # Create associated Bit9Hosts for the user.
    bit9_hosts = test_utils.CreateBit9Hosts(2, users=[username])

    # For each Bit9Host, create some Bit9Events.
    for bit9_host in bit9_hosts:
        for bit9_binary in test_utils.CreateBit9Binaries(5):

            parent_key = model_utils.ConcatenateKeys(user.key, bit9_host.key,
                                                     bit9_binary.key)
            test_utils.CreateBit9Event(
                bit9_binary,
                executing_user=username,
                event_type=constants.EVENT_TYPE.BLOCK_BINARY,
                host_id=bit9_host.key.id(),
                parent=parent_key)
Beispiel #6
0
    def testGet_Success_Bundle(self):
        test_blockables = test_utils.CreateSantaBlockables(4)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=test_blockables)

        with self.LoggedInUser():
            response = self.testapp.get(self.ROUTE % bundle.key.id())
        output = response.json

        self.assertSameElements(
            (blockable.key.id() for blockable in test_blockables),
            (blockable_dict['id'] for blockable_dict in output))
Beispiel #7
0
    def testIsVotingAllowed_HasFlaggedBinary(self):
        # First, create two unflagged binaries.
        blockables = test_utils.CreateSantaBlockables(2)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=blockables)

        with self.LoggedInUser():
            allowed, reason = bundle.IsVotingAllowed()
            self.assertTrue(allowed)

            # Now flag one of the binaries.
            blockables[0].flagged = True
            blockables[0].put()

            allowed, reason = bundle.IsVotingAllowed()
            self.assertFalse(allowed)
            self.assertEqual(
                constants.VOTING_PROHIBITED_REASONS.FLAGGED_BINARY, reason)
Beispiel #8
0
    def testHasFlaggedBinary_No(self):

        bundle_binaries = test_utils.CreateSantaBlockables(10, flagged=False)
        bundle = test_utils.CreateSantaBundle(bundle_binaries=bundle_binaries)

        self.assertFalse(bundle.HasFlaggedBinary())