def test_getRecipientFilterData_empty(self):
     # When there is empty input, there is empty output.
     self.assertEqual(
         BugNotificationSet().getRecipientFilterData(self.bug, {}, []), {})
     self.assertEqual(
         BugNotificationSet().getRecipientFilterData(
             self.bug, {}, [self.notification]), {})
    def test_muted_filters_for_teams_with_contact_addresses_dont_oops(self):
        # If a user holds a mute on a Team subscription,
        # getRecipientFilterData() will handle the mute correctly.
        # This is a regression test for bug 778847.
        team_owner = self.factory.makePerson(name="team-owner")
        team = self.factory.makeTeam(email="*****@*****.**",
                                     owner=team_owner)
        product = self.factory.makeProduct()
        store = Store.of(product)
        with person_logged_in(team_owner):
            subscription = product.addBugSubscription(team, team_owner)
            subscription_filter = subscription.bug_filters.one()
            # We need to add this mute manually instead of calling
            # subscription_filter.mute, since mute() prevents mutes from
            # occurring on teams that have contact addresses. Since
            # we're testing for regression here we cheerfully ignore
            # that rule.
            mute = BugSubscriptionFilterMute()
            mute.person = team_owner
            mute.filter = subscription_filter.id
            store.add(mute)

        bug = self.factory.makeBug(target=product)
        transaction.commit()
        # Ensure that the notification about the bug being created will
        # appear when we call getNotificationsToSend() by setting its
        # message's datecreated time to 1 hour in the past.
        store.execute("""
            UPDATE Message SET
                datecreated = now() at time zone 'utc' - interval '1 hour'
            WHERE id IN (
                SELECT message FROM BugNotification WHERE bug = %s);
            """ % bug.id)
        [notification] = BugNotificationSet().getNotificationsToSend()
        # In this situation, only the team's subscription should be
        # returned, since the Person has muted the subscription (whether
        # or not they'll get email from the team contact address is not
        # covered by this code).
        self.assertEqual(
            {
                team: {
                    'filter descriptions': [],
                    'sources': [notification.recipients[1]]
                }
            },
            BugNotificationSet().getRecipientFilterData(
                bug, {
                    team.teamowner: [notification.recipients[0]],
                    team: [notification.recipients[1]]
                }, [notification]))
 def test_getRecipientFilterData_mute_one_person_of_two(self):
     self.includeFilterInNotification()
     # Mute the first filter.
     BugSubscriptionFilterMute(person=self.subscriber,
                               filter=self.notification.bug_filters.one())
     subscriber2 = self.factory.makePerson()
     subscription2 = self.bug.default_bugtask.target.addSubscription(
         subscriber2, subscriber2)
     notification2 = self.addNotification(subscriber2)
     self.includeFilterInNotification(subscription=subscription2,
                                      description=u'Special Filter!',
                                      notification=notification2)
     sources = list(self.notification.recipients)
     sources2 = list(notification2.recipients)
     self.assertEqual(
         {
             subscriber2: {
                 'sources': sources2,
                 'filter descriptions': [u'Special Filter!']
             }
         },
         BugNotificationSet().getRecipientFilterData(
             self.bug, {
                 self.subscriber: sources,
                 subscriber2: sources2
             }, [self.notification, notification2]))
 def test_getRecipientFilterData_other_persons(self):
     # When there is no named bug filter for the recipient,
     # it returns the recipient but with no filter descriptions.
     self.includeFilterInNotification()
     subscriber2 = self.factory.makePerson()
     subscription2 = self.bug.default_bugtask.target.addSubscription(
         subscriber2, subscriber2)
     notification2 = self.addNotification(subscriber2)
     self.includeFilterInNotification(subscription=subscription2,
                                      description=u'Special Filter!',
                                      notification=notification2)
     sources = list(self.notification.recipients)
     sources2 = list(notification2.recipients)
     self.assertEqual(
         {
             self.subscriber: {
                 'sources': sources,
                 'filter descriptions': []
             },
             subscriber2: {
                 'sources': sources2,
                 'filter descriptions': [u'Special Filter!']
             }
         },
         BugNotificationSet().getRecipientFilterData(
             self.bug, {
                 self.subscriber: sources,
                 subscriber2: sources2
             }, [self.notification, notification2]))
 def test_getRecipientFilterData_mute_bug_mutes_filter(self):
     # Mute the bug for the subscriber.
     self.bug.mute(self.subscriber, self.subscriber)
     self.includeFilterInNotification(description=u'Special Filter!')
     sources = list(self.notification.recipients)
     self.assertEqual({},
                      BugNotificationSet().getRecipientFilterData(
                          self.bug, {self.subscriber: sources},
                          [self.notification]))
 def test_getRecipientFilterData_mute(self):
     # When there are bug filters for the recipient,
     # only those filters are returned.
     self.includeFilterInNotification(description=u'Special Filter!')
     # Mute the first filter.
     BugSubscriptionFilterMute(person=self.subscriber,
                               filter=self.notification.bug_filters.one())
     sources = list(self.notification.recipients)
     self.assertEqual({},
                      BugNotificationSet().getRecipientFilterData(
                          self.bug, {self.subscriber: sources},
                          [self.notification]))
    def test_getRecipientFilterData_mute_bug_mutes(self):
        # Mute the bug for the subscriber.
        self.team = self.factory.makeTeam()
        self.subscriber.join(self.team)

        self.bug.mute(self.subscriber, self.subscriber)
        sources = list(self.notification.recipients)
        # Perform the test.
        self.assertEqual({},
                         BugNotificationSet().getRecipientFilterData(
                             self.bug, {self.subscriber: sources},
                             [self.notification]))
 def test_getRecipientFilterData_match(self):
     # When there are bug filters for the recipient,
     # only those filters are returned.
     self.includeFilterInNotification(description=u'Special Filter!')
     sources = list(self.notification.recipients)
     self.assertEqual(
         {
             self.subscriber: {
                 'sources': sources,
                 'filter descriptions': ['Special Filter!']
             }
         },
         BugNotificationSet().getRecipientFilterData(
             self.bug, {self.subscriber: sources}, [self.notification]))
 def test_addNotification_without_recipients(self):
     # We can call BugNotificationSet.addNotification() with a empty
     # recipient list.
     #
     # No explicit assertion is necessary in this test -- we just want
     # to be sure that calling BugNotificationSet.addNotification()
     # does not lead to an exception caused by an SQL syntax error for
     # a command that ends with "VALUES ;"
     bug = self.factory.makeBug()
     message = MessageSet().fromText(subject='subject', content='content')
     BugNotificationSet().addNotification(bug=bug,
                                          is_comment=False,
                                          message=message,
                                          recipients=[],
                                          activity=None)
 def test_getRecipientFilterData_mute_both_filters_mutes(self):
     self.prepareTwoNotificationsWithFilters()
     # Mute the first filter.
     BugSubscriptionFilterMute(person=self.subscriber,
                               filter=self.notification.bug_filters.one())
     # Mute the second filter.
     BugSubscriptionFilterMute(person=self.subscriber,
                               filter=self.notification2.bug_filters.one())
     sources = list(self.notification.recipients)
     sources.extend(self.notification2.recipients)
     # Perform the test.
     self.assertEqual({},
                      BugNotificationSet().getRecipientFilterData(
                          self.bug, {self.subscriber: sources},
                          [self.notification, self.notification2]))
