Skip to content

Commit

Permalink
Merge pull request #19 from 153957/whitelist
Browse files Browse the repository at this point in the history
Fix parsing of emails for whitelist
  • Loading branch information
vkurup authored Dec 8, 2017
2 parents 168f220 + c4937f3 commit 4c8c795
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion bandit/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

from email.utils import parseaddr
from functools import reduce
from operator import and_

Expand Down Expand Up @@ -31,7 +32,8 @@ def send_messages(self, email_messages):
[email for name, email in admins])

def is_approved(email):
local_part, _, domain = email.rpartition('@')
_, email = parseaddr(email)
_, _, domain = email.rpartition('@')
return email in approved_emails or domain in whitelist_emails

to_send = []
Expand Down
6 changes: 4 additions & 2 deletions bandit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ def test_send_multiple(self):
self.assertEqual(message.get_all('to'), ['[email protected]', ])

def test_whitelist_domain(self):
addresses = ['[email protected]', '[email protected]']
addresses = ['[email protected]',
'<[email protected]>',
'Foo Bar <[email protected]>']
emails = [EmailMessage( 'Subject', 'Content', '[email protected]', addresses)]
num_sent = self.get_connection().send_messages(emails)
self.assertEqual(len(emails), num_sent)
messages = self.get_mailbox_content()
self.assertEqual(messages[0].get_all('to'), [', '.join(addresses)])
self.assertEqual(messages[0].get_all('to')[0].replace('\n', ''), ', '.join(addresses))


class LogOnlyBackendTestCase(BaseBackendTestCase):
Expand Down
7 changes: 3 additions & 4 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@


def runtests():
if django.VERSION > (1, 7):
# http://django.readthedocs.org/en/latest/releases/1.7.html#standalone-scripts
django.setup()
django.setup()
from django.test.utils import get_runner
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=1, interactive=True, failfast=True)
failures = test_runner.run_tests(['bandit', ])
failures = test_runner.run_tests(['bandit'])
if failures:
sys.exit(1)


if __name__ == '__main__':
runtests(*sys.argv[1:])

0 comments on commit 4c8c795

Please sign in to comment.