-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
PreprocessEmail_test.py
652 lines (595 loc) · 26.8 KB
/
PreprocessEmail_test.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import json
from CommonServerPython import FileAttachmentType
import demistomock as demisto
import pytest
from datetime import datetime
def util_open_file(path):
with open(path) as f:
return f.read()
def util_load_json(path):
with open(path) as f:
return json.loads(f.read())
@pytest.mark.parametrize(
"list_response, expected_result",
[
(util_load_json('test_data/getList_querywindow_success.json'), 'success'),
(util_load_json('test_data/getList_querywindow_error.json'), 'fail')
]
)
def test_get_query_window(list_response, expected_result, mocker):
"""
Unit Test Scenario 1 - List exists
Given
- Query window value stored in XSOAR List
When
- List content is returned successfully
Then
- Validate that the function returns the correct window based on the list response
Unit Test Scenario 2 - List retrieval fails
Given
- Query window value stored in XSOAR List
When
- List retrieval results in an error
Then
- Validate that the function returns the default '60 days' window
- Validate that a debug message is saved indicating the list couldn't be fetched
"""
from PreprocessEmail import get_query_window
mocker.patch.object(demisto, 'executeCommand', return_value=list_response)
debug_mocker = mocker.patch.object(demisto, 'debug')
result = get_query_window()
debug_mocker_call_args = debug_mocker.call_args
if expected_result == 'success':
assert result == '90 days'
elif expected_result == 'fail':
assert result == '60 days'
assert debug_mocker_call_args.args[0] == 'Error occurred while trying to load the `XSOAR - Email ' \
'Communication Days To Query` list. Using the default query time - ' \
'60 days'
def test_set_email_reply():
"""Unit test
Given
- Email author, email recipients and email cc.
When
- Setting the email reply.
Then
- Validate that the email reply is in the correct format.
"""
from PreprocessEmail import set_email_reply
expected_result = util_open_file('test_data/email_reply.txt')
result = set_email_reply('test@gmail.com', '["test1@gmail.com"]', 'test2@gmail.com', 'test',
[{'name': 'image.png'}])
assert result in expected_result
EMAIL_HTML = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src="cid:ii_kgjzy6yh0" alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src="cid:ii_kgjzygxz1" alt="image_2.png" width="225" height="224"><br></div></div></div><br>
<div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 22, 2020 at 1:56 AM Avishai Brandeis <
<a href="mailto:avishai@demistodev.onmicrosoft.com">avishai@demistodev.onmicrosoft.com</a>> wrote:<br></div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)"><u></u><div>
<p>please add multiple inline images</p></div></blockquote></div></body></html>"""
EMAIL_HTML_NO_ALT = """
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" style="display:none">
<!–
p
{margin-top:0;
margin-bottom:0}
–>
</style></head>
<body dir="ltr"><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="178792" data-outlook-trace="F:1|T:1" src="cid:89593b98-b18d-46aa-ba4f-26773138c3f7" style="max-width:100%">
</div><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="8023" data-outlook-trace="F:1|T:1" src="cid:6a65eb70-7748-4bba-aaac-fe93235f63bd" style="max-width:100%">
</div></body></html>"""
EXPECTED_RESULT_1 = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src=entry/download/37@119 alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src="cid:ii_kgjzygxz1" alt="image_2.png" width="225" height="224"><br></div></div></div><br>
</body></html>"""
EXPECTED_RESULT_2 = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src="cid:ii_kgjzy6yh0" alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src="cid:ii_kgjzygxz1" alt="image_2.png" width="225" height="224"><br></div></div></div><br>
</body></html>"""
EXPECTED_RESULT_3 = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src=entry/download/37@119 alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src=entry/download/38@120 alt="image_2.png" width="225" height="224"><br></div></div></div><br>
</body></html>"""
EXPECTED_RESULT_NO_ALT = """
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" style="display:none">
<!–
p
{margin-top:0;
margin-bottom:0}
–>
</style></head>
<body dir="ltr"><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="178792" data-outlook-trace="F:1|T:1" src=entry/download/37@119 style="max-width:100%">
</div><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="8023" data-outlook-trace="F:1|T:1" src=entry/download/38@120 style="max-width:100%">
</div></body></html>"""
@pytest.mark.parametrize(
"email_html,entry_id_list,expected",
[(EMAIL_HTML, [('image_1.png', '37@119')], EXPECTED_RESULT_1),
(EMAIL_HTML, [], EXPECTED_RESULT_2),
(EMAIL_HTML, [('image_1.png', '37@119'), ('image_2.png', '38@120')], EXPECTED_RESULT_3),
(EMAIL_HTML_NO_ALT, [('image_1.png', '37@119'), ('image_2.png', '38@120')], EXPECTED_RESULT_NO_ALT),
]
)
def test_create_email_html(email_html, entry_id_list, expected):
"""
Given
- The email's Html representation.
When
1. Only one of the images were uploaded to the server hence only one entry id exists within the entry id list.
2. None of the images were uploaded to the server -> an empty entry_id_list.
3. All images were uploaded to the server.
Then
- The images' src attribute would be replaced as expected.
"""
from PreprocessEmail import create_email_html
result = create_email_html(email_html, entry_id_list)
assert result == expected
def test_get_entry_id_list():
"""
Given
- List of the email's attachments, List of files of the email's related incident.
When
- building an entry id list in order to replace the email's attachments source path.
Then
- Ensures that only the email attachments entry id's were returned and not all files entries.
"""
from PreprocessEmail import get_entry_id_list
attachments = [
{
"description": "",
"name": "image_1.png",
"path": "131_dd98957a-d5c3-42e0-8a81-f3ce7fa68215",
"showMediaFile": False,
"type": ""
},
{
"description": "",
"name": "image_2.png",
"path": "131_17545998-4b16-4e58-8e6c-2221ada856d4",
"showMediaFile": False,
"type": ""
}
]
files = [
{
"EntryID": "30@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "attachment_1.png",
"SHA1": "sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 63111,
"Type": "PNG image data, 1209 x 398, 8-bit/color RGBA, non-interlaced"
},
{
"EntryID": "34@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "attachment_2.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
},
{
"EntryID": "35@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "image_1.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
},
{
"EntryID": "36@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "image_2.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
}]
expected = [('image_1.png', '35@119'), ('image_2.png', '36@119')]
assert expected == get_entry_id_list(attachments, files)
def test_get_entry_id_list_with_attached_file():
"""
Given
- List of the email's attachments - but one attachment is marked as ATTACHED (not inline image)
- List of files of the email's related incident
When
- building an entry id list in order to replace the email's attachments source path.
Then
- Ensures that only the email attachments entry id's were returned and not all files entries
- Ensures that the attached file (attachment_1.pdf) is excluded since it is marked as ATTACHED
"""
from PreprocessEmail import get_entry_id_list
attachments = [
{
"description": FileAttachmentType.ATTACHED,
"name": "attachment_1.pdf",
"path": "131_dd98957a-d5c3-42e0-8a81-f3ce7fa68215",
"showMediaFile": False,
"type": ""
},
{
"description": "",
"name": "image_1.png",
"path": "131_dd98957a-d5c3-42e0-8a81-f3ce7fa68215",
"showMediaFile": False,
"type": ""
},
{
"description": "",
"name": "image_2.png",
"path": "131_17545998-4b16-4e58-8e6c-2221ada856d4",
"showMediaFile": False,
"type": ""
}
]
files = [
{
"EntryID": "30@119",
"Extension": "pdf",
"Info": "application/pdf",
"MD5": "md5",
"Name": "attachment_1.pdf",
"SHA1": "sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 63111,
"Type": "PDF document, version 1.4"
},
{
"EntryID": "34@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "attachment_2.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
},
{
"EntryID": "35@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "image_1.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
},
{
"EntryID": "36@119",
"Extension": "png",
"Info": "image/png",
"MD5": "md5",
"Name": "image_2.png",
"SHA1": "4sha1",
"SHA256": "sha256",
"SHA512": "sha512",
"SSDeep": "ssdeep",
"Size": 9580,
"Type": "PNG image data, 264 x 60, 8-bit/color RGBA, non-interlaced"
}]
expected = [('image_1.png', '35@119'), ('image_2.png', '36@119')]
assert expected == get_entry_id_list(attachments, files)
FILES = [
{
"SHA256": "SHA256"
},
{
"EntryID": "4@131",
"Extension": "png",
"Info": "image/png",
"MD5": "605ebf7bc83a00840a3ea90c8ed56515",
"Name": "image_1.png",
"SHA1": "SHA1",
"SHA256": "SHA256",
"SHA512": "SHA512",
"SSDeep": "SSDeep",
"Size": 127884,
"Type": "PNG image data, 275 x 184, 8-bit/color RGBA, non-interlaced"
},
{
"SHA256": "SHA256"
},
{
"EntryID": "5@131",
"Extension": "png",
"Info": "image/png",
"MD5": "fe96373473915fea94980b588de5fbb6",
"Name": "image_1.png",
"SHA1": "SHA1",
"SHA256": "SHA256",
"SHA512": "SHA512",
"SSDeep": "SSDeep",
"Size": 107596,
"Type": "PNG image data, 225 x 224, 8-bit/color RGBA, non-interlaced"
}
]
EMAIL_THREADS = [{"Contents": {"context": {
"id": "3",
"EmailThreads": [
{
"EmailBCC": "",
"EmailBody": "Outbound test message from XSOAR to User.",
"EmailCC": "",
"EmailCommsThreadId": "69433507",
"EmailCommsThreadNumber": "0",
"EmailFrom": "soc_sender@company.com",
"EmailHTML": "Outbound test message from XSOAR to User.",
"EmailReceived": "",
"EmailReplyTo": "soc_sender@company.com",
"EmailSubject": "<69433507> Test Email 2",
"EmailTo": "end_user@company.com",
'EmailAttachments': "['None']",
"MessageDirection": "outbound",
"MessageID": "",
"MessageTime": "2022-02-04T20:56:53UTC"
},
{
"EmailBCC": "",
"EmailBody": "Response from end user to SOC\r\n\r\n\r\nSignature Line\r\n\r\n\r\n\r\n______________________"
"__________\r\nFrom: SOC <soc_sender@company.com>\r\nSent: Friday, February 4, 2022 3:56 PM"
"\r\nTo: End User <end_user@company.com>\r\nSubject: <69433507> Test Email 2\r\n\r\nOutbound "
"test message from XSOAR to User.\r\n",
"EmailCC": "",
"EmailCommsThreadId": "69433507",
"EmailCommsThreadNumber": "0",
"EmailFrom": "end_user@company.com",
"EmailHTML": "Response from end user to SOC\r\n\r\n\r\nSignature Line\r\n\r\n\r\n\r\n______________________"
"__________\r\nFrom: SOC <soc_sender@company.com>\r\nSent: Friday, February 4, 2022 3:56 PM"
"\r\nTo: End User <end_user@company.com>\r\nSubject: <69433507> Test Email 2\r\n\r\nOutbound "
"test message from XSOAR to User.\r\n",
"EmailReceived": "soc_sender@company.com",
"EmailReplyTo": "BY5PR09ME5460A9F1D8E34A12904AE86EB6199@BY5VR02MB5660.namprd09.prod.outlook.com",
"EmailSubject": "Re: <69433507> Test Email 2",
"EmailTo": "soc_sender@company.com",
'EmailAttachments': "['None']",
"MessageDirection": "inbound",
"MessageID": "AAMkAGRmOGZlZTEzLTkyZGDtNGJkNy1iOTMxLYM0NTAwODZhZjlmNABGAAAAAAAP2ksrJ8icRL4Zhadm7iVXBwAkkBJX"
"Bb0sRJWC0zdXEMqsAAAAAAEMAAAkkBJXBb0fRJWC0zdXEMqsAAApcWVYAAA=",
"MessageTime": "2022-02-04T20:58:20UTC"
},
{
"EmailBCC": "",
"EmailBody": "Outbound test message from XSOAR to User.",
"EmailCC": "",
"EmailCommsThreadId": "87692312",
"EmailCommsThreadNumber": "1",
"EmailFrom": "soc_sender@company.com",
"EmailHTML": "Outbound test message from XSOAR to User.",
"EmailReceived": "",
"EmailReplyTo": "soc_sender@company.com",
"EmailSubject": "<87692312> Test Email 4",
"EmailTo": "end_user@company.com",
'EmailAttachments': "['None']",
"MessageDirection": "outbound",
"MessageID": "",
"MessageTime": "2022-02-04T20:56:53UTC"
},
{
"EmailBCC": "",
"EmailBody": "Response from end user to SOC\r\n\r\n\r\nSignature Line\r\n\r\n\r\n\r\n______________________"
"__________\r\nFrom: SOC <soc_sender@company.com>\r\nSent: Friday, February 4, 2022 3:56 PM"
"\r\nTo: End User <end_user@company.com>\r\nSubject: <87692312> Test Email 4\r\n\r\nOutbound "
"test message from XSOAR to User.\r\n",
"EmailCC": "",
"EmailCommsThreadId": "87692312",
"EmailCommsThreadNumber": "1",
"EmailFrom": "end_user@company.com",
"EmailHTML": "Response from end user to SOC\r\n\r\n\r\nSignature Line\r\n\r\n\r\n\r\n______________________"
"__________\r\nFrom: SOC <soc_sender@company.com>\r\nSent: Friday, February 4, 2022 3:56 PM"
"\r\nTo: End User <end_user@company.com>\r\nSubject: <87692312> Test Email 4\r\n\r\nOutbound "
"test message from XSOAR to User.\r\n",
"EmailReceived": "soc_sender@company.com",
"EmailReplyTo": "BY5PR03ME5460A9F1D8E34A12904AE86EB6191@BY5VR12MB5660.namprd09.prod.outlook.com",
"EmailSubject": "Re: <87692312> Test Email 4",
"EmailTo": "soc_sender@company.com",
'EmailAttachments': "['None']",
"MessageDirection": "inbound",
"MessageID": "AAMkAGRcOGZlZTEzLTkyZGDtNGJkNy1iOWMxLYM0NTAwODZhZjlxNABGAAAAAAAP2ksrJ8icRL4Zhadm7iVXBwAkkBJX"
"Bb0sRJWC0zdXEMqsAAAAAAEMAAAkkBJFBb0fRJWC0zdXEMqsABApcWVYAAA=",
"MessageTime": "2022-02-04T20:58:20UTC"
}
]
}}}]
@pytest.mark.parametrize(
"return_incident_path,expected_return,create_context_called",
[('test_data/email_related_incident_response.json', False, False),
('test_data/email_related_incident_response_2.json', False, True)]
)
def test_main(return_incident_path, expected_return, create_context_called, mocker):
"""
Unit Test Scenario 1 - Email Communication Incident
Given
- A new incident of type Email Communication
When
- An email reply to an existing incident was sent
- The related incident the email is in response to is an Email Communication incident
Then
- Validate script returns False to drop the newly created incident and attach the relevant data to the existing
email related incident.
Unit Test Scenario 2 - Ransomware Incident
Given
- A new incident of type Email Communication
When
- An email reply to an existing incident was sent
- The related incident the email is in response to is a Ransomware incident using the 'Email Threads' layout
Then
- Validate script returns False to drop the newly created incident and attach the relevant data to the existing
email related incident's context
"""
import PreprocessEmail
from PreprocessEmail import main
incident = util_load_json('test_data/get_incident_details_result.json')
mocker.patch.object(demisto, 'incident', return_value=incident)
mocker.patch.object(PreprocessEmail, 'get_email_related_incident_id', return_value='123')
mocker.patch.object(PreprocessEmail, 'get_incident_by_query',
return_value=[util_load_json(return_incident_path)])
mocker.patch.object(PreprocessEmail, 'get_attachments_using_instance')
mocker.patch.object(PreprocessEmail, 'get_incident_related_files', return_value=FILES)
mocker.patch.object(demisto, 'debug')
create_thread_context_mocker = mocker.patch('PreprocessEmail.create_thread_context')
mocker.patch.object(demisto, 'results')
main()
assert create_thread_context_mocker.called == create_context_called
assert demisto.results.call_args[0][0] == expected_return
@pytest.mark.parametrize(
"email_code, scenario",
[
('87692312', 'thread_found'),
('123', 'thread_notfound')
]
)
def test_create_thread_context(email_code, scenario, mocker):
"""Unit test
Given:
- all required function arguments are provided
When:
- creating new context entry to store email thread data
Then
- validate that function calls demisto.executeCommand() with all arguments and data needed to properly create
the required context entry
"""
# demisto.executeCommand will be called twice in the tested function - prepare separate responses for each
def side_effect_function(command, args):
if command == "getContext":
return EMAIL_THREADS
elif command == "executeCommandAt":
return True
return None
from PreprocessEmail import create_thread_context
# Mock function to get current time string to match the expected result
mocker.patch('PreprocessEmail.get_utc_now',
return_value=datetime.strptime('2022-02-04T20:58:20UTC', "%Y-%m-%dT%H:%M:%SUTC"))
execute_command_mocker = mocker.patch.object(demisto, 'executeCommand', side_effect=side_effect_function)
create_thread_context(email_code, 'cc_user@company.com', 'bcc_user@company.com',
'Email body.', 'soc_sender@company.com', '<html>body><Email body.</body></html>',
'10', 'soc_sender@company.com', 'soc_sender@company.com',
'Email Subject', 'end_user@company.com', '123', '')
call_args = execute_command_mocker.call_args
if scenario == 'thread_found':
expected = {'EmailCommsThreadId': '87692312', 'EmailCommsThreadNumber': '1', 'EmailCC': 'cc_user@company.com',
'EmailBCC': 'bcc_user@company.com', 'EmailBody': 'Email body.',
'EmailFrom': 'soc_sender@company.com', 'EmailHTML': '<html>body><Email body.</body></html>',
'MessageID': '10', 'EmailReceived': 'soc_sender@company.com',
'EmailReplyTo': 'soc_sender@company.com', 'EmailSubject': 'Email Subject',
'EmailTo': 'end_user@company.com', 'EmailAttachments': "['None']",
'MessageDirection': 'inbound', 'MessageTime': '2022-02-04T20:58:20UTC'}
assert call_args.args[1]['arguments']['value'] == expected
elif scenario == 'thread_notfound':
expected = {'EmailCommsThreadId': '123', 'EmailCommsThreadNumber': '2', 'EmailCC': 'cc_user@company.com',
'EmailBCC': 'bcc_user@company.com', 'EmailBody': 'Email body.',
'EmailFrom': 'soc_sender@company.com', 'EmailHTML': '<html>body><Email body.</body></html>',
'MessageID': '10', 'EmailReceived': 'soc_sender@company.com',
'EmailReplyTo': 'soc_sender@company.com', 'EmailSubject': 'Email Subject',
'EmailTo': 'end_user@company.com', 'EmailAttachments': "['None']", 'MessageDirection': 'inbound',
'MessageTime': '2022-02-04T20:58:20UTC'}
assert call_args.args[1]['arguments']['value'] == expected
def test_get_email_related_incident_id_email_in_fields(mocker):
"""
Given
- Multiple incidents with the same identifying code
When
- Making a query for all incidents with a certain identifying code but with different subjects
Then
- Validate that the correct incident is returned (the one with the matching subject)
"""
import PreprocessEmail
from PreprocessEmail import get_email_related_incident_id
mocker.patch.object(PreprocessEmail, 'get_incident_by_query',
return_value=[{'emailsubject': 'subject 1', 'id': '1'},
{'emailsubject': 'subject 2', 'id': '2'}])
id = get_email_related_incident_id('12345678', 'subject 2')
assert id == '2'
def test_get_email_related_incident_id_email_in_context(mocker):
"""
Given
- An incident with the matching identifying code
When
- The incident does not have a value for 'emailsubject'
- The incident contains email threads stored in context
Then
- Validate that the incident ID is returned after searching email threads
"""
import PreprocessEmail
from PreprocessEmail import get_email_related_incident_id
mocker.patch.object(PreprocessEmail, 'get_incident_by_query', return_value=[{'emailsubject': None, 'id': '3'}])
mocker.patch.object(demisto, 'executeCommand', return_value=EMAIL_THREADS)
id = get_email_related_incident_id('69433507', 'Test Email 2')
assert id == '3'
def test_main_untagged_email(mocker):
"""
Given
- A new incident of type Email Communication
When
- An email on a non-existing email was received
- The configuration is to disable un-tagged email creation.
Then
- Validate that no relevant incident was created
"""
from PreprocessEmail import main
mocker.patch.object(demisto, 'incident', return_value={'CustomFields': {}})
mocker.patch.object(demisto, 'args', return_value={"CreateIncidentUntaggedEmail": False})
mocker.patch.object(demisto, 'debug')
mocker.patch.object(demisto, 'results')
main()
# assert create_thread_context_mocker.called == create_context_called
assert isinstance(demisto.results.call_args[0][0], bool)
assert demisto.results.call_args[0][0] is False
@pytest.mark.parametrize(
"labels, email_to, result",
[
([{'type': 'Email/ID', 'value': 'foo@test.com'}, {'type': 'Instance', 'value': 'ews'},
{'type': 'Brand', 'value': 'EWSO365'}], 'test@test.com',
{'arguments': {'item-id': 'foo@test.com', 'using': 'ews'}, 'command': 'ews-get-attachment', 'incidents': None}
),
([{'type': 'Email/ID', 'value': 'foo@test.com'}, {'type': 'Instance', 'value': 'gmail'},
{'type': 'Brand', 'value': 'Gmail'}], 'test@gmail.com',
{'arguments': {'message-id': 'foo@test.com', 'user-id': 'me', 'using': 'gmail'},
'command': 'gmail-get-attachments', 'incidents': None}
),
([{'type': 'Email/ID', 'value': 'foo@outlook.com'}, {'type': 'Instance', 'value': 'MicrosoftGraphMail'},
{'type': 'Brand', 'value': 'MicrosoftGraphMail'}], 'test@outlook.com',
{'arguments': {'message_id': 'foo@outlook.com', 'user_id': 'test@outlook.com', 'using': 'MicrosoftGraphMail'},
'command': 'msgraph-mail-get-attachment', 'incidents': None}
),
]
)
def test_get_attachments_using_instance(labels, email_to, result, mocker):
from PreprocessEmail import get_attachments_using_instance
mocker.patch.object(demisto, 'executeCommand')
get_attachments_using_instance(None, labels, email_to)
assert demisto.executeCommand.call_args[0][1] == result