OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import StringIO | 16 import StringIO |
17 import datetime | 17 import datetime |
18 import tarfile | 18 import tarfile |
19 import unittest | 19 import unittest |
20 | 20 |
21 import mock | 21 import mock |
22 | 22 |
23 import sitescripts.notifications.parser as parser | 23 import sitescripts.notifications.parser as parser |
24 | 24 |
25 | 25 |
26 def _create_notification_archive(name, text): | 26 def _create_notification_archive(files): |
27 archive_stream = StringIO.StringIO() | 27 archive_stream = StringIO.StringIO() |
28 file_stream = StringIO.StringIO(text) | |
29 tar_info = tarfile.TarInfo(name) | |
30 tar_info.size = len(file_stream.buf) | |
31 with tarfile.open(mode='w', fileobj=archive_stream) as archive: | 28 with tarfile.open(mode='w', fileobj=archive_stream) as archive: |
32 archive.addfile(tar_info, file_stream) | 29 for name, text in files: |
| 30 file_stream = StringIO.StringIO(text) |
| 31 tar_info = tarfile.TarInfo(name) |
| 32 tar_info.size = len(file_stream.buf) |
| 33 archive.addfile(tar_info, file_stream) |
33 return archive_stream.getvalue() | 34 return archive_stream.getvalue() |
34 | 35 |
35 | 36 |
36 def _format_time(time): | 37 def _format_time(time): |
37 return datetime.datetime.strftime(time, '%Y-%m-%dT%H:%M') | 38 return datetime.datetime.strftime(time, '%Y-%m-%dT%H:%M') |
38 | 39 |
39 | 40 |
40 class TestParser(unittest.TestCase): | 41 class TestParser(unittest.TestCase): |
41 def setUp(self): | 42 def setUp(self): |
42 self.call_patcher = mock.patch('subprocess.call') | 43 self.call_patcher = mock.patch('subprocess.call') |
43 self.call_patcher.start() | 44 self.call_patcher.start() |
44 self.check_output_patcher = mock.patch('subprocess.check_output') | 45 self.check_output_patcher = mock.patch('subprocess.check_output') |
45 check_output_mock = self.check_output_patcher.start() | 46 check_output_mock = self.check_output_patcher.start() |
46 | 47 |
47 def check_output_side_effect(command): | 48 def check_output_side_effect(command): |
48 if 'hg' in command and 'archive' in command: | 49 if 'hg' in command and 'archive' in command: |
49 return _create_notification_archive(*self.notification_to_load) | 50 return _create_notification_archive(self.notification_to_load) |
50 check_output_mock.side_effect = check_output_side_effect | 51 check_output_mock.side_effect = check_output_side_effect |
51 | 52 |
52 def tearDown(self): | 53 def tearDown(self): |
53 self.call_patcher.stop() | 54 self.call_patcher.stop() |
54 self.check_output_patcher.stop() | 55 self.check_output_patcher.stop() |
55 | 56 |
56 def test_typical(self): | 57 def test_typical(self): |
57 self.notification_to_load = ('1', ''' | 58 self.notification_to_load = [('1', ''' |
58 severity = information | 59 severity = information |
59 title.en-US = The title | 60 title.en-US = The title |
60 message.en-US = The message | 61 message.en-US = The message |
61 ''') | 62 ''')] |
62 notifications = parser.load_notifications() | 63 notifications = parser.load_notifications() |
63 self.assertEqual(len(notifications), 1) | 64 self.assertEqual(len(notifications), 1) |
64 self.assertEqual(notifications[0]['id'], '1') | 65 self.assertEqual(notifications[0]['id'], '1') |
65 self.assertEqual(notifications[0]['severity'], 'information') | 66 self.assertEqual(notifications[0]['severity'], 'information') |
66 self.assertEqual(notifications[0]['title']['en-US'], 'The title') | 67 self.assertEqual(notifications[0]['title']['en-US'], 'The title') |
67 self.assertEqual(notifications[0]['message']['en-US'], 'The message') | 68 self.assertEqual(notifications[0]['message']['en-US'], 'The message') |
68 self.assertNotIn('inactive', notifications[0]) | 69 self.assertNotIn('inactive', notifications[0]) |
69 | 70 |
70 def test_inactive(self): | 71 def test_inactive(self): |
71 self.notification_to_load = ('1', ''' | 72 self.notification_to_load = [ |
72 inactive = Yes | 73 ('1', '\ninactive = Yes\n'), |
73 ''') | 74 ('2', '\ninactive = No\n'), |
| 75 ] |
74 notifications = parser.load_notifications() | 76 notifications = parser.load_notifications() |
75 self.assertEqual(len(notifications), 1) | 77 self.assertEqual(len(notifications), 2) |
76 self.assertTrue(notifications[0]['inactive']) | 78 self.assertTrue(notifications[0]['inactive']) |
77 self.notification_to_load = ('1', ''' | 79 self.assertFalse(notifications[1]['inactive']) |
78 inactive = No | |
79 ''') | |
80 notifications = parser.load_notifications() | |
81 self.assertEqual(len(notifications), 1) | |
82 self.assertFalse(notifications[0]['inactive']) | |
83 | 80 |
84 def test_in_range(self): | 81 def test_in_range(self): |
85 current_time = datetime.datetime.now() | 82 current_time = datetime.datetime.now() |
86 hour_delta = datetime.timedelta(hours=1) | 83 hour_delta = datetime.timedelta(hours=1) |
87 start_time = current_time - hour_delta | 84 start_time = current_time - hour_delta |
88 end_time = current_time + hour_delta | 85 end_time = current_time + hour_delta |
89 self.notification_to_load = ('1', ''' | 86 self.notification_to_load = [('1', ''' |
90 start = %s | 87 start = %s |
91 end = %s | 88 end = %s |
92 ''' % (_format_time(start_time), _format_time(end_time))) | 89 ''' % (_format_time(start_time), _format_time(end_time)))] |
93 notifications = parser.load_notifications() | 90 notifications = parser.load_notifications() |
94 self.assertEqual(len(notifications), 1) | 91 self.assertEqual(len(notifications), 1) |
95 self.assertEqual(notifications[0]['id'], '1') | 92 self.assertEqual(notifications[0]['id'], '1') |
96 self.assertNotIn('inactive', notifications[0]) | 93 self.assertNotIn('inactive', notifications[0]) |
97 | 94 |
98 def test_after_range(self): | 95 def test_after_range(self): |
99 current_time = datetime.datetime.now() | 96 current_time = datetime.datetime.now() |
100 start_time = current_time - datetime.timedelta(hours=2) | 97 start_time = current_time - datetime.timedelta(hours=2) |
101 end_time = current_time - datetime.timedelta(hours=1) | 98 end_time = current_time - datetime.timedelta(hours=1) |
102 self.notification_to_load = ('1', ''' | 99 self.notification_to_load = [('1', ''' |
103 start = %s | 100 start = %s |
104 end = %s | 101 end = %s |
105 ''' % (_format_time(start_time), _format_time(end_time))) | 102 ''' % (_format_time(start_time), _format_time(end_time)))] |
106 notifications = parser.load_notifications() | 103 notifications = parser.load_notifications() |
107 self.assertEqual(len(notifications), 1) | 104 self.assertEqual(len(notifications), 1) |
108 self.assertTrue(notifications[0]['inactive']) | 105 self.assertTrue(notifications[0]['inactive']) |
109 | 106 |
110 def test_before_range(self): | 107 def test_before_range(self): |
111 current_time = datetime.datetime.now() | 108 current_time = datetime.datetime.now() |
112 start_time = current_time + datetime.timedelta(hours=1) | 109 start_time = current_time + datetime.timedelta(hours=1) |
113 end_time = current_time + datetime.timedelta(hours=2) | 110 end_time = current_time + datetime.timedelta(hours=2) |
114 self.notification_to_load = ('1', ''' | 111 self.notification_to_load = [('1', ''' |
115 start = %s | 112 start = %s |
116 end = %s | 113 end = %s |
117 ''' % (_format_time(start_time), _format_time(end_time))) | 114 ''' % (_format_time(start_time), _format_time(end_time)))] |
118 notifications = parser.load_notifications() | 115 notifications = parser.load_notifications() |
119 self.assertEqual(len(notifications), 1) | 116 self.assertEqual(len(notifications), 1) |
120 self.assertTrue(notifications[0]['inactive']) | 117 self.assertTrue(notifications[0]['inactive']) |
121 | 118 |
122 def test_start_and_end_not_present(self): | 119 def test_start_and_end_not_present(self): |
123 current_time = datetime.datetime.now() | 120 current_time = datetime.datetime.now() |
124 hour_delta = datetime.timedelta(hours=1) | 121 hour_delta = datetime.timedelta(hours=1) |
125 start_time = current_time - hour_delta | 122 start_time = current_time - hour_delta |
126 end_time = current_time + hour_delta | 123 end_time = current_time + hour_delta |
127 self.notification_to_load = ('1', ''' | 124 self.notification_to_load = [('1', ''' |
128 start = %s | 125 start = %s |
129 end = %s | 126 end = %s |
130 ''' % (_format_time(start_time), _format_time(end_time))) | 127 ''' % (_format_time(start_time), _format_time(end_time)))] |
131 notifications = parser.load_notifications() | 128 notifications = parser.load_notifications() |
132 self.assertEqual(len(notifications), 1) | 129 self.assertEqual(len(notifications), 1) |
133 self.assertNotIn('inactive', notifications[0]) | 130 self.assertNotIn('inactive', notifications[0]) |
134 self.assertNotIn('start', notifications[0]) | 131 self.assertNotIn('start', notifications[0]) |
135 self.assertNotIn('end', notifications[0]) | 132 self.assertNotIn('end', notifications[0]) |
136 | 133 |
| 134 def test_interval(self): |
| 135 self.notification_to_load = [ |
| 136 ('1', '\ninterval = 100\n'), |
| 137 ('2', '\ninterval = onehundred\n'), |
| 138 ] |
| 139 notifications = parser.load_notifications() |
| 140 self.assertEqual(len(notifications), 1) |
| 141 self.assertEqual(notifications[0]['interval'], 100) |
| 142 |
| 143 def test_severity(self): |
| 144 self.notification_to_load = [ |
| 145 ('1', '\nseverity = information\n'), |
| 146 ('2', '\nseverity = critical\n'), |
| 147 ('3', '\nseverity = normal\n'), |
| 148 ('4', '\nseverity = relentless\n'), |
| 149 ] |
| 150 notifications = parser.load_notifications() |
| 151 self.assertEqual(len(notifications), 4) |
| 152 self.assertEqual(notifications[0]['severity'], 'information') |
| 153 self.assertEqual(notifications[1]['severity'], 'critical') |
| 154 self.assertEqual(notifications[2]['severity'], 'normal') |
| 155 self.assertEqual(notifications[3]['severity'], 'relentless') |
| 156 |
| 157 |
137 if __name__ == '__main__': | 158 if __name__ == '__main__': |
138 unittest.main() | 159 unittest.main() |
OLD | NEW |