-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mail to be sent to multiple recipients
Now the recipients argument must be a list, but everywhere that we use this it already was. Add some additional error checking to ensure that configuration is good before sending the message
- Loading branch information
Showing
2 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,37 +2,66 @@ | |
import smtplib | ||
from unittest import mock | ||
|
||
from email.mime.application import MIMEApplication | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from brainzutils import flask | ||
from brainzutils import mail | ||
|
||
class MailTestCase(unittest.TestCase): | ||
|
||
def test_send_email_missing_config(self): | ||
app = flask.CustomFlask(__name__) | ||
with app.app_context(): | ||
with self.assertRaises(ValueError) as err: | ||
mail.send_mail( | ||
subject='ListenBrainz Spotify Importer Error', | ||
text='It is a test mail', | ||
recipients=[], | ||
attachments=None, | ||
from_name='ListenBrainz', | ||
from_addr='[email protected]', | ||
boundary='b' | ||
) | ||
assert "Flask current_app requires config items" in str(err.exception) | ||
|
||
def test_send_email_string_recipients(self): | ||
app = flask.CustomFlask(__name__) | ||
with app.app_context(): | ||
with self.assertRaises(ValueError) as err: | ||
mail.send_mail( | ||
subject='ListenBrainz Spotify Importer Error', | ||
text='It is a test mail', | ||
recipients='[email protected]', | ||
attachments=None, | ||
from_name='ListenBrainz', | ||
from_addr='[email protected]', | ||
boundary='b' | ||
) | ||
assert str(err.exception) == "recipients must be a list of email addresses" | ||
|
||
@mock.patch('smtplib.SMTP') | ||
def test_send_email(self, mock_smtp): | ||
app = flask.CustomFlask(__name__) | ||
app.config['SMTP_SERVER'] = 'localhost' | ||
app.config['SMTP_PORT'] = 8080 | ||
app.config['SMTP_PORT'] = 25 | ||
|
||
with app.app_context(): | ||
from_address = '[email protected]' | ||
recipients = '[email protected]' | ||
recipients = ['[email protected]', '[email protected]'] | ||
text = 'It is a test mail' | ||
from_name = 'ListenBrainz' | ||
subject = 'ListenBrainz Spotify Importer Error' | ||
boundary = '===============2220963697271485568==' | ||
message = MIMEMultipart(boundary=boundary) | ||
message['To'] = '<%s>' % (recipients) | ||
message['To'] = "[email protected], [email protected]" | ||
message['Subject'] = subject | ||
message['From'] = '%s <%s>' % (from_name, from_address) | ||
message.attach(MIMEText(text, _charset='utf-8')) | ||
|
||
mail.send_mail( | ||
subject='ListenBrainz Spotify Importer Error', | ||
text='It is a test mail', | ||
recipients='[email protected]', | ||
recipients=recipients, | ||
attachments=None, | ||
from_name='ListenBrainz', | ||
from_addr='[email protected]', | ||
|