def testOffsetIsRelativeToFilteredResultsWhenFilterIsPresent(self): for i in range(5): self.CreateSampleHunt("foo_hunt_%d" % i, token=self.token) for i in range(3): self.CreateSampleHunt("bar_hunt_%d" % i, token=self.token) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( description_contains="bar", active_within="1d", offset=1), token=self.token) self.assertEqual(len(result["items"]), 2) for item in result["items"]: self.assertTrue("bar" in item["summary"]["description"]["value"]) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( description_contains="bar", active_within="1d", offset=2), token=self.token) self.assertEqual(len(result["items"]), 1) for item in result["items"]: self.assertTrue("bar" in item["summary"]["description"]["value"]) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( description_contains="bar", active_within="1d", offset=3), token=self.token) self.assertEqual(len(result["items"]), 0)
def testRaisesIfDescriptionContainsFilterUsedWithoutActiveWithinFilter( self): self.assertRaises( ValueError, self.renderer.Render, hunt_plugin.ApiHuntsListRendererArgs(description_contains="foo"), token=self.token)
def testFiltersHuntsByDescriptionContainsMatch(self): for i in range(5): self.CreateSampleHunt("foo_hunt_%d" % i, token=self.token) for i in range(3): self.CreateSampleHunt("bar_hunt_%d" % i, token=self.token) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( description_contains="foo", active_within="1d"), token=self.token) self.assertEqual(len(result["items"]), 5) for item in result["items"]: self.assertTrue("foo" in item["summary"]["description"]["value"]) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( description_contains="bar", active_within="1d"), token=self.token) self.assertEqual(len(result["items"]), 3) for item in result["items"]: self.assertTrue("bar" in item["summary"]["description"]["value"])
def testRendersListOfHuntObjects(self): for i in range(10): self.CreateSampleHunt("hunt_%d" % i, token=self.token) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs(), token=self.token) descriptions = set(r["summary"]["description"]["value"] for r in result["items"]) self.assertEqual(len(descriptions), 10) for i in range(10): self.assertTrue("hunt_%d" % i in descriptions)
def testHuntListIsSortedInReversedCreationTimestampOrder(self): for i in range(1, 11): with test_lib.FakeTime(i * 1000): self.CreateSampleHunt("hunt_%d" % i, token=self.token) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs(), token=self.token) create_times = [ r["summary"]["create_time"]["value"] for r in result["items"] ] self.assertEqual(len(create_times), 10) for index, expected_time in enumerate(reversed(range(1, 11))): self.assertEqual(create_times[index], expected_time * 1000000000)
def testRendersSubrangeOfListOfHuntObjects(self): for i in range(1, 11): with test_lib.FakeTime(i * 1000): self.CreateSampleHunt("hunt_%d" % i, token=self.token) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( offset=2, count=2), token=self.token) create_times = [ r["summary"]["create_time"]["value"] for r in result["items"] ] self.assertEqual(len(create_times), 2) self.assertEqual(create_times[0], 8 * 1000000000) self.assertEqual(create_times[1], 7 * 1000000000)
def testFiltersHuntsByCreator(self): for i in range(5): self.CreateSampleHunt( "foo_hunt_%d" % i, token=access_control.ACLToken(username="******")) for i in range(3): self.CreateSampleHunt( "bar_hunt_%d" % i, token=access_control.ACLToken(username="******")) result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( created_by="user-foo", active_within="1d"), token=self.token) self.assertEqual(len(result["items"]), 5) for item in result["items"]: self.assertEqual(item["summary"]["creator"]["value"], "user-foo") result = self.renderer.Render(hunt_plugin.ApiHuntsListRendererArgs( created_by="user-bar", active_within="1d"), token=self.token) self.assertEqual(len(result["items"]), 3) for item in result["items"]: self.assertEqual(item["summary"]["creator"]["value"], "user-bar")
def testFiltersHuntsByActivityTime(self): for i in range(1, 11): with test_lib.FakeTime(i * 60): self.CreateSampleHunt("hunt_%d" % i, token=self.token) with test_lib.FakeTime(10 * 60 + 1): result = self.renderer.Render( hunt_plugin.ApiHuntsListRendererArgs(active_within="2m"), token=self.token) create_times = [ r["summary"]["create_time"]["value"] for r in result["items"] ] self.assertEqual(len(create_times), 2) self.assertEqual(create_times[0], 10 * 60 * 1000000) self.assertEqual(create_times[1], 9 * 60 * 1000000)
def testRaisesIfCreatedByFilterUsedWithoutActiveWithinFilter(self): self.assertRaises( ValueError, self.renderer.Render, hunt_plugin.ApiHuntsListRendererArgs(created_by="user-bar"), token=self.token)