Beispiel #1
0
    def testSearchWithBlacklistedClientIdsReturnsNothing(self):
        result = self.handler.Handle(
            client_plugin.ApiSearchClientsArgs(query=self.client_ids[1]),
            token=self.token)
        self.assertFalse(result.items)

        result = self.handler.Handle(
            client_plugin.ApiSearchClientsArgs(query=self.client_ids[2]),
            token=self.token)
        self.assertFalse(result.items)
Beispiel #2
0
  def testSearchWithDisallowedClientIdsReturnsNothing(self):
    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query=self.client_ids[1]),
        context=self.context)
    self.assertFalse(result.items)

    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query=self.client_ids[2]),
        context=self.context)
    self.assertFalse(result.items)
Beispiel #3
0
  def testSearchWithWhitelistedLabelReturnsSubSet(self):
    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query="label:foo"), token=self.token)
    self.assertEqual(len(result.items), 1)
    self.assertEqual(result.items[0].client_id, self.client_ids[0])

    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query="label:bar"), token=self.token)
    self.assertEqual(len(result.items), 1)
    self.assertEqual(result.items[0].client_id, self.client_ids[3])
Beispiel #4
0
  def testSearchWithAllowedClientIdsReturnsSubSet(self):
    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query=self.client_ids[0]),
        context=self.context)
    self.assertLen(result.items, 1)
    self.assertEqual(result.items[0].client_id, self.client_ids[0])

    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(query=self.client_ids[3]),
        context=self.context)
    self.assertLen(result.items, 1)
    self.assertEqual(result.items[0].client_id, self.client_ids[3])
Beispiel #5
0
    def testSearchWithWhitelistedClientIdsReturnsSubSet(self):
        result = self.handler.Handle(
            client_plugin.ApiSearchClientsArgs(query=self.client_ids[0]),
            token=self.token)
        self.assertLen(result.items, 1)
        self.assertEqual(result.items[0].client_id, self.client_ids[0])

        result = self.handler.Handle(
            client_plugin.ApiSearchClientsArgs(query=self.client_ids[3]),
            token=self.token)
        self.assertLen(result.items, 1)
        self.assertEqual(result.items[0].client_id, self.client_ids[3])
Beispiel #6
0
    def testSearchOrder(self):
        self._Setup100Clients()

        result = self.handler.Handle(client_plugin.ApiSearchClientsArgs(
            query="label:foo", offset=0, count=1000),
                                     token=self.token)
        self.assertEqual([str(res.client_id) for res in result.items],
                         self.client_ids)

        result = []
        for offset, count in [(0, 10), (10, 40), (50, 25), (75, 500)]:
            result.extend(
                self.handler.Handle(client_plugin.ApiSearchClientsArgs(
                    query="label:foo", offset=offset, count=count),
                                    token=self.token).items)
        self.assertEqual([str(res.client_id) for res in result],
                         self.client_ids)
Beispiel #7
0
  def Run(self):
    # Fix the time to avoid regressions.
    with test_lib.FakeTime(42):
      client_id = self.SetupClient(0)

      self.Check(
          "SearchClients",
          args=client_plugin.ApiSearchClientsArgs(query=client_id))
Beispiel #8
0
    def testSearchWithoutArgsReturnsOnlyClientsWithWhitelistedLabels(self):
        result = self.handler.Handle(client_plugin.ApiSearchClientsArgs(),
                                     token=self.token)

        self.assertEqual(len(result.items), 2)
        sorted_items = sorted(result.items, key=lambda r: r.client_id)

        self.assertEqual(sorted_items[0].client_id, self.client_ids[0])
        self.assertEqual(sorted_items[1].client_id, self.client_ids[3])
Beispiel #9
0
  def testSearchWithoutArgsReturnsOnlyClientsWithAllowedLabels(self):
    result = self.handler.Handle(
        client_plugin.ApiSearchClientsArgs(), context=self.context)

    self.assertLen(result.items, 2)
    sorted_items = sorted(result.items, key=lambda r: r.client_id)

    self.assertEqual(sorted_items[0].client_id, self.client_ids[0])
    self.assertEqual(sorted_items[1].client_id, self.client_ids[3])
