← Back to team overview

zeitgeist team mailing list archive

[Branch ~zeitgeist/zeitgeist/bluebird] Rev 388: Enhance test cases for blacklisting. This exposes LP: #928804.

 

------------------------------------------------------------
revno: 388
committer: Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
branch nick: bluebird
timestamp: Wed 2012-02-08 13:49:38 +0100
message:
  Enhance test cases for blacklisting. This exposes LP: #928804.
modified:
  test/dbus/blacklist-test.py


--
lp:zeitgeist
https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird

Your team Zeitgeist Framework Team is subscribed to branch lp:zeitgeist.
To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'test/dbus/blacklist-test.py'
--- test/dbus/blacklist-test.py	2012-02-07 21:08:49 +0000
+++ test/dbus/blacklist-test.py	2012-02-08 12:49:38 +0000
@@ -64,31 +64,170 @@
 		# And ensure that they are indeed gone.
 		self.assertEquals(len(self.blacklist.GetTemplates()), 0)
 
+	def _add_template(self, name, template):
+		self.blacklist.AddTemplate(name, template)
+
+		res = self.blacklist.GetTemplates()
+		self.assertEventsEqual(template, Event(res[name]))
+
+	def _assert_template_count(self, num):
+		self.assertEquals(len(self.blacklist.GetTemplates()), num)
+
+	def _assert_insert_blocked(self, *events):
+		inserted_ids = map(int, self.insertEventsAndWait(events))
+		zeros = filter(lambda x: x == 0, inserted_ids)
+		self.assertEquals(len(events), len(inserted_ids))
+		self.assertEquals(len(events), len(zeros))
+	
+	def _assert_insert_allowed(self, *events):
+		inserted_ids = map(int, self.insertEventsAndWait(events))
+		self.assertEquals(len(events), len(inserted_ids))
+		self.assertEquals([], filter(lambda x: x == 0, inserted_ids))
+
 	def testSetOne(self):
-		orig = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
+		orig = Event.new_for_values(
+			interpretation=Interpretation.ACCESS_EVENT,
 			subject_uri="http://nothingtoseehere.gov";)
-		self.blacklist.AddTemplate("Foobar", orig)
-		res = self.blacklist.GetTemplates()
-		
-		self.assertEquals(len(res), 1)
-		self.assertEventsEqual(orig, Event(res["Foobar"]))
+
+		self._add_template("Foobar", orig)
+		self._assert_template_count(1)
 
 	def testApplyBlacklist(self):
-		self.testSetOne()
-		ev = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
+		ev = Event.new_for_values(
+			interpretation=Interpretation.ACCESS_EVENT,
+			manifestation=Manifestation.USER_ACTIVITY,
+			actor="app://foo.desktop",
 			subject_uri="http://nothingtoseehere.gov";)
-		ev.manifestation = Manifestation.USER_ACTIVITY
-		ev.actor = "app.//foo.desktop"
+		self._add_template("Foobar", ev)
+		self._assert_template_count(1)
 
-		inserted_ids = self.insertEventsAndWait([ev])
-		self.assertEquals(1, len(inserted_ids))
-		self.assertEquals(0, int(inserted_ids[0]))
+		self._assert_insert_blocked(ev)
 
 		# Now change the event to pass the blacklist
