Read smtp server configuration from config.py (#914)

* Read smtp server configuration from config.py

The CTFd/utils/email/smtp.py file has a provision to read SMTP
configuration for all fields from either the UI or CTFd/config.py file.
Two fields, `MAIL_SERVER` and `MAIL_PORT`, were not being read from the
config.py file. This commit fixes this issue.

* Update simple SMTP server tests
This commit is contained in:
s-nirali
2019-03-22 02:10:08 +00:00
committed by Kevin Chung
parent 1eb687a065
commit 37dcfdc568
2 changed files with 31 additions and 4 deletions

View File

@@ -22,8 +22,8 @@ def sendmail(addr, text):
ctf_name = get_config('ctf_name') ctf_name = get_config('ctf_name')
mailfrom_addr = get_config('mailfrom_addr') or get_app_config('MAILFROM_ADDR') mailfrom_addr = get_config('mailfrom_addr') or get_app_config('MAILFROM_ADDR')
data = { data = {
'host': get_config('mail_server'), 'host': get_config('mail_server') or get_app_config('MAIL_SERVER'),
'port': int(get_config('mail_port')) 'port': int(get_config('mail_port') or get_app_config('MAIL_PORT'))
} }
username = get_config('mail_username') or get_app_config('MAIL_USERNAME') username = get_config('mail_username') or get_app_config('MAIL_USERNAME')
password = get_config('mail_password') or get_app_config('MAIL_PASSWORD') password = get_config('mail_password') or get_app_config('MAIL_PASSWORD')

View File

@@ -28,8 +28,35 @@ def test_check_email_format():
@patch('smtplib.SMTP') @patch('smtplib.SMTP')
def test_sendmail_with_smtp(mock_smtp): def test_sendmail_with_smtp_from_config_file(mock_smtp):
"""Does sendmail work properly with simple SMTP mail servers""" """Does sendmail work properly with simple SMTP mail servers using file configuration"""
app = create_ctfd()
with app.app_context():
app.config['MAIL_SERVER'] = 'localhost'
app.config['MAIL_PORT'] = '25'
app.config['MAIL_USEAUTH'] = 'True'
app.config['MAIL_USERNAME'] = 'username'
app.config['MAIL_PASSWORD'] = 'password'
from_addr = get_config('mailfrom_addr') or app.config.get('MAILFROM_ADDR')
to_addr = 'user@user.com'
msg = 'this is a test'
sendmail(to_addr, msg)
ctf_name = get_config('ctf_name')
email_msg = MIMEText(msg)
email_msg['Subject'] = "Message from {0}".format(ctf_name)
email_msg['From'] = from_addr
email_msg['To'] = to_addr
mock_smtp.return_value.sendmail.assert_called_once_with(from_addr, [to_addr], email_msg.as_string())
destroy_ctfd(app)
@patch('smtplib.SMTP')
def test_sendmail_with_smtp_from_db_config(mock_smtp):
"""Does sendmail work properly with simple SMTP mail servers using database configuration"""
app = create_ctfd() app = create_ctfd()
with app.app_context(): with app.app_context():
set_config('mail_server', 'localhost') set_config('mail_server', 'localhost')