forked from mindsdb/mindsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gmail_handler.py
More file actions
240 lines (220 loc) · 10.1 KB
/
test_gmail_handler.py
File metadata and controls
240 lines (220 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from mindsdb.api.executor.data_types.response_type import RESPONSE_TYPE
from mindsdb.integrations.handlers.gmail_handler.gmail_handler import GmailHandler
from mindsdb.integrations.handlers.gmail_handler.gmail_handler import EmailsTable
from google.oauth2.credentials import Credentials
from mindsdb_sql_parser import parse_sql
import unittest
from unittest.mock import Mock, patch
from unittest import mock
class GmailHandlerTest(unittest.TestCase):
def setUp(self) -> None:
self.credentials_file = 'test1_credentials.json'
self.credentials_url = 's3://your-bucket/test_credentials.json'
self.handler = GmailHandler(connection_data={
'credentials_file': self.credentials_file,
'credentials_url': self.credentials_url
})
@patch('mindsdb.integrations.handlers.gmail_handler.gmail_handler.requests.get') # Patching the requests.get method
def test_has_creds_file_with_valid_s3_link(self, mock_get):
# Configure the mock behavior
mock_response = mock_get.return_value
mock_response.status_code = 200
mock_response.text = 'Mocked credentials file content'
result = self.handler._has_creds_file(self.credentials_file)
# Assert that the requests.get method was called with the correct URL
mock_get.assert_called_once_with(self.credentials_url)
# Assert that the method returns True
self.assertTrue(result)
@patch('mindsdb.integrations.handlers.gmail_handler.gmail_handler.requests.get') # Patching the requests.get method
def test_has_creds_file_with_invalid_s3_link(self, mock_get):
# Test when invalid S3 credentials file is provided
mock_response = mock.Mock()
mock_response.status_code = 404
mock_get.return_value = mock_response
# TODO this will be broken now that we don't use global loggers anymore
with patch('mindsdb.utilities.log.logger') as mock_logger:
result = self.handler._has_creds_file(self.credentials_file)
# Assert that the requests.get method was called with the correct URL
self.assertFalse(result)
# Assert that the error message is logged
mock_logger.error.assert_called_once_with("Failed to get credentials from S3", 404)
def test_create_connection_with_mocked_token(self):
with mock.patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_credentials:
mock_credentials.return_value = Credentials('token.json')
with mock.patch('os.path.isfile') as mock_isfile:
mock_isfile.return_value = True
result = self.handler.create_connection()
self.assertIsNotNone(result)
def test_create_connection_with_mocked_credentials_file(self):
with mock.patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_credentials:
mock_credentials.is_valid.return_value = False
with mock.patch('os.path.isfile') as mock_isfile:
mock_isfile.return_value = True
result = self.handler.create_connection()
self.assertIsNotNone(result)
def test_create_connection_with_mocked_credentials_file_and_s3(self):
with mock.patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_credentials:
mock_credentials.is_valid.return_value = False
with mock.patch('os.path.isfile') as mock_isfile:
mock_isfile.return_value = True
with mock.patch(
'mindsdb.integrations.handlers.gmail_handler.gmail_handler.GmailHandler._has_creds_file') \
as mock_has_creds_file:
mock_has_creds_file.return_value = True
result = self.handler.create_connection()
self.assertIsNotNone(result)
def test_parse_parts_with_multipart_mime_type(self):
email_parts = [
{
'mimeType': 'multipart/mixed',
'parts': [
{
'mimeType': 'multipart/alternative',
'parts': [
{
'mimeType': 'text/plain',
'body': {
'data': 'VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBib2R5IG9mIHRoZSBlbWFpbC4='
}
},
{
'mimeType': 'text/html',
'body': {
'data': 'PGh0bWw+CiAgICA8Ym9keT4KICAgICAgPHA+V'
'GhpcyBpcyB0aGUgSFRNTCBib2R5IG9mIHRoZSBlbWFpbC4'
'gPC9wPgogICAgPC9ib2R5PjwvaHRtbD4='
}
}
]
},
{
'mimeType': 'application/pdf',
'filename': 'example.pdf',
'body': {
'attachmentId': '<<attachment_id>>'
}
}
]
}
]
attachments = []
email_body = self.handler._parse_parts(email_parts, attachments)
expected_body = "This is the plain text body of the email."
expected_attachments = [
{
'filename': 'example.pdf',
'mimeType': 'application/pdf',
'attachmentId': '<<attachment_id>>'
}
]
self.assertEqual(email_body, expected_body)
self.assertEqual(attachments, expected_attachments)
def test_parse_parts_with_multipart_mime_type_and_no_parts(self):
email_parts = [
{
'mimeType': 'multipart/mixed',
'parts': []
}
]
attachments = []
email_body = self.handler._parse_parts(email_parts, attachments)
expected_body = ""
expected_attachments = []
self.assertEqual(email_body, expected_body)
self.assertEqual(attachments, expected_attachments)
def test_parse_parts_with_multiple_attachments(self):
email_parts = [
{
'mimeType': 'multipart/mixed',
'parts': [
{
'mimeType': 'multipart/alternative',
'parts': [
{
'mimeType': 'text/plain',
'body': {
'data': 'VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBib2R5IG9mIHRoZSBlbWFpbC4='
}
},
{
'mimeType': 'text/html',
'body': {
'data': 'PGh0bWw+CiAgICA8Ym9keT4KICAgICAgPHA+'
'VGhpcyBpcyB0aGUgSFRNTCBib2R5IG9mIHRoZSBlbWFpb'
'C4gPC9wPgogICAgPC9ib2R5PjwvaHRtbD4='
}
}
]
},
{
'mimeType': 'application/pdf',
'filename': 'example.pdf',
'body': {
'attachmentId': '<<attachment_id>>'
}
},
{
'mimeType': 'application/pdf',
'filename': 'example2.pdf',
'body': {
'attachmentId': '<<attachment_id2>>'
}
}
]
}
]
attachments = []
email_body = self.handler._parse_parts(email_parts, attachments)
expected_body = "This is the plain text body of the email."
expected_attachments = [
{
'filename': 'example.pdf',
'mimeType': 'application/pdf',
'attachmentId': '<<attachment_id>>'
},
{
'filename': 'example2.pdf',
'mimeType': 'application/pdf',
'attachmentId': '<<attachment_id2>>'
}
]
self.assertEqual(email_body, expected_body)
self.assertEqual(attachments, expected_attachments)
class EmailsTableTest(unittest.TestCase):
def test_get_tables(self):
handler = Mock(GmailHandler)
tables = handler.get_tables()
assert tables.type is not RESPONSE_TYPE.ERROR
def test_get_columns_returns_all_columns(self):
gmail_handler = Mock(GmailHandler)
gmail_table = EmailsTable(gmail_handler)
expected_columns = [
'id',
'message_id',
'thread_id',
'label_ids',
'sender',
'to',
'date',
'subject',
'snippet',
'history_id',
'size_estimate',
'body',
'attachments'
]
self.assertListEqual(gmail_table.get_columns(), expected_columns)
def test_delete_method(self):
gmail_handler = Mock(GmailHandler)
gmail_table = EmailsTable(gmail_handler)
query = parse_sql('delete from gmail where id=1')
gmail_table.delete(query)
gmail_handler.call_gmail_api.assert_called_once_with('delete_message', {'id': 1})
def test_update_method(self):
gmail_handler = Mock(GmailHandler)
gmail_table = EmailsTable(gmail_handler)
query = parse_sql('update gmail set addLabel="test1",removeLabel = "test" where id=1')
gmail_table.update(query)
gmail_handler.call_gmail_api.assert_called_once_with('modify_message', {'id': 1,
'body': {'addLabelIds': ['test1'],
'removeLabelIds': ['test']}})