def test_render_allows_markup_in_message_but_escapes_context(self): message = "<foo>{bar}</foo>" context = {"bar": "<BAR>"} notification = Notification(message=message, context=context) self.assertThat( notification.render(), Equals("<foo><BAR></foo>") )
def test_render_combines_message_with_context(self): thing_a = factory.make_name("a") thing_b = random.randrange(1000) message = "There are {b:d} of {a} in my suitcase." context = {"a": thing_a, "b": thing_b} notification = Notification(message=message, context=context) self.assertThat( notification.render(), Equals("There are " + str(thing_b) + " of " + thing_a + " in my suitcase."))
def test_not_dismissable(self): notification = Notification(message="Some notification", dismissable=False) self.assertRaises( NotificationNotDismissable, notification.dismiss, factory.make_User(), )
def test_syncs_deprecation_notifications(self): Notification(ident="deprecation_test", message="some text").save() with post_commit_hooks: start_up.inner_start_up(master=True) # existing deprecations are removed since none is active self.assertEqual( Notification.objects.filter( ident__startswith="deprecation_").count(), 0, )
def test_save_checks_that_rendering_works(self): message = "Dude, where's my {thing}?" notification = Notification(message=message) error = self.assertRaises(ValidationError, notification.save) self.assertThat( error.message_dict, Equals({"__all__": ["Notification cannot be rendered."]}), ) self.assertThat(notification.id, Is(None)) self.assertThat(Notification.objects.all(), HasLength(0))
def test_for_admins(self): notification = Notification(admins=True, message="The cat in the {place}", context=dict(place="lava pit"), category=self.category) self.assertThat( notification, AfterPreprocessing( repr, Equals("<Notification %s user=None users=False admins=True " "'The cat in the lava pit'>" % self.category.upper())))
def test_for_user(self): notification = Notification(user=factory.make_User("foobar"), message="The cat in the {place}", context=dict(place="bear trap"), category=self.category) self.assertThat( notification, AfterPreprocessing( repr, Equals( "<Notification %s user='******' users=False admins=False " "'The cat in the bear trap'>" % self.category.upper())))
def test_for_users(self): notification = Notification( users=True, message="The cat in the {place}", context=dict(place="blender"), category=self.category, ) self.assertThat( notification, AfterPreprocessing( repr, Equals("<Notification %s user=None users=True admins=False " "'The cat in the blender'>" % self.category.upper()), ), )