def test_retrain(self): """ Verify that retraining a few messages will tell our new test filter to learn about a bunch of new messages. """ f = Filter(store=self.store) installOn(f, self.store) tf = TestFilter(store=self.store, test=self) installOn(tf, self.store) COUNT = 10 for j in range(2): for x in range(COUNT): msg = Message.createIncoming( self.store, DummyMessageImplementation(store=self.store), u"test://retrain" ) _spamState(msg, (x % 2), j) # This isn't quite correct. We're relying on the fact that the batch # processor is supposed to run in a subprocess (which isn't going) so # the callback is only going to be triggered for our set of messages # during the test. Bleah. def _(ign): self.assertEquals(tf.trainCount, COUNT) return f.retrain().addCallback(_)
def test_retrain(self): """ Verify that retraining a few messages will tell our new test filter to learn about a bunch of new messages. """ f = Filter(store=self.store) installOn(f, self.store) tf = TestFilter(store=self.store, test=self) installOn(tf, self.store) COUNT = 10 for j in range(2): for x in range(COUNT): msg = Message.createIncoming( self.store, DummyMessageImplementation(store=self.store), u'test://retrain') _spamState(msg, (x % 2), j) # This isn't quite correct. We're relying on the fact that the batch # processor is supposed to run in a subprocess (which isn't going) so # the callback is only going to be triggered for our set of messages # during the test. Bleah. def _(ign): self.assertEquals(tf.trainCount, COUNT) return f.retrain().addCallback(_)
def test_postiniWithoutHeaderHamFiltering(self): """ Check that when postini filtering is enabled but a message has no postini header then the other filter is consulted. """ msg = Message.createIncoming(self.store, Part(), u"test://postiniWithoutHeaderHamFiltering") f = Filter(store=self.store, usePostiniScore=True, postiniThreshhold=1.0) installOn(TestFilter(store=self.store, result=False), self.store) f.processItem(msg) self.assertNotIn(SPAM_STATUS, list(msg.iterStatuses()))
def createDatabase(s): """ Populate the given Store with a deferred message. """ messageData = DummyMessageImplementation(store=s) fakeScheduler = FakeScheduler(store=s) s.powerUp(fakeScheduler, IScheduler) m = Message.createIncoming(s, messageData, u'test') m.classifyClean() m.markRead() now = Time() m.deferFor(timedelta(days=1), timeFactory=lambda: now)
def createDatabase(s): """ Populate the given Store with a deferred message. """ messageData = DummyMessageImplementation(store=s) fakeScheduler = FakeScheduler(store=s) s.powerUp(fakeScheduler, IScheduler) m = Message.createIncoming(s, messageData, u"test") m.classifyClean() m.markRead() now = Time() m.deferFor(timedelta(days=1), timeFactory=lambda: now)
def test_postiniWithoutHeaderHamFiltering(self): """ Check that when postini filtering is enabled but a message has no postini header then the other filter is consulted. """ msg = Message.createIncoming( self.store, Part(), u'test://postiniWithoutHeaderHamFiltering') f = Filter(store=self.store, usePostiniScore=True, postiniThreshhold=1.0) installOn(TestFilter(store=self.store, result=False), self.store) f.processItem(msg) self.assertNotIn(SPAM_STATUS, list(msg.iterStatuses()))
def testEZMLMFilter(self): """ Ensure that match_EZMLM doesn't kerplode when presented with a header that doesn't parse well. """ part = Part() part.addHeader(u'X-Mailman-Version', u"2.1.5") part.addHeader(u'List-Post', u"Random bytes") part.source = FilePath(self.storepath).child("files").child("x") msg = Message.createIncoming(self.store, part, u'test://test_mailing_list_filter') self.mlfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), [])
def test_nonPart(self): """ Test that a message with an implementation which isn't a L{Part} that the Message doesn't get focused. This is primarily here for completeness at this point. The only non-Part Messages which exist are probably created by the test suite. """ impl = DummyMessageImplementation(store=self.userStore) message = Message.createIncoming(self.userStore, impl, u'test://test_nonPart') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failIfIn(FOCUS_STATUS, statuses)
def test_nonPart(self): """ Test that a message with an implementation which isn't a L{Part} that the Message doesn't get focused. This is primarily here for completeness at this point. The only non-Part Messages which exist are probably created by the test suite. """ impl = DummyMessageImplementation(store=self.userStore) message = Message.createIncoming( self.userStore, impl, u'test://test_nonPart') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failIfIn(FOCUS_STATUS, statuses)
def testEZMLMFilter(self): """ Ensure that match_EZMLM doesn't kerplode when presented with a header that doesn't parse well. """ part = Part() part.addHeader(u'X-Mailman-Version', u"2.1.5") part.addHeader(u'List-Post', u"Random bytes") part.source = FilePath(self.storepath).child("files").child("x") msg = Message.createIncoming(self.store, part, u'test://test_mailing_list_filter') self.mlfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), [])
def _setUpMsg(self): """ Install an innocuous incoming message in a newly-created store @rtype: L{xquotient.exmess.Message} """ s = self._setUpStore() m = Message.createIncoming(s, _Part(store=s), u'test://test') m.subject = u'the subject' m.sender = u'sender@host' m.senderDisplay = u'Sender' m.recipient = u'recipient@host' m.sentWhen = Time.fromPOSIXTimestamp(0) m.receivedWhen = Time.fromPOSIXTimestamp(1) m.classifyClean() return m
def testMailingListFilter(self): """ Ensures that mailing list messages are not handled by RuleFilteringPowerup but are handled by MailingListFilteringPowerup. """ part = Part() part.addHeader(u'X-Mailman-Version', u"2.1.5") part.addHeader(u'List-Id', u"Some mailing list <some-list.example.com>") part.source = FilePath(self.storepath).child("files").child("x") msg = Message.createIncoming(self.store, part, u'test://test_mailing_list_filter') self.rfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), []) self.mlfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), [u'some-list.example.com'])
def testMailingListFilter(self): """ Ensures that mailing list messages are not handled by RuleFilteringPowerup but are handled by MailingListFilteringPowerup. """ part = Part() part.addHeader(u'X-Mailman-Version', u"2.1.5") part.addHeader(u'List-Id', u"Some mailing list <some-list.example.com>") part.source = FilePath(self.storepath).child("files").child("x") msg = Message.createIncoming(self.store, part, u'test://test_mailing_list_filter') self.rfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), []) self.mlfp.processItem(msg) self.assertEqual(list(self.tagcatalog.tagsOf(msg)), [u'some-list.example.com'])
def _unfocusedTest(self, part): message = Message.createIncoming(self.userStore, part, u'test://unfocused') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failIfIn(FOCUS_STATUS, statuses)
def _focusedTest(self, part): message = Message.createIncoming( self.userStore, part, u'test://test_otherPrecedence') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failUnlessIn(FOCUS_STATUS, statuses)
def _focusedTest(self, part): message = Message.createIncoming(self.userStore, part, u'test://test_otherPrecedence') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failUnlessIn(FOCUS_STATUS, statuses)
def _unfocusedTest(self, part): message = Message.createIncoming( self.userStore, part, u'test://unfocused') self.focus.processItem(message) statuses = set(message.iterStatuses()) self.failIfIn(FOCUS_STATUS, statuses)
def testMessageFactory(store, archived=False, spam=None, read=False, sentWhen=None, receivedWhen=None, subject=u'', trash=False, outgoing=False, draft=False, impl=None, sent=True, bounced=False, sender=None, recipient=u''): """ Provide a simulacrum of message's old constructor signature to avoid unnecessarily deep modification of tests. @return: an exmess.Message object. """ if impl is None: impl = DummyMessageImplementation(store=store) if sender is not None: impl.senderInfo = sender if outgoing: m = Message.createDraft(store, impl, u'test://test/draft') if not draft: # XXX: this is *actually* the status change that transpires when # you transition a message from "draft" to "sent" status. m.startedSending() if sent: m.sent() m.finishedSending() elif bounced: m.allBounced() if spam is not None: assert spam is False, "That doesn't make any sense." else: m = Message.createIncoming(store, impl, u'test://test') if spam is not None: if spam: m.classifySpam() else: m.classifyClean() m.subject = subject m.recipient = recipient # should this be handled somewhere else? ugh. if read: m.markRead() if archived: m.archive() if trash: m.moveToTrash() # these next bits are definitely wrong. they should be set up by analysis # of the body part, probably? if receivedWhen: m.receivedWhen = receivedWhen # we're supplying our own received date, after message creation, but # this won't be reflected in the statusDate of the statuses that were # added to the message as a result of createIncoming, so we'll remove # them all and re-add them for s in m.iterStatuses(): m.removeStatus(s) m.addStatus(s) if sentWhen: m.sentWhen = sentWhen if sender: m.sender = sender m.senderDisplay = sender # Cheat so that nit test setup will work; this is gross, but inverting # it to be specified properly (in the tests' impl) would be even more # of a pain in the ass right now... -glyph Correspondent(store=store, relation=SENDER_RELATION, message=m, address=sender) return m
def testMessageFactory(store, archived=False, spam=None, read=False, sentWhen=None, receivedWhen=None, subject=u'', trash=False, outgoing=False, draft=False, impl=None, sent=True, bounced=False, sender=None, recipient=u''): """ Provide a simulacrum of message's old constructor signature to avoid unnecessarily deep modification of tests. @return: an exmess.Message object. """ if impl is None: impl = DummyMessageImplementation(store=store) if sender is not None: impl.senderInfo = sender if outgoing: m = Message.createDraft(store, impl, u'test://test/draft') if not draft: # XXX: this is *actually* the status change that transpires when # you transition a message from "draft" to "sent" status. m.startedSending() if sent: m.sent() m.finishedSending() elif bounced: m.allBounced() if spam is not None: assert spam is False, "That doesn't make any sense." else: m = Message.createIncoming(store, impl, u'test://test') if spam is not None: if spam: m.classifySpam() else: m.classifyClean() m.subject = subject m.recipient = recipient # should this be handled somewhere else? ugh. if read: m.markRead() if archived: m.archive() if trash: m.moveToTrash() # these next bits are definitely wrong. they should be set up by analysis # of the body part, probably? if receivedWhen: m.receivedWhen = receivedWhen # we're supplying our own received date, after message creation, but # this won't be reflected in the statusDate of the statuses that were # added to the message as a result of createIncoming, so we'll remove # them all and re-add them for s in m.iterStatuses(): m.removeStatus(s) m.addStatus(s) if sentWhen: m.sentWhen = sentWhen if sender: m.sender = sender m.senderDisplay = sender # Cheat so that nit test setup will work; this is gross, but inverting # it to be specified properly (in the tests' impl) would be even more # of a pain in the ass right now... -glyph Correspondent(store=store, relation=SENDER_RELATION, message=m, address=sender) return m