-		ev.get_subjects()[0].uri = "htpp://totallyvaliduri.com"
-		inserted_ids = self.insertEventsAndWait([ev])
-		self.assertEquals(1, len(inserted_ids))
-		self.assertTrue(0 != inserted_ids[0])
+		ev.get_subjects()[0].uri = "http://totallyvaliduri.com";
+		self._assert_insert_allowed(ev)
+
+	def testApplyBlacklistWithTwoTemplates(self):
+		# Setup an event we'll use to test insertions
+		event = Event.new_for_values(
+			timestamp = 1,
+			interpretation=Interpretation.ACCESS_EVENT,
+			manifestation=Manifestation.SCHEDULED_ACTIVITY,
+			subject_uri="blarg")
+
+		# With no blacklisted templates we can insert it without problems
+		self._assert_insert_allowed(event)
+
+		# We blacklist the event's interpretation
+		templ1 = Event.new_for_values(
+			interpretation=Interpretation.ACCESS_EVENT)
+		self._add_template("One", templ1)
+		self._assert_template_count(1)
+
+		# Now it can't be inserted anymore
+		event.timestamp = 1
+		self._assert_insert_blocked(event)
+
+		# We blacklist the event's URI
+		templ2 = Event.new_for_values(
+			subject_uri="blarg")
+		self._add_template("Two", templ1)
+		self._assert_template_count(2)
+
+		# No way it can be logged now
+		event.timestamp = 2 # change timestamp so it isn't a duplicate event
+		self._assert_insert_blocked(event)
+
+		# We remove the first blacklisted template, it still can't get logged
+		self.blacklist.RemoveTemplate("One")
+		self._assert_template_count(1)
+		event.timestamp = 3
+		self._assert_insert_blocked(event)
+
+		# Removing the second template, now it'll let us insert it
+		self.blacklist.RemoveTemplate("Two")
+		self._assert_template_count(0)
+		event.timestamp = 4
+		self._assert_insert_allowed(event)
+
+		# Finally, we blacklist a template that doesn't match the event
+		templ3 = Event.new_for_values(
+			interpretation=Interpretation.ACCESS_EVENT,
+			manifestation=Manifestation.USER_ACTIVITY)
+		self._add_template("One", templ3) # reuse the template identifier, why not?
+		self._assert_template_count(1)
+
+		# And of course we can still insert it :)
+		event.timestamp = 5
+		self._assert_insert_allowed(event)
+
+	def testApplyBlacklistWithSpacesInURI(self):
+		# We blacklist a particular URI
+		templ1 = Event.new_for_values(subject_uri="New York is a city")
+		self._add_template("One", templ1)
+		self._assert_template_count(1)
+
+		# And check that it works
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri="New York is a city"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri="New York is a city NOT"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri="Do you like cheese?"))
+		self._assert_insert_allowed(Event.new_for_values(
+			interpretation=Interpretation.MOVE_EVENT,
+			subject_uri="kung fu",
+			subject_current_uri="New York is a city"))
+
+	def testApplyBlacklistWithAccentsInURI(self):
+		# We blacklist some particular URIs
+		self._add_template("weirdo", Event.new_for_values(
+			subject_uri=u"çàrßá€"))
+		self._add_template("normalo", Event.new_for_values(
+			subject_uri=u"hello"))
+		self._assert_template_count(2)
+
+		# And check that the blacklisting works
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"çàrßá€"))
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"hello"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri=u"hola"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri=u"çàrßá"))
+
+	def testApplyBlacklistForEventWithEmptyCurrentURI(self):
+		# We blacklist some particular current URI
+		self._add_template("t", Event.new_for_values(subject_current_uri=u"t"))
+		self._assert_template_count(1)
+
+		# Blocking the current_uri works
+		self._assert_insert_blocked(Event.new_for_values(subject_current_uri="t"))
+
+		# But if we only set uri (and leave it up to Zeitgeist to set current_uri
+		# to the same value?
+		self._assert_insert_blocked(Event.new_for_values(subject_uri="t"))
+
+	def testApplyBlacklistWithWildcardInURI(self):
+		# We blacklist some particular URIs
+		self._add_template("wild", Event.new_for_values(
+			subject_uri=u"block me*"))
+		self._assert_template_count(1)
+
+		# And check that the blacklisting works
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"block me"))
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"block me*"))
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"block me now"))
+		self._assert_insert_blocked(Event.new_for_values(
+			subject_uri=u"block meß :)"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri=u"block mNOT"))
+		self._assert_insert_allowed(Event.new_for_values(
+			subject_uri=u"nblock me"))
 
 	def _get_blacklist_iface(self):
 		"""