def test_use_starttls(): conn = SMTPConnection(account_id=1, email_address='*****@*****.**', auth_type='password', auth_token='secret_password', smtp_endpoint=('smtp.gmail.com', 587), log=get_logger()) assert isinstance(conn.connection, smtplib.SMTP)
def test_use_smtp_over_ssl(): # Auth won't actually work but we just want to test connection # initialization here and below. conn = SMTPConnection(account_id=1, email_address='*****@*****.**', auth_type='password', auth_token='secret_password', smtp_endpoint=('smtp.gmail.com', 465), log=get_logger()) assert isinstance(conn.connection, smtplib.SMTP_SSL)
def test_use_starttls(): conn = SMTPConnection( account_id=1, email_address="*****@*****.**", smtp_username="******", auth_type="password", auth_token="secret_password", smtp_endpoint=("smtp.gmail.com", 587), log=get_logger(), ) assert isinstance(conn.connection, smtplib.SMTP)
def test_use_plain(): ssl = True with pytest.raises(SendMailException): conn = SMTPConnection(account_id=1, email_address='*****@*****.**', smtp_username='******', auth_type='password', auth_token='testpwd', smtp_endpoint=('tivertical.com', 587), ssl_required=ssl, log=get_logger()) ssl = False conn = SMTPConnection(account_id=1, email_address='*****@*****.**', smtp_username='******', auth_type='password', auth_token='testpwd', smtp_endpoint=('tivertical.com', 587), ssl_required=ssl, log=get_logger()) assert isinstance(conn.connection, smtplib.SMTP)
def test_use_smtp_over_ssl(): # Auth won't actually work but we just want to test connection # initialization here and below. SMTPConnection.smtp_password = mock.Mock() conn = SMTPConnection( account_id=1, email_address="*****@*****.**", smtp_username="******", auth_type="password", auth_token="secret_password", smtp_endpoint=("smtp.gmail.com", 465), log=get_logger(), ) assert isinstance(conn.connection, smtplib.SMTP_SSL)
def test_handle_disconnect(monkeypatch, smtp_port): def simulate_disconnect(self): raise smtplib.SMTPServerDisconnected() monkeypatch.setattr('smtplib.SMTP.rset', simulate_disconnect) monkeypatch.setattr('smtplib.SMTP.mail', lambda *args: (550, 'NOPE')) conn = SMTPConnection(account_id=1, email_address='*****@*****.**', auth_type='password', auth_token='secret_password', smtp_endpoint=('smtp.gmail.com', smtp_port), log=get_logger()) with pytest.raises(smtplib.SMTPSenderRefused): conn.sendmail(['*****@*****.**'], 'hello there')
def test_use_plain(): ssl = True with pytest.raises(SendMailException): conn = SMTPConnection( account_id=1, email_address="*****@*****.**", smtp_username="******", auth_type="password", auth_token="testpwd", smtp_endpoint=("tivertical.com", 587), log=get_logger(), ) ssl = False conn = SMTPConnection( account_id=1, email_address="*****@*****.**", smtp_username="******", auth_type="password", auth_token="testpwd", smtp_endpoint=("tivertical.com", 587), log=get_logger(), ) assert isinstance(conn.connection, smtplib.SMTP)
def test_handle_disconnect(monkeypatch, smtp_port): def simulate_disconnect(self): raise smtplib.SMTPServerDisconnected() monkeypatch.setattr("smtplib.SMTP.rset", simulate_disconnect) monkeypatch.setattr("smtplib.SMTP.mail", lambda *args: (550, "NOPE")) conn = SMTPConnection( account_id=1, email_address="*****@*****.**", smtp_username="******", auth_type="password", auth_token="secret_password", smtp_endpoint=("smtp.gmail.com", smtp_port), log=get_logger(), ) with pytest.raises(smtplib.SMTPSenderRefused): conn.sendmail(["*****@*****.**"], "hello there")
def try_fill_config_data(email_address, password): response = {} subdomains = ['mail.', 'imap.', 'smtp.', ''] domain = email_address.split('@')[1].lower() smtp_port = 587 imap_port = 993 log = get_logger() for imap_host in [subdomain + domain for subdomain in subdomains]: try: create_imap_connection(imap_host, imap_port, True, use_timeout=10) response['imap_server_host'] = imap_host response['imap_server_port'] = imap_port break except SSLNotSupportedError: create_imap_connection(imap_host, imap_port, False, use_timeout=10) response['imap_server_host'] = imap_host response['imap_server_port'] = imap_port response['ssl_required'] = False break except (IMAPClient.Error, socket.error): continue if 'imap_server_host' not in response: raise ConnectionError( 'IMAP connection failed. Check your password. ' 'If error persists try provide connection details') for smtp_host in [subdomain + domain for subdomain in subdomains]: try: with SMTPConnection(0, email_address, email_address, 'password', password, (smtp_host, smtp_port), True, log): response['smtp_server_host'] = smtp_host response['smtp_server_port'] = smtp_port break except: continue if 'smtp_server_host' not in response: raise ConnectionError( 'SMTP connection failed. Check your password. ' 'If error persists try provide connection details') return response