def test_sendMailQueue(self): c = core.QueueMessageCounter() global count count = 0 def fakeWriteMsgToDisk(a, b): global count if a == "/dev/null" and b == "*****@*****.**": count += 1 c.writeMsgToDisk = fakeWriteMsgToDisk msgs = [ QueueMessageCounterTest.MockMessage("*****@*****.**"), QueueMessageCounterTest.MockMessage("*****@*****.**") ] for msg in msgs: for i in xrange(10): c.saveMsgLater(msg, "/dev/null", "TestMsg-%s" % msg.FROM) c.sendMailQueue("*****@*****.**") self.assertEqual( count, 10, "writeMsgToDisk called %s should be called 10 times" % count) del count # Clean up count for everyone else
def test_saveMsgLater_Single(self): c = core.QueueMessageCounter() c.saveMsgLater(QueueMessageCounterTest.MockMessage("*****@*****.**"), "/dev/null", "test text of really boring type/n" * 20) self.assertEqual(len(c.mailQueues), 1, "mailQueues len should be 1 after save") self.assertEqual(len(c.mailQueues["*****@*****.**"]), 1, "mailQueues len should be 1 after save")
def __init__(self, configPath=""): core.loggerSetup() self.logger = logging.getLogger("MTDaemon") self.loadConfig(configPath) self.counter = core.QueueMessageCounter() self.mailDelivery = mailServer.LocalDelivery(self.counter) self.mailFactory = mailServer.SMTPFactory(self.mailDelivery) self.adminSite = webServer.createSite(self.mailDelivery) self.initaliseReactor()
def test_createCounterStartsAtZero(self): c = core.QueueMessageCounter() self.assertEqual(len(c.queues), 0, "queues len should be 0 on creation") self.assertEqual(len(c.queuetotals), 0, "queuetotals len should be 0 on creation") self.assertEqual(len(c.mailQueues), 0, "mailQueues len should be 0 on creation") self.assertEqual( c.lastExcessEmailSent, datetime.datetime.min, "lastExcessEmailSent should be datetime.min at creation")
def test_clearCount(self): c = core.QueueMessageCounter() for i in xrange(10): c.incrementCount(QueueMessageCounterTest.MockMessage("*****@*****.**")) c.clearCount() self.assertEquals(c.count, 0, "Count should be 0 after clear") self.assertEquals(c.totalCount, 10, "TotalCount should be 10 after clear") self.assertEqual(c.queues["*****@*****.**"], 0, "queues len should be 0 after clear") self.assertEqual(c.queuetotals["*****@*****.**"], 10, "queuetotals len should be 10 after clear")
def test_incrementCountSingleFrom(self): c = core.QueueMessageCounter() c.incrementCount(QueueMessageCounterTest.MockMessage("*****@*****.**")) self.assertEquals(c.count, 1, "Count should be 1") self.assertEquals(c.totalCount, 1, "TotalCount should be 1") self.assertEqual(len(c.queues), 1, "queues len should be 1 after increment") self.assertEqual(len(c.queuetotals), 1, "queuetotals len should be 1 after increment") self.assertEqual(c.queues["*****@*****.**"], 1, "queues len should be 1 after increment") self.assertEqual(c.queuetotals["*****@*****.**"], 1, "queuetotals len should be 1 after increment")
def test_sendExcessEmailWithNoExcess(self): c = core.QueueMessageCounter() def raiseException(*args): raise Exception("createExcessEmail should not be called") c.createExcessEmail = raiseException lastSent = c.lastExcessEmailSent emailSent = c.sendExcessEmail([]) self.assertFalse(emailSent, "An emtpy excess queue should not send an email") self.assertEqual( c.lastExcessEmailSent, lastSent, "lastExcessEmailSent should not change when an email is not sent")
def test_sendExcessEmailWithExcessButSentOneInLastMinTimeDelta(self): c = core.QueueMessageCounter() def raiseException(*args): raise Exception("function should not be called") c.createExcessEmail = raiseException c.writeMsgToDisk = raiseException c.notWithinExcessEmailTimeDelta = lambda *args: False lastSent = c.lastExcessEmailSent emailSent = c.sendExcessEmail(["*****@*****.**"]) self.assertFalse(emailSent, "An emtpy excess queue should not send an email") self.assertEqual( c.lastExcessEmailSent, lastSent, "lastExcessEmailSent should not change when an email is not sent")
def test_sendExcessEmailWithExcess(self): c = core.QueueMessageCounter() c.createExcessEmail = lambda *args: "Test Msg" global count count = 0 def fakeWriteMsgToDisk(a, b): global count if b == "Test Msg": count += 1 c.writeMsgToDisk = fakeWriteMsgToDisk lastSent = c.lastExcessEmailSent emailSent = c.sendExcessEmail(["*****@*****.**"]) self.assertTrue(emailSent, "An excess queue should send an email") self.assertEqual(count, 1, "WriteMsgToDisk should be called") self.assertTrue( c.lastExcessEmailSent != lastSent, "lastExcessEmailSent should change when an email is sent")
def test_saveMsgLater_Mulitple(self): c = core.QueueMessageCounter() msgs = [ QueueMessageCounterTest.MockMessage("*****@*****.**"), QueueMessageCounterTest.MockMessage("*****@*****.**") ] for msg in msgs: for i in xrange(10): c.saveMsgLater(msg, "/dev/null", "test text of really boring type/n" * 20) self.assertEqual( len(c.mailQueues), 2, "mailQueues len should be 2 after save. len is %s" % len(c.mailQueues)) self.assertEqual(len(c.mailQueues["*****@*****.**"]), 10, "mailQueues len should be 10 after save") self.assertEqual(len(c.mailQueues["*****@*****.**"]), 10, "mailQueues len should be 10 after save")
def test_reachedExcessAmount(self): c = core.QueueMessageCounter() excessAmount = int( core.BaseMessageCounter.defaultValues["excessAmount"]) msgs = [ QueueMessageCounterTest.MockMessage("*****@*****.**"), QueueMessageCounterTest.MockMessage("*****@*****.**") ] for msg in msgs: for i in xrange(1, excessAmount * 2): c.incrementCount(msg) if i <= excessAmount: self.assertFalse( c.reachedExcessAmount(msg), "reachedExcessAmount should be false if count %s <= %s" % ((i + 1), excessAmount)) else: self.assertTrue( c.reachedExcessAmount(msg), "reachedExcessAmount should be true if count %i > %s" % ((i + 1), excessAmount))
def test_incrementCountMulipleFrom(self): c = core.QueueMessageCounter() for i in xrange(5): c.incrementCount(QueueMessageCounterTest.MockMessage("*****@*****.**")) for i in xrange(5): c.incrementCount(QueueMessageCounterTest.MockMessage("*****@*****.**")) self.assertEquals(c.count, 10, "Count should be 10") self.assertEquals(c.totalCount, 10, "TotalCount should be 10") self.assertEqual(len(c.queues), 2, "queues len should be 2 after increment") self.assertEqual(len(c.queuetotals), 2, "queuetotals len should be 2 after increment") self.assertEqual(c.queues["*****@*****.**"], 5, "queues len should be 5 after increment") self.assertEqual(c.queuetotals["*****@*****.**"], 5, "queuetotals len should be 5 after increment") self.assertEqual(c.queues["*****@*****.**"], 5, "queues len should be 5 after increment") self.assertEqual(c.queuetotals["*****@*****.**"], 5, "queuetotals len should be 5 after increment")
def test_notWithinExcessEmailTimeDelta(self): #override datetime.today() oldclass = datetime.datetime class fakedatetime(datetime.datetime): @staticmethod def today(): return datetime.datetime(2009, 10, 2, 13, 13, 0) core.datetime = fakedatetime c = core.QueueMessageCounter() val1 = c.notWithinExcessEmailTimeDelta() timedelta = int(core.QueueMessageCounter. defaultValues["sendExcessEmailMinTimeDelta_s"]) c.lastExcessEmailSent = datetime.datetime(2009, 10, 2, 13, 13, 0) val2 = c.notWithinExcessEmailTimeDelta() c.lastExcessEmailSent = datetime.datetime( 2009, 10, 2, 13, 13, 0) - datetime.timedelta(seconds=(timedelta / 2)) val3 = c.notWithinExcessEmailTimeDelta() c.lastExcessEmailSent = datetime.datetime( 2009, 10, 2, 13, 13, 0) - datetime.timedelta(seconds=(timedelta)) val4 = c.notWithinExcessEmailTimeDelta() c.lastExcessEmailSent = datetime.datetime( 2009, 10, 2, 13, 13, 0) - datetime.timedelta(seconds=(timedelta + 1)) val5 = c.notWithinExcessEmailTimeDelta() #restore datetime.today() core.datetime = oldclass self.assertTrue(val1, "A new object should always return true") self.assertFalse( val2, "If lastExcessEmailSent == today should return false") self.assertFalse( val3, "If lastExcessEmailSent - today = 30s should return false") self.assertFalse( val4, "If lastExcessEmailSent - today = 60s should return false") self.assertTrue( val5, "If lastExcessEmailSent - today = 61s should return true")
def test_getCounts(self): c = core.QueueMessageCounter() msgs = [ QueueMessageCounterTest.MockMessage("*****@*****.**"), QueueMessageCounterTest.MockMessage("*****@*****.**") ] for msg in msgs: for i in xrange(10): c.incrementCount(msg) d = c.getCounts() self.assertEquals(d["total"], 20, "total in getCount should be 20") self.assertEquals(d["current"], 20, "current in getCount should be 20") self.assertEqual( d["*****@*****.**"]["total"], 10, "%s in getCount should be %s" % ("[email protected] - total", 10)) self.assertEqual( d["*****@*****.**"]["current"], 10, "%s in getCount should be %s" % ("[email protected] - current", 10)) self.assertEqual( d["*****@*****.**"]["total"], 10, "%s in getCount should be %s" % ("[email protected] - total", 10)) self.assertEqual( d["*****@*****.**"]["current"], 10, "%s in getCount should be %s" % ("[email protected] - current", 10)) c.clearCount() d = c.getCounts() self.assertEquals(d["total"], 20, "total in getCount should be 20") self.assertEquals(d["current"], 0, "current in getCount should be 0") self.assertEqual( d["*****@*****.**"]["total"], 10, "%s in getCount should be %s" % ("[email protected] - total", 10)) self.assertEqual( d["*****@*****.**"]["current"], 0, "%s in getCount should be %s" % ("[email protected] - current", 0)) self.assertEqual( d["*****@*****.**"]["total"], 10, "%s in getCount should be %s" % ("[email protected] - total", 10)) self.assertEqual( d["*****@*****.**"]["current"], 0, "%s in getCount should be %s" % ("[email protected] - current", 0))