''' unit_tester.py - The module contains the MailScriptTester class. Copyright (c) 2012 Intra2net AG Author: Plamen Dimitrov This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ''' import unittest import datetime, date_interpreter class MailScriptTester(unittest.TestCase): # class attributes # DateInterpreter instance testing the DateInterpreter methods date_interp = None # datetime for comparison with extracted datetimes and assertions true_date = None def setUp(self): self.date_interp = date_interpreter.DateInterpreter() self.true_date = datetime.datetime(2007, 12, 11, 18, 24, 35) def test_received_date_extraction1(self): """Tests the date extraction method.""" date = [[0, b"Tue, 11 Dec 2007 18:24:35 +0100"]] extracted_date = self.date_interp.extract_received_date(date) self.assertEqual(extracted_date, self.true_date, "Failed date format 1") def test_received_date_extraction2(self): """Tests the date extraction method.""" date = [[0, b"11 Dec 2007 \r\n18:24:35 +0100"]] extracted_date = self.date_interp.extract_received_date(date) self.assertEqual(extracted_date, self.true_date, "Failed date format 2") return def test_received_date_extraction3(self): """Tests the date extraction method.""" date = [[0, b"11 Dec 2007 18:24:35 +0100"]] extracted_date = self.date_interp.extract_received_date(date) self.assertEqual(extracted_date, self.true_date, "Failed date format 3") def test_received_date_extraction4(self): """Tests the date extraction method.""" date = [[0, b"11 Dec 2007 18:24:35"]] extracted_date = self.date_interp.extract_received_date(date) #should not be equal because of time zone assumption self.assertNotEqual(extracted_date, self.true_date, "Failed date format 4") def test_received_date_extraction5(self): """Tests the received date extraction method.""" date = [[0, b"11 Dec 2007 18:24:35 GMT"]] extracted_date = self.date_interp.extract_received_date(date) #should not be equal because of time zone assumption self.assertNotEqual(extracted_date, self.true_date, "Failed date format 5") def test_received_date_extraction6(self): """Tests the received date extraction method.""" date = [[0, b'Received: from intranator.m.i2n ([unix socket])' b'by intranator.m.i2n with LMTPA; Tue, 11 Dec 2007 18:24:35' b'+0100Received: from localhost (intranator.m.i2n [127.0.0.1])' b'by localhost (Postfix) with ESMTP id 895812AC54for ;' b'Sun, 13 Mar 2011 18:47:18 +0100 (CET)Received: from re04.intra2net.com ' b'(re04.intra2net.com [82.165.46.26])(using TLSv1 with cipher ADH-AES256-SHA ' b'(256/256 bits))(No client certificate requested)by intranator.m.i2n (Postfix) with ' b'ESMTPS id 28DB92AC53for ; Sun, 13 Mar 2011 18:47:15 +0100 ' b'(CET)Received: from postfix.charite.de (postfix.charite.de [141.42.206.35])(using TLSv1 ' b'with cipher ADH-AES256-SHA (256/256 bits))(No client certificate requested)by ' b're04.intra2net.com (Postfix) with ESMTP id C054A3010Afor ; ' b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from localhost (localhost [127.0.0.1])by ' b'de.postfix.org (Postfix) with ESMTP id 7FCCFF7879for ; ' b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from de.postfix.org ([127.0.0.1])by ' b'localhost (de.postfix.org [127.0.0.1]) (amavisd-new, port 10026)with LMTP id ' b'YSXF-vf3+6E1 for ;Sun, 13 Mar 2011 18:47:14 +0100 (CET)' b'Received: from de.postfix.org (localhost [127.0.0.1])by de.postfix.org (Postfix) with ' b'ESMTP id 3C3123DF1Efor ; Sun, 13 Mar 2011 18:46:33 +0100 ' b'(CET)Received: from localhost (localhost [127.0.0.1])by de.postfix.org (Postfix) with ' b'ESMTP id AB6CE3DBD2for ; Sun, 13 Mar 2011 18:45:57 +0100 (CET)' b'Received: from de.postfix.org ([127.0.0.1])by localhost (de.postfix.org [127.0.0.1]) ' b'(amavisd-new, port 10024)with ESMTP id mBYiZO8wREeS for ;Sun, ' b'13 Mar 2011 18:45:56 +0100 (CET)Received: from mail.inetmsg.com (mail.inetmsg.com ' b'[173.10.94.185])by de.postfix.org (Postfix) with ESMTPSfor ; ' b'Sun, 13 Mar 2011 18:45:55 +0100 (CET)Received: from [192.168.1.107] (fw1.inetmsg.com ' b'[10.20.30.253])(using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits))' b'(No client certificate requested)by mail.inetmsg.com (INetMsg Mail Service) with ESMTPSA ' b'id 0B95326CD1for ; Sun, 13 Mar 2011 10:45:41 -0700 (PDT)"]]']] extracted_date = self.date_interp.extract_received_date(date) #should not be equal because of time zone assumption self.assertEqual(extracted_date, self.true_date, "Failed date format 6") def test_compare_dates(self): """Tests the date comparison method.""" self.true_date2 = datetime.datetime(2007, 12, 11, 18, 34, 35) #is difference of 10 mins significant if tolerance is 9 mins self.assertTrue(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 9*60)), "Failed at comparison test") #is difference of 10 mins significant if tolerance is 11 mins self.assertFalse(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 11*60)), "Failed at comparison test") if __name__ == '__main__': unittest.main()