class TestGetDeferredNotifications(TestCaseWithFactory):
    """Test the getDeferredNotifications method."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestGetDeferredNotifications, self).setUp()
        self.bns = BugNotificationSet()

    def test_no_deferred_notifications(self):
        results = self.bns.getDeferredNotifications()
        self.assertEqual(0, results.count())

    def _make_deferred_notification(self):
        bug = self.factory.makeBug()
        empty_recipients = BugNotificationRecipients()
        message = getUtility(IMessageSet).fromText('subject',
                                                   'a comment.',
                                                   bug.owner,
                                                   datecreated=datetime.now(
                                                       pytz.UTC))
        self.bns.addNotification(bug,
                                 False,
                                 message,
                                 empty_recipients,
                                 None,
                                 deferred=True)

    def test_one_deferred_notification(self):
        self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(1, results.count())

    def test_many_deferred_notification(self):
        num = 5
        for i in xrange(num):
            self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(num, results.count())

    def test_destroy_notifications(self):
        self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(1, results.count())
        notification = results[0]
        notification.destroySelf()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(0, results.count())
 def test_getRecipientFilterData_mute_one_filter_of_two(self):
     self.prepareTwoNotificationsWithFilters()
     # Mute the first filter.
     BugSubscriptionFilterMute(person=self.subscriber,
                               filter=self.notification.bug_filters.one())
     sources = list(self.notification.recipients)
     sources.extend(self.notification2.recipients)
     # Perform the test.
     self.assertEqual(
         {
             self.subscriber: {
                 'sources': sources,
                 'filter descriptions': ['Another Filter!']
             }
         },
         BugNotificationSet().getRecipientFilterData(
             self.bug, {self.subscriber: sources},
             [self.notification, self.notification2]))
 def test_getRecipientFilterData_multiple_notifications_match(self):
     # When there are bug filters for the recipient for multiple
     # notifications, return filters for all the notifications.
     self.prepareTwoNotificationsWithFilters()
     # Perform the test.
     sources = list(self.notification.recipients)
     sources.extend(self.notification2.recipients)
     assert (len(sources) == 2)
     self.assertEqual(
         {
             self.subscriber: {
                 'sources': sources,
                 'filter descriptions':
                 ['Another Filter!', 'Special Filter!']
             }
         },
         BugNotificationSet().getRecipientFilterData(
             self.bug, {self.subscriber: sources},
             [self.notification, self.notification2]))
    def test_getRecipientFilterData_mute_bug_mutes_only_themselves(self):
        # Mute the bug for the subscriber.
        self.bug.mute(self.subscriber, self.subscriber)

        # Notification for the other person still goes through.
        person = self.factory.makePerson(name='other')
        self.addNotificationRecipient(self.notification, person)

        sources = list(self.notification.recipients)

        # Perform the test.
        self.assertEqual(
            {person: {
                'filter descriptions': [],
                'sources': sources
            }},
            BugNotificationSet().getRecipientFilterData(
                self.bug, {
                    self.subscriber: sources,
                    person: sources
                }, [self.notification]))
class TestGetDeferredNotifications(TestCaseWithFactory):
    """Test the getDeferredNotifications method."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestGetDeferredNotifications, self).setUp()
        self.bns = BugNotificationSet()

    def test_no_deferred_notifications(self):
        results = self.bns.getDeferredNotifications()
        self.assertEqual(0, results.count())

    def _make_deferred_notification(self):
        bug = self.factory.makeBug()
        empty_recipients = BugNotificationRecipients()
        message = getUtility(IMessageSet).fromText(
            'subject', 'a comment.', bug.owner,
            datecreated=datetime.now(pytz.UTC))
        self.bns.addNotification(
            bug, False, message, empty_recipients, None, deferred=True)

    def test_one_deferred_notification(self):
        self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(1, results.count())

    def test_many_deferred_notification(self):
        num = 5
        for i in xrange(num):
            self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(num, results.count())

    def test_destroy_notifications(self):
        self._make_deferred_notification()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(1, results.count())
        notification = results[0]
        notification.destroySelf()
        results = self.bns.getDeferredNotifications()
        self.assertEqual(0, results.count())
 def setUp(self):
     super(TestGetDeferredNotifications, self).setUp()
     self.bns = BugNotificationSet()
 def setUp(self):
     super(TestGetDeferredNotifications, self).setUp()
     self.bns = BugNotificationSet()