| OLD | NEW |
| 1 import io |
| 1 import types | 2 import types |
| 3 import textwrap |
| 2 import unittest | 4 import unittest |
| 3 import email.policy | 5 import email.policy |
| 6 import email.parser |
| 7 import email.generator |
| 4 | 8 |
| 5 class PolicyAPITests(unittest.TestCase): | 9 class PolicyAPITests(unittest.TestCase): |
| 6 | 10 |
| 7 longMessage = True | 11 longMessage = True |
| 8 | 12 |
| 9 # These default values are the ones set on email.policy.default. | 13 # These default values are the ones set on email.policy.default. |
| 10 # If any of these defaults change, the docs must be updated. | 14 # If any of these defaults change, the docs must be updated. |
| 11 policy_defaults = { | 15 policy_defaults = { |
| 12 'max_line_length': 78, | 16 'max_line_length': 78, |
| 13 'linesep': '\n', | 17 'linesep': '\n', |
| 14 'must_be_7bit': False, | 18 'cte_type': '8bit', |
| 15 'raise_on_defect': False, | 19 'raise_on_defect': False, |
| 16 } | 20 } |
| 17 | 21 |
| 18 # For each policy under test, we give here the values of the attributes | 22 # For each policy under test, we give here the values of the attributes |
| 19 # that are different from the defaults for that policy. | 23 # that are different from the defaults for that policy. |
| 20 policies = { | 24 policies = { |
| 21 email.policy.Policy(): {}, | 25 email.policy.Compat32(): {}, |
| 26 email.policy.compat32: {}, |
| 22 email.policy.default: {}, | 27 email.policy.default: {}, |
| 23 email.policy.SMTP: {'linesep': '\r\n'}, | 28 email.policy.SMTP: {'linesep': '\r\n'}, |
| 24 email.policy.HTTP: {'linesep': '\r\n', 'max_line_length': None}, | 29 email.policy.HTTP: {'linesep': '\r\n', 'max_line_length': None}, |
| 25 email.policy.strict: {'raise_on_defect': True}, | 30 email.policy.strict: {'raise_on_defect': True}, |
| 26 } | 31 } |
| 27 | 32 |
| 28 def test_defaults(self): | 33 def test_defaults(self): |
| 29 for policy, changed_defaults in self.policies.items(): | 34 for policy, changed_defaults in self.policies.items(): |
| 30 expected = self.policy_defaults.copy() | 35 expected = self.policy_defaults.copy() |
| 31 expected.update(changed_defaults) | 36 expected.update(changed_defaults) |
| 32 for attr, value in expected.items(): | 37 for attr, value in expected.items(): |
| 33 self.assertEqual(getattr(policy, attr), value, | 38 self.assertEqual(getattr(policy, attr), value, |
| 34 ("change {} docs/docstrings if defaults have " | 39 ("change {} docs/docstrings if defaults have " |
| 35 "changed").format(policy)) | 40 "changed").format(policy)) |
| 36 | 41 |
| 37 def test_all_attributes_covered(self): | 42 def test_all_attributes_covered(self): |
| 38 for attr in dir(email.policy.default): | 43 for attr in dir(email.policy.default): |
| 39 if (attr.startswith('_') or | 44 if (attr.startswith('_') or |
| 40 isinstance(getattr(email.policy.Policy, attr), | 45 isinstance(getattr(email.policy.Policy, attr), |
| 41 types.FunctionType)): | 46 types.FunctionType)): |
| 42 continue | 47 continue |
| 43 else: | 48 else: |
| 44 self.assertIn(attr, self.policy_defaults, | 49 self.assertIn(attr, self.policy_defaults, |
| 45 "{} is not fully tested".format(attr)) | 50 "{} is not fully tested".format(attr)) |
| 51 |
| 52 def test_abc(self): |
| 53 with self.assertRaises(TypeError) as cm: |
| 54 email.policy.Policy() |
| 55 msg = str(cm.exception) |
| 56 abstract_methods = ('fold', |
| 57 'fold_binary', |
| 58 'header_fetch_parse', |
| 59 'header_source_parse', |
| 60 'header_store_parse') |
| 61 for method in abstract_methods: |
| 62 self.assertIn(method, msg) |
| 46 | 63 |
| 47 def test_policy_is_immutable(self): | 64 def test_policy_is_immutable(self): |
| 48 for policy in self.policies: | 65 for policy in self.policies: |
| 49 for attr in self.policy_defaults: | 66 for attr in self.policy_defaults: |
| 50 with self.assertRaisesRegex(AttributeError, attr+".*read-only"): | 67 with self.assertRaisesRegex(AttributeError, attr+".*read-only"): |
| 51 setattr(policy, attr, None) | 68 setattr(policy, attr, None) |
| 52 with self.assertRaisesRegex(AttributeError, 'no attribute.*foo'): | 69 with self.assertRaisesRegex(AttributeError, 'no attribute.*foo'): |
| 53 policy.foo = None | 70 policy.foo = None |
| 54 | 71 |
| 55 def test_set_policy_attrs_when_calledl(self): | 72 def test_set_policy_attrs_when_calledl(self): |
| (...skipping 25 matching lines...) Expand all Loading... |
| 81 added = added + email.policy.default | 98 added = added + email.policy.default |
| 82 for attr, value in expected.items(): | 99 for attr, value in expected.items(): |
| 83 self.assertEqual(getattr(added, attr), value) | 100 self.assertEqual(getattr(added, attr), value) |
| 84 | 101 |
| 85 def test_register_defect(self): | 102 def test_register_defect(self): |
| 86 class Dummy: | 103 class Dummy: |
| 87 def __init__(self): | 104 def __init__(self): |
| 88 self.defects = [] | 105 self.defects = [] |
| 89 obj = Dummy() | 106 obj = Dummy() |
| 90 defect = object() | 107 defect = object() |
| 91 policy = email.policy.Policy() | 108 policy = email.policy.Compat32() |
| 92 policy.register_defect(obj, defect) | 109 policy.register_defect(obj, defect) |
| 93 self.assertEqual(obj.defects, [defect]) | 110 self.assertEqual(obj.defects, [defect]) |
| 94 defect2 = object() | 111 defect2 = object() |
| 95 policy.register_defect(obj, defect2) | 112 policy.register_defect(obj, defect2) |
| 96 self.assertEqual(obj.defects, [defect, defect2]) | 113 self.assertEqual(obj.defects, [defect, defect2]) |
| 97 | 114 |
| 98 class MyObj: | 115 class MyObj: |
| 99 def __init__(self): | 116 def __init__(self): |
| 100 self.defects = [] | 117 self.defects = [] |
| 101 | 118 |
| 102 class MyDefect(Exception): | 119 class MyDefect(Exception): |
| 103 pass | 120 pass |
| 104 | 121 |
| 105 def test_handle_defect_raises_on_strict(self): | 122 def test_handle_defect_raises_on_strict(self): |
| 106 foo = self.MyObj() | 123 foo = self.MyObj() |
| 107 defect = self.MyDefect("the telly is broken") | 124 defect = self.MyDefect("the telly is broken") |
| 108 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"): | 125 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"): |
| 109 email.policy.strict.handle_defect(foo, defect) | 126 email.policy.strict.handle_defect(foo, defect) |
| 110 | 127 |
| 111 def test_handle_defect_registers_defect(self): | 128 def test_handle_defect_registers_defect(self): |
| 112 foo = self.MyObj() | 129 foo = self.MyObj() |
| 113 defect1 = self.MyDefect("one") | 130 defect1 = self.MyDefect("one") |
| 114 email.policy.default.handle_defect(foo, defect1) | 131 email.policy.default.handle_defect(foo, defect1) |
| 115 self.assertEqual(foo.defects, [defect1]) | 132 self.assertEqual(foo.defects, [defect1]) |
| 116 defect2 = self.MyDefect("two") | 133 defect2 = self.MyDefect("two") |
| 117 email.policy.default.handle_defect(foo, defect2) | 134 email.policy.default.handle_defect(foo, defect2) |
| 118 self.assertEqual(foo.defects, [defect1, defect2]) | 135 self.assertEqual(foo.defects, [defect1, defect2]) |
| 119 | 136 |
| 120 class MyPolicy(email.policy.Policy): | 137 class MyPolicy(email.policy.Compat32): |
| 121 defects = None | 138 defects = None |
| 122 def __init__(self, *args, **kw): | 139 def __init__(self, *args, **kw): |
| 123 super().__init__(*args, defects=[], **kw) | 140 super().__init__(*args, defects=[], **kw) |
| 124 def register_defect(self, obj, defect): | 141 def register_defect(self, obj, defect): |
| 125 self.defects.append(defect) | 142 self.defects.append(defect) |
| 126 | 143 |
| 127 def test_overridden_register_defect_still_raises(self): | 144 def test_overridden_register_defect_still_raises(self): |
| 128 foo = self.MyObj() | 145 foo = self.MyObj() |
| 129 defect = self.MyDefect("the telly is broken") | 146 defect = self.MyDefect("the telly is broken") |
| 130 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"): | 147 with self.assertRaisesRegex(self.MyDefect, "the telly is broken"): |
| 131 self.MyPolicy(raise_on_defect=True).handle_defect(foo, defect) | 148 self.MyPolicy(raise_on_defect=True).handle_defect(foo, defect) |
| 132 | 149 |
| 133 def test_overriden_register_defect_works(self): | 150 def test_overriden_register_defect_works(self): |
| 134 foo = self.MyObj() | 151 foo = self.MyObj() |
| 135 defect1 = self.MyDefect("one") | 152 defect1 = self.MyDefect("one") |
| 136 my_policy = self.MyPolicy() | 153 my_policy = self.MyPolicy() |
| 137 my_policy.handle_defect(foo, defect1) | 154 my_policy.handle_defect(foo, defect1) |
| 138 self.assertEqual(my_policy.defects, [defect1]) | 155 self.assertEqual(my_policy.defects, [defect1]) |
| 139 self.assertEqual(foo.defects, []) | 156 self.assertEqual(foo.defects, []) |
| 140 defect2 = self.MyDefect("two") | 157 defect2 = self.MyDefect("two") |
| 141 my_policy.handle_defect(foo, defect2) | 158 my_policy.handle_defect(foo, defect2) |
| 142 self.assertEqual(my_policy.defects, [defect1, defect2]) | 159 self.assertEqual(my_policy.defects, [defect1, defect2]) |
| 143 self.assertEqual(foo.defects, []) | 160 self.assertEqual(foo.defects, []) |
| 144 | 161 |
| 145 # XXX: Need subclassing tests. | 162 # XXX: Need subclassing tests. |
| 146 # For adding subclassed objects, make sure the usual rules apply (subclass | 163 # For adding subclassed objects, make sure the usual rules apply (subclass |
| 147 # wins), but that the order still works (right overrides left). | 164 # wins), but that the order still works (right overrides left). |
| 148 | 165 |
| 166 |
| 167 class TestPolicyPropagation(unittest.TestCase): |
| 168 |
| 169 # The abstract methods are used by the parser but not by the wrapper |
| 170 # functions that call it, so if the exception gets raised we know that the |
| 171 # policy was actually propagated all the way to feedparser. |
| 172 class MyPolicy(email.policy.Policy): |
| 173 def badmethod(self, *args, **kw): |
| 174 raise Exception("test") |
| 175 fold = fold_binary = header_fetch_parser = badmethod |
| 176 header_source_parse = header_store_parse = badmethod |
| 177 |
| 178 def test_message_from_string(self): |
| 179 with self.assertRaisesRegex(Exception, "^test$"): |
| 180 email.message_from_string("Subject: test\n\n", |
| 181 policy=self.MyPolicy) |
| 182 |
| 183 def test_message_from_bytes(self): |
| 184 with self.assertRaisesRegex(Exception, "^test$"): |
| 185 email.message_from_bytes(b"Subject: test\n\n", |
| 186 policy=self.MyPolicy) |
| 187 |
| 188 def test_message_from_file(self): |
| 189 f = io.StringIO('Subject: test\n\n') |
| 190 with self.assertRaisesRegex(Exception, "^test$"): |
| 191 email.message_from_file(f, policy=self.MyPolicy) |
| 192 |
| 193 def test_message_from_binary_file(self): |
| 194 f = io.BytesIO(b'Subject: test\n\n') |
| 195 with self.assertRaisesRegex(Exception, "^test$"): |
| 196 email.message_from_binary_file(f, policy=self.MyPolicy) |
| 197 |
| 198 # These are redundant, but we need them for black-box completeness. |
| 199 |
| 200 def test_parser(self): |
| 201 p = email.parser.Parser(policy=self.MyPolicy) |
| 202 with self.assertRaisesRegex(Exception, "^test$"): |
| 203 p.parsestr('Subject: test\n\n') |
| 204 |
| 205 def test_bytes_parser(self): |
| 206 p = email.parser.BytesParser(policy=self.MyPolicy) |
| 207 with self.assertRaisesRegex(Exception, "^test$"): |
| 208 p.parsebytes(b'Subject: test\n\n') |
| 209 |
| 210 # Now that we've established that all the parse methods get the |
| 211 # policy in to feedparser, we can use message_from_string for |
| 212 # the rest of the propagation tests. |
| 213 |
| 214 def _make_msg(self, source='Subject: test\n\n', policy=None): |
| 215 self.policy = email.policy.default.clone() if policy is None else policy |
| 216 return email.message_from_string(source, policy=self.policy) |
| 217 |
| 218 def test_parser_propagates_policy_to_message(self): |
| 219 msg = self._make_msg() |
| 220 self.assertIs(msg.policy, self.policy) |
| 221 |
| 222 def test_parser_propagates_policy_to_sub_messages(self): |
| 223 msg = self._make_msg(textwrap.dedent("""\ |
| 224 Subject: mime test |
| 225 MIME-Version: 1.0 |
| 226 Content-Type: multipart/mixed, boundary="XXX" |
| 227 |
| 228 --XXX |
| 229 Content-Type: text/plain |
| 230 |
| 231 test |
| 232 --XXX |
| 233 Content-Type: text/plain |
| 234 |
| 235 test2 |
| 236 --XXX-- |
| 237 """)) |
| 238 for part in msg.walk(): |
| 239 self.assertIs(part.policy, self.policy) |
| 240 |
| 241 def test_message_policy_propagates_to_generator(self): |
| 242 msg = self._make_msg("Subject: test\nTo: foo\n\n", |
| 243 policy=email.policy.default.clone(linesep='X')) |
| 244 s = io.StringIO() |
| 245 g = email.generator.Generator(s) |
| 246 g.flatten(msg) |
| 247 self.assertEqual(s.getvalue(), "Subject: testXTo: fooXX") |
| 248 |
| 249 def test_message_policy_used_by_as_string(self): |
| 250 msg = self._make_msg("Subject: test\nTo: foo\n\n", |
| 251 policy=email.policy.default.clone(linesep='X')) |
| 252 self.assertEqual(msg.as_string(), "Subject: testXTo: fooXX") |
| 253 |
| 254 |
| 149 if __name__ == '__main__': | 255 if __name__ == '__main__': |
| 150 unittest.main() | 256 unittest.main() |
| OLD | NEW |