mirror of
https://github.com/Receipt-Wrangler/receipt-wrangler-api.git
synced 2026-05-05 08:40:05 -05:00
connect via startTLS
This commit is contained in:
@@ -16,8 +16,15 @@ def read_system_email():
|
||||
if __name__ == '__main__':
|
||||
init_logger()
|
||||
credentials = read_system_email()
|
||||
client = ImapClient(credentials["host"], credentials["port"], credentials["username"], credentials["password"], [],
|
||||
[])
|
||||
client = ImapClient(
|
||||
credentials["host"],
|
||||
credentials["port"],
|
||||
credentials["username"],
|
||||
credentials["password"],
|
||||
credentials["useStartTLS"],
|
||||
[],
|
||||
[]
|
||||
)
|
||||
|
||||
try:
|
||||
client.connect()
|
||||
|
||||
@@ -42,6 +42,7 @@ def main():
|
||||
system_email["port"],
|
||||
system_email["username"],
|
||||
system_email["password"],
|
||||
system_email["useStartTLS"],
|
||||
all_subject_line_regexes,
|
||||
all_email_whitelist
|
||||
)
|
||||
|
||||
@@ -21,16 +21,22 @@ class ImapClient:
|
||||
email_whitelist = None
|
||||
client = None
|
||||
|
||||
def __init__(self, host, port, username, password, subject_line_regexes, email_whitelist):
|
||||
def __init__(self, host, port, username, password, use_starttls, subject_line_regexes, email_whitelist):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.use_starttls = use_starttls
|
||||
self.subject_line_regexes = subject_line_regexes or []
|
||||
self.email_whitelist = email_whitelist or []
|
||||
|
||||
def connect(self):
|
||||
self.client = IMAPClient(self.host, self.port)
|
||||
if self.use_starttls:
|
||||
self.client = IMAPClient(self.host, self.port, ssl=False)
|
||||
self.client.starttls()
|
||||
else:
|
||||
self.client = IMAPClient(self.host, self.port)
|
||||
|
||||
self.client.login(self.username, self.password)
|
||||
|
||||
def get_unread_email_metadata(self):
|
||||
|
||||
@@ -21,7 +21,8 @@ class TestClient(unittest.TestCase):
|
||||
"host": "imap.gmail.com",
|
||||
"port": 993,
|
||||
"username": "test@gmail.com",
|
||||
"password": "password"
|
||||
"password": "password",
|
||||
"useStartTLS": False
|
||||
},
|
||||
"subjectLineRegexes": [{"regex": ".*test.*"}],
|
||||
"emailWhiteList": [{"email": "test@example.com"}],
|
||||
@@ -53,6 +54,7 @@ class TestClient(unittest.TestCase):
|
||||
993,
|
||||
"test@gmail.com",
|
||||
"password",
|
||||
False,
|
||||
[{"regex": ".*test.*"}],
|
||||
[{"email": "test@example.com"}]
|
||||
)
|
||||
@@ -94,7 +96,8 @@ class TestClient(unittest.TestCase):
|
||||
"host": "imap.gmail.com",
|
||||
"port": 993,
|
||||
"username": "test@gmail.com",
|
||||
"password": "password"
|
||||
"password": "password",
|
||||
"useStartTLS": False
|
||||
},
|
||||
"subjectLineRegexes": [{"regex": ".*test.*"}],
|
||||
"emailWhiteList": [{"email": "test@example.com"}],
|
||||
@@ -126,6 +129,7 @@ class TestClient(unittest.TestCase):
|
||||
993,
|
||||
"test@gmail.com",
|
||||
"password",
|
||||
False,
|
||||
[{"regex": ".*test.*"}],
|
||||
[{"email": "test@example.com"}]
|
||||
)
|
||||
|
||||
@@ -8,7 +8,7 @@ from imap_client import ImapClient
|
||||
class TestShouldSetUpClientCorrectly(unittest.TestCase):
|
||||
|
||||
def test_constructor(self):
|
||||
client = ImapClient("host", "port", "username", "password", [], [])
|
||||
client = ImapClient("host", "port", "username", "password", False, [], [])
|
||||
self.assertEqual(client.host, "host")
|
||||
self.assertEqual(client.port, "port")
|
||||
self.assertEqual(client.username, "username")
|
||||
@@ -17,13 +17,13 @@ class TestShouldSetUpClientCorrectly(unittest.TestCase):
|
||||
@patch('imap_client.IMAPClient')
|
||||
def test_catch_error_with_bad_connect(self, mock_imapclient):
|
||||
mock_imapclient.side_effect = Exception('Failed to connect')
|
||||
client = ImapClient("host", 993, "username", "password", [], [])
|
||||
client = ImapClient("host", 993, "username", "password", False, [], [])
|
||||
with self.assertRaises(Exception) as context:
|
||||
client.connect()
|
||||
self.assertTrue('Failed to connect' in str(context.exception))
|
||||
|
||||
def setUp(self):
|
||||
self.client = ImapClient('host', 'port', 'username', 'password', [], [])
|
||||
self.client = ImapClient('host', 'port', 'username', 'password', False, [], [])
|
||||
|
||||
def test_get_formatted_to_or_from_data(self):
|
||||
message = Message()
|
||||
|
||||
@@ -52,6 +52,7 @@ func (service SystemEmailService) CheckEmailConnectivity(command commands.CheckE
|
||||
command.Host = systemEmail.Host
|
||||
command.Port = systemEmail.Port
|
||||
command.Username = systemEmail.Username
|
||||
command.UseStartTLS = systemEmail.UseStartTLS
|
||||
|
||||
cleartextPassword, err := utils.DecryptB64EncodedData(config.GetEncryptionKey(), systemEmail.Password)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user