Arguments parser added, conf file clarifications
[imap-fix-internaldate] / unit_tester.py
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
19 import datetime, date_interpreter
20
21 class MailScriptTester(unittest.TestCase):
22
23     # class attributes
24     # DateInterpreter instance testing the DateInterpreter methods
25     date_interp = None
26     # datetime for comparison with extracted datetimes and assertions
27     true_date = None
28
29     def setUp(self):
30         self.date_interp = date_interpreter.DateInterpreter()
31         self.true_date = datetime.datetime(2007, 12, 11, 18, 24, 35)
32
33     def test_received_date_extraction1(self):
34         """Tests the date extraction method."""
35         date = [[0, b"Tue, 11 Dec 2007 18:24:35 +0100"]]
36         extracted_date = self.date_interp.extract_received_date(date)
37         self.assertEqual(extracted_date, self.true_date, "Failed date format 1")
38
39     def test_received_date_extraction2(self):
40         """Tests the date extraction method."""
41         date = [[0, b"11 Dec 2007 \r\n18:24:35 +0100"]]
42         extracted_date = self.date_interp.extract_received_date(date)
43         self.assertEqual(extracted_date, self.true_date, "Failed date format 2")
44         return
45
46     def test_received_date_extraction3(self):
47         """Tests the date extraction method."""  
48         date = [[0, b"11 Dec 2007 18:24:35 +0100"]]
49         extracted_date = self.date_interp.extract_received_date(date)
50         self.assertEqual(extracted_date, self.true_date, "Failed date format 3")
51
52     def test_received_date_extraction4(self):
53         """Tests the date extraction method."""
54         date = [[0, b"11 Dec 2007 18:24:35"]]
55         extracted_date = self.date_interp.extract_received_date(date)
56         #should not be equal because of time zone assumption
57         self.assertNotEqual(extracted_date, self.true_date, "Failed date format 4")
58
59     def test_received_date_extraction5(self):
60         """Tests the received date extraction method."""
61         date = [[0, b"11 Dec 2007 18:24:35 GMT"]]
62         extracted_date = self.date_interp.extract_received_date(date)
63         #should not be equal because of time zone assumption
64         self.assertNotEqual(extracted_date, self.true_date, "Failed date format 5")
65
66     def test_received_date_extraction6(self):
67         """Tests the received date extraction method."""
68         date = [[0, b'Received: from intranator.m.i2n ([unix socket])'
69                 b'by intranator.m.i2n with LMTPA; Tue, 11 Dec 2007 18:24:35'
70                 b'+0100Received: from localhost (intranator.m.i2n [127.0.0.1])'
71                 b'by localhost (Postfix) with ESMTP id 895812AC54for <intra2net_thomas@intranator.m.i2n>;'
72                 b'Sun, 13 Mar 2011 18:47:18 +0100 (CET)Received: from re04.intra2net.com '
73                 b'(re04.intra2net.com [82.165.46.26])(using TLSv1 with cipher ADH-AES256-SHA '
74                 b'(256/256 bits))(No client certificate requested)by intranator.m.i2n (Postfix) with '
75                 b'ESMTPS id 28DB92AC53for <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:47:15 +0100 '
76                 b'(CET)Received: from postfix.charite.de (postfix.charite.de [141.42.206.35])(using TLSv1 '
77                 b'with cipher ADH-AES256-SHA (256/256 bits))(No client certificate requested)by '
78                 b're04.intra2net.com (Postfix) with ESMTP id C054A3010Afor <thomas.jarosch@intra2net.com>; '
79                 b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from localhost (localhost [127.0.0.1])by '
80                 b'de.postfix.org (Postfix) with ESMTP id 7FCCFF7879for <thomas.jarosch@intra2net.com>; '
81                 b'Sun, 13 Mar 2011 18:47:14 +0100 (CET)Received: from de.postfix.org ([127.0.0.1])by '
82                 b'localhost (de.postfix.org [127.0.0.1]) (amavisd-new, port 10026)with LMTP id '
83                 b'YSXF-vf3+6E1 for <thomas.jarosch@intra2net.com>;Sun, 13 Mar 2011 18:47:14 +0100 (CET)'
84                 b'Received: from de.postfix.org (localhost [127.0.0.1])by de.postfix.org (Postfix) with '
85                 b'ESMTP id 3C3123DF1Efor <thomas.jarosch@intra2net.com>; Sun, 13 Mar 2011 18:46:33 +0100 '
86                 b'(CET)Received: from localhost (localhost [127.0.0.1])by de.postfix.org (Postfix) with '
87                 b'ESMTP id AB6CE3DBD2for <amavis-users@amavis.org>; Sun, 13 Mar 2011 18:45:57 +0100 (CET)'
88                 b'Received: from de.postfix.org ([127.0.0.1])by localhost (de.postfix.org [127.0.0.1]) '
89                 b'(amavisd-new, port 10024)with ESMTP id mBYiZO8wREeS for <amavis-users@amavis.org>;Sun, '
90                 b'13 Mar 2011 18:45:56 +0100 (CET)Received: from mail.inetmsg.com (mail.inetmsg.com '
91                 b'[173.10.94.185])by de.postfix.org (Postfix) with ESMTPSfor <amavis-users@amavis.org>; '
92                 b'Sun, 13 Mar 2011 18:45:55 +0100 (CET)Received: from [192.168.1.107] (fw1.inetmsg.com '
93                 b'[10.20.30.253])(using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits))'
94                 b'(No client certificate requested)by mail.inetmsg.com (INetMsg Mail Service) with ESMTPSA '
95                 b'id 0B95326CD1for <amavis-users@amavis.org>; Sun, 13 Mar 2011 10:45:41 -0700 (PDT)"]]']]
96         extracted_date = self.date_interp.extract_received_date(date)
97         #should not be equal because of time zone assumption
98         self.assertEqual(extracted_date, self.true_date, "Failed date format 6")
99
100     def test_compare_dates(self):
101         """Tests the date comparison method."""
102         self.true_date2 = datetime.datetime(2007, 12, 11, 18, 34, 35)
103         #is difference of 10 mins significant if tolerance is 9 mins
104         self.assertTrue(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 9*60)), "Failed at comparison test")
105         #is difference of 10 mins significant if tolerance is 11 mins
106         self.assertFalse(bool(self.date_interp.compare_dates(self.true_date, self.true_date2, 11*60)), "Failed at comparison test")
107
108 if __name__ == '__main__':
109     unittest.main()