예제 #1
0
    def test_on_listen(self):
        user = self.make_user()

        if self.to_user is False:
            to_user = None
        elif self.to_user is True:
            to_user = user
        else:
            to_user = factory.make_User()

        notification = factory.make_Notification(
            user=to_user, users=self.to_users, admins=self.to_admins
        )

        if notification.is_relevant_to(user):
            expected = MatchesAll(
                IsInstance(tuple),
                MatchesListwise(
                    (
                        Equals("notification"),
                        Equals("create"),
                        MatchesRenderedNotification(notification),
                    )
                ),
                first_only=True,
            )
        else:
            expected = Is(None)

        handler = NotificationHandler(user, {}, None)
        self.assertThat(
            handler.on_listen("notification", "create", notification.id),
            expected,
        )
예제 #2
0
    def test_on_listen_for_edited_notification_does_nothing(self):
        super_on_listen = self.patch(Handler, "on_listen")
        super_on_listen.return_value = sentinel.on_listen

        user = factory.make_User()
        handler = NotificationHandler(user, {})
        notification = factory.make_Notification(user=user)
        notification.dismiss(user)

        self.assertThat(
            handler.on_listen("notification", "update", notification.id),
            Is(None))
        self.assertThat(super_on_listen, MockNotCalled())
예제 #3
0
    def test_on_listen_for_notification_up_calls(self):
        super_on_listen = self.patch(Handler, "on_listen")
        super_on_listen.return_value = sentinel.on_listen

        user = factory.make_User()
        handler = NotificationHandler(user, {})

        self.assertThat(
            handler.on_listen("notification", sentinel.action, sentinel.pk),
            Is(sentinel.on_listen))
        self.assertThat(
            super_on_listen,
            MockCalledOnceWith("notification", sentinel.action, sentinel.pk))
예제 #4
0
    def test_on_listen_for_dismissal_for_other_user_does_nothing(self):
        super_on_listen = self.patch(Handler, "on_listen")
        super_on_listen.return_value = sentinel.on_listen

        user = factory.make_User()
        handler = NotificationHandler(user, {})
        notification = factory.make_Notification(user=user)

        # A dismissal notification from the database FOR ANOTHER USER.
        dismissal = "%d:%d" % (notification.id, random.randrange(1, 99999))

        self.assertThat(
            handler.on_listen("notificationdismissal", sentinel.action,
                              dismissal), Is(None))
        self.assertThat(super_on_listen, MockNotCalled())
예제 #5
0
    def test_on_listen_for_dismissal_up_calls_with_delete(self):
        super_on_listen = self.patch(Handler, "on_listen")
        super_on_listen.return_value = sentinel.on_listen

        user = factory.make_User()
        handler = NotificationHandler(user, {})
        notification = factory.make_Notification(user=user)

        # A dismissal notification from the database.
        dismissal = "%d:%d" % (notification.id, user.id)

        self.assertThat(
            handler.on_listen("notificationdismissal", sentinel.action,
                              dismissal), Is(sentinel.on_listen))
        self.assertThat(
            super_on_listen,
            MockCalledOnceWith("notification", "delete", notification.id))