Conversation
| self.assertIn(" ( https://", mail.body) | ||
| self.assertIn("*peter.mueller@kreis-steinfurt.de*", mail.body) | ||
|
|
||
| def test_invalid_mail_address(self): |
There was a problem hiding this comment.
Ideally this should be more a unit test (and less like a integration test): parsing a whole email just to test parsing of addresses makes this test not very specific. Also adding a large email with an image attachment (?!) is too much.
There was a problem hiding this comment.
Thank you for the feedback! I agree this is to unspecific. I will remove this test and do it more like this (without parsing a whole email):
| def test_invalid_mail_address(self): | |
| def test_invalid_mail_address(self): | |
| values = ["To: 'John Doe [#123]' <john.doe@example.com>"] | |
| ret = get_address_list(values) | |
| self.assertEqual('john.doe@example.com', ret[0].email) |
|
|
||
| def check_mail(email: str): | ||
| # Credit for regex to Anđela Niketić at https://stackabuse.com/python-validate-email-address-with-regular-expressions-regex/ | ||
| regex_mail = r"([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)*|\[[\t -Z^-~]*])" |
There was a problem hiding this comment.
This regex is too complex to maintain for my taste (and still doesn't capture the complexity of proper email address parsing if you wanted to go there).
Please use the Django email validator instead.
There was a problem hiding this comment.
Thank you also for this feedback! I used the Django Email validator as you suggested. I recognized that it also catched two cases were the emails in the test data were invalid (according to the django validator):
- In foirequest/tests/testdata/test_mail_05.txt on line 33: "CC: <Kämmerer.a@zdf.de>, Biogiani.D@zdf.de". The test
test_recipient_parsingfails if the django email validator is used, because the first email-address is filtered. This seems so be a general problem with addresses which have umlaut characters in the local part. From what I found the django EmailValidator does not support non-ASCII characters in the local part. The email-validator for example has anallow_smtputf8option which seems to be not available in the django validator. - In foirequest/tests/testdata/test_mail_08.txt on line 2: "X-Original-To: <fragdenstaat class="de"></fragdenstaat>".
I could additionally change the mail addresses in those two cases in the test data if this makes sense? And would it make sense to use another validator because of the umlaut problem?
Possible Fix for #515 and a test case for handling invalid email addresses.
I added a small function which checks if the email addresses that are returned by the email parser are valid. If not they are note appended in the address list.
Additionally, I added a test case with a new test mail including the incorrect mail address mentioned in the issue #515 .