Beispiel #10
0
 def testSearchByLabel(self):
   client_id = self.SetupClient(
       0, ping=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(13))
   self._AddLabels(client_id, labels=["foo"])
   api_result = self.search_handler.Handle(
       client_plugin.ApiSearchClientsArgs(query="label:foo"))
   self.assertLen(api_result.items, 1)
   self.assertEqual(api_result.items[0].client_id, client_id)
   self.assertEqual(api_result.items[0].last_seen_at,
                    rdfvalue.RDFDatetime.FromSecondsSinceEpoch(13))
Beispiel #11
0
  def testUnicodeQuoted(self):
    client_id = self.SetupClient(0)
    self._AddLabels(
        client_id, labels=["dzięcioł białoszyi", "świergotek łąkowy"])

    args = client_plugin.ApiSearchClientsArgs(
        query="label:'dzięcioł białoszyi' label:'świergotek łąkowy'")

    result = self.search_handler.Handle(args, context=self.context)
    self.assertLen(result.items, 1)
    self.assertEqual(result.items[0].client_id, client_id)
Beispiel #12
0
  def testUnicode(self):
    client_a_id = self.SetupClient(0)
    self._AddLabels(client_a_id, labels=["gżegżółka"])

    client_b_id = self.SetupClient(1)
    self._AddLabels(client_b_id, labels=["orłosęp"])

    args_a = client_plugin.ApiSearchClientsArgs(
        query="label:gżegżółka", offset=0, count=128)

    result_a = self.search_handler.Handle(args_a, context=self.context)
    self.assertLen(result_a.items, 1)
    self.assertEqual(result_a.items[0].client_id, client_a_id)

    args_b = client_plugin.ApiSearchClientsArgs(
        query="label:orłosęp", offset=0, count=128)

    result_b = self.search_handler.Handle(args_b, context=self.context)
    self.assertLen(result_b.items, 1)
    self.assertEqual(result_b.items[0].client_id, client_b_id)
Beispiel #13
0
  def testUnicodeMultipleClients(self):
    client_a_id = self.SetupClient(0)
    self._AddLabels(client_a_id, labels=["ścierwnik", "krzyżówka"])

    client_b_id = self.SetupClient(1)
    self._AddLabels(client_b_id, labels=["nurogęś", "ścierwnik"])

    args = client_plugin.ApiSearchClientsArgs(
        query="label:ścierwnik", offset=0, count=1000)

    result = self.search_handler.Handle(args, context=self.context)
    result_client_ids = [item.client_id for item in result.items]
    self.assertCountEqual(result_client_ids, [client_a_id, client_b_id])
Beispiel #14
0
    def Run(self):
        # Fix the time to avoid regressions.
        with test_lib.FakeTime(42):
            if data_store.RelationalDBReadEnabled():
                client_obj = self.SetupTestClientObject(0)
                client_id = client_obj.client_id
            else:
                client_urn = self.SetupClient(0, add_cert=False)
                client_id = client_urn.Basename()

            self.Check(
                "SearchClients",
                args=client_plugin.ApiSearchClientsArgs(query=client_id))
Beispiel #15
0
  def testUnicodeMultipleLabels(self):
    client_a_id = self.SetupClient(0)
    self._AddLabels(client_a_id, labels=["pustułka", "sokół", "raróg"])

    client_b_id = self.SetupClient(1)
    self._AddLabels(client_b_id, labels=["raróg", "żuraw", "białozór"])

    client_c_id = self.SetupClient(2)
    self._AddLabels(client_c_id, labels=["raróg", "sokół", "gołąb"])

    args = client_plugin.ApiSearchClientsArgs(
        query="label:raróg label:sokół", offset=0, count=1000)

    result = self.search_handler.Handle(args, context=self.context)
    result_client_ids = [item.client_id for item in result.items]
    self.assertCountEqual(result_client_ids, [client_a_id, client_c_id])
Beispiel #16
0
 def testSearchWithNonWhitelistedLabelReturnsNothing(self):
     result = self.handler.Handle(
         client_plugin.ApiSearchClientsArgs(query="label:not-foo"),
         token=self.token)
     self.assertFalse(result.items)
Beispiel #17
0
 def testSearchWithNonAllowedLabelReturnsNothing(self):
   result = self.handler.Handle(
       client_plugin.ApiSearchClientsArgs(query="label:not-foo"),
       context=self.context)
   self.assertFalse(result.items)