Commit | Line | Data |
---|---|---|
c9da760a PD |
1 | ''' |
2 | unit_tester.py - The module contains the MailScriptTester class. | |
3 | ||
4 | Copyright (c) 2012 Intra2net AG | |
5 | Author: Plamen Dimitrov | |
6 | ||
7 | This program is free software: you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ''' | |
17 | ||
18 | import unittest | |
db3f09a6 PD |
19 | import datetime |
20 | from mail_date_parser import MailDateParser | |
c9da760a | 21 | |
95fe161a | 22 | class DateParseTester(unittest.TestCase): |
db3f09a6 | 23 | """Used to test the date retrievel and the MailDateParser class.""" |
c9da760a | 24 | |
7a1d4c35 PD |
25 | # class attributes |
26 | # DateInterpreter instance testing the DateInterpreter methods | |
d91be016 | 27 | date_interp = None |
7a1d4c35 | 28 | # datetime for comparison with extracted datetimes and assertions |
d91be016 | 29 | true_date = None |
7a1d4c35 | 30 | |
d91be016 PD |
31 | # wrong naming conventions are due to python unittest library |
32 | def setUp(self): | |
db3f09a6 | 33 | """Prepares the testing confitions.""" |
d91be016 PD |
34 | self.date_interp = MailDateParser() |
35 | self.true_date = datetime.datetime(2007, 12, 11, 18, 24, 35) | |
c9da760a | 36 | |
7a1d4c35 | 37 | def test_received_date_extraction1(self): |
c9da760a | 38 | """Tests the date extraction method.""" |
db3f09a6 | 39 | date = "Tue, 11 Dec 2007 18:24:35 +0100" |
c9da760a PD |
40 | extracted_date = self.date_interp.extract_received_date(date) |
41 | self.assertEqual(extracted_date, self.true_date, "Failed date format 1") | |
42 | ||
7a1d4c35 | 43 | def test_received_date_extraction2(self): |
c9da760a | 44 | """Tests the date extraction method.""" |
db3f09a6 | 45 | date = "11 Dec 2007 \r\n18:24:35 +0100" |
c9da760a PD |
46 | extracted_date = self.date_interp.extract_received_date(date) |
47 | self.assertEqual(extracted_date, self.true_date, "Failed date format 2") | |
48 | return | |
49 | ||
7a1d4c35 | 50 | def test_received_date_extraction3(self): |
c9da760a | 51 | """Tests the date extraction method.""" |
db3f09a6 | 52 | date = "11 Dec 2007 18:24:35 +0100" |
c9da760a PD |
53 | extracted_date = self.date_interp.extract_received_date(date) |
54 | self.assertEqual(extracted_date, self.true_date, "Failed date format 3") | |
55 | ||
7a1d4c35 | 56 | def test_received_date_extraction4(self): |
c9da760a | 57 | """Tests the date extraction method.""" |
db3f09a6 | 58 | date = "11 Dec 2007 18:24:35" |
c9da760a PD |
59 | extracted_date = self.date_interp.extract_received_date(date) |
60 | #should not be equal because of time zone assumption | |
61 | self.assertNotEqual(extracted_date, self.true_date, "Failed date format 4") | |
62 | ||
7a1d4c35 | 63 | def test_received_date_extraction5(self): |
c9da760a | 64 | """Tests the received date extraction method.""" |
db3f09a6 | 65 | date = "11 Dec 2007 18:24:35 GMT" |
c9da760a PD |
66 | extracted_date = self.date_interp.extract_received_date(date) |
67 | #should not be equal because of time zone assumption | |
68 | self.assertNotEqual(extracted_date, self.true_date, "Failed date format 5") | |
69 | ||
7a1d4c35 PD |
70 | def test_received_date_extraction6(self): |
71 | """Tests the received date extraction method.""" | |
db3f09a6 PD |
72 | date = 'Received: from intranator.m.i2n ([unix socket])'\ |
73 | 'by intranator.m.i2n with LMTPA; Tue, 11 Dec 2007 18:24:35'\ | |
74 | '+0100Received: from localhost (intranator.m.i2n [127.0.0.1])'\ | |
75 | 'by localhost (Postfix) with ESMTP id 895812AC54for <intra2net_thomas@intranator.m.i2n>;'\ | |
76 | 'Sun, 13 Mar 2011 18:47:18 +0100 (CET)Received: from re04.intra2net.com '\ | |
77 | '(re04.intra2net.com [82.165.46.26])(using TLSv1 with cipher ADH-AES256-SHA '\ | |
78 | '(256/256 bits))(No client certificate requested)by intranator.m.i2n (Postfix) with '\ | |
79 | 'ESMTPS id 28DB92AC53for <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:47:15 +0100 '\ | |
80 | '(CET)Received: from postfix.charite.de (postfix.charite.de [141.42.206.35])(using TLSv1 '\ | |
81 | 'with cipher ADH-AES256-SHA (256/256 bits))(No client certificate requested)by '\ | |
82 | 're04.intra2net.com (Postfix) with ESMTP id C054A3010Afor <thomas.jarosch@intra2net.com>; '\ | |
83 | 'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from localhost (localhost [127.0.0.1])by '\ | |
84 | 'de.postfix.org (Postfix) with ESMTP id 7FCCFF7879for <thomas.jarosch@intra2net.com>; '\ | |
85 | 'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from de.postfix.org ([127.0.0.1])by '\ | |
86 | 'localhost (de.postfix.org [127.0.0.1]) (amavisd-new, port 10026)with LMTP id '\ | |
87 | 'YSXF-vf3+6E1 for <thomas.jarosch@intra2net.com>;Sun, 13 Mar 2011 18:47:14 +0100 (CET)'\ | |
88 | 'Received: from de.postfix.org (localhost [127.0.0.1])by de.postfix.org (Postfix) with '\ | |
89 | 'ESMTP id 3C3123DF1Efor <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:46:33 +0100 '\ | |
90 | '(CET)Received: from localhost (localhost [127.0.0.1])by de.postfix.org (Postfix) with '\ | |
91 | 'ESMTP id AB6CE3DBD2for <amavis-users@amavis.org>; Sun, 13 Mar 2011 18:45:57 +0100 (CET)'\ | |
92 | 'Received: from de.postfix.org ([127.0.0.1])by localhost (de.postfix.org [127.0.0.1]) '\ | |
93 | '(amavisd-new, port 10024)with ESMTP id mBYiZO8wREeS for <amavis-users@amavis.org>;Sun, '\ | |
94 | '13 Mar 2011 18:45:56 +0100 (CET)Received: from mail.inetmsg.com (mail.inetmsg.com '\ | |
95 | '[173.10.94.185])by de.postfix.org (Postfix) with ESMTPSfor <amavis-users@amavis.org>; '\ | |
96 | 'Sun, 13 Mar 2011 18:45:55 +0100 (CET)Received: from [192.168.1.107] (fw1.inetmsg.com '\ | |
97 | '[10.20.30.253])(using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits))'\ | |
98 | '(No client certificate requested)by mail.inetmsg.com (INetMsg Mail Service) with ESMTPSA '\ | |
99 | 'id 0B95326CD1for <amavis-users@amavis.org>; Sun, 13 Mar 2011 10:45:41 -0700 (PDT)"]]' | |
7a1d4c35 PD |
100 | extracted_date = self.date_interp.extract_received_date(date) |
101 | #should not be equal because of time zone assumption | |
102 | self.assertEqual(extracted_date, self.true_date, "Failed date format 6") | |
103 | ||
c9da760a PD |
104 | def test_compare_dates(self): |
105 | """Tests the date comparison method.""" | |
db3f09a6 | 106 | true_date2 = datetime.datetime(2007, 12, 11, 18, 34, 35) |
c9da760a | 107 | #is difference of 10 mins significant if tolerance is 9 mins |
db3f09a6 | 108 | self.assertTrue(bool(self.date_interp.compare_dates(self.true_date, true_date2, 9*60)), "Failed at comparison test") |
c9da760a | 109 | #is difference of 10 mins significant if tolerance is 11 mins |
db3f09a6 | 110 | self.assertFalse(bool(self.date_interp.compare_dates(self.true_date, true_date2, 11*60)), "Failed at comparison test") |
c9da760a PD |
111 | |
112 | if __name__ == '__main__': | |
113 | unittest.main() |